1 | /* $Id: fileio-win.cpp 8245 2008-04-21 17:24:28Z 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 | */
|
---|
71 | inline 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 | */
|
---|
107 | DECLINLINE(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 |
|
---|
131 | RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, unsigned fOpen)
|
---|
132 | {
|
---|
133 | /*
|
---|
134 | * Validate input.
|
---|
135 | */
|
---|
136 | if (!pFile)
|
---|
137 | {
|
---|
138 | AssertMsgFailed(("Invalid pFile\n"));
|
---|
139 | return VERR_INVALID_PARAMETER;
|
---|
140 | }
|
---|
141 | *pFile = NIL_RTFILE;
|
---|
142 | if (!pszFilename)
|
---|
143 | {
|
---|
144 | AssertMsgFailed(("Invalid pszFilename\n"));
|
---|
145 | return VERR_INVALID_PARAMETER;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Merge forced open flags and validate them.
|
---|
150 | */
|
---|
151 | int rc = rtFileRecalcAndValidateFlags(&fOpen);
|
---|
152 | if (RT_FAILURE(rc))
|
---|
153 | return rc;
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Determine disposition, access, share mode, creation flags, and security attributes
|
---|
157 | * for the CreateFile API call.
|
---|
158 | */
|
---|
159 | DWORD dwCreationDisposition;
|
---|
160 | switch (fOpen & RTFILE_O_ACTION_MASK)
|
---|
161 | {
|
---|
162 | case RTFILE_O_OPEN:
|
---|
163 | dwCreationDisposition = fOpen & RTFILE_O_TRUNCATE ? TRUNCATE_EXISTING : OPEN_EXISTING;
|
---|
164 | break;
|
---|
165 | case RTFILE_O_OPEN_CREATE:
|
---|
166 | dwCreationDisposition = OPEN_ALWAYS;
|
---|
167 | break;
|
---|
168 | case RTFILE_O_CREATE:
|
---|
169 | dwCreationDisposition = CREATE_NEW;
|
---|
170 | break;
|
---|
171 | case RTFILE_O_CREATE_REPLACE:
|
---|
172 | dwCreationDisposition = CREATE_ALWAYS;
|
---|
173 | break;
|
---|
174 | default:
|
---|
175 | AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
|
---|
176 | return VERR_INVALID_PARAMETER;
|
---|
177 | }
|
---|
178 |
|
---|
179 | DWORD dwDesiredAccess;
|
---|
180 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
181 | {
|
---|
182 | case RTFILE_O_READ: dwDesiredAccess = GENERIC_READ; break;
|
---|
183 | case RTFILE_O_WRITE: dwDesiredAccess = GENERIC_WRITE; break;
|
---|
184 | case RTFILE_O_READWRITE: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
|
---|
185 | default:
|
---|
186 | AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
|
---|
187 | return VERR_INVALID_PARAMETER;
|
---|
188 | }
|
---|
189 |
|
---|
190 | DWORD dwShareMode;
|
---|
191 | Assert(RTFILE_O_DENY_READWRITE == RTFILE_O_DENY_ALL && !RTFILE_O_DENY_NONE);
|
---|
192 | switch (fOpen & RTFILE_O_DENY_MASK)
|
---|
193 | {
|
---|
194 | case RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
|
---|
195 | case RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_WRITE; break;
|
---|
196 | case RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_READ; break;
|
---|
197 | case RTFILE_O_DENY_READWRITE: dwShareMode = 0; break;
|
---|
198 |
|
---|
199 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; break;
|
---|
200 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_WRITE; break;
|
---|
201 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ; break;
|
---|
202 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READWRITE:dwShareMode = FILE_SHARE_DELETE; break;
|
---|
203 | default:
|
---|
204 | AssertMsgFailed(("Impossible fOpen=%#x\n", fOpen));
|
---|
205 | return VERR_INVALID_PARAMETER;
|
---|
206 | }
|
---|
207 |
|
---|
208 | SECURITY_ATTRIBUTES SecurityAttributes;
|
---|
209 | PSECURITY_ATTRIBUTES pSecurityAttributes = NULL;
|
---|
210 | if (fOpen & RTFILE_O_INHERIT)
|
---|
211 | {
|
---|
212 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
---|
213 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
---|
214 | SecurityAttributes.bInheritHandle = TRUE;
|
---|
215 | pSecurityAttributes = &SecurityAttributes;
|
---|
216 | }
|
---|
217 |
|
---|
218 | DWORD dwFlagsAndAttributes;
|
---|
219 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
|
---|
220 | if (fOpen & RTFILE_O_WRITE_THROUGH)
|
---|
221 | dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Open/Create the file.
|
---|
225 | */
|
---|
226 | #ifdef RT_DONT_CONVERT_FILENAMES
|
---|
227 | HANDLE hFile = CreateFile(pszFilename,
|
---|
228 | dwDesiredAccess,
|
---|
229 | dwShareMode,
|
---|
230 | pSecurityAttributes,
|
---|
231 | dwCreationDisposition,
|
---|
232 | dwFlagsAndAttributes,
|
---|
233 | NULL);
|
---|
234 | #else
|
---|
235 | PRTUTF16 pwszFilename;
|
---|
236 | rc = RTStrToUtf16(pszFilename, &pwszFilename);
|
---|
237 | if (RT_FAILURE(rc))
|
---|
238 | return rc;
|
---|
239 |
|
---|
240 | HANDLE hFile = CreateFileW(pwszFilename,
|
---|
241 | dwDesiredAccess,
|
---|
242 | dwShareMode,
|
---|
243 | pSecurityAttributes,
|
---|
244 | dwCreationDisposition,
|
---|
245 | dwFlagsAndAttributes,
|
---|
246 | NULL);
|
---|
247 | #endif
|
---|
248 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
249 | {
|
---|
250 | bool fCreated = dwCreationDisposition == CREATE_ALWAYS
|
---|
251 | || dwCreationDisposition == CREATE_NEW
|
---|
252 | || (dwCreationDisposition == OPEN_ALWAYS && GetLastError() == 0);
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * Turn off indexing of directory through Windows Indexing Service.
|
---|
256 | */
|
---|
257 | if ( fCreated
|
---|
258 | && (fOpen & RTFILE_O_NOT_CONTENT_INDEXED))
|
---|
259 | {
|
---|
260 | #ifdef RT_DONT_CONVERT_FILENAMES
|
---|
261 | if (!SetFileAttributes(pszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
|
---|
262 | #else
|
---|
263 | if (!SetFileAttributesW(pwszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
|
---|
264 | #endif
|
---|
265 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
266 | }
|
---|
267 | /*
|
---|
268 | * Do we need to truncate the file?
|
---|
269 | */
|
---|
270 | else if ( !fCreated
|
---|
271 | && (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_ACTION_MASK))
|
---|
272 | == (RTFILE_O_TRUNCATE | RTFILE_O_OPEN_CREATE))
|
---|
273 | {
|
---|
274 | if (!SetEndOfFile(hFile))
|
---|
275 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
276 | }
|
---|
277 | if (RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | *pFile = (RTFILE)hFile;
|
---|
280 | Assert((HANDLE)*pFile == hFile);
|
---|
281 | #ifndef RT_DONT_CONVERT_FILENAMES
|
---|
282 | RTUtf16Free(pwszFilename);
|
---|
283 | #endif
|
---|
284 | return VINF_SUCCESS;
|
---|
285 | }
|
---|
286 |
|
---|
287 | CloseHandle(hFile);
|
---|
288 | }
|
---|
289 | else
|
---|
290 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
291 | #ifndef RT_DONT_CONVERT_FILENAMES
|
---|
292 | RTUtf16Free(pwszFilename);
|
---|
293 | #endif
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | RTR3DECL(int) RTFileClose(RTFILE File)
|
---|
299 | {
|
---|
300 | if (CloseHandle((HANDLE)File))
|
---|
301 | return VINF_SUCCESS;
|
---|
302 | return RTErrConvertFromWin32(GetLastError());
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | RTR3DECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
|
---|
307 | {
|
---|
308 | static ULONG aulSeekRecode[] =
|
---|
309 | {
|
---|
310 | FILE_BEGIN,
|
---|
311 | FILE_CURRENT,
|
---|
312 | FILE_END,
|
---|
313 | };
|
---|
314 |
|
---|
315 | /*
|
---|
316 | * Validate input.
|
---|
317 | */
|
---|
318 | if (uMethod > RTFILE_SEEK_END)
|
---|
319 | {
|
---|
320 | AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
|
---|
321 | return VERR_INVALID_PARAMETER;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Execute the seek.
|
---|
326 | */
|
---|
327 | if (MySetFilePointer(File, offSeek, poffActual, aulSeekRecode[uMethod]))
|
---|
328 | return VINF_SUCCESS;
|
---|
329 | return RTErrConvertFromWin32(GetLastError());
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | RTR3DECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
334 | {
|
---|
335 | if (cbToRead <= 0)
|
---|
336 | return VINF_SUCCESS;
|
---|
337 |
|
---|
338 | ULONG cbRead = 0;
|
---|
339 | if (ReadFile((HANDLE)File, pvBuf, cbToRead, &cbRead, NULL))
|
---|
340 | {
|
---|
341 | if (pcbRead)
|
---|
342 | /* Caller can handle partial reads. */
|
---|
343 | *pcbRead = cbRead;
|
---|
344 | else
|
---|
345 | {
|
---|
346 | /* Caller expects everything to be read. */
|
---|
347 | while (cbToRead > cbRead)
|
---|
348 | {
|
---|
349 | ULONG cbReadPart = 0;
|
---|
350 | if (!ReadFile((HANDLE)File, (char*)pvBuf + cbRead, cbToRead - cbRead, &cbReadPart, NULL))
|
---|
351 | return RTErrConvertFromWin32(GetLastError());
|
---|
352 | if (cbReadPart == 0)
|
---|
353 | return VERR_EOF;
|
---|
354 | cbRead += cbReadPart;
|
---|
355 | }
|
---|
356 | }
|
---|
357 | return VINF_SUCCESS;
|
---|
358 | }
|
---|
359 | return RTErrConvertFromWin32(GetLastError());
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | RTR3DECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
364 | {
|
---|
365 | if (cbToWrite <= 0)
|
---|
366 | return VINF_SUCCESS;
|
---|
367 |
|
---|
368 | ULONG cbWritten = 0;
|
---|
369 | if (WriteFile((HANDLE)File, pvBuf, cbToWrite, &cbWritten, NULL))
|
---|
370 | {
|
---|
371 | if (pcbWritten)
|
---|
372 | /* Caller can handle partial writes. */
|
---|
373 | *pcbWritten = cbWritten;
|
---|
374 | else
|
---|
375 | {
|
---|
376 | /* Caller expects everything to be written. */
|
---|
377 | while (cbToWrite > cbWritten)
|
---|
378 | {
|
---|
379 | ULONG cbWrittenPart = 0;
|
---|
380 | if (!WriteFile((HANDLE)File, (char*)pvBuf + cbWritten, cbToWrite - cbWritten, &cbWrittenPart, NULL))
|
---|
381 | {
|
---|
382 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
383 | if ( rc == VERR_DISK_FULL
|
---|
384 | && IsBeyondLimit(File, cbToWrite - cbWritten, FILE_CURRENT)
|
---|
385 | )
|
---|
386 | rc = VERR_FILE_TOO_BIG;
|
---|
387 | return rc;
|
---|
388 | }
|
---|
389 | if (cbWrittenPart == 0)
|
---|
390 | return VERR_WRITE_ERROR;
|
---|
391 | cbWritten += cbWrittenPart;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | return VINF_SUCCESS;
|
---|
395 | }
|
---|
396 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
397 | if ( rc == VERR_DISK_FULL
|
---|
398 | && IsBeyondLimit(File, cbToWrite - cbWritten, FILE_CURRENT)
|
---|
399 | )
|
---|
400 | rc = VERR_FILE_TOO_BIG;
|
---|
401 | return rc;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | RTR3DECL(int) RTFileFlush(RTFILE File)
|
---|
406 | {
|
---|
407 | int rc;
|
---|
408 |
|
---|
409 | if (FlushFileBuffers((HANDLE)File) == FALSE)
|
---|
410 | {
|
---|
411 | rc = GetLastError();
|
---|
412 | Log(("FlushFileBuffers failed with %d\n", rc));
|
---|
413 | return RTErrConvertFromWin32(rc);
|
---|
414 | }
|
---|
415 | return VINF_SUCCESS;
|
---|
416 | }
|
---|
417 |
|
---|
418 |
|
---|
419 | RTR3DECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize)
|
---|
420 | {
|
---|
421 | /*
|
---|
422 | * Get current file pointer.
|
---|
423 | */
|
---|
424 | int rc;
|
---|
425 | uint64_t offCurrent;
|
---|
426 | if (MySetFilePointer(File, 0, &offCurrent, FILE_CURRENT))
|
---|
427 | {
|
---|
428 | /*
|
---|
429 | * Set new file pointer.
|
---|
430 | */
|
---|
431 | if (MySetFilePointer(File, cbSize, NULL, FILE_BEGIN))
|
---|
432 | {
|
---|
433 | /* file pointer setted */
|
---|
434 | if (SetEndOfFile((HANDLE)File))
|
---|
435 | {
|
---|
436 | /*
|
---|
437 | * Restore file pointer and return.
|
---|
438 | * If the old pointer was beyond the new file end, ignore failure.
|
---|
439 | */
|
---|
440 | if ( MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN)
|
---|
441 | || offCurrent > cbSize)
|
---|
442 | return VINF_SUCCESS;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Failed, try restore file pointer.
|
---|
447 | */
|
---|
448 | rc = GetLastError();
|
---|
449 | MySetFilePointer(File, offCurrent, NULL, FILE_BEGIN);
|
---|
450 | }
|
---|
451 | else
|
---|
452 | rc = GetLastError();
|
---|
453 | }
|
---|
454 | else
|
---|
455 | rc = GetLastError();
|
---|
456 |
|
---|
457 | return RTErrConvertFromWin32(rc);
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | RTR3DECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize)
|
---|
462 | {
|
---|
463 | ULARGE_INTEGER Size;
|
---|
464 | Size.LowPart = GetFileSize((HANDLE)File, &Size.HighPart);
|
---|
465 | if (Size.LowPart != INVALID_FILE_SIZE)
|
---|
466 | {
|
---|
467 | *pcbSize = Size.QuadPart;
|
---|
468 | return VINF_SUCCESS;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /* error exit */
|
---|
472 | return RTErrConvertFromWin32(GetLastError());
|
---|
473 | }
|
---|
474 |
|
---|
475 |
|
---|
476 | RTR3DECL(bool) RTFileIsValid(RTFILE File)
|
---|
477 | {
|
---|
478 | if (File != NIL_RTFILE)
|
---|
479 | {
|
---|
480 | DWORD dwType = GetFileType((HANDLE)File);
|
---|
481 | switch (dwType)
|
---|
482 | {
|
---|
483 | case FILE_TYPE_CHAR:
|
---|
484 | case FILE_TYPE_DISK:
|
---|
485 | case FILE_TYPE_PIPE:
|
---|
486 | case FILE_TYPE_REMOTE:
|
---|
487 | return true;
|
---|
488 |
|
---|
489 | case FILE_TYPE_UNKNOWN:
|
---|
490 | if (GetLastError() == NO_ERROR)
|
---|
491 | return true;
|
---|
492 | break;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | return false;
|
---|
496 | }
|
---|
497 |
|
---|
498 |
|
---|
499 | #define LOW_DWORD(u64) ((DWORD)u64)
|
---|
500 | #define HIGH_DWORD(u64) (((DWORD *)&u64)[1])
|
---|
501 |
|
---|
502 | RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
503 | {
|
---|
504 | Assert(offLock >= 0);
|
---|
505 |
|
---|
506 | /* Check arguments. */
|
---|
507 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
508 | {
|
---|
509 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
510 | return VERR_INVALID_PARAMETER;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* Prepare flags. */
|
---|
514 | Assert(RTFILE_LOCK_WRITE);
|
---|
515 | DWORD dwFlags = (fLock & RTFILE_LOCK_WRITE) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
|
---|
516 | Assert(RTFILE_LOCK_WAIT);
|
---|
517 | if (!(fLock & RTFILE_LOCK_WAIT))
|
---|
518 | dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
|
---|
519 |
|
---|
520 | /* Windows structure. */
|
---|
521 | OVERLAPPED Overlapped;
|
---|
522 | memset(&Overlapped, 0, sizeof(Overlapped));
|
---|
523 | Overlapped.Offset = LOW_DWORD(offLock);
|
---|
524 | Overlapped.OffsetHigh = HIGH_DWORD(offLock);
|
---|
525 |
|
---|
526 | /* Note: according to Microsoft, LockFileEx API call is available starting from NT 3.5 */
|
---|
527 | if (LockFileEx((HANDLE)File, dwFlags, 0, LOW_DWORD(cbLock), HIGH_DWORD(cbLock), &Overlapped))
|
---|
528 | return VINF_SUCCESS;
|
---|
529 |
|
---|
530 | return RTErrConvertFromWin32(GetLastError());
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
535 | {
|
---|
536 | Assert(offLock >= 0);
|
---|
537 |
|
---|
538 | /* Check arguments. */
|
---|
539 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
540 | {
|
---|
541 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
542 | return VERR_INVALID_PARAMETER;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /* Remove old lock. */
|
---|
546 | int rc = RTFileUnlock(File, offLock, cbLock);
|
---|
547 | if (RT_FAILURE(rc))
|
---|
548 | return rc;
|
---|
549 |
|
---|
550 | /* Set new lock. */
|
---|
551 | rc = RTFileLock(File, fLock, offLock, cbLock);
|
---|
552 | if (RT_SUCCESS(rc))
|
---|
553 | return rc;
|
---|
554 |
|
---|
555 | /* Try to restore old lock. */
|
---|
556 | unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
|
---|
557 | rc = RTFileLock(File, fLockOld, offLock, cbLock);
|
---|
558 | if (RT_SUCCESS(rc))
|
---|
559 | return VERR_FILE_LOCK_VIOLATION;
|
---|
560 | else
|
---|
561 | return VERR_FILE_LOCK_LOST;
|
---|
562 | }
|
---|
563 |
|
---|
564 |
|
---|
565 | RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
|
---|
566 | {
|
---|
567 | Assert(offLock >= 0);
|
---|
568 |
|
---|
569 | if (UnlockFile((HANDLE)File, LOW_DWORD(offLock), HIGH_DWORD(offLock), LOW_DWORD(cbLock), HIGH_DWORD(cbLock)))
|
---|
570 | return VINF_SUCCESS;
|
---|
571 |
|
---|
572 | return RTErrConvertFromWin32(GetLastError());
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 |
|
---|
577 | RTR3DECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
578 | {
|
---|
579 | /*
|
---|
580 | * Validate input.
|
---|
581 | */
|
---|
582 | if (File == NIL_RTFILE)
|
---|
583 | {
|
---|
584 | AssertMsgFailed(("Invalid File=%RTfile\n", File));
|
---|
585 | return VERR_INVALID_PARAMETER;
|
---|
586 | }
|
---|
587 | if (!pObjInfo)
|
---|
588 | {
|
---|
589 | AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
|
---|
590 | return VERR_INVALID_PARAMETER;
|
---|
591 | }
|
---|
592 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
593 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
594 | {
|
---|
595 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
596 | return VERR_INVALID_PARAMETER;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * Query file info.
|
---|
601 | */
|
---|
602 | BY_HANDLE_FILE_INFORMATION Data;
|
---|
603 | if (!GetFileInformationByHandle((HANDLE)File, &Data))
|
---|
604 | return RTErrConvertFromWin32(GetLastError());
|
---|
605 |
|
---|
606 | /*
|
---|
607 | * Setup the returned data.
|
---|
608 | */
|
---|
609 | pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
|
---|
610 | | (uint64_t)Data.nFileSizeLow;
|
---|
611 | pObjInfo->cbAllocated = pObjInfo->cbObject;
|
---|
612 |
|
---|
613 | Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
|
---|
614 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
|
---|
615 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
|
---|
616 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
|
---|
617 | pObjInfo->ChangeTime = pObjInfo->ModificationTime;
|
---|
618 |
|
---|
619 | pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT, "", 0);
|
---|
620 |
|
---|
621 | /*
|
---|
622 | * Requested attributes (we cannot provide anything actually).
|
---|
623 | */
|
---|
624 | switch (enmAdditionalAttribs)
|
---|
625 | {
|
---|
626 | case RTFSOBJATTRADD_EASIZE:
|
---|
627 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
628 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
629 | break;
|
---|
630 |
|
---|
631 | case RTFSOBJATTRADD_UNIX:
|
---|
632 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
|
---|
633 | pObjInfo->Attr.u.Unix.uid = ~0U;
|
---|
634 | pObjInfo->Attr.u.Unix.gid = ~0U;
|
---|
635 | pObjInfo->Attr.u.Unix.cHardlinks = Data.nNumberOfLinks ? Data.nNumberOfLinks : 1;
|
---|
636 | pObjInfo->Attr.u.Unix.INodeIdDevice = 0;
|
---|
637 | pObjInfo->Attr.u.Unix.INodeId = 0;
|
---|
638 | pObjInfo->Attr.u.Unix.fFlags = 0;
|
---|
639 | pObjInfo->Attr.u.Unix.GenerationId = 0;
|
---|
640 | pObjInfo->Attr.u.Unix.Device = 0;
|
---|
641 | break;
|
---|
642 |
|
---|
643 | case RTFSOBJATTRADD_NOTHING:
|
---|
644 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
645 | break;
|
---|
646 |
|
---|
647 | default:
|
---|
648 | AssertMsgFailed(("Impossible!\n"));
|
---|
649 | return VERR_INTERNAL_ERROR;
|
---|
650 | }
|
---|
651 |
|
---|
652 | return VINF_SUCCESS;
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 | RTR3DECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
657 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
658 | {
|
---|
659 | if (!pAccessTime && !pModificationTime && !pBirthTime)
|
---|
660 | return VINF_SUCCESS; /* NOP */
|
---|
661 |
|
---|
662 | FILETIME CreationTimeFT;
|
---|
663 | PFILETIME pCreationTimeFT = NULL;
|
---|
664 | if (pBirthTime)
|
---|
665 | pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
|
---|
666 |
|
---|
667 | FILETIME LastAccessTimeFT;
|
---|
668 | PFILETIME pLastAccessTimeFT = NULL;
|
---|
669 | if (pAccessTime)
|
---|
670 | pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
|
---|
671 |
|
---|
672 | FILETIME LastWriteTimeFT;
|
---|
673 | PFILETIME pLastWriteTimeFT = NULL;
|
---|
674 | if (pModificationTime)
|
---|
675 | pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
|
---|
676 |
|
---|
677 | int rc = VINF_SUCCESS;
|
---|
678 | if (!SetFileTime((HANDLE)File, pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
|
---|
679 | {
|
---|
680 | DWORD Err = GetLastError();
|
---|
681 | rc = RTErrConvertFromWin32(Err);
|
---|
682 | Log(("RTFileSetTimes(%RTfile, %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Vrc)\n",
|
---|
683 | File, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
|
---|
684 | }
|
---|
685 | return rc;
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | RTR3DECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode)
|
---|
690 | {
|
---|
691 | /** @todo darn. this needs a full path; probably must be done if the file is closed
|
---|
692 | * It's quite possible that there is an NT API for this. NtSetInformationFile() for instance. */
|
---|
693 | return VINF_SUCCESS;
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | RTR3DECL(int) RTFileDelete(const char *pszFilename)
|
---|
698 | {
|
---|
699 | #ifdef RT_DONT_CONVERT_FILENAMES
|
---|
700 | if (DeleteFile(pszFilename))
|
---|
701 | return VINF_SUCCESS;
|
---|
702 | return RTErrConvertFromWin32(GetLastError());
|
---|
703 |
|
---|
704 | #else
|
---|
705 | PRTUTF16 pwszFilename;
|
---|
706 | int rc = RTStrToUtf16(pszFilename, &pwszFilename);
|
---|
707 | if (RT_SUCCESS(rc))
|
---|
708 | {
|
---|
709 | if (!DeleteFileW(pwszFilename))
|
---|
710 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
711 | RTUtf16Free(pwszFilename);
|
---|
712 | }
|
---|
713 |
|
---|
714 | return rc;
|
---|
715 | #endif
|
---|
716 | }
|
---|
717 |
|
---|
718 |
|
---|
719 | RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
720 | {
|
---|
721 | /*
|
---|
722 | * Validate input.
|
---|
723 | */
|
---|
724 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
725 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
726 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
727 |
|
---|
728 | /*
|
---|
729 | * Hand it on to the worker.
|
---|
730 | */
|
---|
731 | int rc = rtPathWin32MoveRename(pszSrc, pszDst,
|
---|
732 | fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
|
---|
733 | RTFS_TYPE_FILE);
|
---|
734 |
|
---|
735 | LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
736 | pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
737 | return rc;
|
---|
738 |
|
---|
739 | }
|
---|
740 |
|
---|
741 |
|
---|
742 | RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
|
---|
743 | {
|
---|
744 | /*
|
---|
745 | * Validate input.
|
---|
746 | */
|
---|
747 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
748 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
749 | AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
|
---|
750 |
|
---|
751 | /*
|
---|
752 | * Hand it on to the worker.
|
---|
753 | */
|
---|
754 | int rc = rtPathWin32MoveRename(pszSrc, pszDst,
|
---|
755 | fMove & RTFILEMOVE_FLAGS_REPLACE
|
---|
756 | ? MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING
|
---|
757 | : MOVEFILE_COPY_ALLOWED,
|
---|
758 | RTFS_TYPE_FILE);
|
---|
759 |
|
---|
760 | LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
761 | pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
|
---|
762 | return rc;
|
---|
763 | }
|
---|
764 |
|
---|