VirtualBox

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

Last change on this file since 17793 was 16650, checked in by vboxsync, 16 years ago

IPRT: implement RTFileSetMode on Windows.

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

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