/
os_functions_win.cpp
1821 lines (1569 loc) · 41.6 KB
/
os_functions_win.cpp
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011-2016 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "os_functions.h"
#include "../stringtools.h"
#ifdef _WIN_PRE_VISTA
#define _WIN32_WINNT 0x0500
#endif
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <algorithm>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <time.h>
#include <assert.h>
#include <ws2tcpip.h>
#include <stack>
#ifdef USE_NTFS_TXF
#include <KtmW32.h>
#endif
#include <shlwapi.h>
#include "server_compat.h"
//For PathIsRelative
#pragma comment(lib, "Shlwapi.lib")
namespace
{
#define REPARSE_MOUNTPOINT_HEADER_SIZE 8
typedef struct {
DWORD ReparseTag;
DWORD ReparseDataLength;
WORD Reserved;
WORD ReparseTargetLength;
WORD ReparseTargetMaximumLength;
WORD Reserved1;
WCHAR ReparseTarget[1];
} REPARSE_MOUNTPOINT_DATA_BUFFER, *PREPARSE_MOUNTPOINT_DATA_BUFFER;
typedef struct _REPARSE_DATA_BUFFER {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
union {
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
UCHAR DataBuffer[1];
} GenericReparseBuffer;
};
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
namespace usn
{
typedef struct {
DWORD RecordLength;
WORD MajorVersion;
WORD MinorVersion;
BYTE FileReferenceNumber[16];
BYTE ParentFileReferenceNumber[16];
USN Usn;
LARGE_INTEGER TimeStamp;
DWORD Reason;
DWORD SourceInfo;
DWORD SecurityId;
DWORD FileAttributes;
WORD FileNameLength;
WORD FileNameOffset;
WCHAR FileName[1];
} USN_RECORD_V3, *PUSN_RECORD_V3;
}
}
void getMousePos(int &x, int &y)
{
POINT mousepos;
GetCursorPos(&mousepos);
x=mousepos.x;
y=mousepos.y;
}
const int64 WINDOWS_TICK=10000000;
const int64 SEC_TO_UNIX_EPOCH=11644473600LL;
bool os_is_symlink_int(const std::string& path);
int64 os_windows_to_unix_time(int64 windows_filetime)
{
return windows_filetime / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
}
int64 os_to_windows_filetime(int64 unix_time)
{
return (unix_time+SEC_TO_UNIX_EPOCH)*WINDOWS_TICK;
}
std::vector<SFile> getFiles(const std::string &path, bool *has_error, bool ignore_other_fs)
{
return getFilesWin(path, has_error, true, false, ignore_other_fs);
}
std::wstring os_file_prefix(std::wstring path)
{
if(path.size()>=2 && path[0]=='\\' && path[1]=='\\' )
{
if(path.size()>=3 && path[2]=='?')
{
return path;
}
else
{
return L"\\\\?\\UNC"+path.substr(1);
}
}
else
return L"\\\\?\\"+path;
}
bool is_volume(std::string target)
{
if (next(target, 0, "\\??\\"))
{
target = target.substr(4);
}
target = strlower(target);
if (next(target, 0, "volume")
&& getafter("\\", target).empty())
{
return true;
}
return false;
}
std::vector<SFile> getFilesWin(const std::string &path, bool *has_error,
bool exact_filesize, bool with_usn, bool ignore_other_fs)
{
if(has_error!=NULL)
{
*has_error=false;
}
std::vector<char> usn_buffer;
usn_buffer.resize(1024);
std::vector<SFile> tmp;
HANDLE fHandle;
WIN32_FIND_DATAW wfd;
std::wstring tpath=ConvertToWchar(path);
if(!tpath.empty() && tpath[tpath.size()-1]=='\\' ) tpath.erase(tpath.size()-1, 1);
fHandle=FindFirstFileW((tpath+L"\\*").c_str(),&wfd);
if(fHandle==INVALID_HANDLE_VALUE)
{
if(tpath.find(L"\\\\?\\UNC")==0)
{
tpath.erase(0, 7);
tpath=L"\\"+tpath;
fHandle=FindFirstFileW((tpath+L"\\*").c_str(),&wfd);
}
else if(tpath.find(L"\\\\?\\")==0)
{
tpath.erase(0, 4);
fHandle=FindFirstFileW((tpath+L"\\*").c_str(),&wfd);
}
if(fHandle==INVALID_HANDLE_VALUE)
{
if(has_error!=NULL)
{
*has_error=true;
}
return tmp;
}
}
do
{
SFile f;
f.name=ConvertFromWchar(wfd.cFileName);
if(f.name=="." || f.name==".." )
continue;
f.usn=0;
f.isdir=(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0;
LARGE_INTEGER lwt;
lwt.HighPart=wfd.ftLastWriteTime.dwHighDateTime;
lwt.LowPart=wfd.ftLastWriteTime.dwLowDateTime;
f.last_modified=os_windows_to_unix_time(lwt.QuadPart);
LARGE_INTEGER size;
size.HighPart=wfd.nFileSizeHigh;
size.LowPart=wfd.nFileSizeLow;
f.size=size.QuadPart;
lwt.HighPart=wfd.ftCreationTime.dwHighDateTime;
lwt.LowPart=wfd.ftCreationTime.dwLowDateTime;
f.created=os_windows_to_unix_time(lwt.QuadPart);
std::string reparse_target;
if (ignore_other_fs
&& wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT
&& wfd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT
&& os_get_symlink_target(os_file_prefix(path + os_file_sep() + f.name), reparse_target)
&& is_volume(reparse_target) )
{
continue;
}
else if(wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT
&& (wfd.dwReserved0== IO_REPARSE_TAG_SYMLINK
|| wfd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT) )
{
f.issym=true;
f.isspecialf=true;
}
else if (wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT
&& (wfd.dwReserved0 == 0x8000001b /*IO_REPARSE_TAG_APPEXECLINK*/ ))
{
f.isspecialf = true;
}
f.isencrypted = (wfd.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED)>0;
if( (exact_filesize
&& !f.isdir
&& !f.issym ) || with_usn )
{
if(with_usn)
{
HANDLE hFile = CreateFileW(os_file_prefix(tpath+L"\\"+ConvertToWchar(f.name)).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
if (exact_filesize
&& !f.isdir && !f.issym)
{
BY_HANDLE_FILE_INFORMATION fileInformation;
if (GetFileInformationByHandle(hFile, &fileInformation)!=0)
{
LARGE_INTEGER fsize;
fsize.LowPart = fileInformation.nFileSizeLow;
fsize.HighPart = fileInformation.nFileSizeHigh;
f.size = fsize.QuadPart;
f.nlinks = fileInformation.nNumberOfLinks;
}
}
DWORD last_err=0;
do
{
DWORD ret_bytes = 0;
BOOL b = DeviceIoControl(hFile, FSCTL_READ_FILE_USN_DATA, NULL, 0,
usn_buffer.data(), static_cast<DWORD>(usn_buffer.size()), &ret_bytes, NULL);
if(b)
{
USN_RECORD* usnv2=reinterpret_cast<USN_RECORD*>(usn_buffer.data());
if(usnv2->MajorVersion==2)
{
f.usn = usnv2->Usn;
}
else if(usnv2->MajorVersion==3)
{
usn::USN_RECORD_V3* usnv3=reinterpret_cast<usn::USN_RECORD_V3*>(usn_buffer.data());
f.usn = usnv3->Usn;
}
else
{
Log("USN entry major version "+convert(usnv2->MajorVersion)+" of file \""+ConvertFromWchar(tpath)+"\\"+f.name+"\" not supported", LL_ERROR);
}
}
else
{
last_err=GetLastError();
}
if(last_err==ERROR_INSUFFICIENT_BUFFER)
{
usn_buffer.resize(usn_buffer.size()*2);
}
} while (last_err==ERROR_INSUFFICIENT_BUFFER);
CloseHandle(hFile);
}
}
else
{
WIN32_FILE_ATTRIBUTE_DATA fad;
if( GetFileAttributesExW(os_file_prefix(tpath+L"\\"+ConvertToWchar(f.name)).c_str(), GetFileExInfoStandard, &fad) )
{
size.HighPart = fad.nFileSizeHigh;
size.LowPart = fad.nFileSizeLow;
f.size = size.QuadPart;
lwt.HighPart = fad.ftLastWriteTime.dwHighDateTime;
lwt.LowPart = fad.ftLastWriteTime.dwLowDateTime;
f.last_modified = os_windows_to_unix_time(lwt.QuadPart);
lwt.HighPart=fad.ftCreationTime.dwHighDateTime;
lwt.LowPart=fad.ftCreationTime.dwLowDateTime;
f.created=os_windows_to_unix_time(lwt.QuadPart);
}
}
}
if(f.last_modified<0) f.last_modified*=-1;
tmp.push_back(f);
}
while (FindNextFileW(fHandle,&wfd) );
if (GetLastError() != ERROR_NO_MORE_FILES)
{
DWORD err = GetLastError();
if (has_error != NULL)
{
*has_error = true;
}
FindClose(fHandle);
std::sort(tmp.begin(), tmp.end());
SetLastError(err);
return tmp;
}
FindClose(fHandle);
std::sort(tmp.begin(), tmp.end());
return tmp;
}
SFile getFileMetadataWin( const std::string &path, bool with_usn )
{
SFile ret;
ret.name=path;
WIN32_FILE_ATTRIBUTE_DATA fad;
if( GetFileAttributesExW(ConvertToWchar(path).c_str(), GetFileExInfoStandard, &fad) )
{
if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
ret.isdir=true;
}
LARGE_INTEGER size;
size.HighPart = fad.nFileSizeHigh;
size.LowPart = fad.nFileSizeLow;
ret.size = size.QuadPart;
LARGE_INTEGER lwt;
lwt.HighPart = fad.ftLastWriteTime.dwHighDateTime;
lwt.LowPart = fad.ftLastWriteTime.dwLowDateTime;
ret.last_modified = os_windows_to_unix_time(lwt.QuadPart);
lwt.HighPart=fad.ftCreationTime.dwHighDateTime;
lwt.LowPart=fad.ftCreationTime.dwLowDateTime;
ret.created=os_windows_to_unix_time(lwt.QuadPart);
lwt.HighPart = fad.ftLastAccessTime.dwHighDateTime;
lwt.LowPart = fad.ftLastAccessTime.dwLowDateTime;
ret.accessed = os_windows_to_unix_time(lwt.QuadPart);
if (fad.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
if (os_is_symlink_int(path))
{
ret.issym = true;
}
ret.isspecialf = true;
}
if(with_usn && !ret.isdir && !(fad.dwFileAttributes &FILE_ATTRIBUTE_REPARSE_POINT))
{
HANDLE hFile = CreateFileW(ConvertToWchar(path).c_str(), GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
std::vector<char> buffer;
buffer.resize(1024);
DWORD last_err=0;
do
{
DWORD ret_bytes = 0;
BOOL b = DeviceIoControl(hFile, FSCTL_READ_FILE_USN_DATA, NULL, 0,
buffer.data(), static_cast<DWORD>(buffer.size()), &ret_bytes, NULL);
if(b)
{
USN_RECORD* usnv2=reinterpret_cast<USN_RECORD*>(buffer.data());
if(usnv2->MajorVersion==2)
{
ret.usn = usnv2->Usn;
}
else if(usnv2->MajorVersion==3)
{
usn::USN_RECORD_V3* usnv3=reinterpret_cast<usn::USN_RECORD_V3*>(buffer.data());
ret.usn = usnv3->Usn;
}
else
{
Log("USN entry major version "+convert(usnv2->MajorVersion)+" of file \""+path+"\" not supported", LL_ERROR);
}
}
else
{
last_err=GetLastError();
}
if(last_err==ERROR_INSUFFICIENT_BUFFER)
{
buffer.resize(buffer.size()*2);
}
} while (last_err==ERROR_INSUFFICIENT_BUFFER);
CloseHandle(hFile);
}
}
return ret;
}
else
{
return SFile();
}
}
SFile getFileMetadata( const std::string &path )
{
return getFileMetadataWin(path, false);
}
bool removeFile(const std::string &path)
{
return _unlink(path.c_str())==0;
}
bool moveFile(const std::string &src, const std::string &dst)
{
return rename(src.c_str(), dst.c_str() )==0;
}
bool isDirectory(const std::string &path, void* transaction)
{
DWORD attrib;
#ifdef USE_NTFS_TXF
if(transaction!=NULL)
{
WIN32_FILE_ATTRIBUTE_DATA ad;
if(!GetFileAttributesTransactedW(ConvertToWchar(path).c_str(), GetFileExInfoStandard, &ad, transaction))
{
attrib=0xFFFFFFFF;
}
else
{
attrib = ad.dwFileAttributes;
}
}
else
{
#endif
attrib = GetFileAttributesW(ConvertToWchar(path).c_str());
#ifdef USE_NTFS_TXF
}
#endif
if ( attrib == INVALID_FILE_ATTRIBUTES || !(attrib & FILE_ATTRIBUTE_DIRECTORY) )
{
return false;
}
else
{
return true;
}
}
bool os_is_symlink_int(const std::string& path)
{
WIN32_FIND_DATAW wfd;
HANDLE hf = FindFirstFileW(ConvertToWchar(path).c_str(), &wfd);
if (hf == INVALID_HANDLE_VALUE)
{
return false;
}
FindClose(hf);
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
if (wfd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT
|| wfd.dwReserved0 == IO_REPARSE_TAG_SYMLINK)
{
return true;
}
}
return false;
}
int os_get_file_type(const std::string &path)
{
DWORD attrib = GetFileAttributesW(ConvertToWchar(path).c_str());
if (attrib == INVALID_FILE_ATTRIBUTES)
{
return 0;
}
int ret = 0;
if (attrib & FILE_ATTRIBUTE_DIRECTORY)
{
ret |= EFileType_Directory;
}
else
{
ret |= EFileType_File;
}
if (attrib & FILE_ATTRIBUTE_REPARSE_POINT)
{
if (os_is_symlink_int(path))
{
ret |= EFileType_Symlink;
}
ret |= EFileType_Special;
}
return ret;
}
int64 os_atoi64(const std::string &str)
{
return _atoi64(str.c_str());
}
bool os_create_dir(const std::string &dir)
{
return CreateDirectoryW(ConvertToWchar(dir).c_str(), NULL)!=0;
}
namespace
{
namespace reflink
{
#pragma pack(push, 8)
typedef struct _DUPLICATE_EXTENTS_DATA {
HANDLE FileHandle;
LARGE_INTEGER SourceFileOffset;
LARGE_INTEGER TargetFileOffset;
LARGE_INTEGER ByteCount;
} DUPLICATE_EXTENTS_DATA, *PDUPLICATE_EXTENTS_DATA;
#pragma pack(pop)
DWORD LOCAL_FSCTL_DUPLICATE_EXTENTS_TO_FILE = 0x98344;
}
}
bool os_create_reflink(const std::string &linkname, const std::string &fname)
{
HANDLE source_handle = CreateFileW(ConvertToWchar(fname).c_str(),
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (source_handle == INVALID_HANDLE_VALUE)
return false;
BY_HANDLE_FILE_INFORMATION source_info;
if (!GetFileInformationByHandle(source_handle, &source_info))
{
CloseHandle(source_handle);
return false;
}
LARGE_INTEGER source_size;
source_size.LowPart = source_info.nFileSizeLow;
source_size.HighPart = source_info.nFileSizeHigh;
ULONG ret_bytes;
FSCTL_GET_INTEGRITY_INFORMATION_BUFFER get_integrity_info;
if (!DeviceIoControl(source_handle, FSCTL_GET_INTEGRITY_INFORMATION, NULL, 0,
&get_integrity_info, sizeof(get_integrity_info), &ret_bytes, NULL))
{
CloseHandle(source_handle);
return false;
}
HANDLE dest_handle = CreateFileW(ConvertToWchar(linkname).c_str(),
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, 0, source_handle);
if (dest_handle == INVALID_HANDLE_VALUE)
{
CloseHandle(source_handle);
return false;
}
bool has_error = false;
if ((source_info.dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE)>0
&& !DeviceIoControl(dest_handle, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &ret_bytes, NULL))
{
has_error = true;
}
FSCTL_SET_INTEGRITY_INFORMATION_BUFFER set_integrity_info = {
get_integrity_info.ChecksumAlgorithm,
get_integrity_info.Reserved,
get_integrity_info.Flags };
if (!has_error
&& !DeviceIoControl(dest_handle, FSCTL_SET_INTEGRITY_INFORMATION,
&set_integrity_info, sizeof(set_integrity_info), NULL, 0, NULL, NULL))
{
has_error = true;
}
FILE_END_OF_FILE_INFO eof_info = { source_size };
if(!has_error
&& !SetFileInformationByHandle(dest_handle, FileEndOfFileInfo, &eof_info, sizeof(eof_info) ) )
{
has_error = true;
}
int64 cluster_size = get_integrity_info.ClusterSizeInBytes;
int64 total_reflink = source_size.QuadPart;
if(total_reflink%cluster_size!=0)
total_reflink = ((total_reflink) / cluster_size + 1 )*cluster_size;
int64 max_reflink = cluster_size == 0 ? total_reflink : (4LL * 1024 * 1024 * 1024 - cluster_size);
reflink::DUPLICATE_EXTENTS_DATA reflink_data;
reflink_data.FileHandle = source_handle;
int64 reflinked = 0;
while (!has_error
&& reflinked < total_reflink)
{
int64 curr_reflink = (std::min)(total_reflink - reflinked, max_reflink);
reflink_data.ByteCount.QuadPart = curr_reflink;
reflink_data.SourceFileOffset.QuadPart = reflinked;
reflink_data.TargetFileOffset.QuadPart = reflinked;
if (!DeviceIoControl(dest_handle, reflink::LOCAL_FSCTL_DUPLICATE_EXTENTS_TO_FILE,
&reflink_data, sizeof(reflink_data), NULL, 0, &ret_bytes, NULL))
{
has_error = true;
}
reflinked += curr_reflink;
}
DWORD err = GetLastError();
CloseHandle(source_handle);
CloseHandle(dest_handle);
if (has_error)
{
DeleteFileW(ConvertToWchar(linkname).c_str());
SetLastError(err);
return false;
}
return true;
}
bool os_create_hardlink(const std::string &linkname, const std::string &fname, bool use_ioref, bool* too_many_links)
{
if (use_ioref)
{
return os_create_reflink(linkname, fname);
}
BOOL r=CreateHardLinkW(ConvertToWchar(linkname).c_str(), ConvertToWchar(fname).c_str(), NULL);
if(too_many_links!=NULL)
{
*too_many_links=false;
if(!r)
{
int err=GetLastError();
if(err==ERROR_TOO_MANY_LINKS)
{
*too_many_links=true;
}
}
}
return r!=0;
}
int64 os_free_space(const std::string &path)
{
std::string cp=path;
if(path.size()==0)
return -1;
if(cp[cp.size()-1]=='/')
cp.erase(cp.size()-1, 1);
if(cp[cp.size()-1]!='\\')
cp+='\\';
ULARGE_INTEGER li;
BOOL r=GetDiskFreeSpaceExW(ConvertToWchar(cp).c_str(), &li, NULL, NULL);
if(r!=0)
return li.QuadPart;
else
return -1;
}
int64 os_total_space(const std::string &path)
{
std::string cp=path;
if(path.size()==0)
return -1;
if(cp[cp.size()-1]=='/')
cp.erase(cp.size()-1, 1);
if(cp[cp.size()-1]!='\\')
cp+='\\';
ULARGE_INTEGER li;
BOOL r=GetDiskFreeSpaceExW(ConvertToWchar(path).c_str(), NULL, &li, NULL);
if(r!=0)
return li.QuadPart;
else
return -1;
}
bool os_directory_exists(const std::string &path)
{
return isDirectory(path, NULL);
}
bool os_remove_symlink_dir_int(const std::wstring &path)
{
return RemoveDirectoryW(path.c_str())!=FALSE;
}
bool os_remove_nonempty_dir_int(const std::wstring &root_path, os_symlink_callback_t symlink_callback, void* userdata, bool delete_root)
{
std::stack<std::pair<std::wstring, bool> > paths;
paths.push(std::make_pair(root_path, false));
while (!paths.empty())
{
std::wstring path = paths.top().first;
if (paths.top().second)
{
if (paths.size() == 1
&& !delete_root)
{
return true;
}
if (!RemoveDirectoryW(path.c_str()))
{
Log("Error deleting directory \"" + ConvertFromWchar(path) + "\"", LL_ERROR);
}
paths.pop();
continue;
}
paths.top().second = true;
std::wstring tpath = path;
WIN32_FIND_DATAW wfd;
HANDLE hf = FindFirstFileW((tpath + L"\\*").c_str(), &wfd);
if (hf == INVALID_HANDLE_VALUE)
{
if (tpath.find(L"\\\\?\\UNC") == 0)
{
tpath.erase(0, 7);
tpath = L"\\" + tpath;
hf = FindFirstFileW((tpath + L"\\*").c_str(), &wfd);
}
else if (tpath.find(L"\\\\?\\") == 0)
{
tpath.erase(0, 4);
hf = FindFirstFileW((tpath + L"\\*").c_str(), &wfd);
}
if (hf == INVALID_HANDLE_VALUE)
{
return false;
}
}
BOOL b = true;
while (b)
{
if ((std::wstring)wfd.cFileName != L"." && (std::wstring)wfd.cFileName != L"..")
{
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
if (symlink_callback != NULL
&& (wfd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT
|| wfd.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
{
bool isdir = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
symlink_callback(ConvertFromWchar(path + L"\\" + wfd.cFileName), &isdir, userdata);
}
else if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
os_remove_symlink_dir_int(path + L"\\" + wfd.cFileName);
}
else
{
DeleteFileW((path + L"\\" + wfd.cFileName).c_str());
}
}
else if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
paths.push(std::make_pair(tpath + L"\\" + wfd.cFileName, false));
}
else
{
DeleteFileW((path + L"\\" + wfd.cFileName).c_str());
}
}
b = FindNextFileW(hf, &wfd);
}
FindClose(hf);
}
return true;
}
bool os_remove_nonempty_dir(const std::string &path, os_symlink_callback_t symlink_callback, void* userdata, bool delete_root)
{
return os_remove_nonempty_dir_int(ConvertToWchar(path), symlink_callback, userdata, delete_root);
}
bool os_remove_symlink_dir(const std::string &path)
{
return RemoveDirectoryW(ConvertToWchar(path).c_str())!=FALSE;
}
bool os_remove_dir(const std::string &path)
{
return RemoveDirectoryW(ConvertToWchar(path).c_str())!=FALSE;
}
std::string os_file_sep(void)
{
return "\\";
}
bool os_link_symbolic_symlink(const std::string &target, const std::string &lname, void* transaction, bool* isdir)
{
#if (_WIN32_WINNT >= 0x0600)
DWORD flags=0;
if(isdir!=NULL && *isdir)
{
flags|=SYMBOLIC_LINK_FLAG_DIRECTORY;
}
else if(isdir==NULL && isDirectory(target, transaction))
{
flags|=SYMBOLIC_LINK_FLAG_DIRECTORY;
}
BOOL rc;
if(transaction==NULL)
{
rc=CreateSymbolicLink(ConvertToWchar(lname).c_str(), ConvertToWchar(target).c_str(), flags);
}
else
{
rc=CreateSymbolicLinkTransactedW(ConvertToWchar(lname).c_str(), ConvertToWchar(target).c_str(), flags, transaction);
}
if(rc==FALSE)
{
DWORD lasterr = GetLastError();
Log("Creating symbolic link from \""+lname+"\" to \""+target+"\" failed with error "+convert((int)GetLastError()), LL_ERROR);
SetLastError(lasterr);
}
return rc==TRUE;
#else
return false;
#endif
}
bool os_link_symbolic_junctions_raw(const std::string &target, const std::string &lname, void* transaction)
{
bool ret = false;
HANDLE hJunc = INVALID_HANDLE_VALUE;
char* buf = NULL;
std::wstring wtarget = ConvertToWchar(target);
std::wstring wlname = ConvertToWchar(lname);
if (transaction != NULL)
{
if (!CreateDirectoryTransactedW(NULL, wlname.c_str(), NULL, transaction))
{
goto cleanup;
}
}
else
{
if (!CreateDirectoryW(wlname.c_str(), NULL))
{
goto cleanup;
}
}
if (transaction != NULL)
{
USHORT mversion = 0xFFFF;
hJunc = CreateFileTransactedW(wlname.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL,
transaction, &mversion, NULL);
}
else
{
hJunc = CreateFileW(wlname.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
}
if(hJunc==INVALID_HANDLE_VALUE)
goto cleanup;
size_t bsize=sizeof(REPARSE_MOUNTPOINT_DATA_BUFFER) + (wtarget.size()+1) * sizeof(wchar_t)+30;
buf=new char[bsize];
memset(buf, 0, bsize);
REPARSE_MOUNTPOINT_DATA_BUFFER *rb=(REPARSE_MOUNTPOINT_DATA_BUFFER*)buf;
rb->ReparseTag=IO_REPARSE_TAG_MOUNT_POINT;
rb->ReparseTargetMaximumLength=(WORD)((wtarget.size()+1)*sizeof(wchar_t));
rb->ReparseTargetLength=rb->ReparseTargetMaximumLength-1*sizeof(wchar_t);
rb->ReparseDataLength=rb->ReparseTargetLength+12;
memcpy(rb->ReparseTarget, wtarget.c_str(), rb->ReparseTargetMaximumLength);
DWORD bytes_ret;
if(!DeviceIoControl(hJunc, FSCTL_SET_REPARSE_POINT, rb, rb->ReparseDataLength+REPARSE_MOUNTPOINT_HEADER_SIZE, NULL, 0, &bytes_ret, NULL) )
{
goto cleanup;
}
ret=true;
cleanup:
DWORD lasterr;
if(!ret)
{
lasterr = GetLastError();
Log("Creating junction failed. Last error="+convert((int)GetLastError()), LL_ERROR);
}
delete []buf;
if(hJunc!=INVALID_HANDLE_VALUE)
CloseHandle(hJunc);
if(!ret)
{
if (transaction != NULL)
RemoveDirectoryTransactedW(ConvertToWchar(lname).c_str(), transaction);
else
RemoveDirectoryW(ConvertToWchar(lname).c_str());
SetLastError(lasterr);
}