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
36QT_BEGIN_NAMESPACE
37
38struct 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#ifndef QT_NO_TEMPORARYFILE
49
50class QTemporaryFilePrivate : public QFilePrivate
51{
52 Q_DECLARE_PUBLIC(QTemporaryFile)
53
54public:
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
71class QTemporaryFileEngine : public QFSFileEngine
72{
73 Q_DECLARE_PRIVATE(QFSFileEngine)
74public:
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 d->fileEntry = QFileSystemEntry(file);
92 QFSFileEngine::setFileName(file);
93 }
94 }
95 ~QTemporaryFileEngine();
96
97 bool isReallyOpen() const;
98 void setFileName(const QString &file) override;
99
100 bool open(QIODevice::OpenMode flags, std::optional<QFile::Permissions> permissions) override;
101 bool remove() override;
102 bool rename(const QString &newName) override;
103 bool renameOverwrite(const QString &newName) override;
104 bool close() override;
105 QString fileName(FileName file) const override;
106
107 enum MaterializationMode { Overwrite, DontOverwrite, NameIsTemplate };
108 bool materializeUnnamedFile(const QString &newName, MaterializationMode mode);
109 bool isUnnamedFile() const override final;
110
111 const QString &templateName;
112 quint32 fileMode = 0;
113 int flags = 0;
114 bool filePathIsTemplate = true;
115 bool filePathWasTemplate = true;
116 bool unnamedFile = false;
117};
118
119#endif // QT_NO_TEMPORARYFILE
120
121QT_END_NAMESPACE
122
123#endif /* QTEMPORARYFILE_P_H */
124
125

source code of qtbase/src/corelib/io/qtemporaryfile_p.h