~ubuntu-filemanager-dev/ubuntu-filemanager-app/trunk

« back to all changes in this revision

Viewing changes to src/plugin/test_folderlistmodel/regression/tempfiles.cpp

  • Committer: Bileto Bot
  • Date: 2017-04-04 17:06:41 UTC
  • mfrom: (588.1.19 fix-desktop-file)
  • Revision ID: ci-train-bot@canonical.com-20170404170641-1p15lmx8wodlx2ut
* Rename binary file to ubuntu-filemanager-app
* Join plugin packages into the main package 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**************************************************************************
2
 
 *
3
 
 * Copyright 2013 Canonical Ltd.
4
 
 * Copyright 2013 Carlos J Mazieri <carlos.mazieri@gmail.com>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU Lesser General Public License as published by
8
 
 * the Free Software Foundation; version 3.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU Lesser General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Lesser General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 *
18
 
 * File: tempfiles.cpp
19
 
 * Date: 4/2/2013
20
 
 */
21
 
 
22
 
#include "tempfiles.h"
23
 
#include <stdlib.h>
24
 
 
25
 
 
26
 
#include <QDir>
27
 
#include <QFile>
28
 
#include <QFileInfo>
29
 
 
30
 
#define FILES_TO_CREATE   2
31
 
 
32
 
TempFiles::TempFiles() : m_content(QByteArray(4*4096, 'z'))
33
 
{
34
 
    m_dir = QDir::tempPath();
35
 
}
36
 
 
37
 
 
38
 
bool TempFiles::addSubDirLevel(const QString &dir)
39
 
{
40
 
    QFileInfo d;
41
 
    QFileInfo dirInfo(dir);
42
 
    if (dirInfo.isRelative())
43
 
    {
44
 
        d.setFile(m_dir, dir); //append a single directory
45
 
    }
46
 
    else
47
 
    {
48
 
        d.setFile(dir);       //use already made temp path
49
 
    }
50
 
    if (!d.exists() && QDir().mkpath(d.absoluteFilePath()))
51
 
    {             
52
 
        QFile::setPermissions(d.absoluteFilePath(),  QFile::WriteOwner | QFile::ReadOwner | QFile::ExeOwner
53
 
                                                   | QFile::WriteGroup | QFile::ReadGroup | QFile::ExeGroup
54
 
                                                   | QFile::WriteOther | QFile::ReadOther | QFile::ExeOther);
55
 
    }
56
 
    if (d.exists())
57
 
    {
58
 
        m_dir = d.absoluteFilePath();
59
 
        return true;
60
 
    }
61
 
    return false;
62
 
}
63
 
 
64
 
void TempFiles::removeAll()
65
 
{
66
 
    int counter = m_filesCreated.count();
67
 
    while(counter--)
68
 
    {
69
 
        if (QFileInfo(m_filesCreated.at(counter)).exists())
70
 
        {
71
 
            QFile::remove(m_filesCreated.at(counter));
72
 
            m_filesCreated.removeAt(counter);
73
 
        }
74
 
    }
75
 
}
76
 
 
77
 
 
78
 
QStringList TempFiles::createdNames()
79
 
{
80
 
    QStringList names;
81
 
    int counter = m_filesCreated.count();
82
 
    while(counter--) {
83
 
        names.append(QFileInfo(m_filesCreated.at(counter)).fileName());
84
 
    }
85
 
    return names;
86
 
}
87
 
 
88
 
int TempFiles::howManyExist()
89
 
{
90
 
    int ret = 0;
91
 
    int counter = m_filesCreated.count();
92
 
    while(counter--)
93
 
    {
94
 
        if (QFileInfo(m_filesCreated.at(counter)).exists())
95
 
        {
96
 
           ret++;
97
 
        }
98
 
    }
99
 
    return ret;
100
 
}
101
 
 
102
 
bool TempFiles::create(int counter)
103
 
{
104
 
   return  create(QLatin1String("tempfile"), counter);
105
 
}
106
 
 
107
 
bool TempFiles::create(const QString& name, int counter )
108
 
{
109
 
    return createPrivate(name, counter, true);
110
 
}
111
 
 
112
 
