-
Notifications
You must be signed in to change notification settings - Fork 924
Expand file tree
/
Copy pathutility.h
More file actions
452 lines (384 loc) · 15 KB
/
utility.h
File metadata and controls
452 lines (384 loc) · 15 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud GmbH
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef UTILITY_H
#define UTILITY_H
#include "csync/ocsynclib.h"
#include <QString>
#include <QByteArray>
#include <QDateTime>
#include <QElapsedTimer>
#include <QLoggingCategory>
#include <QMap>
#include <QUrl>
#include <QUrlQuery>
#include <functional>
#include <memory>
#ifdef Q_OS_WIN
#include <QRect>
#include <windows.h>
#endif
class QSettings;
namespace OCC {
class SyncJournal;
Q_DECLARE_LOGGING_CATEGORY(lcUtility)
/** \addtogroup libsync
* @{
*/
namespace Utility {
struct ProcessInfosForOpenFile {
ulong processId;
QString processName;
};
/**
* @brief Queries the OS for processes that are keeping the file open(using it)
*
* @param filePath absolute file path
* @return list of ProcessInfosForOpenFile
*/
OCSYNC_EXPORT QVector<ProcessInfosForOpenFile> queryProcessInfosKeepingFileOpen(const QString &filePath);
#ifdef Q_OS_WIN
class OCSYNC_EXPORT Handle
{
public:
/**
* A RAAI for Windows Handles
*/
Handle() = default;
explicit Handle(HANDLE h);
explicit Handle(HANDLE h, std::function<void(HANDLE)> &&close);
Handle(const Handle &) = delete;
Handle &operator=(const Handle &) = delete;
Handle(Handle &&other)
{
std::swap(_handle, other._handle);
std::swap(_close, other._close);
}
Handle &operator=(Handle &&other)
{
if (this != &other) {
std::swap(_handle, other._handle);
std::swap(_close, other._close);
}
return *this;
}
~Handle();
HANDLE &handle() { return _handle; }
void close();
explicit operator bool() const { return _handle != INVALID_HANDLE_VALUE; }
operator HANDLE() const { return _handle; }
private:
HANDLE _handle = INVALID_HANDLE_VALUE;
std::function<void(HANDLE)> _close;
};
#endif
OCSYNC_EXPORT int rand();
OCSYNC_EXPORT void sleep(int sec);
OCSYNC_EXPORT void usleep(int usec);
OCSYNC_EXPORT QString formatFingerprint(const QByteArray &, bool colonSeparated = true);
/**
* @brief Create favorite link for sync folder with application name and icon
*
* @param folder absolute file path to folder
*/
OCSYNC_EXPORT void setupFavLink(const QString &folder);
/**
* @brief Migrate favorite link for sync folder with new application name and icon
*
* @param folder absolute file path to folder
*/
OCSYNC_EXPORT void migrateFavLink(const QString &folder);
/**
* @brief Creates or overwrite the Desktop.ini file to use new folder IconResource shown as a favorite link
*
* @param folder absolute file path to folder
* @param localizedResourceName new folder name to be used as display name (migration)
*/
OCSYNC_EXPORT void setupDesktopIni(const QString &folder, const QString localizedResourceName = {});
/**
* @brief Removes the Desktop.ini file which contains the folder IconResource shown as a favorite link
*
* @param folder absolute file path to folder
*/
OCSYNC_EXPORT void removeFavLink(const QString &folder);
/**
* @brief Return the display name of a folder - to be used in fav links and sync root name (VFS).x
* e.g. Nextcloud1 will become NewAppName1, NewAppName2 or FolderName will be kept as is.
*
* @param currentDisplayName current folder display name string
* @param newName new name to be used for the folder
*/
OCSYNC_EXPORT QString syncFolderDisplayName(const QString ¤tDisplayName, const QString &newName);
// convenience system path to links folder
OCSYNC_EXPORT QString systemPathToLinks();
OCSYNC_EXPORT bool writeRandomFile(const QString &fname, int size = -1);
OCSYNC_EXPORT QString octetsToString(const qint64 octets);
OCSYNC_EXPORT QByteArray userAgentString();
OCSYNC_EXPORT QByteArray friendlyUserAgentString();
/**
* @brief Return whether launch on startup is enabled system wide.
*
* If this returns true, the checkbox for user specific launch
* on startup will be hidden.
*
* Currently only implemented on Windows.
*/
OCSYNC_EXPORT bool hasSystemLaunchOnStartup(const QString &appName);
OCSYNC_EXPORT bool hasLaunchOnStartup(const QString &appName);
OCSYNC_EXPORT void setLaunchOnStartup(const QString &appName, const QString &guiName, const bool launch);
OCSYNC_EXPORT uint convertSizeToUint(size_t &convertVar);
OCSYNC_EXPORT int convertSizeToInt(size_t &convertVar);
#ifdef Q_OS_WIN
OCSYNC_EXPORT DWORD convertSizeToDWORD(size_t &convertVar);
#endif
/**
* Return the amount of free space available.
*
* \a path must point to a directory
*/
OCSYNC_EXPORT qint64 freeDiskSpace(const QString &path);
/**
* @brief compactFormatDouble - formats a double value human readable.
*
* @param value the value to format.
* @param prec the precision.
* @param unit an optional unit that is appended if present.
* @return the formatted string.
*/
OCSYNC_EXPORT QString compactFormatDouble(double value, int prec, const QString &unit = QString());
// porting methods
OCSYNC_EXPORT QString escape(const QString &);
// conversion function QDateTime <-> time_t (because the ones builtin work on only unsigned 32bit)
OCSYNC_EXPORT QDateTime qDateTimeFromTime_t(qint64 t);
OCSYNC_EXPORT qint64 qDateTimeToTime_t(const QDateTime &t);
/**
* @brief Convert milliseconds duration to human readable string.
* @param quint64 msecs the milliseconds to convert to string.
* @return an HMS representation of the milliseconds value.
*
* durationToDescriptiveString1 describes the duration in a single
* unit, like "5 minutes" or "2 days".
*
* durationToDescriptiveString2 uses two units where possible, so
* "5 minutes 43 seconds" or "1 month 3 days".
*/
OCSYNC_EXPORT QString durationToDescriptiveString1(quint64 msecs);
OCSYNC_EXPORT QString durationToDescriptiveString2(quint64 msecs);
/**
* @brief hasDarkSystray - determines whether the systray is dark or light.
*
* Use this to check if the OS has a dark or a light systray.
*
* The value might change during the execution of the program
* (e.g. on OS X 10.10).
*
* @return bool which is true for systems with dark systray.
*/
OCSYNC_EXPORT bool hasDarkSystray();
// convenience OS detection methods
constexpr bool isWindows();
constexpr bool isMac();
constexpr bool isUnix();
constexpr bool isLinux(); // use with care
constexpr bool isBSD(); // use with care, does not match OS X
OCSYNC_EXPORT QString platformName();
// crash helper for --debug
OCSYNC_EXPORT void crash();
// Case preserving file system underneath?
// if this function returns true, the file system is case preserving,
// that means "test" means the same as "TEST" for filenames.
// if false, the two cases are two different files.
OCSYNC_EXPORT bool fsCasePreserving();
// Check if two paths that MUST exist are equal. This function
// uses QDir::canonicalPath() to judge and cares for the systems
// case sensitivity.
OCSYNC_EXPORT bool fileNamesEqual(const QString &fn1, const QString &fn2);
// Call the given command with the switch --version and rerun the first line
// of the output.
// If command is empty, the function calls the running application which, on
// Linux, might have changed while this one is running.
// For Mac and Windows, it returns QString()
OCSYNC_EXPORT QByteArray versionOfInstalledBinary(const QString &command = QString());
OCSYNC_EXPORT QString fileNameForGuiUse(const QString &fName);
OCSYNC_EXPORT QByteArray normalizeEtag(QByteArray etag);
/**
* @brief timeAgoInWords - human readable time span
*
* Use this to get a string that describes the timespan between the first and
* the second timestamp in a human readable and understandable form.
*
* If the second parameter is omitted, the current time is used.
*/
OCSYNC_EXPORT QString timeAgoInWords(const QDateTime &dt, const QDateTime &from = QDateTime());
class OCSYNC_EXPORT StopWatch
{
private:
QMap<QString, quint64> _lapTimes;
QDateTime _startTime;
QElapsedTimer _timer;
public:
void start();
quint64 stop();
quint64 addLapTime(const QString &lapName);
void reset();
// out helpers, return the measured times.
[[nodiscard]] QDateTime startTime() const;
[[nodiscard]] QDateTime timeOfLap(const QString &lapName) const;
[[nodiscard]] quint64 durationOfLap(const QString &lapName) const;
};
/**
* @brief Sort a QStringList in a way that's appropriate for filenames
*/
OCSYNC_EXPORT void sortFilenames(QStringList &fileNames);
/** Appends concatPath and queryItems to the url */
OCSYNC_EXPORT QUrl concatUrlPath(
const QUrl &url, const QString &concatPath,
const QUrlQuery &queryItems = {});
/** Returns a new settings pre-set in a specific group. The Settings will be created
with the given parent. If no parent is specified, the caller must destroy the settings */
OCSYNC_EXPORT std::unique_ptr<QSettings> settingsWithGroup(const QString &group, QObject *parent = nullptr);
/** Sanitizes a string that shall become part of a filename.
*
* Filters out reserved characters like
* - unicode control and format characters
* - reserved characters: /, ?, <, >, \, :, *, |, and "
*
* Warning: This does not sanitize the whole resulting string, so
* - unix reserved filenames ('.', '..')
* - trailing periods and spaces
* - windows reserved filenames ('CON' etc)
* will pass unchanged.
*/
OCSYNC_EXPORT QString sanitizeForFileName(const QString &name);
/** Returns a file name based on \a fn that's suitable for a conflict.
*/
OCSYNC_EXPORT QString makeConflictFileName(
const QString &fn, const QDateTime &dt, const QString &user);
OCSYNC_EXPORT QString makeCaseClashConflictFileName(const QString &filename, const QDateTime &datetime);
/** Returns whether a file name indicates a conflict file
*/
bool isConflictFile(const char *name) = delete;
OCSYNC_EXPORT bool isConflictFile(const QString &name);
OCSYNC_EXPORT bool isCaseClashConflictFile(const QString &name);
/** Find the base name for a conflict file name, using name pattern only
*
* Will return an empty string if it's not a conflict file.
*
* Prefer to use the data from the conflicts table in the journal to determine
* a conflict's base file, see SyncJournal::conflictFileBaseName()
*/
OCSYNC_EXPORT QByteArray conflictFileBaseNameFromPattern(const QByteArray &conflictName);
/**
* @brief Check whether the path is a root of a Windows drive partition ([c:/, d:/, e:/, etc.)
*/
OCSYNC_EXPORT bool isPathWindowsDrivePartitionRoot(const QString &path);
/**
* @brief Retrieves current logged-in user name from the OS
*/
OCSYNC_EXPORT QString getCurrentUserName();
/**
* @brief Registers the desktop app as a handler for a custom URI to enable local editing
*/
OCSYNC_EXPORT void registerUriHandlerForLocalEditing();
OCSYNC_EXPORT QString leadingSlashPath(const QString &path);
OCSYNC_EXPORT QString trailingSlashPath(const QString &path);
OCSYNC_EXPORT QString noLeadingSlashPath(const QString &path);
OCSYNC_EXPORT QString noTrailingSlashPath(const QString &path);
OCSYNC_EXPORT QString fullRemotePathToRemoteSyncRootRelative(const QString &fullRemotePath, const QString &remoteSyncRoot);
#ifdef Q_OS_WIN
OCSYNC_EXPORT bool registryKeyExists(HKEY hRootKey, const QString &subKey);
OCSYNC_EXPORT QVariant registryGetKeyValue(HKEY hRootKey, const QString &subKey, const QString &valueName);
OCSYNC_EXPORT bool registrySetKeyValue(HKEY hRootKey, const QString &subKey, const QString &valueName, DWORD type, const QVariant &value);
OCSYNC_EXPORT bool registryDeleteKeyTree(HKEY hRootKey, const QString &subKey);
OCSYNC_EXPORT bool registryDeleteKeyValue(HKEY hRootKey, const QString &subKey, const QString &valueName);
OCSYNC_EXPORT bool registryWalkSubKeys(HKEY hRootKey, const QString &subKey, const std::function<void(HKEY, const QString &)> &callback);
OCSYNC_EXPORT bool registryWalkValues(HKEY hRootKey, const QString &subKey, const std::function<void(const QString &, bool *)> &callback);
OCSYNC_EXPORT QRect getTaskbarDimensions();
OCSYNC_EXPORT void UnixTimeToLargeIntegerFiletime(time_t t, LARGE_INTEGER *hundredNSecs);
OCSYNC_EXPORT QString formatWinError(long error);
OCSYNC_EXPORT bool canCreateFileInPath(const QString &path);
class OCSYNC_EXPORT NtfsPermissionLookupRAII
{
public:
/**
* NTFS permissions lookup is disabled by default for performance reasons
* Enable it and disable it again once we leave the scope
* https://doc.qt.io/Qt-5/qfileinfo.html#ntfs-permissions
*/
NtfsPermissionLookupRAII();
~NtfsPermissionLookupRAII();
private:
Q_DISABLE_COPY(NtfsPermissionLookupRAII);
};
/**
* Closes a Win32 HANDLE if the HANDLE is valid (i.e. not `INVALID_HANDLE_VALUE`).
*/
struct OCSYNC_EXPORT HandleDeleter {
typedef HANDLE pointer; // HANDLEs are not really pointers even though they're treated as such
void operator()(HANDLE handle) const;
};
/**
* A `std::unique_ptr` that automatically closes a HANDLE.
*/
using UniqueHandle = std::unique_ptr<HANDLE, HandleDeleter>;
/**
* Releases a pointer previously allocated by `LocalAlloc`.
*/
struct OCSYNC_EXPORT LocalFreeDeleter {
void operator()(void *p) const;
};
/**
* A `std::unique_ptr` that automatically cleans up `P*` types (e.g. `PSID`).
*
* Use this whenever the Win32 API docs of a given function tell you to free a returned buffer
* by calling the `LocalFree` function.
*/
template<typename T>
using UniqueLocalFree = std::unique_ptr<typename std::remove_pointer<T>::type, LocalFreeDeleter>;
#endif
}
/** @} */ // \addtogroup
inline constexpr bool Utility::isWindows()
{
#ifdef Q_OS_WIN
return true;
#else
return false;
#endif
}
inline constexpr bool Utility::isMac()
{
#ifdef Q_OS_MACOS
return true;
#else
return false;
#endif
}
inline constexpr bool Utility::isUnix()
{
#ifdef Q_OS_UNIX
return true;
#else
return false;
#endif
}
inline constexpr bool Utility::isLinux()
{
#if defined(Q_OS_LINUX)
return true;
#else
return false;
#endif
}
inline constexpr bool Utility::isBSD()
{
#if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
return true;
#else
return false;
#endif
}
}
#endif // UTILITY_H