1
/**************************************************************************
3
* Copyright 2013 Canonical Ltd.
4
* Copyright 2013 Carlos J Mazieri <carlos.mazieri@gmail.com>
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.
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.
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/>.
22
#include "tempfiles.h"
30
#define FILES_TO_CREATE 2
32
TempFiles::TempFiles() : m_content(QByteArray(4*4096, 'z'))
34
m_dir = QDir::tempPath();
38
bool TempFiles::addSubDirLevel(const QString &dir)
41
QFileInfo dirInfo(dir);
42
if (dirInfo.isRelative())
44
d.setFile(m_dir, dir); //append a single directory
48
d.setFile(dir); //use already made temp path
50
if (!d.exists() && QDir().mkpath(d.absoluteFilePath()))
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);
58
m_dir = d.absoluteFilePath();
64
void TempFiles::removeAll()
66
int counter = m_filesCreated.count();
69
if (QFileInfo(m_filesCreated.at(counter)).exists())
71
QFile::remove(m_filesCreated.at(counter));
72
m_filesCreated.removeAt(counter);
78
QStringList TempFiles::createdNames()
81
int counter = m_filesCreated.count();
83
names.append(QFileInfo(m_filesCreated.at(counter)).fileName());
88
int TempFiles::howManyExist()
91
int counter = m_filesCreated.count();
94
if (QFileInfo(m_filesCreated.at(counter)).exists())
102
bool TempFiles::create(int counter)
104
return create(QLatin1String("tempfile"), counter);
107
bool TempFiles::create(const QString& name, int counter )
109
return createPrivate(name, counter, true);
112
bool TempFiles::touch(int counter)
114
return touch(QLatin1String("emptyfile"), counter);
117
bool TempFiles::touch(const QString& name, int counter )
119
return createPrivate(name, counter, false);
122
bool TempFiles::createPrivate(const QString& name, int counter, bool putContent)
127
myName.sprintf("%s%c%s_%02d", m_dir.toLocal8Bit().constData(),
128
QDir::separator().toLatin1(),
129
name.toLocal8Bit().constData(),
132
if (file.open(QFile::WriteOnly))
134
m_filesCreated.append(myName);
137
if (file.write(m_content) == (qint64)m_content.size())
139
m_content += QByteArray(1024, 'z');
160
QString TempFiles::lastFileCreated()
163
if (m_filesCreated.count() > 0)
165
ret = m_filesCreated.at(m_filesCreated.count() -1);
170
QString TempFiles::lastNameCreated()
172
QFileInfo f(lastFileCreated());
176
DeepDir::DeepDir(const QString &rootDir, int level) :
177
root(QDir::tempPath() + QDir::separator() + rootDir),
181
QFileInfo rootDirInfo(rootDir);
182
if (rootDirInfo.isAbsolute())
186
if (!rootDir.isEmpty())
191
if (temp.addSubDirLevel(rootDir))
193
for(int counter=1 ; counter <= level; counter++)
195
levelStr.sprintf("level_%02d", counter);
196
if ( !temp.addSubDirLevel(levelStr) || !temp.create(FILES_TO_CREATE) )
200
totalFiles += FILES_TO_CREATE;
201
totalItems += FILES_TO_CREATE + 1;
204
firstDirLevel = temp.lastPath();
207
lastDirLevel = temp.lastPath();
216
bool DeepDir::remove()
219
if (!root.isEmpty() && QFileInfo(root).exists())
221
QString cmd("/bin/rm -rf " + root);
222
ret = ::system(cmd.toLocal8Bit().constData()) == 0 ;
225
qWarning("*** Could not remove %s, if it refers to Samba try to configure Samba using: 'force user' or 'create mask' plus 'directory mask'",