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
|
/* $Id: fileio-win.cpp 28918 2010-04-29 18:30:09Z vboxsync $ */
/** @file
* IPRT - File I/O, native implementation for the Windows host platform.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_DIR
#include <Windows.h>
#include <iprt/file.h>
#include <iprt/path.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#include <iprt/err.h>
#include <iprt/log.h>
#include "internal/file.h"
#include "internal/fs.h"
#include "internal/path.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/**
* This is wrapper around the ugly SetFilePointer api.
*
* It's equivalent to SetFilePointerEx which we so unfortunately cannot use because of
* it not being present in NT4 GA.
*
* @returns Success indicator. Extended error information obtainable using GetLastError().
* @param File Filehandle.
* @param offSeek Offset to seek.
* @param poffNew Where to store the new file offset. NULL allowed.
* @param uMethod Seek method. (The windows one!)
*/
DECLINLINE(bool) MySetFilePointer(RTFILE File, uint64_t offSeek, uint64_t *poffNew, unsigned uMethod)
{
bool fRc;
LARGE_INTEGER off;
off.QuadPart = offSeek;
#if 1
if (off.LowPart != INVALID_SET_FILE_POINTER)
{
off.LowPart = SetFilePointer((HANDLE)File, off.LowPart, &off.HighPart, uMethod);
fRc = off.LowPart != INVALID_SET_FILE_POINTER;
}
else
{
SetLastError(NO_ERROR);
off.LowPart = SetFilePointer((HANDLE)File, off.LowPart, &off.HighPart, uMethod);
fRc = GetLastError() == NO_ERROR;
}
#else
fRc = SetFilePointerEx((HANDLE)File, off, &off, uMethod);
#endif
if (fRc && poffNew)
*poffNew = off.QuadPart;
return fRc;
}
/**
* This is a helper to check if an attempt was made to grow a file beyond the
* limit of the filesystem.
*
* @returns true for file size limit exceeded.
* @param File Filehandle.
* @param offSeek Offset to seek.
* @param uMethod The seek method.
*/
DECLINLINE(bool) IsBeyondLimit(RTFILE File, uint64_t offSeek, unsigned uMethod)
{
bool fIsBeyondLimit = false;
/*
* Get the current file position and try set the new one.
* If it fails with a seek error it's because we hit the file system limit.
*/
/** @todo r=bird: I'd be very interested to know on which versions of windows and on which file systems
* this supposedly works. The fastfat sources in the latest WDK makes no limit checks during
* file seeking, only at the time of writing (and some other odd ones we cannot make use of). */
uint64_t offCurrent;
if (MySetFilePointer(File, 0, &offCurrent, FILE_CURRENT))
{
if (!MySetFilePointer(File, offSeek, NULL, uMethod))
fIsBeyondLimit = GetLastError() == ERROR_SEEK;
else /* Restore file pointer on success. */
MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN);
}
return fIsBeyondLimit;
}
RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
{
HANDLE h = (HANDLE)uNative;
if ( h == INVALID_HANDLE_VALUE
|| (RTFILE)uNative != uNative)
{
AssertMsgFailed(("%p\n", uNative));
*pFile = NIL_RTFILE;
return VERR_INVALID_HANDLE;
}
*pFile = (RTFILE)h;
return VINF_SUCCESS;
}
RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
{
AssertReturn(File != NIL_RTFILE, (RTHCINTPTR)INVALID_HANDLE_VALUE);
return (RTHCINTPTR)File;
}
RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint32_t fOpen)
{
/*
* Validate input.
*/
if (!pFile)
{
AssertMsgFailed(("Invalid pFile\n"));
return VERR_INVALID_PARAMETER;
}
*pFile = NIL_RTFILE;
if (!pszFilename)
{
AssertMsgFailed(("Invalid pszFilename\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Merge forced open flags and validate them.
*/
int rc = rtFileRecalcAndValidateFlags(&fOpen);
if (RT_FAILURE(rc))
return rc;
/*
* Determine disposition, access, share mode, creation flags, and security attributes
* for the CreateFile API call.
*/
DWORD dwCreationDisposition;
switch (fOpen & RTFILE_O_ACTION_MASK)
{
case RTFILE_O_OPEN:
dwCreationDisposition = fOpen & RTFILE_O_TRUNCATE ? TRUNCATE_EXISTING : OPEN_EXISTING;
break;
case RTFILE_O_OPEN_CREATE:
dwCreationDisposition = OPEN_ALWAYS;
break;
case RTFILE_O_CREATE:
dwCreationDisposition = CREATE_NEW;
break;
case RTFILE_O_CREATE_REPLACE:
dwCreationDisposition = CREATE_ALWAYS;
break;
default:
AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
return VERR_INVALID_PARAMETER;
}
DWORD dwDesiredAccess;
switch (fOpen & RTFILE_O_ACCESS_MASK)
{
case RTFILE_O_READ:
dwDesiredAccess = FILE_GENERIC_READ; /* RTFILE_O_APPEND is ignored. */
break;
case RTFILE_O_WRITE:
dwDesiredAccess = fOpen & RTFILE_O_APPEND
? FILE_GENERIC_WRITE & ~FILE_WRITE_DATA
: FILE_GENERIC_WRITE;
break;
case RTFILE_O_READWRITE:
dwDesiredAccess = fOpen & RTFILE_O_APPEND
? FILE_GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
: FILE_GENERIC_READ | FILE_GENERIC_WRITE;
break;
default:
AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
return VERR_INVALID_PARAMETER;
}
if (dwCreationDisposition == TRUNCATE_EXISTING)
/* Required for truncating the file (see MSDN), it is *NOT* part of FILE_GENERIC_WRITE. */
dwDesiredAccess |= GENERIC_WRITE;
/* RTFileSetMode needs following rights as well. */
switch (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
{
case RTFILE_O_ACCESS_ATTR_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
case RTFILE_O_ACCESS_ATTR_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
case RTFILE_O_ACCESS_ATTR_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
default:
/* Attributes access is the same as the file access. */
switch (fOpen & RTFILE_O_ACCESS_MASK)
{
case RTFILE_O_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
case RTFILE_O_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
case RTFILE_O_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
default:
AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
return VERR_INVALID_PARAMETER;
}
}
DWORD dwShareMode;
switch (fOpen & RTFILE_O_DENY_MASK)
{
case RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
case RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_WRITE; break;
case RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_READ; break;
case RTFILE_O_DENY_READWRITE: dwShareMode = 0; break;
case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; break;
case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_WRITE; break;
case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ; break;
case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READWRITE:dwShareMode = FILE_SHARE_DELETE; break;
default:
AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
return VERR_INVALID_PARAMETER;
}
SECURITY_ATTRIBUTES SecurityAttributes;
PSECURITY_ATTRIBUTES pSecurityAttributes = NULL;
if (fOpen & RTFILE_O_INHERIT)
{
SecurityAttributes.nLength = sizeof(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor = NULL;
SecurityAttributes.bInheritHandle = TRUE;
pSecurityAttributes = &SecurityAttributes;
}
DWORD dwFlagsAndAttributes;
dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
if (fOpen & RTFILE_O_WRITE_THROUGH)
dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
if (fOpen & RTFILE_O_ASYNC_IO)
dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
if (fOpen & RTFILE_O_NO_CACHE)
{
dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
dwDesiredAccess &= ~FILE_APPEND_DATA;
}
/*
* Open/Create the file.
*/
PRTUTF16 pwszFilename;
rc = RTStrToUtf16(pszFilename, &pwszFilename);
if (RT_FAILURE(rc))
return rc;
HANDLE hFile = CreateFileW(pwszFilename,
dwDesiredAccess,
dwShareMode,
pSecurityAttributes,
dwCreationDisposition,
dwFlagsAndAttributes,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
bool fCreated = dwCreationDisposition == CREATE_ALWAYS
|| dwCreationDisposition == CREATE_NEW
|| (dwCreationDisposition == OPEN_ALWAYS && GetLastError() == 0);
/*
* Turn off indexing of directory through Windows Indexing Service.
*/
if ( fCreated
&& (fOpen & RTFILE_O_NOT_CONTENT_INDEXED))
{
if (!SetFileAttributesW(pwszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
rc = RTErrConvertFromWin32(GetLastError());
}
/*
* Do we need to truncate the file?
*/
else if ( !fCreated
&& (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_ACTION_MASK))
== (RTFILE_O_TRUNCATE | RTFILE_O_OPEN_CREATE))
{
if (!SetEndOfFile(hFile))
rc = RTErrConvertFromWin32(GetLastError());
}
if (RT_SUCCESS(rc))
{
*pFile = (RTFILE)hFile;
Assert((HANDLE)*pFile == hFile);
RTUtf16Free(pwszFilename);
return VINF_SUCCESS;
}
CloseHandle(hFile);
}
else
rc = RTErrConvertFromWin32(GetLastError());
RTUtf16Free(pwszFilename);
return rc;
}
RTR3DECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint32_t fAccess)
{
AssertReturn( fAccess == RTFILE_O_READ
|| fAccess == RTFILE_O_WRITE
|| fAccess == RTFILE_O_READWRITE,
VERR_INVALID_PARAMETER);
return RTFileOpen(phFile, "NUL", fAccess | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
}
RTR3DECL(int) RTFileClose(RTFILE File)
{
if (CloseHandle((HANDLE)File))
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
{
static ULONG aulSeekRecode[] =
{
FILE_BEGIN,
FILE_CURRENT,
FILE_END,
};
/*
* Validate input.
*/
if (uMethod > RTFILE_SEEK_END)
{
AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
return VERR_INVALID_PARAMETER;
}
/*
* Execute the seek.
*/
if (MySetFilePointer(File, offSeek, poffActual, aulSeekRecode[uMethod]))
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
if (cbToRead <= 0)
return VINF_SUCCESS;
ULONG cbToReadAdj = (ULONG)cbToRead;
AssertReturn(cbToReadAdj == cbToRead, VERR_NUMBER_TOO_BIG);
ULONG cbRead = 0;
if (ReadFile((HANDLE)File, pvBuf, cbToReadAdj, &cbRead, NULL))
{
if (pcbRead)
/* Caller can handle partial reads. */
*pcbRead = cbRead;
else
{
/* Caller expects everything to be read. */
while (cbToReadAdj > cbRead)
{
ULONG cbReadPart = 0;
if (!ReadFile((HANDLE)File, (char*)pvBuf + cbRead, cbToReadAdj - cbRead, &cbReadPart, NULL))
return RTErrConvertFromWin32(GetLastError());
if (cbReadPart == 0)
return VERR_EOF;
cbRead += cbReadPart;
}
}
return VINF_SUCCESS;
}
return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
if (cbToWrite <= 0)
return VINF_SUCCESS;
ULONG cbToWriteAdj = (ULONG)cbToWrite;
AssertReturn(cbToWriteAdj == cbToWrite, VERR_NUMBER_TOO_BIG);
ULONG cbWritten = 0;
if (WriteFile((HANDLE)File, pvBuf, cbToWriteAdj, &cbWritten, NULL))
{
if (pcbWritten)
/* Caller can handle partial writes. */
*pcbWritten = cbWritten;
else
{
/* Caller expects everything to be written. */
while (cbToWriteAdj > cbWritten)
{
ULONG cbWrittenPart = 0;
if (!WriteFile((HANDLE)File, (char*)pvBuf + cbWritten, cbToWriteAdj - cbWritten, &cbWrittenPart, NULL))
{
int rc = RTErrConvertFromWin32(GetLastError());
if ( rc == VERR_DISK_FULL
&& IsBeyondLimit(File, cbToWriteAdj - cbWritten, FILE_CURRENT)
)
rc = VERR_FILE_TOO_BIG;
return rc;
}
if (cbWrittenPart == 0)
return VERR_WRITE_ERROR;
cbWritten += cbWrittenPart;
}
}
return VINF_SUCCESS;
}
int rc = RTErrConvertFromWin32(GetLastError());
if ( rc == VERR_DISK_FULL
&& IsBeyondLimit(File, cbToWriteAdj - cbWritten, FILE_CURRENT))
rc = VERR_FILE_TOO_BIG;
return rc;
}
RTR3DECL(int) RTFileFlush(RTFILE File)
{
if (!FlushFileBuffers((HANDLE)File))
{
int rc = GetLastError();
Log(("FlushFileBuffers failed with %d\n", rc));
return RTErrConvertFromWin32(rc);
}
return VINF_SUCCESS;
}
RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
{
/*
* Get current file pointer.
*/
int rc;
uint64_t offCurrent;
if (MySetFilePointer(File, 0, &offCurrent, FILE_CURRENT))
{
/*
* Set new file pointer.
*/
if (MySetFilePointer(File, cbSize, NULL, FILE_BEGIN))
{
/* file pointer setted */
if (SetEndOfFile((HANDLE)File))
{
/*
* Restore file pointer and return.
* If the old pointer was beyond the new file end, ignore failure.
*/
if ( MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN)
|| offCurrent > cbSize)
return VINF_SUCCESS;
}
/*
* Failed, try restore file pointer.
*/
rc = GetLastError();
MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN);
}
else
rc = GetLastError();
}
else
rc = GetLastError();
return RTErrConvertFromWin32(rc);
}
RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
{
ULARGE_INTEGER Size;
Size.LowPart = GetFileSize((HANDLE)File, &Size.HighPart);
if (Size.LowPart != INVALID_FILE_SIZE)
{
*pcbSize = Size.QuadPart;
return VINF_SUCCESS;
}
/* error exit */
return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
{
/** @todo r=bird:
* We might have to make this code OS specific...
* In the worse case, we'll have to try GetVolumeInformationByHandle on vista and fall
* back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation) else where, and
* check for known file system names. (For LAN shares we'll have to figure out the remote
* file system.) */
return VERR_NOT_IMPLEMENTED;
}
RTR3DECL(bool) RTFileIsValid(RTFILE File)
{
if (File != NIL_RTFILE)
{
DWORD dwType = GetFileType((HANDLE)File);
switch (dwType)
{
case FILE_TYPE_CHAR:
case FILE_TYPE_DISK:
case FILE_TYPE_PIPE:
case FILE_TYPE_REMOTE:
return true;
case FILE_TYPE_UNKNOWN:
if (GetLastError() == NO_ERROR)
return true;
break;
}
}
return false;
}
#define LOW_DWORD(u64) ((DWORD)u64)
#define HIGH_DWORD(u64) (((DWORD *)&u64)[1])
RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
{
Assert(offLock >= 0);
/* Check arguments. */
if (fLock & ~RTFILE_LOCK_MASK)
{
AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
return VERR_INVALID_PARAMETER;
}
/* Prepare flags. */
Assert(RTFILE_LOCK_WRITE);
DWORD dwFlags = (fLock & RTFILE_LOCK_WRITE) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
Assert(RTFILE_LOCK_WAIT);
if (!(fLock & RTFILE_LOCK_WAIT))
dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
/* Windows structure. */
OVERLAPPED Overlapped;
memset(&Overlapped, 0, sizeof(Overlapped));
Overlapped.Offset = LOW_DWORD(offLock);
Overlapped.OffsetHigh = HIGH_DWORD(offLock);
/* Note: according to Microsoft, LockFileEx API call is available starting from NT 3.5 */
if (LockFileEx((HANDLE)File, dwFlags, 0, LOW_DWORD(cbLock), HIGH_DWORD(cbLock), &Overlapped))
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
{
Assert(offLock >= 0);
/* Check arguments. */
if (fLock & ~RTFILE_LOCK_MASK)
{
AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
return VERR_INVALID_PARAMETER;
}
/* Remove old lock. */
int rc = RTFileUnlock(File, offLock, cbLock);
if (RT_FAILURE(rc))
return rc;
/* Set new lock. */
rc = RTFileLock(File, fLock, offLock, cbLock);
if (RT_SUCCESS(rc))
return rc;
/* Try to restore old lock. */
unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
rc = RTFileLock(File, fLockOld, offLock, cbLock);
if (RT_SUCCESS(rc))
return VERR_FILE_LOCK_VIOLATION;
else
return VERR_FILE_LOCK_LOST;
}
RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
{
Assert(offLock >= 0);
if (UnlockFile((HANDLE)File, LOW_DWORD(offLock), HIGH_DWORD(offLock), LOW_DWORD(cbLock), HIGH_DWORD(cbLock)))
return VINF_SUCCESS;
return RTErrConvertFromWin32(GetLastError());
}
RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
{
/*
* Validate input.
*/
if (File == NIL_RTFILE)
{
AssertMsgFailed(("Invalid File=%RTfile\n", File));
return VERR_INVALID_PARAMETER;
}
if (!pObjInfo)
{
AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
return VERR_INVALID_PARAMETER;
}
if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|| enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
{
AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
return VERR_INVALID_PARAMETER;
}
/*
* Query file info.
*/
BY_HANDLE_FILE_INFORMATION Data;
if (!GetFileInformationByHandle((HANDLE)File, &Data))
return RTErrConvertFromWin32(GetLastError());
/*
* Setup the returned data.
*/
pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
| (uint64_t)Data.nFileSizeLow;
pObjInfo->cbAllocated = pObjInfo->cbObject;
Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
pObjInfo->ChangeTime = pObjInfo->ModificationTime;
pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT, "", 0);
/*
* Requested attributes (we cannot provide anything actually).
*/
switch (enmAdditionalAttribs)
{
case RTFSOBJATTRADD_EASIZE:
pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
pObjInfo->Attr.u.EASize.cb = 0;
break;
case RTFSOBJATTRADD_UNIX:
pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
pObjInfo->Attr.u.Unix.uid = ~0U;
pObjInfo->Attr.u.Unix.gid = ~0U;
pObjInfo->Attr.u.Unix.cHardlinks = Data.nNumberOfLinks ? Data.nNumberOfLinks : 1;
pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /** @todo Use the volume serial number (see GetFileInformationByHandle). */
pObjInfo->Attr.u.Unix.INodeId = 0; /** @todo Use the fileid (see GetFileInformationByHandle). */
pObjInfo->Attr.u.Unix.fFlags = 0;
pObjInfo->Attr.u.Unix.GenerationId = 0;
pObjInfo->Attr.u.Unix.Device = 0;
break;
case RTFSOBJATTRADD_NOTHING:
pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
break;
default:
AssertMsgFailed(("Impossible!\n"));
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
{
if (!pAccessTime && !pModificationTime && !pBirthTime)
return VINF_SUCCESS; /* NOP */
FILETIME CreationTimeFT;
PFILETIME pCreationTimeFT = NULL;
if (pBirthTime)
pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
FILETIME LastAccessTimeFT;
PFILETIME pLastAccessTimeFT = NULL;
if (pAccessTime)
pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
FILETIME LastWriteTimeFT;
PFILETIME pLastWriteTimeFT = NULL;
if (pModificationTime)
pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
int rc = VINF_SUCCESS;
if (!SetFileTime((HANDLE)File, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
{
DWORD Err = GetLastError();
rc = RTErrConvertFromWin32(Err);
Log(("RTFileSetTimes(%RTfile, %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
File, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
}
return rc;
}
/* This comes from a source file with a different set of system headers (DDK)
* so it can't be declared in a common header, like internal/file.h.
*/
extern int rtFileNativeSetAttributes(HANDLE FileHandle, ULONG FileAttributes);
RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
{
/*
* Normalize the mode and call the API.
*/
fMode = rtFsModeNormalize(fMode, NULL, 0);
if (!rtFsModeIsValid(fMode))
return VERR_INVALID_PARAMETER;
ULONG FileAttributes = (fMode & RTFS_DOS_MASK) >> RTFS_DOS_SHIFT;
int Err = rtFileNativeSetAttributes((HANDLE)File, FileAttributes);
if (Err != ERROR_SUCCESS)
{
int rc = RTErrConvertFromWin32(Err);
Log(("RTFileSetMode(%RTfile, %RTfmode): rtFileNativeSetAttributes (0x%08X) failed with err %d (%Rrc)\n",
File, fMode, FileAttributes, Err, rc));
return rc;
}
return VINF_SUCCESS;
}
RTR3DECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
uint32_t *pcbBlock, uint32_t *pcbSector)
{
/** @todo implement this using NtQueryVolumeInformationFile(hFile,,,,
* FileFsSizeInformation). */
return VERR_NOT_SUPPORTED;
}
RTR3DECL(int) RTFileDelete(const char *pszFilename)
{
PRTUTF16 pwszFilename;
int rc = RTStrToUtf16(pszFilename, &pwszFilename);
if (RT_SUCCESS(rc))
{
if (!DeleteFileW(pwszFilename))
rc = RTErrConvertFromWin32(GetLastError());
RTUtf16Free(pwszFilename);
}
return rc;
}
RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
{
/*
* Validate input.
*/
AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
/*
* Hand it on to the worker.
*/
int rc = rtPathWin32MoveRename(pszSrc, pszDst,
fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
RTFS_TYPE_FILE);
LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
return rc;
}
RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
{
/*
* Validate input.
*/
AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
/*
* Hand it on to the worker.
*/
int rc = rtPathWin32MoveRename(pszSrc, pszDst,
fMove & RTFILEMOVE_FLAGS_REPLACE
? MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING
: MOVEFILE_COPY_ALLOWED,
RTFS_TYPE_FILE);
LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
return rc;
}
|