VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/fileio-win.cpp@ 28863

Last change on this file since 28863 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.2 KB
Line 
1/* $Id: fileio-win.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, native implementation for the Windows host platform.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DIR
32#include <Windows.h>
33
34#include <iprt/file.h>
35#include <iprt/path.h>
36#include <iprt/assert.h>
37#include <iprt/string.h>
38#include <iprt/err.h>
39#include <iprt/log.h>
40#include "internal/file.h"
41#include "internal/fs.h"
42#include "internal/path.h"
43
44
45/*******************************************************************************
46* Defined Constants And Macros *
47*******************************************************************************/
48/** @def RT_DONT_CONVERT_FILENAMES
49 * Define this to pass UTF-8 unconverted to the kernel. */
50#ifdef DOXYGEN_RUNNING
51# define RT_DONT_CONVERT_FILENAMES 1
52#endif
53
54
55/**
56 * This is wrapper around the ugly SetFilePointer api.
57 *
58 * It's equivalent to SetFilePointerEx which we so unfortunately cannot use because of
59 * it not being present in NT4 GA.
60 *
61 * @returns Success indicator. Extended error information obtainable using GetLastError().
62 * @param File Filehandle.
63 * @param offSeek Offset to seek.
64 * @param poffNew Where to store the new file offset. NULL allowed.
65 * @param uMethod Seek method. (The windows one!)
66 */
67DECLINLINE(bool) MySetFilePointer(RTFILE File, uint64_t offSeek, uint64_t *poffNew, unsigned uMethod)
68{
69 bool fRc;
70 LARGE_INTEGER off;
71
72 off.QuadPart = offSeek;
73#if 1
74 if (off.LowPart != INVALID_SET_FILE_POINTER)
75 {
76 off.LowPart = SetFilePointer((HANDLE)File, off.LowPart, &off.HighPart, uMethod);
77 fRc = off.LowPart != INVALID_SET_FILE_POINTER;
78 }
79 else
80 {
81 SetLastError(NO_ERROR);
82 off.LowPart = SetFilePointer((HANDLE)File, off.LowPart, &off.HighPart, uMethod);
83 fRc = GetLastError() == NO_ERROR;
84 }
85#else
86 fRc = SetFilePointerEx((HANDLE)File, off, &off, uMethod);
87#endif
88 if (fRc && poffNew)
89 *poffNew = off.QuadPart;
90 return fRc;
91}
92
93
94/**
95 * This is a helper to check if an attempt was made to grow a file beyond the
96 * limit of the filesystem.
97 *
98 * @returns true for file size limit exceeded.
99 * @param File Filehandle.
100 * @param offSeek Offset to seek.
101 * @param uMethod The seek method.
102 */
103DECLINLINE(bool) IsBeyondLimit(RTFILE File, uint64_t offSeek, unsigned uMethod)
104{
105 bool fIsBeyondLimit = false;
106
107 /*
108 * Get the current file position and try set the new one.
109 * If it fails with a seek error it's because we hit the file system limit.
110 */
111/** @todo r=bird: I'd be very interested to know on which versions of windows and on which file systems
112 * this supposedly works. The fastfat sources in the latest WDK makes no limit checks during
113 * file seeking, only at the time of writing (and some other odd ones we cannot make use of). */
114 uint64_t offCurrent;
115 if (MySetFilePointer(File, 0, &offCurrent, FILE_CURRENT))
116 {
117 if (!MySetFilePointer(File, offSeek, NULL, uMethod))
118 fIsBeyondLimit = GetLastError() == ERROR_SEEK;
119 else /* Restore file pointer on success. */
120 MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN);
121 }
122
123 return fIsBeyondLimit;
124}
125
126
127RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
128{
129 HANDLE h = (HANDLE)uNative;
130 if ( h == INVALID_HANDLE_VALUE
131 || (RTFILE)uNative != uNative)
132 {
133 AssertMsgFailed(("%p\n", uNative));
134 *pFile = NIL_RTFILE;
135 return VERR_INVALID_HANDLE;
136 }
137 *pFile = (RTFILE)h;
138 return VINF_SUCCESS;
139}
140
141
142RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE File)
143{
144 AssertReturn(File != NIL_RTFILE, (RTHCINTPTR)INVALID_HANDLE_VALUE);
145 return (RTHCINTPTR)File;
146}
147
148
149RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint32_t fOpen)
150{
151 /*
152 * Validate input.
153 */
154 if (!pFile)
155 {
156 AssertMsgFailed(("Invalid pFile\n"));
157 return VERR_INVALID_PARAMETER;
158 }
159 *pFile = NIL_RTFILE;
160 if (!pszFilename)
161 {
162 AssertMsgFailed(("Invalid pszFilename\n"));
163 return VERR_INVALID_PARAMETER;
164 }
165
166 /*
167 * Merge forced open flags and validate them.
168 */
169 int rc = rtFileRecalcAndValidateFlags(&fOpen);
170 if (RT_FAILURE(rc))
171 return rc;
172
173 /*
174 * Determine disposition, access, share mode, creation flags, and security attributes
175 * for the CreateFile API call.
176 */
177 DWORD dwCreationDisposition;
178 switch (fOpen & RTFILE_O_ACTION_MASK)
179 {
180 case RTFILE_O_OPEN:
181 dwCreationDisposition = fOpen & RTFILE_O_TRUNCATE ? TRUNCATE_EXISTING : OPEN_EXISTING;
182 break;
183 case RTFILE_O_OPEN_CREATE:
184 dwCreationDisposition = OPEN_ALWAYS;
185 break;
186 case RTFILE_O_CREATE:
187 dwCreationDisposition = CREATE_NEW;
188 break;
189 case RTFILE_O_CREATE_REPLACE:
190 dwCreationDisposition = CREATE_ALWAYS;
191 break;
192 default:
193 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
194 return VERR_INVALID_PARAMETER;
195 }
196
197 DWORD dwDesiredAccess;
198 switch (fOpen & RTFILE_O_ACCESS_MASK)
199 {
200 case RTFILE_O_READ:
201 dwDesiredAccess = FILE_GENERIC_READ; /* RTFILE_O_APPEND is ignored. */
202 break;
203 case RTFILE_O_WRITE:
204 dwDesiredAccess = fOpen & RTFILE_O_APPEND
205 ? FILE_GENERIC_WRITE & ~FILE_WRITE_DATA
206 : FILE_GENERIC_WRITE;
207 break;
208 case RTFILE_O_READWRITE:
209 dwDesiredAccess = fOpen & RTFILE_O_APPEND
210 ? FILE_GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
211 : FILE_GENERIC_READ | FILE_GENERIC_WRITE;
212 break;
213 default:
214 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
215 return VERR_INVALID_PARAMETER;
216 }
217 if (dwCreationDisposition == TRUNCATE_EXISTING)
218 /* Required for truncating the file (see MSDN), it is *NOT* part of FILE_GENERIC_WRITE. */
219 dwDesiredAccess |= GENERIC_WRITE;
220
221 /* RTFileSetMode needs following rights as well. */
222 switch (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
223 {
224 case RTFILE_O_ACCESS_ATTR_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
225 case RTFILE_O_ACCESS_ATTR_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
226 case RTFILE_O_ACCESS_ATTR_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
227 default:
228 /* Attributes access is the same as the file access. */
229 switch (fOpen & RTFILE_O_ACCESS_MASK)
230 {
231 case RTFILE_O_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
232 case RTFILE_O_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
233 case RTFILE_O_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
234 default:
235 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
236 return VERR_INVALID_PARAMETER;
237 }
238 }
239
240 DWORD dwShareMode;
241 switch (fOpen & RTFILE_O_DENY_MASK)
242 {
243 case RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
244 case RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_WRITE; break;
245 case RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_READ; break;
246 case RTFILE_O_DENY_READWRITE: dwShareMode = 0; break;
247
248 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; break;
249 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_WRITE; break;
250 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ; break;
251 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READWRITE:dwShareMode = FILE_SHARE_DELETE; break;
252 default:
253 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
254 return VERR_INVALID_PARAMETER;
255 }
256
257 SECURITY_ATTRIBUTES SecurityAttributes;
258 PSECURITY_ATTRIBUTES pSecurityAttributes = NULL;
259 if (fOpen & RTFILE_O_INHERIT)
260 {
261 SecurityAttributes.nLength = sizeof(SecurityAttributes);
262 SecurityAttributes.lpSecurityDescriptor = NULL;
263 SecurityAttributes.bInheritHandle = TRUE;
264 pSecurityAttributes = &SecurityAttributes;
265 }
266
267 DWORD dwFlagsAndAttributes;
268 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
269 if (fOpen & RTFILE_O_WRITE_THROUGH)
270 dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
271 if (fOpen & RTFILE_O_ASYNC_IO)
272 dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
273 if (fOpen & RTFILE_O_NO_CACHE)
274 {
275 dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
276 dwDesiredAccess &= ~FILE_APPEND_DATA;
277 }
278
279 /*
280 * Open/Create the file.
281 */
282#ifdef RT_DONT_CONVERT_FILENAMES
283 HANDLE hFile = CreateFile(pszFilename,
284 dwDesiredAccess,
285 dwShareMode,
286 pSecurityAttributes,
287 dwCreationDisposition,
288 dwFlagsAndAttributes,
289 NULL);
290#else
291 PRTUTF16 pwszFilename;
292 rc = RTStrToUtf16(pszFilename, &pwszFilename);
293 if (RT_FAILURE(rc))
294 return rc;
295
296 HANDLE hFile = CreateFileW(pwszFilename,
297 dwDesiredAccess,
298 dwShareMode,
299 pSecurityAttributes,
300 dwCreationDisposition,
301 dwFlagsAndAttributes,
302 NULL);
303#endif
304 if (hFile != INVALID_HANDLE_VALUE)
305 {
306 bool fCreated = dwCreationDisposition == CREATE_ALWAYS
307 || dwCreationDisposition == CREATE_NEW
308 || (dwCreationDisposition == OPEN_ALWAYS && GetLastError() == 0);
309
310 /*
311 * Turn off indexing of directory through Windows Indexing Service.
312 */
313 if ( fCreated
314 && (fOpen & RTFILE_O_NOT_CONTENT_INDEXED))
315 {
316#ifdef RT_DONT_CONVERT_FILENAMES
317 if (!SetFileAttributes(pszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
318#else
319 if (!SetFileAttributesW(pwszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
320#endif
321 rc = RTErrConvertFromWin32(GetLastError());
322 }
323 /*
324 * Do we need to truncate the file?
325 */
326 else if ( !fCreated
327 && (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_ACTION_MASK))
328 == (RTFILE_O_TRUNCATE | RTFILE_O_OPEN_CREATE))
329 {
330 if (!SetEndOfFile(hFile))
331 rc = RTErrConvertFromWin32(GetLastError());
332 }
333 if (RT_SUCCESS(rc))
334 {
335 *pFile = (RTFILE)hFile;
336 Assert((HANDLE)*pFile == hFile);
337#ifndef RT_DONT_CONVERT_FILENAMES
338 RTUtf16Free(pwszFilename);
339#endif
340 return VINF_SUCCESS;
341 }
342
343 CloseHandle(hFile);
344 }
345 else
346 rc = RTErrConvertFromWin32(GetLastError());
347#ifndef RT_DONT_CONVERT_FILENAMES
348 RTUtf16Free(pwszFilename);
349#endif
350 return rc;
351}
352
353
354RTR3DECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint32_t fAccess)
355{
356 AssertReturn( fAccess == RTFILE_O_READ
357 || fAccess == RTFILE_O_WRITE
358 || fAccess == RTFILE_O_READWRITE,
359 VERR_INVALID_PARAMETER);
360 return RTFileOpen(phFile, "NUL", fAccess | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
361}
362
363
364RTR3DECL(int) RTFileClose(RTFILE File)
365{
366 if (CloseHandle((HANDLE)File))
367 return VINF_SUCCESS;
368 return RTErrConvertFromWin32(GetLastError());
369}
370
371
372RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
373{
374 static ULONG aulSeekRecode[] =
375 {
376 FILE_BEGIN,
377 FILE_CURRENT,
378 FILE_END,
379 };
380
381 /*
382 * Validate input.
383 */
384 if (uMethod > RTFILE_SEEK_END)
385 {
386 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
387 return VERR_INVALID_PARAMETER;
388 }
389
390 /*
391 * Execute the seek.
392 */
393 if (MySetFilePointer(File, offSeek, poffActual, aulSeekRecode[uMethod]))
394 return VINF_SUCCESS;
395 return RTErrConvertFromWin32(GetLastError());
396}
397
398
399RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
400{
401 if (cbToRead <= 0)
402 return VINF_SUCCESS;
403 ULONG cbToReadAdj = (ULONG)cbToRead;
404 AssertReturn(cbToReadAdj == cbToRead, VERR_NUMBER_TOO_BIG);
405
406 ULONG cbRead = 0;
407 if (ReadFile((HANDLE)File, pvBuf, cbToReadAdj, &cbRead, NULL))
408 {
409 if (pcbRead)
410 /* Caller can handle partial reads. */
411 *pcbRead = cbRead;
412 else
413 {
414 /* Caller expects everything to be read. */
415 while (cbToReadAdj > cbRead)
416 {
417 ULONG cbReadPart = 0;
418 if (!ReadFile((HANDLE)File, (char*)pvBuf + cbRead, cbToReadAdj - cbRead, &cbReadPart, NULL))
419 return RTErrConvertFromWin32(GetLastError());
420 if (cbReadPart == 0)
421 return VERR_EOF;
422 cbRead += cbReadPart;
423 }
424 }
425 return VINF_SUCCESS;
426 }
427 return RTErrConvertFromWin32(GetLastError());
428}
429
430
431RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
432{
433 if (cbToWrite <= 0)
434 return VINF_SUCCESS;
435 ULONG cbToWriteAdj = (ULONG)cbToWrite;
436 AssertReturn(cbToWriteAdj == cbToWrite, VERR_NUMBER_TOO_BIG);
437
438 ULONG cbWritten = 0;
439 if (WriteFile((HANDLE)File, pvBuf, cbToWriteAdj, &cbWritten, NULL))
440 {
441 if (pcbWritten)
442 /* Caller can handle partial writes. */
443 *pcbWritten = cbWritten;
444 else
445 {
446 /* Caller expects everything to be written. */
447 while (cbToWriteAdj > cbWritten)
448 {
449 ULONG cbWrittenPart = 0;
450 if (!WriteFile((HANDLE)File, (char*)pvBuf + cbWritten, cbToWriteAdj - cbWritten, &cbWrittenPart, NULL))
451 {
452 int rc = RTErrConvertFromWin32(GetLastError());
453 if ( rc == VERR_DISK_FULL
454 && IsBeyondLimit(File, cbToWriteAdj - cbWritten, FILE_CURRENT)
455 )
456 rc = VERR_FILE_TOO_BIG;
457 return rc;
458 }
459 if (cbWrittenPart == 0)
460 return VERR_WRITE_ERROR;
461 cbWritten += cbWrittenPart;
462 }
463 }
464 return VINF_SUCCESS;
465 }
466 int rc = RTErrConvertFromWin32(GetLastError());
467 if ( rc == VERR_DISK_FULL
468 && IsBeyondLimit(File, cbToWriteAdj - cbWritten, FILE_CURRENT))
469 rc = VERR_FILE_TOO_BIG;
470 return rc;
471}
472
473
474RTR3DECL(int) RTFileFlush(RTFILE File)
475{
476 if (!FlushFileBuffers((HANDLE)File))
477 {
478 int rc = GetLastError();
479 Log(("FlushFileBuffers failed with %d\n", rc));
480 return RTErrConvertFromWin32(rc);
481 }
482 return VINF_SUCCESS;
483}
484
485
486RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
487{
488 /*
489 * Get current file pointer.
490 */
491 int rc;
492 uint64_t offCurrent;
493 if (MySetFilePointer(File, 0, &offCurrent, FILE_CURRENT))
494 {
495 /*
496 * Set new file pointer.
497 */
498 if (MySetFilePointer(File, cbSize, NULL, FILE_BEGIN))
499 {
500 /* file pointer setted */
501 if (SetEndOfFile((HANDLE)File))
502 {
503 /*
504 * Restore file pointer and return.
505 * If the old pointer was beyond the new file end, ignore failure.
506 */
507 if ( MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN)
508 || offCurrent > cbSize)
509 return VINF_SUCCESS;
510 }
511
512 /*
513 * Failed, try restore file pointer.
514 */
515 rc = GetLastError();
516 MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN);
517 }
518 else
519 rc = GetLastError();
520 }
521 else
522 rc = GetLastError();
523
524 return RTErrConvertFromWin32(rc);
525}
526
527
528RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
529{
530 ULARGE_INTEGER Size;
531 Size.LowPart = GetFileSize((HANDLE)File, &Size.HighPart);
532 if (Size.LowPart != INVALID_FILE_SIZE)
533 {
534 *pcbSize = Size.QuadPart;
535 return VINF_SUCCESS;
536 }
537
538 /* error exit */
539 return RTErrConvertFromWin32(GetLastError());
540}
541
542
543RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
544{
545 /** @todo r=bird:
546 * We might have to make this code OS specific...
547 * In the worse case, we'll have to try GetVolumeInformationByHandle on vista and fall
548 * back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation) else where, and
549 * check for known file system names. (For LAN shares we'll have to figure out the remote
550 * file system.) */
551 return VERR_NOT_IMPLEMENTED;
552}
553
554
555RTR3DECL(bool) RTFileIsValid(RTFILE File)
556{
557 if (File != NIL_RTFILE)
558 {
559 DWORD dwType = GetFileType((HANDLE)File);
560 switch (dwType)
561 {
562 case FILE_TYPE_CHAR:
563 case FILE_TYPE_DISK:
564 case FILE_TYPE_PIPE:
565 case FILE_TYPE_REMOTE:
566 return true;
567
568 case FILE_TYPE_UNKNOWN:
569 if (GetLastError() == NO_ERROR)
570 return true;
571 break;
572 }
573 }
574 return false;
575}
576
577
578#define LOW_DWORD(u64) ((DWORD)u64)
579#define HIGH_DWORD(u64) (((DWORD *)&u64)[1])
580
581RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
582{
583 Assert(offLock >= 0);
584
585 /* Check arguments. */
586 if (fLock & ~RTFILE_LOCK_MASK)
587 {
588 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
589 return VERR_INVALID_PARAMETER;
590 }
591
592 /* Prepare flags. */
593 Assert(RTFILE_LOCK_WRITE);
594 DWORD dwFlags = (fLock & RTFILE_LOCK_WRITE) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
595 Assert(RTFILE_LOCK_WAIT);
596 if (!(fLock & RTFILE_LOCK_WAIT))
597 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
598
599 /* Windows structure. */
600 OVERLAPPED Overlapped;
601 memset(&Overlapped, 0, sizeof(Overlapped));
602 Overlapped.Offset = LOW_DWORD(offLock);
603 Overlapped.OffsetHigh = HIGH_DWORD(offLock);
604
605 /* Note: according to Microsoft, LockFileEx API call is available starting from NT 3.5 */
606 if (LockFileEx((HANDLE)File, dwFlags, 0, LOW_DWORD(cbLock), HIGH_DWORD(cbLock), &Overlapped))
607 return VINF_SUCCESS;
608
609 return RTErrConvertFromWin32(GetLastError());
610}
611
612
613RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
614{
615 Assert(offLock >= 0);
616
617 /* Check arguments. */
618 if (fLock & ~RTFILE_LOCK_MASK)
619 {
620 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
621 return VERR_INVALID_PARAMETER;
622 }
623
624 /* Remove old lock. */
625 int rc = RTFileUnlock(File, offLock, cbLock);
626 if (RT_FAILURE(rc))
627 return rc;
628
629 /* Set new lock. */
630 rc = RTFileLock(File, fLock, offLock, cbLock);
631 if (RT_SUCCESS(rc))
632 return rc;
633
634 /* Try to restore old lock. */
635 unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
636 rc = RTFileLock(File, fLockOld, offLock, cbLock);
637 if (RT_SUCCESS(rc))
638 return VERR_FILE_LOCK_VIOLATION;
639 else
640 return VERR_FILE_LOCK_LOST;
641}
642
643
644RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
645{
646 Assert(offLock >= 0);
647
648 if (UnlockFile((HANDLE)File, LOW_DWORD(offLock), HIGH_DWORD(offLock), LOW_DWORD(cbLock), HIGH_DWORD(cbLock)))
649 return VINF_SUCCESS;
650
651 return RTErrConvertFromWin32(GetLastError());
652}
653
654
655
656RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
657{
658 /*
659 * Validate input.
660 */
661 if (File == NIL_RTFILE)
662 {
663 AssertMsgFailed(("Invalid File=%RTfile\n", File));
664 return VERR_INVALID_PARAMETER;
665 }
666 if (!pObjInfo)
667 {
668 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
669 return VERR_INVALID_PARAMETER;
670 }
671 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
672 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
673 {
674 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
675 return VERR_INVALID_PARAMETER;
676 }
677
678 /*
679 * Query file info.
680 */
681 BY_HANDLE_FILE_INFORMATION Data;
682 if (!GetFileInformationByHandle((HANDLE)File, &Data))
683 return RTErrConvertFromWin32(GetLastError());
684
685 /*
686 * Setup the returned data.
687 */
688 pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
689 | (uint64_t)Data.nFileSizeLow;
690 pObjInfo->cbAllocated = pObjInfo->cbObject;
691
692 Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
693 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
694 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
695 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
696 pObjInfo->ChangeTime = pObjInfo->ModificationTime;
697
698 pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT, "", 0);
699
700 /*
701 * Requested attributes (we cannot provide anything actually).
702 */
703 switch (enmAdditionalAttribs)
704 {
705 case RTFSOBJATTRADD_EASIZE:
706 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
707 pObjInfo->Attr.u.EASize.cb = 0;
708 break;
709
710 case RTFSOBJATTRADD_UNIX:
711 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
712 pObjInfo->Attr.u.Unix.uid = ~0U;
713 pObjInfo->Attr.u.Unix.gid = ~0U;
714 pObjInfo->Attr.u.Unix.cHardlinks = Data.nNumberOfLinks ? Data.nNumberOfLinks : 1;
715 pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /** @todo Use the volume serial number (see GetFileInformationByHandle). */
716 pObjInfo->Attr.u.Unix.INodeId = 0; /** @todo Use the fileid (see GetFileInformationByHandle). */
717 pObjInfo->Attr.u.Unix.fFlags = 0;
718 pObjInfo->Attr.u.Unix.GenerationId = 0;
719 pObjInfo->Attr.u.Unix.Device = 0;
720 break;
721
722 case RTFSOBJATTRADD_NOTHING:
723 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
724 break;
725
726 default:
727 AssertMsgFailed(("Impossible!\n"));
728 return VERR_INTERNAL_ERROR;
729 }
730
731 return VINF_SUCCESS;
732}
733
734
735RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
736 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
737{
738 if (!pAccessTime && !pModificationTime && !pBirthTime)
739 return VINF_SUCCESS; /* NOP */
740
741 FILETIME CreationTimeFT;
742 PFILETIME pCreationTimeFT = NULL;
743 if (pBirthTime)
744 pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
745
746 FILETIME LastAccessTimeFT;
747 PFILETIME pLastAccessTimeFT = NULL;
748 if (pAccessTime)
749 pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
750
751 FILETIME LastWriteTimeFT;
752 PFILETIME pLastWriteTimeFT = NULL;
753 if (pModificationTime)
754 pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
755
756 int rc = VINF_SUCCESS;
757 if (!SetFileTime((HANDLE)File, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
758 {
759 DWORD Err = GetLastError();
760 rc = RTErrConvertFromWin32(Err);
761 Log(("RTFileSetTimes(%RTfile, %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
762 File, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
763 }
764 return rc;
765}
766
767
768/* This comes from a source file with a different set of system headers (DDK)
769 * so it can't be declared in a common header, like internal/file.h.
770 */
771extern int rtFileNativeSetAttributes(HANDLE FileHandle, ULONG FileAttributes);
772
773
774RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
775{
776 /*
777 * Normalize the mode and call the API.
778 */
779 fMode = rtFsModeNormalize(fMode, NULL, 0);
780 if (!rtFsModeIsValid(fMode))
781 return VERR_INVALID_PARAMETER;
782
783 ULONG FileAttributes = (fMode & RTFS_DOS_MASK) >> RTFS_DOS_SHIFT;
784 int Err = rtFileNativeSetAttributes((HANDLE)File, FileAttributes);
785 if (Err != ERROR_SUCCESS)
786 {
787 int rc = RTErrConvertFromWin32(Err);
788 Log(("RTFileSetMode(%RTfile, %RTfmode): rtFileNativeSetAttributes (0x%08X) failed with err %d (%Rrc)\n",
789 File, fMode, FileAttributes, Err, rc));
790 return rc;
791 }
792 return VINF_SUCCESS;
793}
794
795
796RTR3DECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
797 uint32_t *pcbBlock, uint32_t *pcbSector)
798{
799 /** @todo implement this using NtQueryVolumeInformationFile(hFile,,,,
800 * FileFsSizeInformation). */
801 return VERR_NOT_SUPPORTED;
802}
803
804
805RTR3DECL(int) RTFileDelete(const char *pszFilename)
806{
807#ifdef RT_DONT_CONVERT_FILENAMES
808 if (DeleteFile(pszFilename))
809 return VINF_SUCCESS;
810 return RTErrConvertFromWin32(GetLastError());
811
812#else
813 PRTUTF16 pwszFilename;
814 int rc = RTStrToUtf16(pszFilename, &pwszFilename);
815 if (RT_SUCCESS(rc))
816 {
817 if (!DeleteFileW(pwszFilename))
818 rc = RTErrConvertFromWin32(GetLastError());
819 RTUtf16Free(pwszFilename);
820 }
821
822 return rc;
823#endif
824}
825
826
827RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
828{
829 /*
830 * Validate input.
831 */
832 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
833 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
834 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
835
836 /*
837 * Hand it on to the worker.
838 */
839 int rc = rtPathWin32MoveRename(pszSrc, pszDst,
840 fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
841 RTFS_TYPE_FILE);
842
843 LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
844 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
845 return rc;
846
847}
848
849
850RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
851{
852 /*
853 * Validate input.
854 */
855 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
856 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
857 AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
858
859 /*
860 * Hand it on to the worker.
861 */
862 int rc = rtPathWin32MoveRename(pszSrc, pszDst,
863 fMove & RTFILEMOVE_FLAGS_REPLACE
864 ? MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING
865 : MOVEFILE_COPY_ALLOWED,
866 RTFS_TYPE_FILE);
867
868 LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
869 pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
870 return rc;
871}
872
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette