VirtualBox

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

Last change on this file since 20371 was 19186, checked in by vboxsync, 16 years ago

Runtime/Aio: Add Windows backend

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