VirtualBox

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

Last change on this file since 27046 was 26761, checked in by vboxsync, 15 years ago

IPRT: Implemented RTFileOpenBitBucket.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.4 KB
Line 
1/* $Id: fileio-win.cpp 26761 2010-02-24 18:57:58Z 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_RUNNING
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 */
71DECLINLINE(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, uint32_t 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:
205 dwDesiredAccess = FILE_GENERIC_READ; /* RTFILE_O_APPEND is ignored. */
206 break;
207 case RTFILE_O_WRITE:
208 dwDesiredAccess = fOpen & RTFILE_O_APPEND
209 ? FILE_GENERIC_WRITE & ~FILE_WRITE_DATA
210 : FILE_GENERIC_WRITE;
211 break;
212 case RTFILE_O_READWRITE:
213 dwDesiredAccess = fOpen & RTFILE_O_APPEND
214 ? FILE_GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
215 : FILE_GENERIC_READ | FILE_GENERIC_WRITE;
216 break;
217 default:
218 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
219 return VERR_INVALID_PARAMETER;
220 }
221 if (dwCreationDisposition == TRUNCATE_EXISTING)
222 /* Required for truncating the file (see MSDN), it is *NOT* part of FILE_GENERIC_WRITE. */
223 dwDesiredAccess |= GENERIC_WRITE;
224
225 /* RTFileSetMode needs following rights as well. */
226 switch (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
227 {
228 case RTFILE_O_ACCESS_ATTR_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
229 case RTFILE_O_ACCESS_ATTR_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
230 case RTFILE_O_ACCESS_ATTR_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
231 default:
232 /* Attributes access is the same as the file access. */
233 switch (fOpen & RTFILE_O_ACCESS_MASK)
234 {
235 case RTFILE_O_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
236 case RTFILE_O_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
237 case RTFILE_O_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
238 default:
239 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
240 return VERR_INVALID_PARAMETER;
241 }
242 }
243
244 DWORD dwShareMode;
245 switch (fOpen & RTFILE_O_DENY_MASK)
246 {
247 case RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
248 case RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_WRITE; break;
249 case RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_READ; break;
250 case RTFILE_O_DENY_READWRITE: dwShareMode = 0; break;
251
252 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; break;
253 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_WRITE; break;
254 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ; break;
255 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READWRITE:dwShareMode = FILE_SHARE_DELETE; break;
256 default:
257 AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
258 return VERR_INVALID_PARAMETER;
259 }
260
261 SECURITY_ATTRIBUTES SecurityAttributes;
262 PSECURITY_ATTRIBUTES pSecurityAttributes = NULL;
263 if (fOpen & RTFILE_O_INHERIT)
264 {
265 SecurityAttributes.nLength = sizeof(SecurityAttributes);
266 SecurityAttributes.lpSecurityDescriptor = NULL;
267 SecurityAttributes.bInheritHandle = TRUE;
268 pSecurityAttributes = &SecurityAttributes;
269 }
270
271 DWORD dwFlagsAndAttributes;
272 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
273 if (fOpen & RTFILE_O_WRITE_THROUGH)
274 dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
275 if (fOpen & RTFILE_O_ASYNC_IO)
276 dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
277 if (fOpen & RTFILE_O_NO_CACHE)
278 {
279 dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
280 dwDesiredAccess &= ~FILE_APPEND_DATA;
281 }
282
283 /*
284 * Open/Create the file.
285 */
286#ifdef RT_DONT_CONVERT_FILENAMES
287 HANDLE hFile = CreateFile(pszFilename,
288 dwDesiredAccess,
289 dwShareMode,
290 pSecurityAttributes,
291 dwCreationDisposition,
292 dwFlagsAndAttributes,
293 NULL);
294#else
295 PRTUTF16 pwszFilename;
296 rc = RTStrToUtf16(pszFilename, &pwszFilename);
297 if (RT_FAILURE(rc))
298 return rc;
299
300 HANDLE hFile = CreateFileW(pwszFilename,
301 dwDesiredAccess,
302 dwShareMode,
303 pSecurityAttributes,
304 dwCreationDisposition,
305 dwFlagsAndAttributes,
306 NULL);
307#endif
308 if (hFile != INVALID_HANDLE_VALUE)
309 {
310 bool fCreated = dwCreationDisposition == CREATE_ALWAYS
311 || dwCreationDisposition == CREATE_NEW
312 || (dwCreationDisposition == OPEN_ALWAYS && GetLastError() == 0);
313
314 /*
315 * Turn off indexing of directory through Windows Indexing Service.
316 */
317 if ( fCreated
318 && (fOpen & RTFILE_O_NOT_CONTENT_INDEXED))
319 {
320#ifdef RT_DONT_CONVERT_FILENAMES
321 if (!SetFileAttributes(pszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
322#else
323 if (!SetFileAttributesW(pwszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
324#endif
325 rc = RTErrConvertFromWin32(GetLastError());
326 }
327 /*
328 * Do we need to truncate the file?
329 */
330 else if ( !fCreated
331 && (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_ACTION_MASK))
332 == (RTFILE_O_TRUNCATE | RTFILE_O_OPEN_CREATE))
333 {
334 if (!SetEndOfFile(hFile))
335 rc = RTErrConvertFromWin32(GetLastError());
336 }
337 if (RT_SUCCESS(rc))
338 {
339 *pFile = (RTFILE)hFile;
340 Assert((HANDLE)*pFile == hFile);
341#ifndef RT_DONT_CONVERT_FILENAMES
342 RTUtf16Free(pwszFilename);
343#endif
344 return VINF_SUCCESS;
345 }
346
347 CloseHandle(hFile);
348 }
349 else
350 rc = RTErrConvertFromWin32(GetLastError());
351#ifndef RT_DONT_CONVERT_FILENAMES
352 RTUtf16Free(pwszFilename);
353#endif
354 return rc;
355}
356
357
358RTR3DECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint32_t fAccess)
359{
360 AssertReturn( fAccess == RTFILE_O_READ
361 || fAccess == RTFILE_O_WRITE
362 || fAccess == RTFILE_O_READWRITE,
363 VERR_INVALID_PARAMETER);
364 return RTFileOpen(phFile, "NUL", fAccess | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
365}
366
367
368RTR3DECL(int) RTFileClose(RTFILE File)
369{
370 if (CloseHandle((HANDLE)File))
371 return VINF_SUCCESS;
372 return RTErrConvertFromWin32(GetLastError());
373}
374
375
376RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
377{
378 static ULONG aulSeekRecode[] =
379 {
380 FILE_BEGIN,
381 FILE_CURRENT,
382 FILE_END,
383 };
384
385 /*
386 * Validate input.
387 */
388 if (uMethod > RTFILE_SEEK_END)
389 {
390 AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
391 return VERR_INVALID_PARAMETER;
392 }
393
394 /*
395 * Execute the seek.
396 */
397 if (MySetFilePointer(File, offSeek, poffActual, aulSeekRecode[uMethod]))
398 return VINF_SUCCESS;
399 return RTErrConvertFromWin32(GetLastError());
400}
401
402
403RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
404{
405 if (cbToRead <= 0)
406 return VINF_SUCCESS;
407 ULONG cbToReadAdj = (ULONG)cbToRead;
408 AssertReturn(cbToReadAdj == cbToRead, VERR_NUMBER_TOO_BIG);
409
410 ULONG cbRead = 0;
411 if (ReadFile((HANDLE)File, pvBuf, cbToReadAdj, &cbRead, NULL))
412 {
413 if (pcbRead)
414 /* Caller can handle partial reads. */
415 *pcbRead = cbRead;
416 else
417 {
418 /* Caller expects everything to be read. */
419 while (cbToReadAdj > cbRead)
420 {
421 ULONG cbReadPart = 0;
422 if (!ReadFile((HANDLE)File, (char*)pvBuf + cbRead, cbToReadAdj - cbRead, &cbReadPart, NULL))
423 return RTErrConvertFromWin32(GetLastError());
424 if (cbReadPart == 0)
425 return VERR_EOF;
426 cbRead += cbReadPart;
427 }
428 }
429 return VINF_SUCCESS;
430 }
431 return RTErrConvertFromWin32(GetLastError());
432}
433
434
435RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
436{
437 if (cbToWrite <= 0)
438 return VINF_SUCCESS;
439 ULONG cbToWriteAdj = (ULONG)cbToWrite;
440 AssertReturn(cbToWriteAdj == cbToWrite, VERR_NUMBER_TOO_BIG);
441
442 ULONG cbWritten = 0;
443 if (WriteFile((HANDLE)File, pvBuf, cbToWriteAdj, &cbWritten, NULL))
444 {
445 if (pcbWritten)
446 /* Caller can handle partial writes. */
447 *pcbWritten = cbWritten;
448 else
449 {
450 /* Caller expects everything to be written. */
451 while (cbToWriteAdj > cbWritten)
452 {
453 ULONG cbWrittenPart = 0;
454 if (!WriteFile((HANDLE)File, (char*)pvBuf + cbWritten, cbToWriteAdj - cbWritten, &cbWrittenPart, NULL))
455 {
456 int rc = RTErrConvertFromWin32(GetLastError());
457 if ( rc == VERR_DISK_FULL
458 && IsBeyondLimit(File, cbToWriteAdj - cbWritten, FILE_CURRENT)
459 )
460 rc = VERR_FILE_TOO_BIG;
461 return rc;
462 }
463 if (cbWrittenPart == 0)
464 return VERR_WRITE_ERROR;
465 cbWritten += cbWrittenPart;
466 }
467 }
468 return VINF_SUCCESS;
469 }
470 int rc = RTErrConvertFromWin32(GetLastError());
471 if ( rc == VERR_DISK_FULL
472 && IsBeyondLimit(File, cbToWriteAdj - cbWritten, FILE_CURRENT))
473 rc = VERR_FILE_TOO_BIG;
474 return rc;
475}
476
477
478RTR3DECL(int) RTFileFlush(RTFILE File)
479{
480 int rc;
481
482 if (FlushFileBuffers((HANDLE)File) == FALSE)
483 {
484 rc = GetLastError();
485 Log(("FlushFileBuffers failed with %d\n", rc));
486 return RTErrConvertFromWin32(rc);
487 }
488 return VINF_SUCCESS;
489}
490
491
492RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
493{
494 /*
495 * Get current file pointer.
496 */
497 int rc;
498 uint64_t offCurrent;
499 if (MySetFilePointer(File, 0, &offCurrent, FILE_CURRENT))
500 {
501 /*
502 * Set new file pointer.
503 */
504 if (MySetFilePointer(File, cbSize, NULL, FILE_BEGIN))
505 {
506 /* file pointer setted */
507 if (SetEndOfFile((HANDLE)File))
508 {
509 /*
510 * Restore file pointer and return.
511 * If the old pointer was beyond the new file end, ignore failure.
512 */
513 if ( MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN)
514 || offCurrent > cbSize)
515 return VINF_SUCCESS;
516 }
517
518 /*
519 * Failed, try restore file pointer.
520 */
521 rc = GetLastError();
522 MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN);
523 }
524 else
525 rc = GetLastError();
526 }
527 else
528 rc = GetLastError();
529
530 return RTErrConvertFromWin32(rc);
531}
532
533
534RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
535{
536 ULARGE_INTEGER Size;
537 Size.LowPart = GetFileSize((HANDLE)File, &Size.HighPart);
538 if (Size.LowPart != INVALID_FILE_SIZE)
539 {
540 *pcbSize = Size.QuadPart;
541 return VINF_SUCCESS;
542 }
543
544 /* error exit */
545 return RTErrConvertFromWin32(GetLastError());
546}
547
548
549RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
550{
551 /** @todo r=bird:
552 * We might have to make this code OS specific...
553 * In the worse case, we'll have to try GetVolumeInformationByHandle on vista and fall
554 * back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation) else where, and
555 * check for known file system names. (For LAN shares we'll have to figure out the remote
556 * file system.) */
557 return VERR_NOT_IMPLEMENTED;
558}
559
560
561RTR3DECL(bool) RTFileIsValid(RTFILE File)
562{
563 if (File != NIL_RTFILE)
564 {
565 DWORD dwType = GetFileType((HANDLE)File);
566 switch (dwType)
567 {
568 case FILE_TYPE_CHAR:
569 case FILE_TYPE_DISK:
570 case FILE_TYPE_PIPE:
571 case FILE_TYPE_REMOTE:
572 return true;
573
574 case FILE_TYPE_UNKNOWN:
575 if (GetLastError() == NO_ERROR)
576 return true;
577 break;
578 }
579 }
580 return false;
581}
582
583
584#define LOW_DWORD(u64) ((DWORD)u64)
585#define HIGH_DWORD(u64) (((DWORD *)&u64)[1])
586
587RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
588{
589 Assert(offLock >= 0);
590
591 /* Check arguments. */
592 if (fLock & ~RTFILE_LOCK_MASK)
593 {
594 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
595 return VERR_INVALID_PARAMETER;
596 }
597
598 /* Prepare flags. */
599 Assert(RTFILE_LOCK_WRITE);
600 DWORD dwFlags = (fLock & RTFILE_LOCK_WRITE) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
601 Assert(RTFILE_LOCK_WAIT);
602 if (!(fLock & RTFILE_LOCK_WAIT))
603 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
604
605 /* Windows structure. */
606 OVERLAPPED Overlapped;
607 memset(&Overlapped, 0, sizeof(Overlapped));
608 Overlapped.Offset = LOW_DWORD(offLock);
609 Overlapped.OffsetHigh = HIGH_DWORD(offLock);
610
611 /* Note: according to Microsoft, LockFileEx API call is available starting from NT 3.5 */
612 if (LockFileEx((HANDLE)File, dwFlags, 0, LOW_DWORD(cbLock), HIGH_DWORD(cbLock), &Overlapped))
613 return VINF_SUCCESS;
614
615 return RTErrConvertFromWin32(GetLastError());
616}
617
618
619RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
620{
621 Assert(offLock >= 0);
622
623 /* Check arguments. */
624 if (fLock & ~RTFILE_LOCK_MASK)
625 {
626 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
627 return VERR_INVALID_PARAMETER;
628 }
629
630 /* Remove old lock. */
631 int rc = RTFileUnlock(File, offLock, cbLock);
632 if (RT_FAILURE(rc))
633 return rc;
634
635 /* Set new lock. */
636 rc = RTFileLock(File, fLock, offLock, cbLock);
637 if (RT_SUCCESS(rc))
638 return rc;
639
640 /* Try to restore old lock. */
641 unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
642 rc = RTFileLock(File, fLockOld, offLock, cbLock);
643 if (RT_SUCCESS(rc))
644 return VERR_FILE_LOCK_VIOLATION;
645 else
646 return VERR_FILE_LOCK_LOST;
647}
648
649
650RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
651{
652 Assert(offLock >= 0);
653
654 if (UnlockFile((HANDLE)File, LOW_DWORD(offLock), HIGH_DWORD(offLock), LOW_DWORD(cbLock), HIGH_DWORD(cbLock)))
655 return VINF_SUCCESS;
656
657 return RTErrConvertFromWin32(GetLastError());
658}
659
660
661
662RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
663{
664 /*
665 * Validate input.
666 */
667 if (File == NIL_RTFILE)
668 {
669 AssertMsgFailed(("Invalid File=%RTfile\n", File));
670 return VERR_INVALID_PARAMETER;
671 }
672 if (!pObjInfo)
673 {
674 AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
675 return VERR_INVALID_PARAMETER;
676 }
677 if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
678 || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
679 {
680 AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
681 return VERR_INVALID_PARAMETER;
682 }
683
684 /*
685 * Query file info.
686 */
687 BY_HANDLE_FILE_INFORMATION Data;
688 if (!GetFileInformationByHandle((HANDLE)File, &Data))
689 return RTErrConvertFromWin32(GetLastError());
690
691 /*
692 * Setup the returned data.
693 */
694 pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
695 | (uint64_t)Data.nFileSizeLow;
696 pObjInfo->cbAllocated = pObjInfo->cbObject;
697
698 Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
699 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
700 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
701 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
702 pObjInfo->ChangeTime = pObjInfo->ModificationTime;
703
704 pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT, "", 0);
705
706 /*
707 * Requested attributes (we cannot provide anything actually).
708 */
709 switch (enmAdditionalAttribs)
710 {
711 case RTFSOBJATTRADD_EASIZE:
712 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
713 pObjInfo->Attr.u.EASize.cb = 0;
714 break;
715
716 case RTFSOBJATTRADD_UNIX:
717 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
718 pObjInfo->Attr.u.Unix.uid = ~0U;
719 pObjInfo->Attr.u.Unix.gid = ~0U;
720 pObjInfo->Attr.u.Unix.cHardlinks = Data.nNumberOfLinks ? Data.nNumberOfLinks : 1;
721 pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /** @todo Use the volume serial number (see GetFileInformationByHandle). */
722 pObjInfo->Attr.u.Unix.INodeId = 0; /** @todo Use the fileid (see GetFileInformationByHandle). */
723 pObjInfo->Attr.u.Unix.fFlags = 0;
724 pObjInfo->Attr.u.Unix.GenerationId = 0;
725 pObjInfo->Attr.u.Unix.Device = 0;
726 break;
727
728 case RTFSOBJATTRADD_NOTHING:
729 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
730 break;
731
732 default:
733 AssertMsgFailed(("Impossible!\n"));
734 return VERR_INTERNAL_ERROR;
735 }
736
737 return VINF_SUCCESS;
738}
739
740
741RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
742 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
743{
744 if (!pAccessTime && !pModificationTime && !pBirthTime)
745 return VINF_SUCCESS; /* NOP */
746
747 FILETIME CreationTimeFT;
748 PFILETIME pCreationTimeFT = NULL;
749 if (pBirthTime)
750 pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
751
752 FILETIME LastAccessTimeFT;
753 PFILETIME pLastAccessTimeFT = NULL;
754 if (pAccessTime)
755 pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
756
757 FILETIME LastWriteTimeFT;
758 PFILETIME pLastWriteTimeFT = NULL;
759 if (pModificationTime)
760 pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
761
762 int rc = VINF_SUCCESS;
763 if (!SetFileTime((HANDLE)File, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
764 {
765 DWORD Err = GetLastError();
766 rc = RTErrConvertFromWin32(Err);
767 Log(("RTFileSetTimes(%RTfile, %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
768 File, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
769 }
770 return rc;
771}
772
773
774/* This comes from a source file with a different set of system headers (DDK)
775 * so it can't be declared in a common header, like internal/file.h.
776 */
777extern int rtFileNativeSetAttributes(HANDLE FileHandle, ULONG FileAttributes);
778
779
780RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
781{
782 /*
783 * Normalize the mode and call the API.
784 */
785 fMode = rtFsModeNormalize(fMode, NULL, 0);
786 if (!rtFsModeIsValid(fMode))
787 return VERR_INVALID_PARAMETER;
788
789 ULONG FileAttributes = (fMode & RTFS_DOS_MASK) >> RTFS_DOS_SHIFT;
790 int Err = rtFileNativeSetAttributes((HANDLE)File, FileAttributes);
791 if (Err != ERROR_SUCCESS)
792 {
793 int rc = RTErrConvertFromWin32(Err);
794 Log(("RTFileSetMode(%RTfile, %RTfmode): rtFileNativeSetAttributes (0x%08X) failed with err %d (%Rrc)\n",
795 File, fMode, FileAttributes, Err, rc));
796 return rc;
797 }
798 return VINF_SUCCESS;
799}
800
801
802RTR3DECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
803 uint32_t *pcbBlock, uint32_t *pcbSector)
804{
805 /** @todo implement this using NtQueryVolumeInformationFile(hFile,,,,
806 * FileFsSizeInformation). */
807 return VERR_NOT_SUPPORTED;
808}
809
810
811RTR3DECL(int) RTFileDelete(const char *pszFilename)
812{
813#ifdef RT_DONT_CONVERT_FILENAMES
814 if (DeleteFile(pszFilename))
815 return VINF_SUCCESS;
816 return RTErrConvertFromWin32(GetLastError());
817
818#else
819 PRTUTF16 pwszFilename;
820 int rc = RTStrToUtf16(pszFilename, &pwszFilename);
821 if (RT_SUCCESS(rc))
822 {
823 if (!DeleteFileW(pwszFilename))
824 rc = RTErrConvertFromWin32(GetLastError());
825 RTUtf16Free(pwszFilename);
826 }
827
828 return rc;
829#endif
830}
831
832
833RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
834{
835 /*
836 * Validate input.
837 */
838 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
839 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
840 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
841
842 /*
843 * Hand it on to the worker.
844 */
845 int rc = rtPathWin32MoveRename(pszSrc, pszDst,
846 fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
847 RTFS_TYPE_FILE);
848
849 LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
850 pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
851 return rc;
852
853}
854
855
856RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
857{
858 /*
859 * Validate input.
860 */
861 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
862 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
863 AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
864
865 /*
866 * Hand it on to the worker.
867 */
868 int rc = rtPathWin32MoveRename(pszSrc, pszDst,
869 fMove & RTFILEMOVE_FLAGS_REPLACE
870 ? MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING
871 : MOVEFILE_COPY_ALLOWED,
872 RTFS_TYPE_FILE);
873
874 LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
875 pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
876 return rc;
877}
878
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