forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileinfo.h
286 lines (248 loc) · 8.76 KB
/
fileinfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#pragma once
#include <gtest/gtest_prod.h>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QUrl>
#include <QtDebug>
#include "util/assert.h"
namespace mixxx {
/// A thin wrapper (shim) around QFileInfo with a limited API and
/// some additional methods.
///
/// Could be used as a drop-in replacement of QFileInfo with very
/// few exceptions where the name of member functions differ. Despite
/// the name it represents either a file, a directory, or a symbolic link.
///
/// This class adds support for the higher-level concept of a "location"
/// that is used to reference a permanent file path.
///
/// All single-argument are declared as explicit to prevent implicit conversions.
///
/// Implementation note: Inheriting from QFileInfo would violate the
/// Liskov Substution Principle. It is also invalid, because QFileInfo
/// has a non-virtual destructor and we cannot override non-virtual
/// member functions.
class FileInfo final {
public:
explicit FileInfo(
QFileInfo&& fileInfo)
: m_fileInfo(std::move(fileInfo)) {
}
explicit FileInfo(
const QFileInfo& fileInfo)
: m_fileInfo(fileInfo) {
}
explicit FileInfo(
const QFile& file)
: m_fileInfo(file) {
}
explicit FileInfo(
const QString& file)
: m_fileInfo(file) {
}
explicit FileInfo(
const QDir& dir,
const QString& file = QString())
: m_fileInfo(dir, file) {
}
FileInfo() = default;
FileInfo(FileInfo&&) = default;
FileInfo(const FileInfo&) = default;
FileInfo& operator=(FileInfo&&) = default;
FileInfo& operator=(const FileInfo&) = default;
/// Directly access to the wrapped QFileInfo (immutable)
const QFileInfo& asQFileInfo() const {
return m_fileInfo;
}
/// Explicit conversion from QFile.
static FileInfo fromQFile(const QFile& file) {
return FileInfo(file);
}
/// Explicit conversion to QFile.
QFile toQFile(QObject* parent = nullptr) const {
return QFile(location(), parent);
}
/// Explicit conversion from QDir.
static FileInfo fromQDir(const QDir& dir) {
return FileInfo(dir);
}
/// Explicit conversion to QDir.
QDir toQDir() const {
// Due to false negatives we must assert for !isFile() instead
// of isDir()!
DEBUG_ASSERT(!isFile());
// We cannot use QFileInfo::dir() which instead returns the
// parent directory.
return QDir(location());
}
/// Explicit conversion from a local file QUrl.
static FileInfo fromQUrl(const QUrl& url) {
return FileInfo(url.toLocalFile());
}
/// Explicit conversion to a local file QUrl.
QUrl toQUrl() const {
return QUrl::fromLocalFile(location());
}
/// Check that the given QFileInfo is context-insensitive to avoid
/// implicitly accessing any transient working directory when
/// resolving relative paths. We need to exclude these unintended
/// side-effects!
static bool hasLocation(const QFileInfo& fileInfo) {
DEBUG_ASSERT(QFileInfo().isRelative()); // special case (should be excluded)
return fileInfo.isAbsolute();
}
bool hasLocation() const {
return hasLocation(m_fileInfo);
}
/// Returns the permanent location of a file.
static QString location(const QFileInfo& fileInfo) {
DEBUG_ASSERT(hasLocation(fileInfo));
return fileInfo.absoluteFilePath();
}
QString location() const {
return location(m_fileInfo);
}
/// The directory part of the location, i.e. excluding the file name.
static QString locationPath(const QFileInfo& fileInfo) {
DEBUG_ASSERT(hasLocation(fileInfo));
return fileInfo.absolutePath();
}
QString locationPath() const {
return locationPath(m_fileInfo);
}
/// Refresh the canonical location if it is still empty, i.e. if
/// the file may have re-appeared after mounting the corresponding
/// drive while Mixxx is already running.
///
/// We ignore the case when the user changes a symbolic link to
/// point a file to an other location, since this is a user action.
/// We also don't care if a file disappears while Mixxx is running.
/// Opening a non-existent file is already handled and doesn't cause
/// any malfunction.
///
/// Note: Refreshing will never invalidate the canonical location
/// once it has been set, even after the corresponding file has
/// been deleted! A non-empty canonical location is immutable and
/// does not disappear, other than by explicitly refresh()ing the
/// file info manually. See also: FileInfoTest
QString resolveCanonicalLocation();
/// Returns the current location of a physical file, i.e.
/// without aliasing by symbolic links and without any redundant
/// relative paths.
///
/// Does only access the file system if file metadata is not
/// already cached, depending on the caching mode of QFileInfo.
static QString canonicalLocation(const QFileInfo& fileInfo) {
DEBUG_ASSERT(hasLocation(fileInfo));
return fileInfo.canonicalFilePath();
}
QString canonicalLocation() const {
return canonicalLocation(m_fileInfo);
}
/// The directory part of the canonical location, i.e. excluding
/// the file name.
///
/// Does only access the file system if file metadata is not
/// already cached, depending on the caching mode of QFileInfo.
static QString canonicalLocationPath(const QFileInfo& fileInfo) {
DEBUG_ASSERT(hasLocation(fileInfo));
return fileInfo.canonicalPath();
}
QString canonicalLocationPath() const {
return canonicalLocationPath(m_fileInfo);
}
/// Decide if two canonical locations have a parent/child
/// relationship, i.e. if the sub location is contained
/// within the tree originating from the root location.
///
/// Both canonical locations must not be empty, otherwise
/// false is returned.
static bool isRootSubCanonicalLocation(
const QString& rootCanonicalLocation,
const QString& subCanonicalLocation);
/// Check if the file actually exists on the file system,
/// bypassing any internal caching.
bool checkFileExists() const {
// Using filePath() is faster than location()
return QFileInfo::exists(filePath());
}
void refresh() {
m_fileInfo.refresh();
}
bool exists() const {
return m_fileInfo.exists();
}
QString fileName() const {
return m_fileInfo.fileName();
}
QString baseName() const {
return m_fileInfo.baseName();
}
QString completeBaseName() const {
return m_fileInfo.completeBaseName();
}
QDateTime birthTime() const {
return m_fileInfo.birthTime();
}
QDateTime lastModified() const {
return m_fileInfo.lastModified();
}
// Both isFile() and isDir() might return false, but they
// will never return true at the same time, i.e. consider
// false negatives when using these functions.
bool isFile() const {
return m_fileInfo.isFile();
}
bool isDir() const {
return m_fileInfo.isDir();
}
bool isReadable() const {
return m_fileInfo.isReadable();
}
bool isWritable() const {
return m_fileInfo.isWritable();
}
bool isAbsolute() const {
return m_fileInfo.isAbsolute();
}
/// Note: An empty file path is relative!
bool isRelative() const {
return m_fileInfo.isRelative();
}
QString suffix() const {
return m_fileInfo.suffix();
}
QString completeSuffix() const {
return m_fileInfo.completeSuffix();
}
/// Query the file size in bytes.
///
/// Note: The longer name of this method compared to QFileInfo::size()
/// has been chosen deliberately.
qint64 sizeInBytes() const {
return m_fileInfo.size();
}
friend bool operator==(const FileInfo& lhs, const FileInfo& rhs) {
return lhs.m_fileInfo == rhs.m_fileInfo;
}
friend QDebug operator<<(QDebug dbg, const mixxx::FileInfo& arg) {
return dbg << arg.m_fileInfo;
}
private:
FRIEND_TEST(FileInfoTest, freshCanonicalFileInfo);
// The internal file path should only be used for implementation
// purpose. Using location() instead of filePath() is recommended
// for all public use cases.
QString filePath() const {
return m_fileInfo.filePath();
}
QFileInfo m_fileInfo;
};
inline bool operator!=(const FileInfo& lhs, const FileInfo& rhs) {
return !(lhs == rhs);
}
} // namespace mixxx
Q_DECLARE_TYPEINFO(mixxx::FileInfo, Q_MOVABLE_TYPE); // QFileInfo is movable
Q_DECLARE_METATYPE(mixxx::FileInfo)