VirtualBox

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

Last change on this file since 19167 was 18128, checked in by vboxsync, 16 years ago

IPRT, Shared Folders: Implemented file attributes access flags for RTFileOpen on Windows host (xTracker #3739).

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