-
Notifications
You must be signed in to change notification settings - Fork 30k
/
fs.c
3569 lines (2952 loc) Β· 95.9 KB
/
fs.c
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
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdlib.h>
#include <direct.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/utime.h>
#include <stdio.h>
#include "uv.h"
/* <winioctl.h> requires <windows.h>, included via "uv.h" above, but needs to
be included before our "winapi.h", included via "internal.h" below. */
#include <winioctl.h>
#include "internal.h"
#include "req-inl.h"
#include "handle-inl.h"
#include "fs-fd-hash-inl.h"
#define UV_FS_FREE_PATHS 0x0002
#define UV_FS_FREE_PTR 0x0008
#define UV_FS_CLEANEDUP 0x0010
#ifndef FILE_DISPOSITION_DELETE
#define FILE_DISPOSITION_DELETE 0x0001
#endif /* FILE_DISPOSITION_DELETE */
#ifndef FILE_DISPOSITION_POSIX_SEMANTICS
#define FILE_DISPOSITION_POSIX_SEMANTICS 0x0002
#endif /* FILE_DISPOSITION_POSIX_SEMANTICS */
#ifndef FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE
#define FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE 0x0010
#endif /* FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE */
#define INIT(subtype) \
do { \
if (req == NULL) \
return UV_EINVAL; \
uv__fs_req_init(loop, req, subtype, cb); \
} \
while (0)
#define POST \
do { \
if (cb != NULL) { \
uv__req_register(loop); \
uv__work_submit(loop, \
&req->work_req, \
UV__WORK_FAST_IO, \
uv__fs_work, \
uv__fs_done); \
return 0; \
} else { \
uv__fs_work(&req->work_req); \
return req->result; \
} \
} \
while (0)
#define SET_REQ_RESULT(req, result_value) \
do { \
req->result = (result_value); \
assert(req->result != -1); \
} while (0)
#define SET_REQ_WIN32_ERROR(req, sys_errno) \
do { \
req->sys_errno_ = (sys_errno); \
req->result = uv_translate_sys_error(req->sys_errno_); \
} while (0)
#define SET_REQ_UV_ERROR(req, uv_errno, sys_errno) \
do { \
req->result = (uv_errno); \
req->sys_errno_ = (sys_errno); \
} while (0)
#define VERIFY_FD(fd, req) \
if (fd == -1) { \
req->result = UV_EBADF; \
req->sys_errno_ = ERROR_INVALID_HANDLE; \
return; \
}
#define NSEC_PER_TICK 100
#define TICKS_PER_SEC ((int64_t) 1e9 / NSEC_PER_TICK)
static const int64_t WIN_TO_UNIX_TICK_OFFSET = 11644473600 * TICKS_PER_SEC;
static void uv__filetime_to_timespec(uv_timespec_t *ts, int64_t filetime) {
filetime -= WIN_TO_UNIX_TICK_OFFSET;
ts->tv_sec = filetime / TICKS_PER_SEC;
ts->tv_nsec = (filetime % TICKS_PER_SEC) * NSEC_PER_TICK;
if (ts->tv_nsec < 0) {
ts->tv_sec -= 1;
ts->tv_nsec += 1e9;
}
}
#define TIME_T_TO_FILETIME(time, filetime_ptr) \
do { \
int64_t bigtime = ((time) * TICKS_PER_SEC + WIN_TO_UNIX_TICK_OFFSET); \
(filetime_ptr)->dwLowDateTime = (uint64_t) bigtime & 0xFFFFFFFF; \
(filetime_ptr)->dwHighDateTime = (uint64_t) bigtime >> 32; \
} while(0)
#define IS_SLASH(c) ((c) == L'\\' || (c) == L'/')
#define IS_LETTER(c) (((c) >= L'a' && (c) <= L'z') || \
((c) >= L'A' && (c) <= L'Z'))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
const WCHAR JUNCTION_PREFIX[] = L"\\??\\";
const WCHAR JUNCTION_PREFIX_LEN = 4;
const WCHAR LONG_PATH_PREFIX[] = L"\\\\?\\";
const WCHAR LONG_PATH_PREFIX_LEN = 4;
const WCHAR UNC_PATH_PREFIX[] = L"\\\\?\\UNC\\";
const WCHAR UNC_PATH_PREFIX_LEN = 8;
static int uv__file_symlink_usermode_flag = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
static DWORD uv__allocation_granularity;
typedef enum {
FS__STAT_PATH_SUCCESS,
FS__STAT_PATH_ERROR,
FS__STAT_PATH_TRY_SLOW
} fs__stat_path_return_t;
INLINE static void fs__stat_assign_statbuf_null(uv_stat_t* statbuf);
INLINE static void fs__stat_assign_statbuf(uv_stat_t* statbuf,
FILE_STAT_BASIC_INFORMATION stat_info, int do_lstat);
void uv__fs_init(void) {
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
uv__allocation_granularity = system_info.dwAllocationGranularity;
uv__fd_hash_init();
}
INLINE static int fs__readlink_handle(HANDLE handle,
char** target_ptr,
size_t* target_len_ptr) {
char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
REPARSE_DATA_BUFFER* reparse_data = (REPARSE_DATA_BUFFER*) buffer;
WCHAR* w_target;
DWORD w_target_len;
DWORD bytes;
size_t i;
size_t len;
if (!DeviceIoControl(handle,
FSCTL_GET_REPARSE_POINT,
NULL,
0,
buffer,
sizeof buffer,
&bytes,
NULL)) {
return -1;
}
if (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
/* Real symlink */
w_target = reparse_data->SymbolicLinkReparseBuffer.PathBuffer +
(reparse_data->SymbolicLinkReparseBuffer.SubstituteNameOffset /
sizeof(WCHAR));
w_target_len =
reparse_data->SymbolicLinkReparseBuffer.SubstituteNameLength /
sizeof(WCHAR);
/* Real symlinks can contain pretty much everything, but the only thing we
* really care about is undoing the implicit conversion to an NT namespaced
* path that CreateSymbolicLink will perform on absolute paths. If the path
* is win32-namespaced then the user must have explicitly made it so, and
* we better just return the unmodified reparse data. */
if (w_target_len >= 4 &&
w_target[0] == L'\\' &&
w_target[1] == L'?' &&
w_target[2] == L'?' &&
w_target[3] == L'\\') {
/* Starts with \??\ */
if (w_target_len >= 6 &&
((w_target[4] >= L'A' && w_target[4] <= L'Z') ||
(w_target[4] >= L'a' && w_target[4] <= L'z')) &&
w_target[5] == L':' &&
(w_target_len == 6 || w_target[6] == L'\\')) {
/* \??\<drive>:\ */
w_target += 4;
w_target_len -= 4;
} else if (w_target_len >= 8 &&
(w_target[4] == L'U' || w_target[4] == L'u') &&
(w_target[5] == L'N' || w_target[5] == L'n') &&
(w_target[6] == L'C' || w_target[6] == L'c') &&
w_target[7] == L'\\') {
/* \??\UNC\<server>\<share>\ - make sure the final path looks like
* \\<server>\<share>\ */
w_target += 6;
w_target[0] = L'\\';
w_target_len -= 6;
}
}
} else if (reparse_data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
/* Junction. */
w_target = reparse_data->MountPointReparseBuffer.PathBuffer +
(reparse_data->MountPointReparseBuffer.SubstituteNameOffset /
sizeof(WCHAR));
w_target_len = reparse_data->MountPointReparseBuffer.SubstituteNameLength /
sizeof(WCHAR);
/* Only treat junctions that look like \??\<drive>:\ as symlink. Junctions
* can also be used as mount points, like \??\Volume{<guid>}, but that's
* confusing for programs since they wouldn't be able to actually
* understand such a path when returned by uv_readlink(). UNC paths are
* never valid for junctions so we don't care about them. */
if (!(w_target_len >= 6 &&
w_target[0] == L'\\' &&
w_target[1] == L'?' &&
w_target[2] == L'?' &&
w_target[3] == L'\\' &&
((w_target[4] >= L'A' && w_target[4] <= L'Z') ||
(w_target[4] >= L'a' && w_target[4] <= L'z')) &&
w_target[5] == L':' &&
(w_target_len == 6 || w_target[6] == L'\\'))) {
SetLastError(ERROR_SYMLINK_NOT_SUPPORTED);
return -1;
}
/* Remove leading \??\ */
w_target += 4;
w_target_len -= 4;
} else if (reparse_data->ReparseTag == IO_REPARSE_TAG_APPEXECLINK) {
/* String #3 in the list has the target filename. */
if (reparse_data->AppExecLinkReparseBuffer.StringCount < 3) {
SetLastError(ERROR_SYMLINK_NOT_SUPPORTED);
return -1;
}
w_target = reparse_data->AppExecLinkReparseBuffer.StringList;
/* The StringList buffer contains a list of strings separated by "\0", */
/* with "\0\0" terminating the list. Move to the 3rd string in the list: */
for (i = 0; i < 2; ++i) {
len = wcslen(w_target);
if (len == 0) {
SetLastError(ERROR_SYMLINK_NOT_SUPPORTED);
return -1;
}
w_target += len + 1;
}
w_target_len = wcslen(w_target);
if (w_target_len == 0) {
SetLastError(ERROR_SYMLINK_NOT_SUPPORTED);
return -1;
}
/* Make sure it is an absolute path. */
if (!(w_target_len >= 3 &&
((w_target[0] >= L'a' && w_target[0] <= L'z') ||
(w_target[0] >= L'A' && w_target[0] <= L'Z')) &&
w_target[1] == L':' &&
w_target[2] == L'\\')) {
SetLastError(ERROR_SYMLINK_NOT_SUPPORTED);
return -1;
}
} else {
/* Reparse tag does not indicate a symlink. */
SetLastError(ERROR_SYMLINK_NOT_SUPPORTED);
return -1;
}
assert(target_ptr == NULL || *target_ptr == NULL);
return uv_utf16_to_wtf8(w_target, w_target_len, target_ptr, target_len_ptr);
}
INLINE static int fs__capture_path(uv_fs_t* req, const char* path,
const char* new_path, const int copy_path) {
WCHAR* buf;
WCHAR* pos;
size_t buf_sz = 0;
size_t path_len = 0;
ssize_t pathw_len = 0;
ssize_t new_pathw_len = 0;
/* new_path can only be set if path is also set. */
assert(new_path == NULL || path != NULL);
if (path != NULL) {
pathw_len = uv_wtf8_length_as_utf16(path);
if (pathw_len < 0)
return ERROR_INVALID_NAME;
buf_sz += pathw_len * sizeof(WCHAR);
}
if (path != NULL && copy_path) {
path_len = 1 + strlen(path);
buf_sz += path_len;
}
if (new_path != NULL) {
new_pathw_len = uv_wtf8_length_as_utf16(new_path);
if (new_pathw_len < 0)
return ERROR_INVALID_NAME;
buf_sz += new_pathw_len * sizeof(WCHAR);
}
if (buf_sz == 0) {
req->file.pathw = NULL;
req->fs.info.new_pathw = NULL;
req->path = NULL;
return 0;
}
buf = uv__malloc(buf_sz);
if (buf == NULL) {
return ERROR_OUTOFMEMORY;
}
pos = buf;
if (path != NULL) {
uv_wtf8_to_utf16(path, pos, pathw_len);
req->file.pathw = pos;
pos += pathw_len;
} else {
req->file.pathw = NULL;
}
if (new_path != NULL) {
uv_wtf8_to_utf16(new_path, pos, new_pathw_len);
req->fs.info.new_pathw = pos;
pos += new_pathw_len;
} else {
req->fs.info.new_pathw = NULL;
}
req->path = path;
if (path != NULL && copy_path) {
memcpy(pos, path, path_len);
assert(path_len == buf_sz - (pos - buf) * sizeof(WCHAR));
req->path = (char*) pos;
}
req->flags |= UV_FS_FREE_PATHS;
return 0;
}
INLINE static void uv__fs_req_init(uv_loop_t* loop, uv_fs_t* req,
uv_fs_type fs_type, const uv_fs_cb cb) {
uv__once_init();
UV_REQ_INIT(req, UV_FS);
req->loop = loop;
req->flags = 0;
req->fs_type = fs_type;
req->sys_errno_ = 0;
req->result = 0;
req->ptr = NULL;
req->path = NULL;
req->cb = cb;
memset(&req->fs, 0, sizeof(req->fs));
}
void fs__open(uv_fs_t* req) {
DWORD access;
DWORD share;
DWORD disposition;
DWORD attributes = 0;
HANDLE file;
int fd, current_umask;
int flags = req->fs.info.file_flags;
struct uv__fd_info_s fd_info;
/* Adjust flags to be compatible with the memory file mapping. Save the
* original flags to emulate the correct behavior. */
if (flags & UV_FS_O_FILEMAP) {
fd_info.flags = flags;
fd_info.current_pos.QuadPart = 0;
if ((flags & (UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR)) ==
UV_FS_O_WRONLY) {
/* CreateFileMapping always needs read access */
flags = (flags & ~UV_FS_O_WRONLY) | UV_FS_O_RDWR;
}
if (flags & UV_FS_O_APPEND) {
/* Clear the append flag and ensure RDRW mode */
flags &= ~UV_FS_O_APPEND;
flags &= ~(UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR);
flags |= UV_FS_O_RDWR;
}
}
/* Obtain the active umask. umask() never fails and returns the previous
* umask. */
current_umask = _umask(0);
_umask(current_umask);
/* convert flags and mode to CreateFile parameters */
switch (flags & (UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR)) {
case UV_FS_O_RDONLY:
access = FILE_GENERIC_READ;
break;
case UV_FS_O_WRONLY:
access = FILE_GENERIC_WRITE;
break;
case UV_FS_O_RDWR:
access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
break;
default:
goto einval;
}
if (flags & UV_FS_O_APPEND) {
access &= ~FILE_WRITE_DATA;
access |= FILE_APPEND_DATA;
}
/*
* Here is where we deviate significantly from what CRT's _open()
* does. We indiscriminately use all the sharing modes, to match
* UNIX semantics. In particular, this ensures that the file can
* be deleted even whilst it's open, fixing issue
* https://github.com/nodejs/node-v0.x-archive/issues/1449.
* We still support exclusive sharing mode, since it is necessary
* for opening raw block devices, otherwise Windows will prevent
* any attempt to write past the master boot record.
*/
if (flags & UV_FS_O_EXLOCK) {
share = 0;
} else {
share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
}
switch (flags & (UV_FS_O_CREAT | UV_FS_O_EXCL | UV_FS_O_TRUNC)) {
case 0:
case UV_FS_O_EXCL:
disposition = OPEN_EXISTING;
break;
case UV_FS_O_CREAT:
disposition = OPEN_ALWAYS;
break;
case UV_FS_O_CREAT | UV_FS_O_EXCL:
case UV_FS_O_CREAT | UV_FS_O_TRUNC | UV_FS_O_EXCL:
disposition = CREATE_NEW;
break;
case UV_FS_O_TRUNC:
case UV_FS_O_TRUNC | UV_FS_O_EXCL:
disposition = TRUNCATE_EXISTING;
break;
case UV_FS_O_CREAT | UV_FS_O_TRUNC:
disposition = CREATE_ALWAYS;
break;
default:
goto einval;
}
attributes |= FILE_ATTRIBUTE_NORMAL;
if (flags & UV_FS_O_CREAT) {
if (!((req->fs.info.mode & ~current_umask) & _S_IWRITE)) {
attributes |= FILE_ATTRIBUTE_READONLY;
}
}
if (flags & UV_FS_O_TEMPORARY ) {
attributes |= FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_TEMPORARY;
access |= DELETE;
}
if (flags & UV_FS_O_SHORT_LIVED) {
attributes |= FILE_ATTRIBUTE_TEMPORARY;
}
switch (flags & (UV_FS_O_SEQUENTIAL | UV_FS_O_RANDOM)) {
case 0:
break;
case UV_FS_O_SEQUENTIAL:
attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
break;
case UV_FS_O_RANDOM:
attributes |= FILE_FLAG_RANDOM_ACCESS;
break;
default:
goto einval;
}
if (flags & UV_FS_O_DIRECT) {
/*
* FILE_APPEND_DATA and FILE_FLAG_NO_BUFFERING are mutually exclusive.
* Windows returns 87, ERROR_INVALID_PARAMETER if these are combined.
*
* FILE_APPEND_DATA is included in FILE_GENERIC_WRITE:
*
* FILE_GENERIC_WRITE = STANDARD_RIGHTS_WRITE |
* FILE_WRITE_DATA |
* FILE_WRITE_ATTRIBUTES |
* FILE_WRITE_EA |
* FILE_APPEND_DATA |
* SYNCHRONIZE
*
* Note: Appends are also permitted by FILE_WRITE_DATA.
*
* In order for direct writes and direct appends to succeed, we therefore
* exclude FILE_APPEND_DATA if FILE_WRITE_DATA is specified, and otherwise
* fail if the user's sole permission is a direct append, since this
* particular combination is invalid.
*/
if (access & FILE_APPEND_DATA) {
if (access & FILE_WRITE_DATA) {
access &= ~FILE_APPEND_DATA;
} else {
goto einval;
}
}
attributes |= FILE_FLAG_NO_BUFFERING;
}
switch (flags & (UV_FS_O_DSYNC | UV_FS_O_SYNC)) {
case 0:
break;
case UV_FS_O_DSYNC:
case UV_FS_O_SYNC:
attributes |= FILE_FLAG_WRITE_THROUGH;
break;
default:
goto einval;
}
/* Setting this flag makes it possible to open a directory. */
attributes |= FILE_FLAG_BACKUP_SEMANTICS;
file = CreateFileW(req->file.pathw,
access,
share,
NULL,
disposition,
attributes,
NULL);
if (file == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
if (error == ERROR_FILE_EXISTS && (flags & UV_FS_O_CREAT) &&
!(flags & UV_FS_O_EXCL)) {
/* Special case: when ERROR_FILE_EXISTS happens and UV_FS_O_CREAT was
* specified, it means the path referred to a directory. */
SET_REQ_UV_ERROR(req, UV_EISDIR, error);
} else {
SET_REQ_WIN32_ERROR(req, GetLastError());
}
return;
}
fd = _open_osfhandle((intptr_t) file, flags);
if (fd < 0) {
/* The only known failure mode for _open_osfhandle() is EMFILE, in which
* case GetLastError() will return zero. However we'll try to handle other
* errors as well, should they ever occur.
*/
if (errno == EMFILE)
SET_REQ_UV_ERROR(req, UV_EMFILE, ERROR_TOO_MANY_OPEN_FILES);
else if (GetLastError() != ERROR_SUCCESS)
SET_REQ_WIN32_ERROR(req, GetLastError());
else
SET_REQ_WIN32_ERROR(req, (DWORD) UV_UNKNOWN);
CloseHandle(file);
return;
}
if (flags & UV_FS_O_FILEMAP) {
FILE_STANDARD_INFO file_info;
if (!GetFileInformationByHandleEx(file,
FileStandardInfo,
&file_info,
sizeof file_info)) {
SET_REQ_WIN32_ERROR(req, GetLastError());
CloseHandle(file);
return;
}
fd_info.is_directory = file_info.Directory;
if (fd_info.is_directory) {
fd_info.size.QuadPart = 0;
fd_info.mapping = INVALID_HANDLE_VALUE;
} else {
if (!GetFileSizeEx(file, &fd_info.size)) {
SET_REQ_WIN32_ERROR(req, GetLastError());
CloseHandle(file);
return;
}
if (fd_info.size.QuadPart == 0) {
fd_info.mapping = INVALID_HANDLE_VALUE;
} else {
DWORD flProtect = (fd_info.flags & (UV_FS_O_RDONLY | UV_FS_O_WRONLY |
UV_FS_O_RDWR)) == UV_FS_O_RDONLY ? PAGE_READONLY : PAGE_READWRITE;
fd_info.mapping = CreateFileMapping(file,
NULL,
flProtect,
fd_info.size.HighPart,
fd_info.size.LowPart,
NULL);
if (fd_info.mapping == NULL) {
SET_REQ_WIN32_ERROR(req, GetLastError());
CloseHandle(file);
return;
}
}
}
uv__fd_hash_add(fd, &fd_info);
}
SET_REQ_RESULT(req, fd);
return;
einval:
SET_REQ_UV_ERROR(req, UV_EINVAL, ERROR_INVALID_PARAMETER);
}
void fs__close(uv_fs_t* req) {
int fd = req->file.fd;
int result;
struct uv__fd_info_s fd_info;
VERIFY_FD(fd, req);
if (uv__fd_hash_remove(fd, &fd_info)) {
if (fd_info.mapping != INVALID_HANDLE_VALUE) {
CloseHandle(fd_info.mapping);
}
}
if (fd > 2)
result = _close(fd);
else
result = 0;
/* _close doesn't set _doserrno on failure, but it does always set errno
* to EBADF on failure.
*/
if (result == -1) {
assert(errno == EBADF);
SET_REQ_UV_ERROR(req, UV_EBADF, ERROR_INVALID_HANDLE);
} else {
SET_REQ_RESULT(req, 0);
}
}
LONG fs__filemap_ex_filter(LONG excode, PEXCEPTION_POINTERS pep,
int* perror) {
if (excode != (LONG)EXCEPTION_IN_PAGE_ERROR) {
return EXCEPTION_CONTINUE_SEARCH;
}
assert(perror != NULL);
if (pep != NULL && pep->ExceptionRecord != NULL &&
pep->ExceptionRecord->NumberParameters >= 3) {
NTSTATUS status = (NTSTATUS)pep->ExceptionRecord->ExceptionInformation[3];
*perror = pRtlNtStatusToDosError(status);
if (*perror != ERROR_SUCCESS) {
return EXCEPTION_EXECUTE_HANDLER;
}
}
*perror = UV_UNKNOWN;
return EXCEPTION_EXECUTE_HANDLER;
}
void fs__read_filemap(uv_fs_t* req, struct uv__fd_info_s* fd_info) {
int fd = req->file.fd; /* VERIFY_FD done in fs__read */
int rw_flags = fd_info->flags &
(UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR);
size_t read_size, done_read;
unsigned int index;
LARGE_INTEGER pos, end_pos;
size_t view_offset;
LARGE_INTEGER view_base;
void* view;
if (rw_flags == UV_FS_O_WRONLY) {
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_FLAGS);
return;
}
if (fd_info->is_directory) {
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_FUNCTION);
return;
}
if (req->fs.info.offset == -1) {
pos = fd_info->current_pos;
} else {
pos.QuadPart = req->fs.info.offset;
}
/* Make sure we wont read past EOF. */
if (pos.QuadPart >= fd_info->size.QuadPart) {
SET_REQ_RESULT(req, 0);
return;
}
read_size = 0;
for (index = 0; index < req->fs.info.nbufs; ++index) {
read_size += req->fs.info.bufs[index].len;
}
read_size = (size_t) MIN((LONGLONG) read_size,
fd_info->size.QuadPart - pos.QuadPart);
if (read_size == 0) {
SET_REQ_RESULT(req, 0);
return;
}
end_pos.QuadPart = pos.QuadPart + read_size;
view_offset = pos.QuadPart % uv__allocation_granularity;
view_base.QuadPart = pos.QuadPart - view_offset;
view = MapViewOfFile(fd_info->mapping,
FILE_MAP_READ,
view_base.HighPart,
view_base.LowPart,
view_offset + read_size);
if (view == NULL) {
SET_REQ_WIN32_ERROR(req, GetLastError());
return;
}
done_read = 0;
for (index = 0;
index < req->fs.info.nbufs && done_read < read_size;
++index) {
size_t this_read_size = MIN(req->fs.info.bufs[index].len,
read_size - done_read);
#ifdef _MSC_VER
int err = 0;
__try {
#endif
memcpy(req->fs.info.bufs[index].base,
(char*)view + view_offset + done_read,
this_read_size);
#ifdef _MSC_VER
}
__except (fs__filemap_ex_filter(GetExceptionCode(),
GetExceptionInformation(), &err)) {
SET_REQ_WIN32_ERROR(req, err);
UnmapViewOfFile(view);
return;
}
#endif
done_read += this_read_size;
}
assert(done_read == read_size);
if (!UnmapViewOfFile(view)) {
SET_REQ_WIN32_ERROR(req, GetLastError());
return;
}
if (req->fs.info.offset == -1) {
fd_info->current_pos = end_pos;
uv__fd_hash_add(fd, fd_info);
}
SET_REQ_RESULT(req, read_size);
return;
}
void fs__read(uv_fs_t* req) {
int fd = req->file.fd;
int64_t offset = req->fs.info.offset;
HANDLE handle;
OVERLAPPED overlapped, *overlapped_ptr;
LARGE_INTEGER offset_;
DWORD bytes;
DWORD error;
int result;
unsigned int index;
LARGE_INTEGER original_position;
LARGE_INTEGER zero_offset;
int restore_position;
struct uv__fd_info_s fd_info;
VERIFY_FD(fd, req);
if (uv__fd_hash_get(fd, &fd_info)) {
fs__read_filemap(req, &fd_info);
return;
}
zero_offset.QuadPart = 0;
restore_position = 0;
handle = uv__get_osfhandle(fd);
if (handle == INVALID_HANDLE_VALUE) {
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_HANDLE);
return;
}
if (offset != -1) {
memset(&overlapped, 0, sizeof overlapped);
overlapped_ptr = &overlapped;
if (SetFilePointerEx(handle, zero_offset, &original_position,
FILE_CURRENT)) {
restore_position = 1;
}
} else {
overlapped_ptr = NULL;
}
index = 0;
bytes = 0;
do {
DWORD incremental_bytes;
if (offset != -1) {
offset_.QuadPart = offset + bytes;
overlapped.Offset = offset_.LowPart;
overlapped.OffsetHigh = offset_.HighPart;
}
result = ReadFile(handle,
req->fs.info.bufs[index].base,
req->fs.info.bufs[index].len,
&incremental_bytes,
overlapped_ptr);
bytes += incremental_bytes;
++index;
} while (result && index < req->fs.info.nbufs);
if (restore_position)
SetFilePointerEx(handle, original_position, NULL, FILE_BEGIN);
if (result || bytes > 0) {
SET_REQ_RESULT(req, bytes);
} else {
error = GetLastError();
if (error == ERROR_ACCESS_DENIED) {
error = ERROR_INVALID_FLAGS;
}
if (error == ERROR_HANDLE_EOF || error == ERROR_BROKEN_PIPE) {
SET_REQ_RESULT(req, bytes);
} else {
SET_REQ_WIN32_ERROR(req, error);
}
}
}
void fs__write_filemap(uv_fs_t* req, HANDLE file,
struct uv__fd_info_s* fd_info) {
int fd = req->file.fd; /* VERIFY_FD done in fs__write */
int force_append = fd_info->flags & UV_FS_O_APPEND;
int rw_flags = fd_info->flags &
(UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR);
size_t write_size, done_write;
unsigned int index;
LARGE_INTEGER pos, end_pos;
size_t view_offset;
LARGE_INTEGER view_base;
void* view;
FILETIME ft;
if (rw_flags == UV_FS_O_RDONLY) {
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_FLAGS);
return;
}
if (fd_info->is_directory) {
SET_REQ_WIN32_ERROR(req, ERROR_INVALID_FUNCTION);
return;
}
write_size = 0;
for (index = 0; index < req->fs.info.nbufs; ++index) {
write_size += req->fs.info.bufs[index].len;
}
if (write_size == 0) {
SET_REQ_RESULT(req, 0);
return;
}
if (force_append) {
pos = fd_info->size;
} else if (req->fs.info.offset == -1) {
pos = fd_info->current_pos;
} else {
pos.QuadPart = req->fs.info.offset;
}
end_pos.QuadPart = pos.QuadPart + write_size;
/* Recreate the mapping to enlarge the file if needed */
if (end_pos.QuadPart > fd_info->size.QuadPart) {
if (fd_info->mapping != INVALID_HANDLE_VALUE) {
CloseHandle(fd_info->mapping);
}
fd_info->mapping = CreateFileMapping(file,
NULL,
PAGE_READWRITE,
end_pos.HighPart,
end_pos.LowPart,
NULL);
if (fd_info->mapping == NULL) {
SET_REQ_WIN32_ERROR(req, GetLastError());
CloseHandle(file);
fd_info->mapping = INVALID_HANDLE_VALUE;
fd_info->size.QuadPart = 0;
fd_info->current_pos.QuadPart = 0;
uv__fd_hash_add(fd, fd_info);
return;
}
fd_info->size = end_pos;
uv__fd_hash_add(fd, fd_info);
}
view_offset = pos.QuadPart % uv__allocation_granularity;
view_base.QuadPart = pos.QuadPart - view_offset;
view = MapViewOfFile(fd_info->mapping,
FILE_MAP_WRITE,
view_base.HighPart,
view_base.LowPart,
view_offset + write_size);
if (view == NULL) {
SET_REQ_WIN32_ERROR(req, GetLastError());
return;
}
done_write = 0;
for (index = 0; index < req->fs.info.nbufs; ++index) {
#ifdef _MSC_VER
int err = 0;
__try {
#endif
memcpy((char*)view + view_offset + done_write,
req->fs.info.bufs[index].base,
req->fs.info.bufs[index].len);
#ifdef _MSC_VER
}
__except (fs__filemap_ex_filter(GetExceptionCode(),
GetExceptionInformation(), &err)) {
SET_REQ_WIN32_ERROR(req, err);
UnmapViewOfFile(view);
return;
}
#endif
done_write += req->fs.info.bufs[index].len;
}
assert(done_write == write_size);
if (!FlushViewOfFile(view, 0)) {
SET_REQ_WIN32_ERROR(req, GetLastError());
UnmapViewOfFile(view);
return;
}
if (!UnmapViewOfFile(view)) {
SET_REQ_WIN32_ERROR(req, GetLastError());
return;
}
if (req->fs.info.offset == -1) {
fd_info->current_pos = end_pos;
uv__fd_hash_add(fd, fd_info);
}