Repository navigation
Expand file tree
/
Copy pathfileapi.cpp
More file actions
1099 lines (1009 loc) · 33.5 KB
/
Copy pathfileapi.cpp
File metadata and controls
1099 lines (1009 loc) · 33.5 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
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <attr/xattr.h>
#include <linux/xattr.h>
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <memory>
#include "acl.h"
#include "handleapi.h"
#include "fileapi.h"
#include "winbase.h"
#include "winerror.h"
#include "file_holder.h"
namespace
{
// These blocks are close together to let system save space in lock table merging the locks
constexpr off_t ModeOffset = 0; // Base offset for open mode locks
constexpr off_t ShareOffset = 3; // Base offset for share deny locks
// Offsets for particular locks
constexpr off_t ReadLock = 0;
constexpr off_t WriteLock = 1;
constexpr off_t DeleteLock = 2;
}
struct FileHandle: public handle_t
{
~FileHandle();
public:
std::string fileName; // Preserve name for further usage with unlink if necessary
std::string lockFileName;
int handle = -1;
int lockFile = -1;
FileHandle(int h): handle(h) { type = handle_type::HANDLE_FILE; }
int GetWaitableHandle() override { return handle; }
};
FileHandle::~FileHandle()
{
if (handle != -1)
{
if (lockFile != -1 && flock(handle, LOCK_EX) == 0)
{
struct flock lock;
// Try to exclusively lock whole range.
// If any lock is held by any other descriptor - it would fail.
// If lock is success it is the only opened handle for this file
// so we can delete lock file.
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset;
lock.l_len = ShareOffset + DeleteLock - ModeOffset;
lock.l_pid = 0;
if (fcntl(lockFile, F_OFD_SETLK, &lock) == 0)
{
shm_unlink(lockFileName.c_str());
}
flock(handle, LOCK_UN);
}
close(handle);
}
if (lockFile != -1)
close(lockFile);
}
HANDLE CreateFileHandle(int handle)
{
return new FileHandle(handle);
}
HANDLE CreateFile(const char* lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAtrributes, [[maybe_unused]]HANDLE hTemplateFile)
{
// MSDN: If there are existing open handles to a file,
// the call fails unless they were all opened with FILE_SHARE_DELETE share mode
if (dwFlagsAndAtrributes & FILE_FLAG_DELETE_ON_CLOSE)
{
dwDesiredAccess |= DELETE;
}
DWORD thisMode = dwDesiredAccess & (FILE_READ_DATA | FILE_WRITE_DATA | DELETE); // These flags are mapped directly
// These flags more tricky
if (dwDesiredAccess & GENERIC_ALL)
{
thisMode |= FILE_READ_DATA | FILE_WRITE_DATA | DELETE;
}
if (dwDesiredAccess & GENERIC_READ)
{
thisMode |= FILE_READ_DATA;
}
if (dwDesiredAccess & GENERIC_WRITE)
{
thisMode |= FILE_WRITE_DATA;
}
int flags = O_PATH;
// TODO: Somehow handle cases when file is requested to be opened for DELETE only
switch (thisMode & (FILE_READ_DATA | FILE_WRITE_DATA))
{
case FILE_READ_DATA:
{
flags = O_RDONLY;
break;
}
case FILE_WRITE_DATA:
{
flags = O_WRONLY;
break;
}
case FILE_READ_DATA | FILE_WRITE_DATA:
{
flags = O_RDWR;
break;
}
}
if (lpSecurityAttributes == nullptr || lpSecurityAttributes->bInheritHandle != TRUE)
{
flags |= O_CLOEXEC;
}
// Handle dispositions
switch (dwCreationDisposition)
{
case CREATE_NEW:
{
flags |= O_CREAT | O_EXCL;
break;
}
case CREATE_ALWAYS:
{
flags |= O_CREAT | O_TRUNC;
break;
}
case OPEN_EXISTING:
{
// Default behavior
break;
}
case OPEN_ALWAYS:
{
// This deviates Windows specs by not setting errno to ERROR_ALREADY_EXISTS on success
// but I don't want to create loop trying to open-create file which is not robust anyway.
flags |= O_CREAT;
break;
}
case TRUNCATE_EXISTING:
{
flags |= O_TRUNC;
break;
}
default:
{
errno = ERROR_INVALID_PARAMETER;
return INVALID_HANDLE_VALUE;
}
}
bool defaultAccess = true;
uid_t desiredUser = -1;
gid_t desiredGroup = -1;
mode_t mode = 0664; // Sane default
acl::AceSet perms;
// Check if custom security is requested for newly created file
if (flags & O_CREAT)
{
if (lpSecurityAttributes != nullptr && lpSecurityAttributes->lpSecurityDescriptor != nullptr)
{
SECURITY_DESCRIPTOR* desc = reinterpret_cast<SECURITY_DESCRIPTOR*>(lpSecurityAttributes->lpSecurityDescriptor);
// Check if owner or group must be set.
// There is an option to play with effective fs uid but it would also affect opening.
// Besides chown() allow to change group to any group that the user is in
// while setfsgid is limited to the primary one.
// So force deferred settings.
if ((desc->Control & SE_OWNER_DEFAULTED) == 0 && desc->Owner != nullptr)
{
uint32_t id;
SID_NAME_USE t = acl::recognizeSid(desc->Owner, id);
if (t == SidTypeUser)
{
if (id != geteuid())
{
desiredUser = id;
defaultAccess = false;
}
}
else
{
errno = ERROR_INVALID_SID;
return INVALID_HANDLE_VALUE;
}
}
if ((desc->Control & SE_GROUP_DEFAULTED) == 0 && desc->Group != nullptr)
{
uint32_t id;
SID_NAME_USE t = acl::recognizeSid(desc->Group, id);
if (t == SidTypeGroup)
{
if (id != getegid())
{
desiredGroup = id;
defaultAccess = false;
}
}
else
{
errno = ERROR_INVALID_SID;
return INVALID_HANDLE_VALUE;
}
}
if ((desc->Control & SE_DACL_PRESENT) != 0)
{
if (desc->Dacl == nullptr)
{
// NULL DACL == full access
mode = 0666;
}
else
{
perms.owner = desiredUser == (uid_t)-1 ? geteuid() : desiredUser;
perms.group = desiredGroup == (gid_t)-1 ? getegid() : desiredGroup;
// Parse DACL and check if it may fit standard bitmask
if (!perms.mergeDacl(desc->Dacl))
{
return INVALID_HANDLE_VALUE;
}
if (perms.size() <= 3)
{
// Only mandatory entries are set
mode = perms.toMode();
}
else
{
// Attributes must be set later
defaultAccess = false;
}
}
}
}
// else default security is used
if (defaultAccess && (dwFlagsAndAtrributes & FILE_ATTRIBUTE_READONLY) != 0)
{
// there is no dedicated R/O flag on Linux so just revoke write permissions
mode &= ~(mode_t)0222;
}
}
int h = open(lpFileName, flags, defaultAccess ? mode : 0000);
if (h != -1)
{
bool failure = false;
bool fileCreated = false;
struct stat s;
if (fstat(h, &s) == 0)
{
if (!defaultAccess // Permissions must be set explicitly
&& (0777 & s.st_mode) == 0 // This file perhaps has been created but it may indeed has zero mode
&& s.st_uid == geteuid() // More probability.
&& s.st_gid == getegid() // Even more
// struct stat has no creation time so we cannot check it here to reduce false positive probability even more
)
{
// File has been created.
fileCreated = true;
// Set proper owner and permissions if they were specified in security attributes
if (desiredUser != (uid_t)-1 || desiredGroup != (gid_t)-1)
{
if (fchown(h, desiredUser, desiredGroup) == -1)
{
failure = true;
}
else
{
// On success adjust credentials in stat structure to match desired
if (desiredUser != (uid_t)-1)
s.st_uid = desiredUser;
if (desiredGroup != (gid_t)-1)
s.st_gid = desiredGroup;
}
}
// Set permissions
if (!failure)
{
SECURITY_DESCRIPTOR* desc = reinterpret_cast<SECURITY_DESCRIPTOR*>(lpSecurityAttributes->lpSecurityDescriptor);
if ((desc->Control & SE_DACL_PRESENT) != 0 && desc->Dacl != nullptr)
{
// Windows in such case owerwrites every existing permissions and so we have to do
acl::Attributes attr(perms.toAttr());
if (fsetxattr(h, XATTR_NAME_POSIX_ACL_ACCESS, attr.get(), attr.size(), 0) == -1)
{
failure = true;
}
}
else
{
if (fchmod(h, (~S_IFMT & s.st_mode) | mode) == -1)
{
failure = true;
}
}
}
s.st_mode |= mode;
}
}
else
{
failure = true;
}
// If anything bad happened above and file has been created - file must not exists after return
if (failure)
{
int olderr = errno;
// At this point fileCreate may be false if file has been created with default security.
// TODO: Fix that somehow
if (fileCreated)
{
unlink(lpFileName); // And don't check for errors
}
close(h);
errno = olderr;
return INVALID_HANDLE_VALUE;
}
std::unique_ptr<FileHandle> result(new FileHandle(h));
// WINE rely on server keeping all opened files in the same memory so there is no way to be compatible with it.
// It is not written in Windows docs but request to open file with no desired access don't block it
// even if share mode is NONE.
if (thisMode != 0)
{
// Lock the main file while messing with lock file to avoid race conditions.
// If it fail... nothing bad happen. Perhaps.
// Documentation insists that this lock is released automatically on descriptor close
// so we don't have to care about RAII holder
int lockRes = flock(result->handle, LOCK_EX);
// Problem here that RO descriptors cannot place WR locks and vice versa.
// So we must create another object specifically for locking purpose.
// It could be a real file in temp dir but shared memory seems to be better
// though its ability to accept locks is not explicitly documented.
result->lockFileName = "/WinCompLck.";
result->lockFileName += std::to_string(major(s.st_dev));
result->lockFileName += '.';
result->lockFileName += std::to_string(minor(s.st_dev));
result->lockFileName += '.';
result->lockFileName += std::to_string(s.st_ino);
// Allow access to everyone because there is no way to synchronize rights with the main file
result->lockFile = shm_open(result->lockFileName.c_str(), O_CREAT | O_RDWR, 0666);
if (result->lockFile == -1)
{
return INVALID_HANDLE_VALUE;
}
// Force the lock file to be accessible to anybody.
if (fchmod(result->lockFile, s.st_mode & 0666) != 0)
{
return INVALID_HANDLE_VALUE;
}
struct flock lock;
// First check that requested mode is not prohibited by already set locks.
if (thisMode & FILE_READ_DATA)
{
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ShareOffset + ReadLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_GETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
if (lock.l_type != F_UNLCK)
{
errno = ERROR_SHARING_VIOLATION;
return INVALID_HANDLE_VALUE;
}
}
if (thisMode & FILE_WRITE_DATA)
{
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ShareOffset + WriteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_GETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
if (lock.l_type != F_UNLCK)
{
errno = ERROR_SHARING_VIOLATION;
return INVALID_HANDLE_VALUE;
}
}
if (thisMode & DELETE)
{
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ShareOffset + DeleteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_GETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
if (lock.l_type != F_UNLCK)
{
errno = ERROR_SHARING_VIOLATION;
return INVALID_HANDLE_VALUE;
}
}
// Then check if some already opened handles conflict with requested share mode
if ((dwShareMode & FILE_SHARE_READ) == 0)
{
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset + ReadLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_GETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
if (lock.l_type != F_UNLCK)
{
errno = ERROR_SHARING_VIOLATION;
return INVALID_HANDLE_VALUE;
}
}
if ((dwShareMode & FILE_SHARE_WRITE) == 0)
{
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset + WriteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_GETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
if (lock.l_type != F_UNLCK)
{
errno = ERROR_SHARING_VIOLATION;
return INVALID_HANDLE_VALUE;
}
}
if ((dwShareMode & FILE_SHARE_DELETE) == 0)
{
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset + DeleteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_GETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
if (lock.l_type != F_UNLCK)
{
errno = ERROR_SHARING_VIOLATION;
return INVALID_HANDLE_VALUE;
}
}
// Finally set locks for open mode and share mode
if (thisMode & FILE_READ_DATA)
{
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset + ReadLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_SETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
}
if (thisMode & FILE_WRITE_DATA)
{
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset + WriteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_SETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
}
if (thisMode & DELETE)
{
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ModeOffset + DeleteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_SETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
}
if ((dwShareMode & FILE_SHARE_READ) == 0)
{
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ShareOffset + ReadLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_SETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
}
if ((dwShareMode & FILE_SHARE_WRITE) == 0)
{
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ShareOffset + WriteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_SETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
}
if ((dwShareMode & FILE_SHARE_DELETE) == 0)
{
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ShareOffset + DeleteLock;
lock.l_len = 1;
lock.l_pid = 0;
if (fcntl(result->lockFile, F_OFD_SETLK, &lock) == -1)
{
return INVALID_HANDLE_VALUE;
}
}
// Finally we can release EX lock if it has been aquired
if (lockRes == 0)
flock(result->handle, LOCK_UN);
}
result->fileName = lpFileName;
// TODO: Move unlink to close from this temporary stub
if (dwFlagsAndAtrributes & FILE_FLAG_DELETE_ON_CLOSE)
{
unlink(lpFileName);
}
if (dwFlagsAndAtrributes & FILE_FLAG_SEQUENTIAL_SCAN)
{
posix_fadvise(h, 0, 0, POSIX_FADV_SEQUENTIAL);
}
if (dwFlagsAndAtrributes & FILE_ATTRIBUTE_TEMPORARY)
{
// I hope kernel can recognize this hint and avoid flushes as much as possible
const uint64_t hint = RWH_WRITE_LIFE_SHORT;
fcntl(h, F_SET_FILE_RW_HINT, &hint);
}
// Per specification the error must be set if CREATE_ALWAYS or OPEN_ALWAYS was used and file already exists
errno = !fileCreated && (flags & O_CREAT) != 0 ? ERROR_ALREADY_EXISTS : 0;
return result.release();
}
return INVALID_HANDLE_VALUE;
}
HANDLE CreateFile(const wchar_t* lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAtrributes, HANDLE hTemplateFile)
{
if (lpFileName == nullptr)
{
errno = ERROR_PATH_NOT_FOUND;
return INVALID_HANDLE_VALUE;
}
// Skip \\?\ prefix if used
constexpr ptrdiff_t prefixLen = 4;
if (wcsncmp(lpFileName, L"\\\\?\\", prefixLen) == 0)
{
lpFileName += prefixLen;
}
// Convert Unicode name into ANSI and reuse ANSI version of the function
ssize_t n = wcstombs(nullptr, lpFileName, 0);
if (n == (ssize_t)-1)
{
errno = ERROR_INVALID_NAME;
return INVALID_HANDLE_VALUE;
}
std::unique_ptr<char[]> name(new char[n + 1]);
wcstombs(name.get(), lpFileName, n + 1);
return CreateFile(name.get(), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAtrributes, hTemplateFile);
}
WINBOOL ReadFile(HANDLE hFile, void* lpBuffer, DWORD nNumberOfBytesToRead, DWORD* lpNumberOfBytesRead, OVERLAPPED* lpOverlapped)
{
if (lpNumberOfBytesRead != nullptr)
*lpNumberOfBytesRead = 0;
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
if (lpBuffer == nullptr)
{
errno = ERROR_NOACCESS;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
ssize_t r = 0;
if (lpOverlapped == nullptr)
{
// Synchronous execution
// Despite MSDN Windows (10 at least) allows both lpOverlapped and lpNumberOfBytesRead to be nullptr
// if (lpNumberOfBytesRead == nullptr)
// {
// errno = ERROR_INVALID_PARAMETERS;
// return FALSE;
// }
r = read(handle->handle, lpBuffer, nNumberOfBytesToRead);
if (r == -1)
{
return FALSE;
}
}
else
{
off64_t offset = lpOverlapped->OffsetHigh;
offset <<= 32;
offset |= lpOverlapped->Offset;
r = pread(handle->handle, lpBuffer, nNumberOfBytesToRead, offset);
if (r == -1)
{
return FALSE;
}
if (r == 0)
{
errno = ERROR_HANDLE_EOF;
return FALSE;
}
offset += r;
lpOverlapped->Offset = offset & 0xFFFFFFFF;
lpOverlapped->OffsetHigh = offset >> 32;
}
if (lpNumberOfBytesRead != nullptr)
{
*lpNumberOfBytesRead = r;
}
return TRUE;
}
WINBOOL WriteFile(HANDLE hFile, const void* lpBuffer, DWORD nNumberOfBytesToWrite, DWORD* lpNumberOfBytesWritten, OVERLAPPED* lpOverlapped)
{
if (lpNumberOfBytesWritten != nullptr)
*lpNumberOfBytesWritten = 0;
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
if (lpBuffer == nullptr)
{
errno = ERROR_NOACCESS;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
ssize_t r = 0;
if (lpOverlapped == nullptr)
{
// Synchronous execution
// Despite MSDN Windows (10 at least) allows both lpOverlapped and lpNumberOfBytesWritten to be nullptr
// if (lpNumberOfBytesRead == nullptr)
// {
// errno = ERROR_INVALID_PARAMETERS;
// return FALSE;
// }
r = write(handle->handle, lpBuffer, nNumberOfBytesToWrite);
if (r == -1)
{
return FALSE;
}
}
else
{
off64_t offset = lpOverlapped->OffsetHigh;
offset <<= 32;
offset |= lpOverlapped->Offset;
r = pwrite(handle->handle, lpBuffer, nNumberOfBytesToWrite, offset);
if (r == -1)
{
return FALSE;
}
offset += r;
lpOverlapped->Offset = offset & 0xFFFFFFFF;
lpOverlapped->OffsetHigh = offset >> 32;
}
if (lpNumberOfBytesWritten != nullptr)
{
*lpNumberOfBytesWritten = r;
}
return TRUE;
}
WINBOOL FlushFileBuffers(HANDLE hFile)
{
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
return fsync(handle->handle) == -1?FALSE:TRUE;
}
DWORD SetFilePointer(HANDLE hFile, int32_t lDistanceToMove, LONG* lpDistanceToMoveHigh, DWORD dwMoveMethod)
{
LARGE_INTEGER newPos;
newPos.QuadPart = lDistanceToMove; // Let sign expand
if (lpDistanceToMoveHigh != nullptr)
{
// Override high bits
newPos.HighPart = *lpDistanceToMoveHigh;
}
if (SetFilePointerEx(hFile, newPos, &newPos, dwMoveMethod) == FALSE)
{
return INVALID_SET_FILE_POINTER;
}
errno = 0;
if (lpDistanceToMoveHigh != nullptr)
{
*lpDistanceToMoveHigh = newPos.HighPart;
}
else
{
if (newPos.HighPart != 0)
return INVALID_SET_FILE_POINTER;
}
return newPos.LowPart;
}
WINBOOL SetFilePointerEx(HANDLE hFile, const LARGE_INTEGER& liDistanceToMove, LARGE_INTEGER* lpNewFilePointer, DWORD dwMoveMethod)
{
static_assert(FILE_BEGIN == SEEK_SET, "FILE_BEGIN wrong");
static_assert(FILE_CURRENT == SEEK_CUR, "FILE_CURRENT wrong");
static_assert(FILE_END == SEEK_END, "FILE_END wrong");
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
off64_t newPos = lseek64(handle->handle, liDistanceToMove.QuadPart, dwMoveMethod);
if (newPos == (__off64_t)-1)
return FALSE;
if (lpNewFilePointer != nullptr)
{
lpNewFilePointer->QuadPart = newPos;
}
return TRUE;
}
WINBOOL SetFileInformationByHandle(HANDLE hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, void* lpFileInformation, DWORD dwBufferSize)
{
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
if (lpFileInformation == nullptr)
{
errno = ERROR_NOACCESS;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
switch (FileInformationClass)
{
case FileEndOfFileInfo:
{
if (dwBufferSize != sizeof(FILE_END_OF_FILE_INFO))
{
errno = ERROR_BAD_LENGTH;
return FALSE;
}
FILE_END_OF_FILE_INFO* info = reinterpret_cast<FILE_END_OF_FILE_INFO*>(lpFileInformation);
return ftruncate64(handle->handle, info->EndOfFile.QuadPart) == 0 ? TRUE : FALSE;
break;
}
case FileDispositionInfo:
{
if (dwBufferSize != sizeof(FILE_DISPOSITION_INFO))
{
errno = ERROR_BAD_LENGTH;
return FALSE;
}
FILE_DISPOSITION_INFO* info = reinterpret_cast<FILE_DISPOSITION_INFO*>(lpFileInformation);
// I'm not sure if this call on Windows delete file immediatelly or only on close.
// Let's do it now!
if (info->DeleteFile)
{
return unlink(handle->fileName.c_str()) == 0 ? TRUE : FALSE;
}
// else what, link it back? Theoretically possible as we have preserved file name.
break;
}
default:
{
errno = ERROR_INVALID_PARAMETER;
return FALSE;
}
}
return TRUE;
}
WINBOOL SetEndOfFile(HANDLE hFile)
{
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
off64_t CurPos = lseek64(handle->handle, 0, SEEK_CUR);
if (CurPos >= 0 && ftruncate64(handle->handle, CurPos) != -1)
{
return TRUE;
}
return FALSE;
}
WINBOOL GetFileSizeEx(HANDLE hFile, LARGE_INTEGER* lpFileSize)
{
if (hFile == INVALID_HANDLE_VALUE || hFile->type != handle_t::handle_type::HANDLE_FILE)
{
errno = ERROR_INVALID_HANDLE;
return FALSE;
}
if (lpFileSize == nullptr)
{
errno = ERROR_NOACCESS;
return FALSE;
}
FileHandle* handle = static_cast<FileHandle*>(hFile);
struct stat64 stats;
if (fstat64(handle->handle, &stats) == -1)
{
return FALSE;
}
static_assert(sizeof(stats.st_size) == sizeof(lpFileSize->QuadPart), "Size of size mismatch");
lpFileSize->QuadPart = stats.st_size;
return TRUE;
}
const char* getTempEnv()
{
const char* tmpdir = getenv("TMPDIR"); // Main POSIX candidate
if (tmpdir == nullptr)
tmpdir = getenv("TMP"); // These are non-standard
if (tmpdir == nullptr)
tmpdir = getenv("TEMP");
if (tmpdir == nullptr)
tmpdir = "/tmp/"; // Last resort fallback
return tmpdir;
}
DWORD GetTempPath(DWORD nBufferLength, char* lpBuffer)
{
const char* tmpdir = getTempEnv();
size_t l = strlen(tmpdir);
// Clean buffer just in case
if (lpBuffer != nullptr && nBufferLength != 0)
{
*lpBuffer = '\0';
}
// Sanity checks
if (l > 0)
{
if (l < nBufferLength)
{
memcpy(lpBuffer, tmpdir, l + 1);
// Ensure directory separator at the end
if (lpBuffer[l - 1] != '/')
{
lpBuffer[l++] = '/';
if (l < nBufferLength)
{
lpBuffer[l] = '\0';
}
else
{
// Missing separator cannot fit, pretend that whole result is failure
*lpBuffer = '\0';
}
}
}
}
return l;
}
DWORD GetTempPath(DWORD nBufferLength, wchar_t* lpBuffer)
{
const char* tmpdir = getTempEnv();
size_t l = strlen(tmpdir);
// Clean buffer just in case
if (lpBuffer != nullptr && nBufferLength != 0)
{
*lpBuffer = L'\0';
}
// Sanity checks
if (l > 0)
{
size_t n = mbstowcs(nullptr, tmpdir, 0);
if (n == (size_t)-1)
{
//errno = ???
return 0;
}
if (n < nBufferLength)
{
n = mbstowcs(lpBuffer, tmpdir, nBufferLength);
if (lpBuffer[n - 1] != L'/')
{
lpBuffer[n++] = L'/';
if (n < nBufferLength)
{
lpBuffer[n] = L'\0';
}
else
{
// Missing separator cannot fit, pretend that whole result is failure
*lpBuffer = L'\0';
}
}
}
return n;
}
return 0;
}
// It doesn't really belong here but let it stay for a while...
FILE* _wfopen(const wchar_t* filename, const wchar_t* mode)
{
size_t fn_len = wcstombs(nullptr, filename, 0);
if (fn_len == (size_t)-1)
return nullptr;
std::unique_ptr<char[]> fn(new char[++fn_len]);