Repository navigation
Expand file tree
/
Copy pathmz_os_win32.c
More file actions
1249 lines (1027 loc) · 39.4 KB
/
Copy pathmz_os_win32.c
File metadata and controls
1249 lines (1027 loc) · 39.4 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
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
/* mz_os_win32.c -- System functions for Windows
part of the minizip-ng project
Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_os.h"
#include "mz_strm_os.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <wchar.h>
#include <windows.h>
#include <winioctl.h>
/***************************************************************************/
#ifndef SYMBOLIC_LINK_FLAG_DIRECTORY
# define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1
#endif
#ifndef SYMLINK_FLAG_RELATIVE
# define SYMLINK_FLAG_RELATIVE 0x1
#endif
#ifndef _WIN32_WINNT_WIN8
# define _WIN32_WINNT_WIN8 0x0602
#endif
#ifndef ERROR_UNABLE_TO_MOVE_REPLACEMENT
# define ERROR_UNABLE_TO_MOVE_REPLACEMENT 1176L
#endif
#ifndef ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
# define ERROR_UNABLE_TO_MOVE_REPLACEMENT_2 1177L
#endif
#ifndef FILE_NAME_NORMALIZED
# define FILE_NAME_NORMALIZED 0x0
#endif
#ifndef VOLUME_NAME_DOS
# define VOLUME_NAME_DOS 0x0
#endif
typedef struct MZ_REPARSE_DATA_BUFFER_s {
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;
};
} MZ_REPARSE_DATA_BUFFER;
typedef DWORD(WINAPI *mz_get_final_path_name_by_handle_w)(HANDLE, LPWSTR, DWORD, DWORD);
/***************************************************************************/
typedef struct DIR_int_s {
void *find_handle;
WIN32_FIND_DATAW find_data;
struct dirent entry;
uint8_t end;
} DIR_int;
/***************************************************************************/
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding) {
wchar_t *string_wide = NULL;
uint32_t string_wide_size = 0;
string_wide_size = MultiByteToWideChar(encoding, 0, string, -1, NULL, 0);
if (string_wide_size == 0)
return NULL;
string_wide = (wchar_t *)calloc(string_wide_size + 1, sizeof(wchar_t));
if (!string_wide)
return NULL;
MultiByteToWideChar(encoding, 0, string, -1, string_wide, string_wide_size);
return string_wide;
}
void mz_os_unicode_string_delete(wchar_t **string) {
if (string) {
free(*string);
*string = NULL;
}
}
char *mz_os_utf8_string_create(const char *string, int32_t encoding) {
wchar_t *string_wide = NULL;
char *string_utf8 = NULL;
string_wide = mz_os_unicode_string_create(string, encoding);
if (string_wide) {
string_utf8 = mz_os_utf8_string_create_from_unicode(string_wide, MZ_ENCODING_UTF8);
mz_os_unicode_string_delete(&string_wide);
}
return string_utf8;
}
char *mz_os_utf8_string_create_from_unicode(const wchar_t *string, int32_t encoding) {
char *string_utf8 = NULL;
uint32_t string_utf8_size = 0;
MZ_UNUSED(encoding);
string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
if (string_utf8_size == 0)
return NULL;
string_utf8 = (char *)calloc(string_utf8_size, sizeof(char));
if (string_utf8 && WideCharToMultiByte(CP_UTF8, 0, string, -1, string_utf8, string_utf8_size, NULL, NULL) == 0) {
free(string_utf8);
string_utf8 = NULL;
}
return string_utf8;
}
void mz_os_utf8_string_delete(char **string) {
if (string) {
free(*string);
*string = NULL;
}
}
/* Gets the system default ANSI code page used for legacy string conversion */
int32_t mz_os_get_default_encoding(void) {
return (int32_t)GetACP();
}
/***************************************************************************/
int32_t mz_os_rand(uint8_t *buf, int32_t size) {
unsigned __int64 pentium_tsc[1];
int32_t len = 0;
for (len = 0; len < (int)size; len += 1) {
if (len % 8 == 0)
QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
buf[len] = ((unsigned char *)pentium_tsc)[len % 8];
}
return len;
}
int32_t mz_os_path_same_fs(const char *path_a, const char *path_b) {
wchar_t *path_a_wide = NULL;
wchar_t *path_b_wide = NULL;
struct _stati64 sa;
struct _stati64 sb;
int32_t err = MZ_OK;
if (!path_a || !path_b)
return MZ_PARAM_ERROR;
path_a_wide = mz_os_unicode_string_create(path_a, MZ_ENCODING_UTF8);
if (!path_a_wide)
return MZ_PARAM_ERROR;
path_b_wide = mz_os_unicode_string_create(path_b, MZ_ENCODING_UTF8);
if (!path_b_wide) {
mz_os_unicode_string_delete(&path_a_wide);
return MZ_PARAM_ERROR;
}
if (_wstati64(path_a_wide, &sa) != 0 || _wstati64(path_b_wide, &sb) != 0)
err = MZ_EXIST_ERROR;
else if (sa.st_dev != sb.st_dev)
err = MZ_EXIST_ERROR;
mz_os_unicode_string_delete(&path_a_wide);
mz_os_unicode_string_delete(&path_b_wide);
return err;
}
int32_t mz_os_path_same_file(const char *path_a, const char *path_b) {
BY_HANDLE_FILE_INFORMATION info_a;
BY_HANDLE_FILE_INFORMATION info_b;
wchar_t *path_a_wide = NULL;
wchar_t *path_b_wide = NULL;
HANDLE handle_a = INVALID_HANDLE_VALUE;
HANDLE handle_b = INVALID_HANDLE_VALUE;
int32_t err = MZ_EXIST_ERROR;
if (!path_a || !path_b)
return MZ_PARAM_ERROR;
path_a_wide = mz_os_unicode_string_create(path_a, MZ_ENCODING_UTF8);
path_b_wide = mz_os_unicode_string_create(path_b, MZ_ENCODING_UTF8);
if (!path_a_wide || !path_b_wide) {
err = MZ_MEM_ERROR;
goto cleanup;
}
handle_a = CreateFileW(path_a_wide, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
handle_b = CreateFileW(path_b_wide, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (handle_a == INVALID_HANDLE_VALUE || handle_b == INVALID_HANDLE_VALUE)
goto cleanup;
if (!GetFileInformationByHandle(handle_a, &info_a) || !GetFileInformationByHandle(handle_b, &info_b))
goto cleanup;
if (info_a.dwVolumeSerialNumber == info_b.dwVolumeSerialNumber && info_a.nFileIndexHigh == info_b.nFileIndexHigh &&
info_a.nFileIndexLow == info_b.nFileIndexLow)
err = MZ_OK;
cleanup:
if (handle_a != INVALID_HANDLE_VALUE)
CloseHandle(handle_a);
if (handle_b != INVALID_HANDLE_VALUE)
CloseHandle(handle_b);
if (path_a_wide)
mz_os_unicode_string_delete(&path_a_wide);
if (path_b_wide)
mz_os_unicode_string_delete(&path_b_wide);
return err;
}
int32_t mz_os_rename(const char *source_path, const char *target_path) {
wchar_t *source_path_wide = NULL;
wchar_t *target_path_wide = NULL;
int32_t result = 0;
int32_t err = MZ_OK;
if (!source_path || !target_path)
return MZ_PARAM_ERROR;
source_path_wide = mz_os_unicode_string_create(source_path, MZ_ENCODING_UTF8);
if (!source_path_wide) {
err = MZ_PARAM_ERROR;
} else {
target_path_wide = mz_os_unicode_string_create(target_path, MZ_ENCODING_UTF8);
if (!target_path_wide)
err = MZ_PARAM_ERROR;
}
if (err == MZ_OK) {
#if _WIN32_WINNT >= _WIN32_WINNT_WINXP
result = MoveFileExW(source_path_wide, target_path_wide, MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED);
#else
result = MoveFileW(source_path_wide, target_path_wide);
#endif
if (result == 0)
err = MZ_EXIST_ERROR;
}
if (target_path_wide)
mz_os_unicode_string_delete(&target_path_wide);
if (source_path_wide)
mz_os_unicode_string_delete(&source_path_wide);
return err;
}
static wchar_t *mz_os_wide_string_duplicate(const wchar_t *string) {
wchar_t *duplicate = NULL;
size_t string_length = 0;
if (!string)
return NULL;
string_length = wcslen(string);
duplicate = (wchar_t *)malloc((string_length + 1) * sizeof(wchar_t));
if (duplicate)
memcpy(duplicate, string, (string_length + 1) * sizeof(wchar_t));
return duplicate;
}
static int32_t mz_os_get_full_path_wide(const wchar_t *path, wchar_t **full_path) {
wchar_t *path_buffer = NULL;
DWORD path_length = 0;
DWORD result = 0;
if (!path || !full_path)
return MZ_PARAM_ERROR;
*full_path = NULL;
path_length = GetFullPathNameW(path, 0, NULL, NULL);
if (path_length == 0)
return MZ_EXIST_ERROR;
path_buffer = (wchar_t *)malloc(((size_t)path_length + 1) * sizeof(wchar_t));
if (!path_buffer)
return MZ_MEM_ERROR;
result = GetFullPathNameW(path, path_length + 1, path_buffer, NULL);
if (result == 0 || result > path_length) {
free(path_buffer);
return MZ_EXIST_ERROR;
}
*full_path = path_buffer;
return MZ_OK;
}
static int32_t mz_os_get_final_path_wide(HANDLE handle, const wchar_t *fallback_path, wchar_t **final_path) {
HMODULE kernel32_mod = NULL;
mz_get_final_path_name_by_handle_w get_final_path = NULL;
wchar_t *path_buffer = NULL;
DWORD path_length = 0;
DWORD result = 0;
if (handle == INVALID_HANDLE_VALUE || !fallback_path || !final_path)
return MZ_PARAM_ERROR;
*final_path = NULL;
kernel32_mod = GetModuleHandleW(L"kernel32.dll");
if (kernel32_mod)
get_final_path = (mz_get_final_path_name_by_handle_w)GetProcAddress(kernel32_mod, "GetFinalPathNameByHandleW");
if (!get_final_path) {
*final_path = mz_os_wide_string_duplicate(fallback_path);
return *final_path ? MZ_OK : MZ_MEM_ERROR;
}
path_length = get_final_path(handle, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (path_length == 0)
return MZ_EXIST_ERROR;
path_buffer = (wchar_t *)malloc(((size_t)path_length + 1) * sizeof(wchar_t));
if (!path_buffer)
return MZ_MEM_ERROR;
result = get_final_path(handle, path_buffer, path_length + 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (result == 0 || result > path_length) {
free(path_buffer);
return MZ_EXIST_ERROR;
}
*final_path = path_buffer;
return MZ_OK;
}
static int32_t mz_os_get_parent_path_wide(const wchar_t *path, wchar_t **parent_path, const wchar_t **basename) {
const wchar_t *slash = NULL;
const wchar_t *slash_forward = NULL;
size_t parent_length = 0;
if (!path || !parent_path || !basename)
return MZ_PARAM_ERROR;
*parent_path = NULL;
*basename = NULL;
slash = wcsrchr(path, L'\\');
slash_forward = wcsrchr(path, L'/');
if (!slash || (slash_forward && slash_forward > slash))
slash = slash_forward;
if (!slash)
return MZ_EXIST_ERROR;
*basename = slash + 1;
parent_length = (size_t)(slash - path);
if (parent_length == 2 && path[1] == L':')
parent_length += 1;
else if (parent_length == 0)
parent_length = 1;
*parent_path = (wchar_t *)malloc((parent_length + 1) * sizeof(wchar_t));
if (!*parent_path)
return MZ_MEM_ERROR;
memcpy(*parent_path, path, parent_length * sizeof(wchar_t));
(*parent_path)[parent_length] = 0;
return MZ_OK;
}
static int32_t mz_os_join_path_wide(const wchar_t *parent_path, const wchar_t *name, wchar_t **path) {
size_t parent_length = 0;
size_t name_length = 0;
size_t path_length = 0;
uint8_t append_slash = 0;
if (!parent_path || !name || !path)
return MZ_PARAM_ERROR;
*path = NULL;
parent_length = wcslen(parent_path);
name_length = wcslen(name);
append_slash = parent_length > 0 && parent_path[parent_length - 1] != L'\\' &&
parent_path[parent_length - 1] != L'/';
if (parent_length > (size_t)-1 - name_length - append_slash - 1)
return MZ_MEM_ERROR;
path_length = parent_length + append_slash + name_length + 1;
*path = (wchar_t *)malloc(path_length * sizeof(wchar_t));
if (!*path)
return MZ_MEM_ERROR;
memcpy(*path, parent_path, parent_length * sizeof(wchar_t));
if (append_slash)
(*path)[parent_length++] = L'\\';
memcpy(*path + parent_length, name, (name_length + 1) * sizeof(wchar_t));
return MZ_OK;
}
static int32_t mz_os_get_reparse_target_wide(const wchar_t *path, wchar_t **target_path, uint8_t *relative) {
MZ_REPARSE_DATA_BUFFER *reparse_data = NULL;
const wchar_t *path_buffer = NULL;
wchar_t *result_path = NULL;
DWORD length = 0;
HANDLE handle = INVALID_HANDLE_VALUE;
USHORT name_length = 0;
USHORT name_offset = 0;
uint8_t buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
uint8_t substitute_name = 0;
size_t buffer_offset = 0;
size_t result_length = 0;
if (!path || !target_path || !relative)
return MZ_PARAM_ERROR;
*target_path = NULL;
*relative = 0;
handle = CreateFileW(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
if (handle == INVALID_HANDLE_VALUE)
return MZ_EXIST_ERROR;
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, sizeof(buffer), &length, NULL)) {
CloseHandle(handle);
return MZ_SYMLINK_ERROR;
}
CloseHandle(handle);
reparse_data = (MZ_REPARSE_DATA_BUFFER *)buffer;
if (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
*relative = (uint8_t)((reparse_data->SymbolicLinkReparseBuffer.Flags & SYMLINK_FLAG_RELATIVE) != 0);
path_buffer = reparse_data->SymbolicLinkReparseBuffer.PathBuffer;
name_length = reparse_data->SymbolicLinkReparseBuffer.PrintNameLength;
name_offset = reparse_data->SymbolicLinkReparseBuffer.PrintNameOffset;
if (name_length == 0) {
name_length = reparse_data->SymbolicLinkReparseBuffer.SubstituteNameLength;
name_offset = reparse_data->SymbolicLinkReparseBuffer.SubstituteNameOffset;
substitute_name = 1;
}
} else if (reparse_data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
path_buffer = reparse_data->MountPointReparseBuffer.PathBuffer;
name_length = reparse_data->MountPointReparseBuffer.PrintNameLength;
name_offset = reparse_data->MountPointReparseBuffer.PrintNameOffset;
if (name_length == 0) {
name_length = reparse_data->MountPointReparseBuffer.SubstituteNameLength;
name_offset = reparse_data->MountPointReparseBuffer.SubstituteNameOffset;
substitute_name = 1;
}
} else {
return MZ_SYMLINK_ERROR;
}
buffer_offset = (size_t)((const uint8_t *)path_buffer - buffer);
if ((name_length % sizeof(wchar_t)) != 0 || buffer_offset > length || name_offset > length - buffer_offset ||
name_length > length - buffer_offset - name_offset || name_length == 0)
return MZ_SYMLINK_ERROR;
path_buffer += name_offset / sizeof(wchar_t);
result_length = name_length / sizeof(wchar_t);
result_path = (wchar_t *)malloc((result_length + 1) * sizeof(wchar_t));
if (!result_path)
return MZ_MEM_ERROR;
memcpy(result_path, path_buffer, result_length * sizeof(wchar_t));
result_path[result_length] = 0;
if (substitute_name && result_length >= 4 && wcsncmp(result_path, L"\\??\\", 4) == 0) {
result_path[0] = L'\\';
result_path[1] = L'\\';
result_path[2] = L'?';
result_path[3] = L'\\';
}
*target_path = result_path;
return MZ_OK;
}
int32_t mz_os_get_replace_path(const char *target_path, char **replace_path) {
const wchar_t *basename = NULL;
wchar_t *current_path = NULL;
wchar_t *final_parent = NULL;
wchar_t *final_path = NULL;
wchar_t *full_path = NULL;
wchar_t *link_path = NULL;
wchar_t *next_path = NULL;
wchar_t *parent_path = NULL;
wchar_t *target_path_wide = NULL;
HANDLE handle = INVALID_HANDLE_VALUE;
uint32_t link_count = 0;
uint8_t relative = 0;
int32_t err = MZ_OK;
if (!target_path || !replace_path)
return MZ_PARAM_ERROR;
*replace_path = NULL;
target_path_wide = mz_os_unicode_string_create(target_path, MZ_ENCODING_UTF8);
if (!target_path_wide)
return MZ_MEM_ERROR;
err = mz_os_get_full_path_wide(target_path_wide, ¤t_path);
if (err != MZ_OK)
goto cleanup;
while (link_count < 64) {
handle = CreateFileW(current_path, FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (handle != INVALID_HANDLE_VALUE) {
err = mz_os_get_final_path_wide(handle, current_path, &final_path);
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
if (err == MZ_OK) {
*replace_path = mz_os_utf8_string_create_from_unicode(final_path, MZ_ENCODING_UTF8);
if (!*replace_path)
err = MZ_MEM_ERROR;
}
goto cleanup;
}
err = mz_os_get_reparse_target_wide(current_path, &link_path, &relative);
if (err == MZ_OK) {
if (relative) {
err = mz_os_get_parent_path_wide(current_path, &parent_path, &basename);
if (err == MZ_OK)
err = mz_os_join_path_wide(parent_path, link_path, &next_path);
} else {
next_path = mz_os_wide_string_duplicate(link_path);
if (!next_path)
err = MZ_MEM_ERROR;
}
if (err == MZ_OK)
err = mz_os_get_full_path_wide(next_path, &full_path);
if (err != MZ_OK)
goto cleanup;
free(current_path);
current_path = full_path;
full_path = NULL;
free(link_path);
link_path = NULL;
free(next_path);
next_path = NULL;
free(parent_path);
parent_path = NULL;
link_count += 1;
continue;
}
if (err != MZ_EXIST_ERROR)
goto cleanup;
err = mz_os_get_parent_path_wide(current_path, &parent_path, &basename);
if (err != MZ_OK || !*basename)
goto cleanup;
handle = CreateFileW(parent_path, FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (handle == INVALID_HANDLE_VALUE) {
err = MZ_EXIST_ERROR;
goto cleanup;
}
err = mz_os_get_final_path_wide(handle, parent_path, &final_parent);
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
if (err == MZ_OK)
err = mz_os_join_path_wide(final_parent, basename, &final_path);
if (err == MZ_OK) {
*replace_path = mz_os_utf8_string_create_from_unicode(final_path, MZ_ENCODING_UTF8);
if (!*replace_path)
err = MZ_MEM_ERROR;
}
goto cleanup;
}
err = MZ_SYMLINK_ERROR;
cleanup:
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
free(target_path_wide);
free(parent_path);
free(next_path);
free(link_path);
free(full_path);
free(final_path);
free(final_parent);
free(current_path);
return err;
}
static int32_t mz_os_copy_hard_link_target(const wchar_t *source_path, const wchar_t *replace_path,
const BY_HANDLE_FILE_INFORMATION *expected_info) {
BY_HANDLE_FILE_INFORMATION source_info;
BY_HANDLE_FILE_INFORMATION target_info;
LARGE_INTEGER zero;
HANDLE source_handle = INVALID_HANDLE_VALUE;
HANDLE target_handle = INVALID_HANDLE_VALUE;
DWORD bytes_read = 0;
DWORD bytes_written = 0;
DWORD total_written = 0;
uint8_t buffer[UINT16_MAX];
int32_t err = MZ_OK;
uint8_t source_is_target = 0;
source_handle = CreateFileW(source_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
target_handle = CreateFileW(replace_path, GENERIC_WRITE | FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (source_handle == INVALID_HANDLE_VALUE || target_handle == INVALID_HANDLE_VALUE) {
err = MZ_OPEN_ERROR;
goto cleanup;
}
if (!GetFileInformationByHandle(source_handle, &source_info) ||
!GetFileInformationByHandle(target_handle, &target_info) ||
target_info.dwVolumeSerialNumber != expected_info->dwVolumeSerialNumber ||
target_info.nFileIndexHigh != expected_info->nFileIndexHigh ||
target_info.nFileIndexLow != expected_info->nFileIndexLow) {
err = MZ_EXIST_ERROR;
goto cleanup;
}
if (source_info.dwVolumeSerialNumber == target_info.dwVolumeSerialNumber &&
source_info.nFileIndexHigh == target_info.nFileIndexHigh &&
source_info.nFileIndexLow == target_info.nFileIndexLow) {
source_is_target = 1;
goto cleanup;
}
zero.QuadPart = 0;
if (!SetFilePointerEx(target_handle, zero, NULL, FILE_BEGIN) || !SetEndOfFile(target_handle)) {
err = MZ_INTERNAL_ERROR;
goto cleanup;
}
do {
if (!ReadFile(source_handle, buffer, sizeof(buffer), &bytes_read, NULL)) {
err = MZ_INTERNAL_ERROR;
break;
}
total_written = 0;
while (total_written < bytes_read) {
if (!WriteFile(target_handle, buffer + total_written, bytes_read - total_written, &bytes_written, NULL) ||
bytes_written == 0) {
err = MZ_INTERNAL_ERROR;
break;
}
total_written += bytes_written;
}
} while (err == MZ_OK && bytes_read > 0);
if (err == MZ_OK && !FlushFileBuffers(target_handle))
err = MZ_INTERNAL_ERROR;
cleanup:
if (source_handle != INVALID_HANDLE_VALUE)
CloseHandle(source_handle);
if (target_handle != INVALID_HANDLE_VALUE)
CloseHandle(target_handle);
if (err == MZ_OK && !source_is_target && !DeleteFileW(source_path))
err = MZ_EXIST_ERROR;
return err;
}
int32_t mz_os_replace_resolved(const char *source_path, const char *replace_path) {
BY_HANDLE_FILE_INFORMATION replace_info;
wchar_t *source_path_wide = NULL;
wchar_t *replace_path_wide = NULL;
HANDLE replace_handle = INVALID_HANDLE_VALUE;
DWORD replace_error = ERROR_SUCCESS;
int32_t result = 0;
int32_t err = MZ_OK;
if (!source_path || !replace_path)
return MZ_PARAM_ERROR;
err = mz_os_path_same_file(source_path, replace_path);
if (err == MZ_OK)
return MZ_OK;
if (err == MZ_MEM_ERROR)
return err;
err = MZ_OK;
source_path_wide = mz_os_unicode_string_create(source_path, MZ_ENCODING_UTF8);
replace_path_wide = mz_os_unicode_string_create(replace_path, MZ_ENCODING_UTF8);
if (!source_path_wide || !replace_path_wide) {
err = MZ_MEM_ERROR;
goto cleanup;
}
replace_handle = CreateFileW(replace_path_wide, FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (replace_handle != INVALID_HANDLE_VALUE && GetFileInformationByHandle(replace_handle, &replace_info) &&
replace_info.nNumberOfLinks > 1) {
err = mz_os_copy_hard_link_target(source_path_wide, replace_path_wide, &replace_info);
goto cleanup;
}
if (mz_os_file_exists(replace_path) == MZ_OK)
result = ReplaceFileW(replace_path_wide, source_path_wide, NULL, 0, NULL, NULL);
else
result = MoveFileExW(source_path_wide, replace_path_wide, MOVEFILE_WRITE_THROUGH);
if (result == 0) {
replace_error = GetLastError();
if (replace_error == ERROR_UNABLE_TO_MOVE_REPLACEMENT || replace_error == ERROR_UNABLE_TO_MOVE_REPLACEMENT_2)
err = MZ_INTERNAL_ERROR;
else
err = MZ_EXIST_ERROR;
}
cleanup:
if (replace_handle != INVALID_HANDLE_VALUE)
CloseHandle(replace_handle);
if (source_path_wide)
mz_os_unicode_string_delete(&source_path_wide);
if (replace_path_wide)
mz_os_unicode_string_delete(&replace_path_wide);
return err;
}
int32_t mz_os_replace(const char *source_path, const char *target_path) {
char *replace_path = NULL;
int32_t err = MZ_OK;
if (!source_path || !target_path)
return MZ_PARAM_ERROR;
err = mz_os_get_replace_path(target_path, &replace_path);
if (err == MZ_OK)
err = mz_os_replace_resolved(source_path, replace_path);
free(replace_path);
return err;
}
int32_t mz_os_unlink(const char *path) {
wchar_t *path_wide = NULL;
int32_t result = 0;
if (!path)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
if (mz_os_is_dir(path) == MZ_OK)
result = RemoveDirectoryW(path_wide);
else
result = DeleteFileW(path_wide);
mz_os_unicode_string_delete(&path_wide);
if (result == 0)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_os_file_exists(const char *path) {
wchar_t *path_wide = NULL;
DWORD attribs = 0;
if (!path)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
attribs = GetFileAttributesW(path_wide);
mz_os_unicode_string_delete(&path_wide);
if (attribs == 0xFFFFFFFF)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int64_t mz_os_get_file_size(const char *path) {
HANDLE handle = NULL;
LARGE_INTEGER large_size;
wchar_t *path_wide = NULL;
if (!path)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
handle = CreateFile2(path_wide, GENERIC_READ, 0, OPEN_EXISTING, NULL);
#else
handle = CreateFileW(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif
mz_os_unicode_string_delete(&path_wide);
large_size.QuadPart = 0;
if (handle != INVALID_HANDLE_VALUE) {
GetFileSizeEx(handle, &large_size);
CloseHandle(handle);
}
return large_size.QuadPart;
}
static void mz_os_file_to_unix_time(FILETIME file_time, time_t *unix_time) {
uint64_t quad_file_time = 0;
quad_file_time = file_time.dwLowDateTime;
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32);
*unix_time = (time_t)((quad_file_time - 116444736000000000LL) / 10000000);
}
static void mz_os_unix_to_file_time(time_t unix_time, FILETIME *file_time) {
uint64_t quad_file_time = 0;
quad_file_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
file_time->dwHighDateTime = (quad_file_time >> 32);
file_time->dwLowDateTime = (uint32_t)(quad_file_time);
}
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date) {
WIN32_FILE_ATTRIBUTE_DATA wfad;
wchar_t *path_wide = NULL;
int32_t err = MZ_INTERNAL_ERROR;
if (!path)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
if (GetFileAttributesExW(path_wide, GetFileExInfoStandard, &wfad)) {
if (modified_date)
mz_os_file_to_unix_time(wfad.ftLastWriteTime, modified_date);
if (accessed_date)
mz_os_file_to_unix_time(wfad.ftLastAccessTime, accessed_date);
if (creation_date)
mz_os_file_to_unix_time(wfad.ftCreationTime, creation_date);
err = MZ_OK;
}
free(path_wide);
return err;
}
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date) {
HANDLE handle = NULL;
FILETIME ftm_creation, ftm_accessed, ftm_modified;
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
if (!path)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
handle = CreateFile2(path_wide, GENERIC_READ | GENERIC_WRITE, 0, OPEN_EXISTING, NULL);
#else
handle = CreateFileW(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
#endif
mz_os_unicode_string_delete(&path_wide);
if (handle != INVALID_HANDLE_VALUE) {
GetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified);
if (modified_date != 0)
mz_os_unix_to_file_time(modified_date, &ftm_modified);
if (accessed_date != 0)
mz_os_unix_to_file_time(accessed_date, &ftm_accessed);
if (creation_date != 0)
mz_os_unix_to_file_time(creation_date, &ftm_creation);
if (SetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified) == 0)
err = MZ_INTERNAL_ERROR;
CloseHandle(handle);
}
return err;
}
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes) {
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
if (!path || !attributes)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
*attributes = GetFileAttributesW(path_wide);
/* If target is a reparse point, open with default flags to get attributes */
if (*attributes != INVALID_FILE_ATTRIBUTES && (*attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
HANDLE handle = INVALID_HANDLE_VALUE;
BY_HANDLE_FILE_INFORMATION info;
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
CREATEFILE2_EXTENDED_PARAMETERS extended_params;
memset(&extended_params, 0, sizeof(extended_params));
extended_params.dwSize = sizeof(extended_params);
extended_params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
handle = CreateFile2(path_wide, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, OPEN_EXISTING,
&extended_params);
#else
handle = CreateFileW(path_wide, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
#endif
if (handle != INVALID_HANDLE_VALUE) {
if (GetFileInformationByHandle(handle, &info))
*attributes = info.dwFileAttributes;
CloseHandle(handle);
}
}
mz_os_unicode_string_delete(&path_wide);
if (*attributes == INVALID_FILE_ATTRIBUTES)
err = MZ_INTERNAL_ERROR;
return err;
}
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes) {
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
if (!path)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
if (SetFileAttributesW(path_wide, attributes) == 0)
err = MZ_INTERNAL_ERROR;
mz_os_unicode_string_delete(&path_wide);
return err;
}
int32_t mz_os_make_dir(const char *path) {
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
if (!path)
return MZ_PARAM_ERROR;
/* Don't try to create a drive letter */
if ((path[0] != 0) && (strlen(path) <= 3) && (path[1] == ':'))
return mz_os_is_dir(path);
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (!path_wide)
return MZ_PARAM_ERROR;
if (CreateDirectoryW(path_wide, NULL) == 0) {
if (GetLastError() != ERROR_ALREADY_EXISTS)
err = MZ_INTERNAL_ERROR;
}
mz_os_unicode_string_delete(&path_wide);
return err;
}
DIR *mz_os_open_dir(const char *path) {
WIN32_FIND_DATAW find_data;
DIR_int *dir_int = NULL;
wchar_t *path_wide = NULL;
char fixed_path[320];
void *handle = NULL;
if (!path)
return NULL;
strncpy(fixed_path, path, sizeof(fixed_path) - 1);
fixed_path[sizeof(fixed_path) - 1] = 0;
mz_path_append_slash(fixed_path, sizeof(fixed_path), MZ_PATH_SLASH_PLATFORM);
mz_path_combine(fixed_path, "*", sizeof(fixed_path));
path_wide = mz_os_unicode_string_create(fixed_path, MZ_ENCODING_UTF8);
if (!path_wide)
return NULL;
handle = FindFirstFileW(path_wide, &find_data);