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