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