1// Copyright (C) 2022 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 QFILEDEVICE_P_H
5#define QFILEDEVICE_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 "private/qiodevice_p.h"
19#include "qfiledevice.h"
20
21#include <memory>
22#if defined(Q_OS_UNIX)
23# include <sys/types.h> // for mode_t
24# include <sys/stat.h> // for mode_t constants
25#elif defined(Q_OS_WINDOWS)
26# include <qt_windows.h>
27# include <winnt.h> // for SECURITY_DESCRIPTOR
28# include <optional>
29# if defined(QT_BOOTSTRAPPED)
30# define QT_FEATURE_fslibs -1
31# else
32# define QT_FEATURE_fslibs 1
33# endif // QT_BOOTSTRAPPED
34#endif
35
36QT_BEGIN_NAMESPACE
37
38class QAbstractFileEngine;
39class QFSFileEngine;
40
41class QFileDevicePrivate : public QIODevicePrivate
42{
43 Q_DECLARE_PUBLIC(QFileDevice)
44protected:
45 QFileDevicePrivate();
46 ~QFileDevicePrivate();
47
48 virtual QAbstractFileEngine *engine() const;
49
50 inline bool ensureFlushed() const;
51
52 bool putCharHelper(char c) override;
53
54 void setError(QFileDevice::FileError err);
55 void setError(QFileDevice::FileError err, const QString &errorString);
56 void setError(QFileDevice::FileError err, int errNum);
57
58 mutable std::unique_ptr<QAbstractFileEngine> fileEngine;
59 mutable qint64 cachedSize;
60
61 QFileDevice::FileHandleFlags handleFlags;
62 QFileDevice::FileError error;
63
64 bool lastWasWrite;
65};
66
67inline bool QFileDevicePrivate::ensureFlushed() const
68{
69 // This function ensures that the write buffer has been flushed (const
70 // because certain const functions need to call it.
71 if (lastWasWrite) {
72 const_cast<QFileDevicePrivate *>(this)->lastWasWrite = false;
73 if (!const_cast<QFileDevice *>(q_func())->flush())
74 return false;
75 }
76 return true;
77}
78
79#ifdef Q_OS_UNIX
80namespace QtPrivate {
81
82constexpr mode_t toMode_t(QFileDevice::Permissions permissions)
83{
84 mode_t mode = 0;
85 if (permissions & (QFileDevice::ReadOwner | QFileDevice::ReadUser))
86 mode |= S_IRUSR;
87 if (permissions & (QFileDevice::WriteOwner | QFileDevice::WriteUser))
88 mode |= S_IWUSR;
89 if (permissions & (QFileDevice::ExeOwner | QFileDevice::ExeUser))
90 mode |= S_IXUSR;
91 if (permissions & QFileDevice::ReadGroup)
92 mode |= S_IRGRP;
93 if (permissions & QFileDevice::WriteGroup)
94 mode |= S_IWGRP;
95 if (permissions & QFileDevice::ExeGroup)
96 mode |= S_IXGRP;
97 if (permissions & QFileDevice::ReadOther)
98 mode |= S_IROTH;
99 if (permissions & QFileDevice::WriteOther)
100 mode |= S_IWOTH;
101 if (permissions & QFileDevice::ExeOther)
102 mode |= S_IXOTH;
103 return mode;
104}
105
106} // namespace QtPrivate
107#elif defined(Q_OS_WINDOWS)
108
109class QNativeFilePermissions
110{
111public:
112 QNativeFilePermissions(std::optional<QFileDevice::Permissions> perms, bool isDir);
113
114 SECURITY_ATTRIBUTES *securityAttributes();
115 bool isOk() const { return ok; }
116
117private:
118 bool ok = false;
119 bool isNull = true;
120
121 // At most 1 allow + 1 deny ACEs for user and group, 1 allow ACE for others
122 static constexpr auto MaxNumACEs = 5;
123
124 static constexpr auto MaxACLSize =
125 sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE) + SECURITY_MAX_SID_SIZE) * MaxNumACEs;
126
127 SECURITY_ATTRIBUTES sa;
128#if QT_CONFIG(fslibs)
129 SECURITY_DESCRIPTOR sd;
130 alignas(DWORD) char aclStorage[MaxACLSize];
131#endif
132};
133
134#endif // Q_OS_UNIX
135
136QT_END_NAMESPACE
137
138#endif // QFILEDEVICE_P_H
139

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