| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QTEMPORARYFILE_P_H |
| 6 | #define QTEMPORARYFILE_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtCore/qglobal.h> |
| 20 | |
| 21 | #include "private/qfsfileengine_p.h" |
| 22 | #include "private/qfilesystemengine_p.h" |
| 23 | #include "private/qfile_p.h" |
| 24 | #include "qtemporaryfile.h" |
| 25 | |
| 26 | #if defined(Q_OS_LINUX) && QT_CONFIG(linkat) |
| 27 | # include <fcntl.h> |
| 28 | # ifdef O_TMPFILE |
| 29 | // some early libc support had the wrong values for O_TMPFILE |
| 30 | // (see https://bugzilla.gnome.org/show_bug.cgi?id=769453#c18) |
| 31 | # if (O_TMPFILE & O_DIRECTORY) == O_DIRECTORY |
| 32 | # define LINUX_UNNAMED_TMPFILE |
| 33 | # endif |
| 34 | # endif |
| 35 | #endif |
| 36 | |
| 37 | QT_BEGIN_NAMESPACE |
| 38 | |
| 39 | struct QTemporaryFileName |
| 40 | { |
| 41 | QFileSystemEntry::NativePath path; |
| 42 | qsizetype pos; |
| 43 | qsizetype length; |
| 44 | |
| 45 | QTemporaryFileName(const QString &templateName); |
| 46 | QFileSystemEntry::NativePath generateNext(); |
| 47 | }; |
| 48 | |
| 49 | #if QT_CONFIG(temporaryfile) |
| 50 | |
| 51 | class QTemporaryFilePrivate : public QFilePrivate |
| 52 | { |
| 53 | Q_DECLARE_PUBLIC(QTemporaryFile) |
| 54 | |
| 55 | public: |
| 56 | QTemporaryFilePrivate(); |
| 57 | explicit QTemporaryFilePrivate(const QString &templateNameIn); |
| 58 | ~QTemporaryFilePrivate(); |
| 59 | |
| 60 | bool rename(const QString &newName, bool overwrite); |
| 61 | |
| 62 | QAbstractFileEngine *engine() const override; |
| 63 | void resetFileEngine() const; |
| 64 | void materializeUnnamedFile(); |
| 65 | |
| 66 | bool autoRemove = true; |
| 67 | QString templateName = defaultTemplateName(); |
| 68 | |
| 69 | static QString defaultTemplateName(); |
| 70 | |
| 71 | friend class QLockFilePrivate; |
| 72 | }; |
| 73 | |
| 74 | class QTemporaryFileEngine : public QFSFileEngine |
| 75 | { |
| 76 | Q_DECLARE_PRIVATE(QFSFileEngine) |
| 77 | public: |
| 78 | enum Flags { Win32NonShared = 0x1 }; |
| 79 | |
| 80 | explicit QTemporaryFileEngine(const QString *_templateName, int _flags = 0) |
| 81 | : templateName(*_templateName), flags(_flags) |
| 82 | {} |
| 83 | |
| 84 | void initialize(const QString &file, quint32 mode, bool nameIsTemplate = true) |
| 85 | { |
| 86 | Q_D(QFSFileEngine); |
| 87 | Q_ASSERT(!isReallyOpen()); |
| 88 | fileMode = mode; |
| 89 | filePathIsTemplate = filePathWasTemplate = nameIsTemplate; |
| 90 | |
| 91 | if (filePathIsTemplate) { |
| 92 | d->fileEntry.clear(); |
| 93 | } else { |
| 94 | QFSFileEngine::setFileEntry(QFileSystemEntry(file)); |
| 95 | } |
| 96 | } |
| 97 | ~QTemporaryFileEngine(); |
| 98 | |
| 99 | bool isReallyOpen() const; |
| 100 | void setFileName(const QString &file) override; |
| 101 | |
| 102 | bool open(QIODevice::OpenMode flags, std::optional<QFile::Permissions> permissions) override; |
| 103 | bool remove() override; |
| 104 | bool rename(const QString &newName) override; |
| 105 | bool renameOverwrite(const QString &newName) override; |
| 106 | bool close() override; |
| 107 | QString fileName(FileName file) const override; |
| 108 | |
| 109 | enum MaterializationMode { Overwrite, DontOverwrite, NameIsTemplate }; |
| 110 | bool materializeUnnamedFile(const QString &newName, MaterializationMode mode); |
| 111 | bool isUnnamedFile() const override final; |
| 112 | |
| 113 | const QString &templateName; |
| 114 | quint32 fileMode = 0; |
| 115 | int flags = 0; |
| 116 | bool filePathIsTemplate = true; |
| 117 | bool filePathWasTemplate = true; |
| 118 | bool unnamedFile = false; |
| 119 | }; |
| 120 | |
| 121 | #endif // QT_CONFIG(temporaryfile) |
| 122 | |
| 123 | QT_END_NAMESPACE |
| 124 | |
| 125 | #endif /* QTEMPORARYFILE_P_H */ |
| 126 | |
| 127 | |