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 | QAbstractFileEngine *engine() const override; |
60 | void resetFileEngine() const; |
61 | void materializeUnnamedFile(); |
62 | |
63 | bool autoRemove = true; |
64 | QString templateName = defaultTemplateName(); |
65 | |
66 | static QString defaultTemplateName(); |
67 | |
68 | friend class QLockFilePrivate; |
69 | }; |
70 | |
71 | class QTemporaryFileEngine : public QFSFileEngine |
72 | { |
73 | Q_DECLARE_PRIVATE(QFSFileEngine) |
74 | public: |
75 | enum Flags { Win32NonShared = 0x1 }; |
76 | |
77 | explicit QTemporaryFileEngine(const QString *_templateName, int _flags = 0) |
78 | : templateName(*_templateName), flags(_flags) |
79 | {} |
80 | |
81 | void initialize(const QString &file, quint32 mode, bool nameIsTemplate = true) |
82 | { |
83 | Q_D(QFSFileEngine); |
84 | Q_ASSERT(!isReallyOpen()); |
85 | fileMode = mode; |
86 | filePathIsTemplate = filePathWasTemplate = nameIsTemplate; |
87 | |
88 | if (filePathIsTemplate) { |
89 | d->fileEntry.clear(); |
90 | } else { |
91 | QFSFileEngine::setFileEntry(QFileSystemEntry(file)); |
92 | } |
93 | } |
94 | ~QTemporaryFileEngine(); |
95 | |
96 | bool isReallyOpen() const; |
97 | void setFileName(const QString &file) override; |
98 | |
99 | bool open(QIODevice::OpenMode flags, std::optional<QFile::Permissions> permissions) override; |
100 | bool remove() override; |
101 | bool rename(const QString &newName) override; |
102 | bool renameOverwrite(const QString &newName) override; |
103 | bool close() override; |
104 | QString fileName(FileName file) const override; |
105 | |
106 | enum MaterializationMode { Overwrite, DontOverwrite, NameIsTemplate }; |
107 | bool materializeUnnamedFile(const QString &newName, MaterializationMode mode); |
108 | bool isUnnamedFile() const override final; |
109 | |
110 | const QString &templateName; |
111 | quint32 fileMode = 0; |
112 | int flags = 0; |
113 | bool filePathIsTemplate = true; |
114 | bool filePathWasTemplate = true; |
115 | bool unnamedFile = false; |
116 | }; |
117 | |
118 | #endif // QT_CONFIG(temporaryfile) |
119 | |
120 | QT_END_NAMESPACE |
121 | |
122 | #endif /* QTEMPORARYFILE_P_H */ |
123 | |
124 |