-
Notifications
You must be signed in to change notification settings - Fork 920
Expand file tree
/
Copy pathfilesystembase.cpp
More file actions
952 lines (845 loc) · 32.9 KB
/
filesystembase.cpp
File metadata and controls
952 lines (845 loc) · 32.9 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2018 ownCloud GmbH
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "filesystembase.h"
#include "utility.h"
#include "common/asserts.h"
#include <QDateTime>
#include <QDir>
#include <QUrl>
#include <QFile>
#include <QCoreApplication>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef Q_OS_WIN
#include <windows.h>
#include <windef.h>
#include <winbase.h>
#include <fcntl.h>
#include <io.h>
#include <securitybaseapi.h>
#include <aclapi.h>
#include <sddl.h>
#endif
namespace OCC {
Q_LOGGING_CATEGORY(lcFileSystem, "nextcloud.sync.filesystem", QtInfoMsg)
QString FileSystem::longWinPath(const QString &inpath)
{
#ifdef Q_OS_WIN
return pathtoUNC(inpath);
#else
return inpath;
#endif
}
void FileSystem::setFileHidden(const QString &filename, bool hidden)
{
if (filename.isEmpty()) {
return;
}
#ifdef _WIN32
const QString fName = longWinPath(filename);
DWORD dwAttrs = 0;
dwAttrs = GetFileAttributesW((wchar_t *)fName.utf16());
if (dwAttrs != INVALID_FILE_ATTRIBUTES) {
if (hidden && !(dwAttrs & FILE_ATTRIBUTE_HIDDEN)) {
SetFileAttributesW((wchar_t *)fName.utf16(), dwAttrs | FILE_ATTRIBUTE_HIDDEN);
} else if (!hidden && (dwAttrs & FILE_ATTRIBUTE_HIDDEN)) {
SetFileAttributesW((wchar_t *)fName.utf16(), dwAttrs & ~FILE_ATTRIBUTE_HIDDEN);
}
}
#else
Q_UNUSED(filename);
Q_UNUSED(hidden);
#endif
}
bool FileSystem::isFileHidden(const QString &filename)
{
#ifdef _WIN32
if (isLnkFile(filename)) {
const QString fName = longWinPath(filename);
DWORD dwAttrs = 0;
dwAttrs = GetFileAttributesW((wchar_t *)fName.utf16());
if (dwAttrs == INVALID_FILE_ATTRIBUTES) {
return false;
}
return dwAttrs & FILE_ATTRIBUTE_HIDDEN;
}
#endif
return QFileInfo(filename).isHidden();
}
static QFile::Permissions getDefaultWritePermissions()
{
QFile::Permissions result = QFile::WriteUser;
#ifndef Q_OS_WIN
mode_t mask = umask(0);
umask(mask);
if (!(mask & S_IWGRP)) {
result |= QFile::WriteGroup;
}
if (!(mask & S_IWOTH)) {
result |= QFile::WriteOther;
}
#endif
return result;
}
void FileSystem::setFileReadOnly(const QString &filename, bool readonly)
{
#ifdef Q_OS_WIN
if (!fileExists(filename)) {
Q_ASSERT(false);
return;
}
const auto windowsFilename = longWinPath(filename);
const auto rawWindowsFilename = reinterpret_cast<const wchar_t *>(windowsFilename.utf16());
const auto fileAttributes = GetFileAttributesW(rawWindowsFilename);
if (fileAttributes == INVALID_FILE_ATTRIBUTES) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem()).nospace() << "GetFileAttributesW failed, action=" << (readonly ? "readonly" : "read write") << " filename=" << windowsFilename << " lastError=" << lastError << " errorMessage=" << Utility::formatWinError(lastError);
return;
}
auto newFileAttributes = fileAttributes;
if (readonly) {
// replace any existing access denied ACE on this object with one that allows us to at least modify the file attributes
setAclPermission(filename, FileSystem::FolderPermissions::ReadOnly);
newFileAttributes = newFileAttributes | FILE_ATTRIBUTE_READONLY;
} else {
// remove the access denied ACE from this object in case we have a too restrictive setting that does not allow to modify file attributes
setAclPermission(filename, FileSystem::FolderPermissions::ReadWrite);
newFileAttributes = newFileAttributes & (~FILE_ATTRIBUTE_READONLY);
}
if (SetFileAttributesW(rawWindowsFilename, newFileAttributes) == 0) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem()).nospace() << "SetFileAttributesW failed, action=" << (readonly ? "readonly" : "read write") << " filename=" << windowsFilename << " lastError=" << lastError << " errorMessage=" << Utility::formatWinError(lastError);
}
return;
#endif
QFile file(filename);
QFile::Permissions permissions = file.permissions();
QFile::Permissions allWritePermissions = QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther | QFile::WriteOwner;
static QFile::Permissions defaultWritePermissions = getDefaultWritePermissions();
permissions &= ~allWritePermissions;
if (!readonly) {
permissions |= defaultWritePermissions;
}
file.setPermissions(permissions);
}
void FileSystem::setFolderMinimumPermissions(const QString &filename)
{
#ifdef Q_OS_MACOS
QFile::Permissions perm = QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner;
QFile file(filename);
file.setPermissions(perm);
#else
Q_UNUSED(filename);
#endif
}
bool FileSystem::setFileReadOnlyWeak(const QString &filename, bool readonly)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
try {
const auto permissions = filePermissionsWin(filename);
if (!readonly && static_cast<bool>((permissions & std::filesystem::perms::owner_write))) {
return false; // already writable enough
}
setFileReadOnly(filename, readonly);
return true;
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << filename << (readonly ? "readonly" : "read write") << e.what();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << filename << e.what();
}
catch (...)
{
qCWarning(lcFileSystem()) << filename;
}
return false;
}
#endif
QFile file(filename);
QFile::Permissions permissions = file.permissions();
if (!readonly && (permissions & QFile::WriteOwner)) {
return false; // already writable enough
}
setFileReadOnly(filename, readonly);
return true;
}
bool FileSystem::rename(const QString &originFileName,
const QString &destinationFileName,
QString *errorString)
{
bool success = false;
QString error;
#ifdef Q_OS_WIN
QString orig = longWinPath(originFileName);
QString dest = longWinPath(destinationFileName);
if (isLnkFile(originFileName) || isLnkFile(destinationFileName)) {
success = MoveFileEx((wchar_t *)orig.utf16(),
(wchar_t *)dest.utf16(),
MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH);
if (!success) {
error = Utility::formatWinError(GetLastError());
}
} else
#endif
{
QFile orig(originFileName);
success = orig.rename(destinationFileName);
if (!success) {
error = orig.errorString();
}
}
if (!success) {
qCWarning(lcFileSystem) << "Error renaming file" << originFileName
<< "to" << destinationFileName
<< "failed: " << error;
if (errorString) {
*errorString = error;
}
}
return success;
}
bool FileSystem::openAndSeekFileSharedRead(QFile *file, QString *errorOrNull, qint64 seek)
{
QString errorDummy;
// avoid many if (errorOrNull) later.
QString &error = errorOrNull ? *errorOrNull : errorDummy;
error.clear();
#ifdef Q_OS_WIN
//
// The following code is adapted from Qt's QFSFileEnginePrivate::nativeOpen()
// by including the FILE_SHARE_DELETE share mode.
//
// Enable full sharing.
DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
int accessRights = GENERIC_READ;
DWORD creationDisp = OPEN_EXISTING;
// Create the file handle.
SECURITY_ATTRIBUTES securityAtts = { sizeof(SECURITY_ATTRIBUTES), nullptr, FALSE };
QString fName = longWinPath(file->fileName());
HANDLE fileHandle = CreateFileW(
(const wchar_t *)fName.utf16(),
accessRights,
shareMode,
&securityAtts,
creationDisp,
FILE_ATTRIBUTE_NORMAL,
nullptr);
// Bail out on error.
if (fileHandle == INVALID_HANDLE_VALUE) {
error = qt_error_string();
return false;
}
// Convert the HANDLE to an fd and pass it to QFile's foreign-open
// function. The fd owns the handle, so when QFile later closes
// the fd the handle will be closed too.
int fd = _open_osfhandle((intptr_t)fileHandle, _O_RDONLY);
if (fd == -1) {
error = QStringLiteral("could not make fd from handle");
CloseHandle(fileHandle);
return false;
}
if (!file->open(fd, QIODevice::ReadOnly, QFile::AutoCloseHandle)) {
error = file->errorString();
_close(fd); // implicitly closes fileHandle
return false;
}
// Seek to the right spot
auto li = reinterpret_cast<LARGE_INTEGER *>(&seek);
DWORD newFilePointer = SetFilePointer(fileHandle, li->LowPart, &li->HighPart, FILE_BEGIN);
if (newFilePointer == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
error = qt_error_string();
return false;
}
return true;
#else
if (!file->open(QFile::ReadOnly)) {
error = file->errorString();
return false;
}
if (!file->seek(seek)) {
error = file->errorString();
return false;
}
return true;
#endif
}
QString FileSystem::joinPath(const QString& path, const QString& file)
{
if (path.isEmpty()) {
qCWarning(lcFileSystem).nospace() << "joinPath called with an empty path; returning file=" << file;
return QDir::toNativeSeparators(file);
}
if (file.isEmpty()) {
qCWarning(lcFileSystem).nospace() << "joinPath called with an empty file; returning path=" << path;
return QDir::toNativeSeparators(path);
}
if (const auto lastChar = path[path.size() - 1]; lastChar == QLatin1Char{'/'} || lastChar == QLatin1Char{'\\'}) {
return QDir::toNativeSeparators(path + file);
}
return QDir::toNativeSeparators(path + QDir::separator() + file);
}
#ifdef Q_OS_WIN
std::filesystem::perms FileSystem::filePermissionsWinSymlinkSafe(const QString &filename)
{
try {
return std::filesystem::symlink_status(filename.toStdWString()).permissions();
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink" << e.what() << "- path1:" << e.path1().c_str() << "- path2:" << e.path2().c_str();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink" << e.what() << "- path:" << filename;
}
catch (...)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink - path:" << filename;
}
return {};
}
std::filesystem::perms FileSystem::filePermissionsWin(const QString &filename)
{
try {
return std::filesystem::status(filename.toStdWString()).permissions();
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink" << e.what() << "- path1:" << e.path1().c_str() << "- path2:" << e.path2().c_str();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink" << e.what() << "- path:" << filename;
}
catch (...)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink - path:" << filename;
}
return {};
}
void FileSystem::setFilePermissionsWin(const QString &filename, const std::filesystem::perms &perms)
{
if (!fileExists(filename)) {
return;
}
try {
std::filesystem::permissions(filename.toStdWString(), perms);
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink" << e.what() << "- path1:" << e.path1().c_str() << "- path2:" << e.path2().c_str();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink" << e.what() << "- path:" << filename;
}
catch (...)
{
qCWarning(lcFileSystem()) << "exception when checking permissions of symlink - path:" << filename;
}
}
static bool fileExistsWin(const QString &filename)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = nullptr;
const QString fName = FileSystem::longWinPath(filename);
hFind = FindFirstFileW((wchar_t *)fName.utf16(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
FindClose(hFind);
return true;
}
static bool isDirWin(const QString &filename)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = nullptr;
const QString fName = FileSystem::longWinPath(filename);
hFind = FindFirstFileW((wchar_t *)fName.utf16(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
FindClose(hFind);
return FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
}
static bool isFileWin(const QString &filename)
{
return !isDirWin(filename);
}
#endif
bool FileSystem::fileExists(const QString &filename, const QFileInfo &fileInfo)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
// Use a native check.
return fileExistsWin(filename);
}
#endif
bool re = fileInfo.exists();
// if the filename is different from the filename in fileInfo, the fileInfo is
// not valid. There needs to be one initialised here. Otherwise the incoming
// fileInfo is re-used.
if (fileInfo.filePath() != filename) {
re = QFileInfo::exists(filename);
}
return re;
}
bool FileSystem::isDir(const QString &filename, const QFileInfo &fileInfo)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
// Use a native check.
return isDirWin(filename);
}
#endif
bool re = fileInfo.isDir();
// if the filename is different from the filename in fileInfo, the fileInfo is
// not valid. There needs to be one initialised here. Otherwise the incoming
// fileInfo is re-used.
if (fileInfo.filePath() != filename) {
QFileInfo myFI(filename);
re = myFI.isDir();
}
return re;
}
bool FileSystem::isFile(const QString &filename, const QFileInfo &fileInfo)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
// Use a native check.
return isFileWin(filename);
}
#endif
bool re = fileInfo.isDir();
// if the filename is different from the filename in fileInfo, the fileInfo is
// not valid. There needs to be one initialised here. Otherwise the incoming
// fileInfo is re-used.
if (fileInfo.filePath() != filename) {
QFileInfo myFI(filename);
re = myFI.isFile();
}
return re;
}
bool FileSystem::isWritable(const QString &filename, const QFileInfo &fileInfo)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
try {
const auto permissions = filePermissionsWin(filename);
return static_cast<bool>((permissions & std::filesystem::perms::owner_write));
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << filename << e.what();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << filename << e.what();
}
catch (...)
{
qCWarning(lcFileSystem()) << filename;
}
return false;
}
#endif
bool re = fileInfo.isWritable();
// if the filename is different from the filename in fileInfo, the fileInfo is
// not valid. There needs to be one initialised here. Otherwise the incoming
// fileInfo is re-used.
if (fileInfo.filePath() != filename) {
QFileInfo myFI(filename);
re = myFI.isWritable();
}
return re;
}
bool FileSystem::isReadable(const QString &filename, const QFileInfo &fileInfo)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
try {
const auto permissions = filePermissionsWin(filename);
return static_cast<bool>((permissions & std::filesystem::perms::owner_read));
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << filename << e.what();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << filename << e.what();
}
catch (...)
{
qCWarning(lcFileSystem()) << filename;
}
return false;
}
#endif
bool re = fileInfo.isReadable();
// if the filename is different from the filename in fileInfo, the fileInfo is
// not valid. There needs to be one initialised here. Otherwise the incoming
// fileInfo is re-used.
if (fileInfo.filePath() != filename) {
QFileInfo myFI(filename);
re = myFI.isReadable();
}
return re;
}
bool FileSystem::isSymLink(const QString &filename, const QFileInfo &fileInfo)
{
#ifdef Q_OS_WIN
if (isLnkFile(filename)) {
return isJunction(filename);
}
#endif
bool re = fileInfo.isSymLink();
// if the filename is different from the filename in fileInfo, the fileInfo is
// not valid. There needs to be one initialised here. Otherwise the incoming
// fileInfo is re-used.
if (fileInfo.filePath() != filename) {
QFileInfo myFI(filename);
re = myFI.isSymLink();
}
return re;
}
#ifdef Q_OS_WIN
QString FileSystem::fileSystemForPath(const QString &path)
{
// See also QStorageInfo (Qt >=5.4) and GetVolumeInformationByHandleW (>= Vista)
QString drive = path.left(2);
if (!drive.endsWith(QLatin1Char(':')))
return QString();
drive.append(QLatin1Char('\\'));
const size_t fileSystemBufferSize = 4096;
TCHAR fileSystemBuffer[fileSystemBufferSize];
if (!GetVolumeInformationW(
reinterpret_cast<LPCWSTR>(drive.utf16()),
nullptr, 0,
nullptr, nullptr, nullptr,
fileSystemBuffer, fileSystemBufferSize)) {
return QString();
}
return QString::fromUtf16(reinterpret_cast<const ushort *>(fileSystemBuffer));
}
#endif
bool FileSystem::remove(const QString &fileName, QString *errorString)
{
const auto &windowsSafeFileName = FileSystem::longWinPath(fileName);
#ifdef Q_OS_WIN
// You cannot delete a read-only file on windows, but we want to
// allow that.
setFileReadOnly(windowsSafeFileName, false);
#endif
const auto deletedFileInfo = QFileInfo{windowsSafeFileName};
if (!deletedFileInfo.exists()) {
qCWarning(lcFileSystem()) << windowsSafeFileName << "has been already deleted";
}
QFile f(windowsSafeFileName);
if (!f.remove()) {
if (errorString) {
*errorString = f.errorString();
}
qCWarning(lcFileSystem()) << f.errorString() << windowsSafeFileName;
#if defined Q_OS_WIN
try {
const auto permissionsDisplayHelper = [] (std::filesystem::perms currentPermissions) {
const auto unitaryHelper = [currentPermissions] (std::filesystem::perms testedPermission, char permissionChar) {
return (static_cast<bool>(currentPermissions & testedPermission) ? permissionChar : '-');
};
qCInfo(lcFileSystem()) << unitaryHelper(std::filesystem::perms::owner_read, 'r')
<< unitaryHelper(std::filesystem::perms::owner_write, 'w')
<< unitaryHelper(std::filesystem::perms::owner_exec, 'x')
<< unitaryHelper(std::filesystem::perms::group_read, 'r')
<< unitaryHelper(std::filesystem::perms::group_write, 'w')
<< unitaryHelper(std::filesystem::perms::group_exec, 'x')
<< unitaryHelper(std::filesystem::perms::others_read, 'r')
<< unitaryHelper(std::filesystem::perms::others_write, 'w')
<< unitaryHelper(std::filesystem::perms::others_exec, 'x');
};
const auto unsafeFilePermissions = filePermissionsWin(windowsSafeFileName);
permissionsDisplayHelper(unsafeFilePermissions);
const auto safeFilePermissions = filePermissionsWinSymlinkSafe(windowsSafeFileName);
permissionsDisplayHelper(safeFilePermissions);
}
catch (const std::filesystem::filesystem_error &e)
{
qCWarning(lcFileSystem()) << "exception when modifying permissions" << e.what() << "- path1:" << e.path1().c_str() << "- path2:" << e.path2().c_str();
}
catch (const std::system_error &e)
{
qCWarning(lcFileSystem()) << "exception when modifying permissions" << e.what() << "- path:" << windowsSafeFileName;
}
catch (...)
{
qCWarning(lcFileSystem()) << "exception when modifying permissions - path:" << windowsSafeFileName;
}
#endif
return false;
}
return true;
}
bool FileSystem::moveToTrash(const QString &fileName, QString *errorString)
{
QFile f(fileName);
if (!f.moveToTrash()) {
if (errorString) {
*errorString = f.errorString();
}
return false;
}
return true;
}
namespace {
/**
* This function creates a file handle with the desired LockMode
*/
#if defined Q_OS_WIN
Utility::Handle lockFile(const QString &fileName, FileSystem::LockMode mode)
{
const QString fName = FileSystem::longWinPath(fileName);
int shareMode = 0;
int accessMode = GENERIC_READ | GENERIC_WRITE;
switch (mode) {
case FileSystem::LockMode::Exclusive:
shareMode = 0;
break;
case FileSystem::LockMode::Shared:
shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
break;
case FileSystem::LockMode::SharedRead:
shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
accessMode = GENERIC_READ;
break;
}
// Check if file exists
DWORD attr = GetFileAttributesW(reinterpret_cast<const wchar_t *>(fName.utf16()));
if (attr != INVALID_FILE_ATTRIBUTES) {
// Try to open the file with as much access as possible..
auto out = Utility::Handle{CreateFileW(reinterpret_cast<const wchar_t *>(fName.utf16()), accessMode, shareMode, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, nullptr)};
if (out) {
LARGE_INTEGER start;
start.QuadPart = 0;
LARGE_INTEGER end;
end.QuadPart = -1;
if (LockFile(out.handle(), start.LowPart, start.HighPart, end.LowPart, end.HighPart)) {
return out;
} else {
return {};
}
}
return out;
}
return {};
}
#endif
}
bool FileSystem::isFileLocked(const QString &fileName, LockMode mode)
{
#ifdef Q_OS_WIN
const auto handle = lockFile(fileName, mode);
if (!handle) {
const auto error = GetLastError();
if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
return true;
} else if (error != ERROR_FILE_NOT_FOUND && error != ERROR_PATH_NOT_FOUND) {
qCWarning(lcFileSystem()) << Q_FUNC_INFO << Utility::formatWinError(error);
}
}
#else
Q_UNUSED(fileName);
Q_UNUSED(mode);
#endif
return false;
}
bool FileSystem::isLnkFile(const QString &filename)
{
return filename.endsWith(QLatin1String(".lnk"));
}
bool FileSystem::isExcludeFile(const QString &filename)
{
return filename.compare(QStringLiteral(".sync-exclude.lst"), Qt::CaseInsensitive) == 0
|| filename.compare(QStringLiteral("exclude.lst"), Qt::CaseInsensitive) == 0
|| filename.endsWith(QStringLiteral("/.sync-exclude.lst"), Qt::CaseInsensitive)
|| filename.endsWith(QStringLiteral("/exclude.lst"), Qt::CaseInsensitive);
}
bool FileSystem::isJunction(const QString &filename)
{
#ifdef Q_OS_WIN
WIN32_FIND_DATA findData;
HANDLE hFind = FindFirstFileEx(reinterpret_cast<const wchar_t *>(longWinPath(filename).utf16()), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0);
if (hFind != INVALID_HANDLE_VALUE) {
FindClose(hFind);
return false;
}
return findData.dwFileAttributes != INVALID_FILE_ATTRIBUTES
&& findData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT
&& findData.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
#else
Q_UNUSED(filename);
return false;
#endif
}
#ifdef Q_OS_WIN
QString FileSystem::pathtoUNC(const QString &_str)
{
Q_ASSERT(QFileInfo(_str).isAbsolute());
if (_str.isEmpty()) {
return _str;
}
const QString str = QDir::toNativeSeparators(_str);
const QLatin1Char sep('\\');
// we already have a unc path
if (str.startsWith(sep + sep)) {
return str;
}
// prepend \\?\ and to support long names
if (str.at(0) == sep) {
// should not happen as we require the path to be absolute
return QStringLiteral(R"(\\?)") + str;
}
return QStringLiteral(R"(\\?\)") + str;
}
bool FileSystem::setAclPermission(const QString &unsafePath, FolderPermissions permissions)
{
Utility::UniqueHandle fileHandle;
constexpr SECURITY_INFORMATION securityInfo = DACL_SECURITY_INFORMATION | READ_CONTROL | WRITE_DAC;
PACL resultDacl = nullptr; // this is a part of the `securityDescriptor` and won't need to be free
Utility::UniqueLocalFree<PSECURITY_DESCRIPTOR> securityDescriptor;
Utility::UniqueLocalFree<PSID> sid;
const auto path = longWinPath(unsafePath);
const auto rawPath = reinterpret_cast<const wchar_t *>(path.utf16());
const auto safePathFileInfo = QFileInfo{path};
// CreateFileW is known to work with long paths in the \\?\ variant
// MAXIMUM_ALLOWED will not propagate ACEs to children when setting the DACL
constexpr DWORD desiredAccess = READ_CONTROL | WRITE_DAC | MAXIMUM_ALLOWED;
constexpr DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
constexpr DWORD creationDisposition = OPEN_EXISTING;
constexpr DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT;
fileHandle.reset(CreateFileW(rawPath, desiredAccess, shareMode, nullptr, creationDisposition, flagsAndAttributes, nullptr));
if (fileHandle.get() == INVALID_HANDLE_VALUE) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "CreateFileW failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
{
PSECURITY_DESCRIPTOR securityDescriptorUnmanaged = nullptr;
if (const auto lastError = GetSecurityInfo(fileHandle.get(), SE_FILE_OBJECT, securityInfo, nullptr, nullptr, &resultDacl, nullptr, &securityDescriptorUnmanaged); lastError != ERROR_SUCCESS) {
qCWarning(lcFileSystem).nospace() << "GetSecurityInfo failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
securityDescriptor.reset(securityDescriptorUnmanaged);
}
if (!resultDacl) {
qCWarning(lcFileSystem).nospace() << "failed to retrieve DACL needed to set a folder read-only or read-write, path=" << path;
return false;
}
{
PSID sidUnmanaged = nullptr;
if (!ConvertStringSidToSidW(L"S-1-5-32-545", &sidUnmanaged)) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "ConvertStringSidToSidW failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
sid.reset(sidUnmanaged);
}
ACL_SIZE_INFORMATION aclSize;
if (!GetAclInformation(resultDacl, &aclSize, sizeof(aclSize), AclSizeInformation)) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "GetAclInformation failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
qCDebug(lcFileSystem).nospace() << "retrieved ACL size information"
<< " aclSize.AceCount=" << aclSize.AceCount
<< " aclSize.AclBytesInUse=" << aclSize.AclBytesInUse
<< " aclSize.AclBytesFree=" << aclSize.AclBytesFree;
auto newAclSize = aclSize.AclBytesInUse;
if (permissions == FileSystem::FolderPermissions::ReadOnly) {
// When marking a folder as read-only also allocate the size of the `ACCESS_DENIED_ACE` excluding the `SidStart`
// (`DWORD`) member, + the SID the ACE uses
// https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-initializeacl#remarks
//
// Changing the ACL to read-write will remove all previously defined `ACCESS_DENIED_ACE`s, reducing the ACL size.
// There is no need to allocate more than what is already present.
if (const auto accessDeniedAceSize = sizeof(ACCESS_DENIED_ACE) - sizeof(DWORD) + GetLengthSid(sid.get());
newAclSize + accessDeniedAceSize < 65532) {
// Apparently the maximum size of an ACL is 65532 bytes (quite close to u16_max). Any value above that
// causes the `InitialiceAcl()` function to fail with "WindowsError 57: The parameter is incorrect".
//
// Client versions < 4.0.2 had a bug where a new access denied ACE was always prepended when a folder was marked
// as read-only. This worked fine until the ACL was reaching that size limit ...
//
// Since 4.0.2 the client skips these duplicate access denied ACE entries. This however only works if the ACL
// wasn't already too big. In that case let's assume that the ACL has grown to that size due to this bug -- the
// current ACL is already large enough to hold the single access denied ACE.
newAclSize += accessDeniedAceSize;
}
}
std::unique_ptr<ACL> newDacl{reinterpret_cast<PACL>(new char[newAclSize])};
int newAceIndex = 0;
qCDebug(lcFileSystem) << "allocated a new DACL object of size" << newAclSize;
if (!InitializeAcl(newDacl.get(), newAclSize, ACL_REVISION)) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "InitializeAcl failed,"
<< " path=" << path
<< " aclSize.AclBytesInUse=" << aclSize.AclBytesInUse
<< " newAclSize=" << newAclSize
<< " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
if (permissions == FileSystem::FolderPermissions::ReadOnly) {
// the access denied ACE needs to appear at the start of the ACL
if (!AddAccessDeniedAceEx(newDacl.get(), ACL_REVISION, NO_PROPAGATE_INHERIT_ACE,
FILE_DELETE_CHILD | DELETE | FILE_WRITE_DATA | FILE_WRITE_EA | FILE_APPEND_DATA, sid.get())) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "AddAccessDeniedAceEx failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
newAceIndex++;
}
for (int currentAceIndex = 0; currentAceIndex < aclSize.AceCount; ++currentAceIndex) {
void *currentAce = nullptr;
if (!GetAce(resultDacl, currentAceIndex, ¤tAce)) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "GetAce failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
const auto currentAceHeader = reinterpret_cast<PACE_HEADER>(currentAce);
if (ACCESS_DENIED_ACE_TYPE == (currentAceHeader->AceType & ACCESS_DENIED_ACE_TYPE)) {
// skip any access denied ACEs from the previous ACL
// in case the item should be read-only the ACCESS_DENIED_ACE was already added before this loop
qCDebug(lcFileSystem).nospace() << "skipping AceHeader of type ACCESS_DENIED_ACE_TYPE"
<< " path=" << path
<< " AceFlags=" << currentAceHeader->AceFlags
<< " AceSize=" << currentAceHeader->AceSize
<< " AceType=" << currentAceHeader->AceType;
// no need to increment newAceIndex
continue;
}
if (!AddAce(newDacl.get(), ACL_REVISION, newAceIndex, currentAce, currentAceHeader->AceSize)) {
const auto lastError = GetLastError();
qCWarning(lcFileSystem).nospace() << "AddAce failed,"
<< " path=" << path
<< " errorMessage=" << Utility::formatWinError(lastError)
<< " newAclSize=" << newAclSize
<< " newAceIndex=" << newAceIndex;
return false;
}
newAceIndex++;
}
if (const auto lastError = SetSecurityInfo(fileHandle.get(), SE_FILE_OBJECT, PROTECTED_DACL_SECURITY_INFORMATION | securityInfo, nullptr, nullptr, newDacl.get(), nullptr); lastError != ERROR_SUCCESS) {
qCWarning(lcFileSystem).nospace() << "SetSecurityInfo failed, path=" << path << " errorMessage=" << Utility::formatWinError(lastError);
return false;
}
return true;
}
#endif
} // namespace OCC