1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qplatformdefs.h"
31
#include "qtemporaryfile.h"
32
#include <qfileengine.h>
33
#include <private/qfile_p.h>
34
#include <private/qfileengine_p.h>
35
#include <private/qbufferedfsfileengine_p.h>
44
//************* QTemporaryFileEngine
45
class QTemporaryFileEngine : public QFSFileEngine
47
Q_DECLARE_PRIVATE(QFSFileEngine)
49
QTemporaryFileEngine(const QString &file) : QFSFileEngine(file) { }
50
~QTemporaryFileEngine();
55
QTemporaryFileEngine::~QTemporaryFileEngine()
59
bool QTemporaryFileEngine::open(int)
63
QString qfilename = d->file;
64
if(!qfilename.endsWith(QLatin1String("XXXXXX")))
65
qfilename += QLatin1String(".XXXXXX");
67
char *filename = strdup(qfilename.toLocal8Bit());
69
d->fd = mkstemp(filename);
71
#if defined(_MSC_VER) && _MSC_VER >= 1400
72
int len = strnlen(filename, _MAX_FNAME) + 1;
73
if(_mktemp_s(filename, len) == 0) {
75
if(mktemp(filename)) {
77
int oflags = QT_OPEN_RDWR | QT_OPEN_CREAT;
78
#if defined(Q_OS_MSDOS) || defined(Q_OS_WIN32) || defined(Q_OS_OS2)
79
oflags |= QT_OPEN_BINARY; // we handle all text translations our self.
81
d->fd = d->sysOpen(filename, oflags);
85
d->file = QString::fromLocal8Bit(filename); //changed now!
91
d->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, errno);
96
//************* QTemporaryFilePrivate
97
class QTemporaryFilePrivate : public QFilePrivate
99
Q_DECLARE_PUBLIC(QTemporaryFile)
102
QTemporaryFilePrivate();
103
~QTemporaryFilePrivate();
106
QString templateName;
107
mutable QTemporaryFileEngine *fileEngine;
110
QTemporaryFilePrivate::QTemporaryFilePrivate() : autoRemove(true), fileEngine(0)
114
QTemporaryFilePrivate::~QTemporaryFilePrivate()
120
//************* QTemporaryFile
123
\class QTemporaryFile
125
\brief The QTemporaryFile class is an I/O device that operates on temporary files.
130
QTemporaryFile is an I/O device that will get its input/output
131
from the local disk. The filename for the temporary file will be
132
guaranteed to be unique once the file is opened and will
133
subsequently be removed upon destruction of the QTemporaryFile
136
A temporary file will have some static part of the name and some
137
part that is calculated to be unique. The default filename qt_temp
138
will be placed into the temporary path as returned by
141
\sa QDir::tempPath(), QFile
145
QTemporaryFile::QTemporaryFile()
146
: QFile(*new QTemporaryFilePrivate)
149
d->templateName = QDir::tempPath() + "/" + QLatin1String("qt_temp.XXXXXX");
152
QTemporaryFile::QTemporaryFile(const QString &templateName)
153
: QFile(*new QTemporaryFilePrivate)
156
d->templateName = templateName;
161
Constructs a QTemporaryFile with no name.
163
\sa setFileTemplate()
165
QTemporaryFile::QTemporaryFile()
166
: QFile(*new QTemporaryFilePrivate, 0)
169
d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");
173
Constructs a QTemporaryFile with a template filename of \a
174
templateName. Upon opening the temporary file this will be used to
175
create a unique filename. If the \a templateName does end in
176
XXXXXX it will automatically be appended and used as the dynamic
177
portion of the filename.
179
\sa open(), fileTemplate()
181
QTemporaryFile::QTemporaryFile(const QString &templateName)
182
: QFile(*new QTemporaryFilePrivate, 0)
185
d->templateName = templateName;
189
Constructs a QTemporaryFile with the given \a parent, but with no name.
191
\sa setFileTemplate()
193
QTemporaryFile::QTemporaryFile(QObject *parent)
194
: QFile(*new QTemporaryFilePrivate, parent)
197
d->templateName = QDir::tempPath() + QLatin1String("qt_temp.XXXXXX");
201
Constructs a QTemporaryFile with a template filename of \a
202
templateName and the specified \a parent.
203
Upon opening the temporary file this will be used to
204
create a unique filename. If the \a templateName does end in
205
XXXXXX it will automatically be appended and used as the dynamic
206
portion of the filename.
208
\sa open(), fileTemplate()
210
QTemporaryFile::QTemporaryFile(const QString &templateName, QObject *parent)
211
: QFile(*new QTemporaryFilePrivate, parent)
214
d->templateName = templateName;
219
Destroys the temporary file object, the file is automatically
220
closed if necessary and if in auto remove mode it will
221
automatically delete the file.
225
QTemporaryFile::~QTemporaryFile()
229
if (!d->fileName.isEmpty() && d->autoRemove)
234
\fn bool QTemporaryFile::open()
236
A QTemporaryFile will always be opened in QIODevice::ReadWrite mode,
237
this allows easy access to the data in the file. This function will
238
return true upon success and will set the fileName() to the unique
245
Returns true if the QTemporaryFile is in auto remove
246
mode. Auto-remove mode will automatically delete the filename from
247
disk upon destruction. This makes it very easy to create your
248
QTemporaryFile object on the stack, fill it with data, read from
249
it, and finally on function return it will automatically clean up
252
\sa setAutoRemove(), remove()
254
bool QTemporaryFile::autoRemove() const
256
Q_D(const QTemporaryFile);
257
return d->autoRemove;
261
Sets the QTemporaryFile into auto-remove mode if \a b is true.
263
\sa autoRemove(), remove()
265
void QTemporaryFile::setAutoRemove(bool b)
272
Returns the complete unique filename backing the QTemporaryFile
273
object. This string is null before the QTemporaryFile is opened,
274
afterwards it will contain the fileTemplate() plus
275
additional characters to make it unique.
280
QString QTemporaryFile::fileName() const
284
return fileEngine()->fileName(QFileEngine::DefaultName);
288
Returns the set file template. The default file template will be
289
called qt_temp and be placed in QDir::tempPath().
291
\sa setFileTemplate()
293
QString QTemporaryFile::fileTemplate() const
295
Q_D(const QTemporaryFile);
296
return d->templateName;
300
Sets the static portion of the file name to \a name. If the file
301
template ends in XXXXXX that will automatically be replaced with
302
the unique part of the filename, otherwise a filename will be
303
determined automatically based on the static portion specified.
307
void QTemporaryFile::setFileTemplate(const QString &name)
311
fileEngine()->setFileName(name);
312
d->templateName = name;
316
\fn QTemporaryFile *QTemporaryFile::createLocalFile(const QString &fileName)
318
Works on the given \a fileName rather than an existing QFile
324
Creates and returns a local temporary file whose contents are a
325
copy of the contens of the given \a file.
327
QTemporaryFile *QTemporaryFile::createLocalFile(QFile &file)
329
if(QFileEngine *engine = file.fileEngine()) {
330
if(engine->fileFlags(QFileEngine::FlagsMask) & QFileEngine::LocalDiskFlag)
331
return 0; //local already
333
bool wasOpen = file.isOpen();
336
old_off = file.pos();
338
file.open(QIODevice::ReadOnly);
340
QTemporaryFile *ret = new QTemporaryFile;
345
qint64 len = file.read(buffer, 1024);
348
ret->write(buffer, len);
366
QFileEngine *QTemporaryFile::fileEngine() const
368
Q_D(const QTemporaryFile);
370
d->fileEngine = new QTemporaryFileEngine(d->templateName);
371
return d->fileEngine;
378
bool QTemporaryFile::open(OpenMode flags)
381
if (QFile::open(flags)) {
382
d->fileName = d->fileEngine->fileName(QFileEngine::DefaultName);