bool TempFiles::touch(int counter)
113
 
{
114
 
    return  touch(QLatin1String("emptyfile"), counter);
115
 
}
116
 
 
117
 
bool TempFiles::touch(const QString& name, int counter )
118
 
{
119
 
    return createPrivate(name, counter, false);
120
 
}
121
 
 
122
 
bool TempFiles::createPrivate(const QString& name, int counter, bool putContent)
123
 
{
124
 
    QString myName;
125
 
    while(counter--)
126
 
    {
127
 
        myName.sprintf("%s%c%s_%02d", m_dir.toLocal8Bit().constData(),
128
 
                       QDir::separator().toLatin1(),
129
 
                       name.toLocal8Bit().constData(),
130
 
                       counter);
131
 
        QFile file(myName);
132
 
        if (file.open(QFile::WriteOnly))
133
 
        {
134
 
            m_filesCreated.append(myName);
135
 
            if(putContent)
136
 
            {
137
 
                if (file.write(m_content) == (qint64)m_content.size())
138
 
                {
139
 
                    m_content += QByteArray(1024, 'z');
140
 
                }
141
 
                else {
142
 
                    return false;
143
 
                }
144
 
            }
145
 
        }
146
 
        else
147
 
        {
148
 
            return false;
149
 
        }
150
 
 
151
 
    }
152
 
    return true;
153
 
}
154
 
 
155
 
 
156
 
 
157
 
 
158
 
 
159
 
 
160
 
QString TempFiles::lastFileCreated()
161
 
{
162
 
    QString ret;
163
 
    if (m_filesCreated.count() > 0)
164
 
    {
165
 
        ret = m_filesCreated.at(m_filesCreated.count() -1);
166
 
    }
167
 
    return ret;
168
 
}
169
 
 
170
 
QString TempFiles::lastNameCreated()
171
 
{
172
 
    QFileInfo f(lastFileCreated());
173
 
    return f.fileName();
174
 
}
175
 
 
176
 
DeepDir::DeepDir(const QString &rootDir, int level) :
177
 
    root(QDir::tempPath() + QDir::separator() + rootDir),
178
 
    totalFiles(0),
179
 
    totalItems(0)
180
 
{
181
 
    QFileInfo rootDirInfo(rootDir);
182
 
    if (rootDirInfo.isAbsolute())
183
 
    {
184
 
       root = rootDir;
185
 
    }
186
 
    if (!rootDir.isEmpty())
187
 
    {
188
 
        remove(); // clear
189
 
        QString levelStr;
190
 
        TempFiles temp;
191
 
        if (temp.addSubDirLevel(rootDir))
192
 
        {
193
 
            for(int counter=1 ; counter <= level; counter++)
194
 
            {
195
 
                levelStr.sprintf("level_%02d", counter);
196
 
                if ( !temp.addSubDirLevel(levelStr) || !temp.create(FILES_TO_CREATE) )
197
 
                {
198
 
                    break;
199
 
                }
200
 
                totalFiles += FILES_TO_CREATE;
201
 
                totalItems += FILES_TO_CREATE + 1;
202
 
                if (counter == 1)
203
 
                {
204
 
                   firstDirLevel =  temp.lastPath();
205
 
                }
206
 
            }
207
 
            lastDirLevel = temp.lastPath();
208
 
        }
209
 
    }
210
 
    else
211
 
    {
212
 
        root.clear();
213
 
    }
214
 
}
215
 
 
216
 
bool DeepDir::remove()
217
 
{
218
 
    bool ret = false;
219
 
    if (!root.isEmpty() && QFileInfo(root).exists())
220
 
    {
221
 
        QString cmd("/bin/rm -rf " + root);
222
 
        ret = ::system(cmd.toLocal8Bit().constData()) == 0 ;
223
 
        if (!ret)
224
 
        {
225
 
            qWarning("*** Could not remove %s, if it refers to Samba try to configure Samba using: 'force user' or 'create mask' plus 'directory mask'",
226
 
                     qPrintable(root));
227
 
        }
228
 
    }
229
 
    return ret;
230
 
}
231
 
 
232