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