| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
|---|
| 4 | ** Contact: https://www.qt.io/licensing/ | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial License Usage | 
|---|
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
|---|
| 11 | ** accordance with the commercial license agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
|---|
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
|---|
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
|---|
| 16 | ** | 
|---|
| 17 | ** GNU Lesser General Public License Usage | 
|---|
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 19 | ** General Public License version 3 as published by the Free Software | 
|---|
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
|---|
| 21 | ** packaging of this file. Please review the following information to | 
|---|
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
|---|
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
|---|
| 24 | ** | 
|---|
| 25 | ** GNU General Public License Usage | 
|---|
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
|---|
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
|---|
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
|---|
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
|---|
| 31 | ** included in the packaging of this file. Please review the following | 
|---|
| 32 | ** information to ensure the GNU General Public License requirements will | 
|---|
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
|---|
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** $QT_END_LICENSE$ | 
|---|
| 37 | ** | 
|---|
| 38 | ****************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | #ifndef QTEMPORARYFILE_P_H | 
|---|
| 41 | #define QTEMPORARYFILE_P_H | 
|---|
| 42 |  | 
|---|
| 43 | // | 
|---|
| 44 | //  W A R N I N G | 
|---|
| 45 | //  ------------- | 
|---|
| 46 | // | 
|---|
| 47 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 48 | // implementation detail.  This header file may change from version to | 
|---|
| 49 | // version without notice, or even be removed. | 
|---|
| 50 | // | 
|---|
| 51 | // We mean it. | 
|---|
| 52 | // | 
|---|
| 53 |  | 
|---|
| 54 | #include <QtCore/qglobal.h> | 
|---|
| 55 |  | 
|---|
| 56 | #include "private/qfsfileengine_p.h" | 
|---|
| 57 | #include "private/qfilesystemengine_p.h" | 
|---|
| 58 | #include "private/qfile_p.h" | 
|---|
| 59 | #include "qtemporaryfile.h" | 
|---|
| 60 |  | 
|---|
| 61 | #if defined(Q_OS_LINUX) && QT_CONFIG(linkat) | 
|---|
| 62 | #  include <fcntl.h> | 
|---|
| 63 | #  ifdef O_TMPFILE | 
|---|
| 64 | // some early libc support had the wrong values for O_TMPFILE | 
|---|
| 65 | // (see https://bugzilla.gnome.org/show_bug.cgi?id=769453#c18) | 
|---|
| 66 | #    if (O_TMPFILE & O_DIRECTORY) == O_DIRECTORY | 
|---|
| 67 | #      define LINUX_UNNAMED_TMPFILE | 
|---|
| 68 | #    endif | 
|---|
| 69 | #  endif | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | QT_BEGIN_NAMESPACE | 
|---|
| 73 |  | 
|---|
| 74 | struct QTemporaryFileName | 
|---|
| 75 | { | 
|---|
| 76 | QFileSystemEntry::NativePath path; | 
|---|
| 77 | qsizetype pos; | 
|---|
| 78 | qsizetype length; | 
|---|
| 79 |  | 
|---|
| 80 | QTemporaryFileName(const QString &templateName); | 
|---|
| 81 | QFileSystemEntry::NativePath generateNext(); | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 | #ifndef QT_NO_TEMPORARYFILE | 
|---|
| 85 |  | 
|---|
| 86 | class QTemporaryFilePrivate : public QFilePrivate | 
|---|
| 87 | { | 
|---|
| 88 | Q_DECLARE_PUBLIC(QTemporaryFile) | 
|---|
| 89 |  | 
|---|
| 90 | public: | 
|---|
| 91 | QTemporaryFilePrivate(); | 
|---|
| 92 | explicit QTemporaryFilePrivate(const QString &templateNameIn); | 
|---|
| 93 | ~QTemporaryFilePrivate(); | 
|---|
| 94 |  | 
|---|
| 95 | QAbstractFileEngine *engine() const override; | 
|---|
| 96 | void resetFileEngine() const; | 
|---|
| 97 | void materializeUnnamedFile(); | 
|---|
| 98 |  | 
|---|
| 99 | bool autoRemove = true; | 
|---|
| 100 | QString templateName = defaultTemplateName(); | 
|---|
| 101 |  | 
|---|
| 102 | static QString defaultTemplateName(); | 
|---|
| 103 |  | 
|---|
| 104 | friend class QLockFilePrivate; | 
|---|
| 105 | }; | 
|---|
| 106 |  | 
|---|
| 107 | class QTemporaryFileEngine : public QFSFileEngine | 
|---|
| 108 | { | 
|---|
| 109 | Q_DECLARE_PRIVATE(QFSFileEngine) | 
|---|
| 110 | public: | 
|---|
| 111 | enum Flags { Win32NonShared = 0x1 }; | 
|---|
| 112 |  | 
|---|
| 113 | explicit QTemporaryFileEngine(const QString *_templateName, int _flags = 0) | 
|---|
| 114 | : templateName(*_templateName), flags(_flags) | 
|---|
| 115 | {} | 
|---|
| 116 |  | 
|---|
| 117 | void initialize(const QString &file, quint32 mode, bool nameIsTemplate = true) | 
|---|
| 118 | { | 
|---|
| 119 | Q_D(QFSFileEngine); | 
|---|
| 120 | Q_ASSERT(!isReallyOpen()); | 
|---|
| 121 | fileMode = mode; | 
|---|
| 122 | filePathIsTemplate = filePathWasTemplate = nameIsTemplate; | 
|---|
| 123 |  | 
|---|
| 124 | if (filePathIsTemplate) { | 
|---|
| 125 | d->fileEntry.clear(); | 
|---|
| 126 | } else { | 
|---|
| 127 | d->fileEntry = QFileSystemEntry(file); | 
|---|
| 128 | QFSFileEngine::setFileName(file); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 | ~QTemporaryFileEngine(); | 
|---|
| 132 |  | 
|---|
| 133 | bool isReallyOpen() const; | 
|---|
| 134 | void setFileName(const QString &file) override; | 
|---|
| 135 |  | 
|---|
| 136 | bool open(QIODevice::OpenMode flags) override; | 
|---|
| 137 | bool remove() override; | 
|---|
| 138 | bool rename(const QString &newName) override; | 
|---|
| 139 | bool renameOverwrite(const QString &newName) override; | 
|---|
| 140 | bool close() override; | 
|---|
| 141 | QString fileName(FileName file) const override; | 
|---|
| 142 |  | 
|---|
| 143 | enum MaterializationMode { Overwrite, DontOverwrite, NameIsTemplate }; | 
|---|
| 144 | bool materializeUnnamedFile(const QString &newName, MaterializationMode mode); | 
|---|
| 145 | bool isUnnamedFile() const override final; | 
|---|
| 146 |  | 
|---|
| 147 | const QString &templateName; | 
|---|
| 148 | quint32 fileMode = 0; | 
|---|
| 149 | int flags = 0; | 
|---|
| 150 | bool filePathIsTemplate = true; | 
|---|
| 151 | bool filePathWasTemplate = true; | 
|---|
| 152 | bool unnamedFile = false; | 
|---|
| 153 | }; | 
|---|
| 154 |  | 
|---|
| 155 | #endif // QT_NO_TEMPORARYFILE | 
|---|
| 156 |  | 
|---|
| 157 | QT_END_NAMESPACE | 
|---|
| 158 |  | 
|---|
| 159 | #endif /* QTEMPORARYFILE_P_H */ | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|