1 | /* $Id: fileio-win.cpp 90781 2021-08-23 09:26:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, native implementation for the Windows host platform.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
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 | #ifndef _WIN32_WINNT
|
---|
33 | # define _WIN32_WINNT 0x0500
|
---|
34 | #endif
|
---|
35 | #include <iprt/nt/nt-and-windows.h>
|
---|
36 |
|
---|
37 | #include <iprt/file.h>
|
---|
38 |
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/path.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/ldr.h>
|
---|
45 | #include <iprt/log.h>
|
---|
46 | #include <iprt/utf16.h>
|
---|
47 | #include "internal/file.h"
|
---|
48 | #include "internal/fs.h"
|
---|
49 | #include "internal/path.h"
|
---|
50 | #include "internal-r3-win.h" /* For g_enmWinVer + kRTWinOSType_XXX */
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Defined Constants And Macros *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | typedef BOOL WINAPI FNVERIFYCONSOLEIOHANDLE(HANDLE);
|
---|
57 | typedef FNVERIFYCONSOLEIOHANDLE *PFNVERIFYCONSOLEIOHANDLE; /* No, nobody fell on the keyboard, really! */
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * This is wrapper around the ugly SetFilePointer api.
|
---|
62 | *
|
---|
63 | * It's equivalent to SetFilePointerEx which we so unfortunately cannot use because of
|
---|
64 | * it not being present in NT4 GA.
|
---|
65 | *
|
---|
66 | * @returns Success indicator. Extended error information obtainable using GetLastError().
|
---|
67 | * @param hFile Filehandle.
|
---|
68 | * @param offSeek Offset to seek.
|
---|
69 | * @param poffNew Where to store the new file offset. NULL allowed.
|
---|
70 | * @param uMethod Seek method. (The windows one!)
|
---|
71 | */
|
---|
72 | DECLINLINE(bool) MySetFilePointer(RTFILE hFile, uint64_t offSeek, uint64_t *poffNew, unsigned uMethod)
|
---|
73 | {
|
---|
74 | bool fRc;
|
---|
75 | LARGE_INTEGER off;
|
---|
76 |
|
---|
77 | off.QuadPart = offSeek;
|
---|
78 | #if 1
|
---|
79 | if (off.LowPart != INVALID_SET_FILE_POINTER)
|
---|
80 | {
|
---|
81 | off.LowPart = SetFilePointer((HANDLE)RTFileToNative(hFile), off.LowPart, &off.HighPart, uMethod);
|
---|
82 | fRc = off.LowPart != INVALID_SET_FILE_POINTER;
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | SetLastError(NO_ERROR);
|
---|
87 | off.LowPart = SetFilePointer((HANDLE)RTFileToNative(hFile), off.LowPart, &off.HighPart, uMethod);
|
---|
88 | fRc = GetLastError() == NO_ERROR;
|
---|
89 | }
|
---|
90 | #else
|
---|
91 | fRc = SetFilePointerEx((HANDLE)RTFileToNative(hFile), off, &off, uMethod);
|
---|
92 | #endif
|
---|
93 | if (fRc && poffNew)
|
---|
94 | *poffNew = off.QuadPart;
|
---|
95 | return fRc;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Helper for checking if a VERR_DISK_FULL isn't a VERR_FILE_TOO_BIG.
|
---|
101 | * @returns VERR_DISK_FULL or VERR_FILE_TOO_BIG.
|
---|
102 | */
|
---|
103 | static int rtFileWinCheckIfDiskReallyFull(RTFILE hFile, uint64_t cbDesired)
|
---|
104 | {
|
---|
105 | /*
|
---|
106 | * Windows doesn't appear to have a way to query the file size limit of a
|
---|
107 | * file system, so we have to deduce the limit from the file system driver name.
|
---|
108 | * This means it will only work for known file systems.
|
---|
109 | */
|
---|
110 | if (cbDesired >= _2G - 1)
|
---|
111 | {
|
---|
112 | uint64_t cbMaxFile = UINT64_MAX;
|
---|
113 | RTFSTYPE enmFsType;
|
---|
114 | int rc = rtNtQueryFsType((HANDLE)RTFileToNative(hFile), &enmFsType);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | switch (enmFsType)
|
---|
117 | {
|
---|
118 | case RTFSTYPE_NTFS:
|
---|
119 | case RTFSTYPE_EXFAT:
|
---|
120 | case RTFSTYPE_UDF:
|
---|
121 | cbMaxFile = UINT64_C(0xffffffffffffffff); /* (May be limited by IFS.) */
|
---|
122 | break;
|
---|
123 |
|
---|
124 | case RTFSTYPE_ISO9660:
|
---|
125 | cbMaxFile = 8 *_1T;
|
---|
126 | break;
|
---|
127 |
|
---|
128 | case RTFSTYPE_FAT:
|
---|
129 | cbMaxFile = _4G;
|
---|
130 | break;
|
---|
131 |
|
---|
132 | case RTFSTYPE_HPFS:
|
---|
133 | cbMaxFile = _2G;
|
---|
134 | break;
|
---|
135 |
|
---|
136 | default:
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | if (cbDesired >= cbMaxFile)
|
---|
140 | return VERR_FILE_TOO_BIG;
|
---|
141 | }
|
---|
142 | return VERR_DISK_FULL;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | RTR3DECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative)
|
---|
147 | {
|
---|
148 | HANDLE h = (HANDLE)uNative;
|
---|
149 | AssertCompile(sizeof(h) == sizeof(uNative));
|
---|
150 | if (h == INVALID_HANDLE_VALUE)
|
---|
151 | {
|
---|
152 | AssertMsgFailed(("%p\n", uNative));
|
---|
153 | *pFile = NIL_RTFILE;
|
---|
154 | return VERR_INVALID_HANDLE;
|
---|
155 | }
|
---|
156 | *pFile = (RTFILE)h;
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | RTR3DECL(RTHCINTPTR) RTFileToNative(RTFILE hFile)
|
---|
162 | {
|
---|
163 | AssertReturn(hFile != NIL_RTFILE, (RTHCINTPTR)INVALID_HANDLE_VALUE);
|
---|
164 | return (RTHCINTPTR)hFile;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | RTR3DECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint64_t fOpen)
|
---|
169 | {
|
---|
170 | return RTFileOpenEx(pszFilename, fOpen, pFile, NULL);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(int) RTFileOpenEx(const char *pszFilename, uint64_t fOpen, PRTFILE phFile, PRTFILEACTION penmActionTaken)
|
---|
175 | {
|
---|
176 | /*
|
---|
177 | * Validate input.
|
---|
178 | */
|
---|
179 | AssertReturn(phFile, VERR_INVALID_PARAMETER);
|
---|
180 | *phFile = NIL_RTFILE;
|
---|
181 | if (penmActionTaken)
|
---|
182 | *penmActionTaken = RTFILEACTION_INVALID;
|
---|
183 | AssertReturn(pszFilename, VERR_INVALID_PARAMETER);
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Merge forced open flags and validate them.
|
---|
187 | */
|
---|
188 | int rc = rtFileRecalcAndValidateFlags(&fOpen);
|
---|
189 | if (RT_FAILURE(rc))
|
---|
190 | return rc;
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Determine disposition, access, share mode, creation flags, and security attributes
|
---|
194 | * for the CreateFile API call.
|
---|
195 | */
|
---|
196 | DWORD dwCreationDisposition;
|
---|
197 | switch (fOpen & RTFILE_O_ACTION_MASK)
|
---|
198 | {
|
---|
199 | case RTFILE_O_OPEN:
|
---|
200 | dwCreationDisposition = fOpen & RTFILE_O_TRUNCATE ? TRUNCATE_EXISTING : OPEN_EXISTING;
|
---|
201 | break;
|
---|
202 | case RTFILE_O_OPEN_CREATE:
|
---|
203 | dwCreationDisposition = OPEN_ALWAYS;
|
---|
204 | break;
|
---|
205 | case RTFILE_O_CREATE:
|
---|
206 | dwCreationDisposition = CREATE_NEW;
|
---|
207 | break;
|
---|
208 | case RTFILE_O_CREATE_REPLACE:
|
---|
209 | dwCreationDisposition = CREATE_ALWAYS;
|
---|
210 | break;
|
---|
211 | default:
|
---|
212 | AssertMsgFailedReturn(("Impossible fOpen=%#llx\n", fOpen), VERR_INVALID_FLAGS);
|
---|
213 | }
|
---|
214 |
|
---|
215 | DWORD dwDesiredAccess;
|
---|
216 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
217 | {
|
---|
218 | case RTFILE_O_READ:
|
---|
219 | dwDesiredAccess = FILE_GENERIC_READ; /* RTFILE_O_APPEND is ignored. */
|
---|
220 | break;
|
---|
221 | case RTFILE_O_WRITE:
|
---|
222 | dwDesiredAccess = fOpen & RTFILE_O_APPEND
|
---|
223 | ? FILE_GENERIC_WRITE & ~FILE_WRITE_DATA
|
---|
224 | : FILE_GENERIC_WRITE;
|
---|
225 | break;
|
---|
226 | case RTFILE_O_READWRITE:
|
---|
227 | dwDesiredAccess = fOpen & RTFILE_O_APPEND
|
---|
228 | ? FILE_GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
|
---|
229 | : FILE_GENERIC_READ | FILE_GENERIC_WRITE;
|
---|
230 | break;
|
---|
231 | case RTFILE_O_ATTR_ONLY:
|
---|
232 | if (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
|
---|
233 | {
|
---|
234 | dwDesiredAccess = 0;
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | RT_FALL_THRU();
|
---|
238 | default:
|
---|
239 | AssertMsgFailedReturn(("Impossible fOpen=%#llx\n", fOpen), VERR_INVALID_FLAGS);
|
---|
240 | }
|
---|
241 | if (dwCreationDisposition == TRUNCATE_EXISTING)
|
---|
242 | /* Required for truncating the file (see MSDN), it is *NOT* part of FILE_GENERIC_WRITE. */
|
---|
243 | dwDesiredAccess |= GENERIC_WRITE;
|
---|
244 |
|
---|
245 | /* RTFileSetMode needs following rights as well. */
|
---|
246 | switch (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
|
---|
247 | {
|
---|
248 | case RTFILE_O_ACCESS_ATTR_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
|
---|
249 | case RTFILE_O_ACCESS_ATTR_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
|
---|
250 | case RTFILE_O_ACCESS_ATTR_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
|
---|
251 | default:
|
---|
252 | /* Attributes access is the same as the file access. */
|
---|
253 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
254 | {
|
---|
255 | case RTFILE_O_READ: dwDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
|
---|
256 | case RTFILE_O_WRITE: dwDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
|
---|
257 | case RTFILE_O_READWRITE: dwDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
|
---|
258 | default:
|
---|
259 | AssertMsgFailedReturn(("Impossible fOpen=%#llx\n", fOpen), VERR_INVALID_FLAGS);
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | DWORD dwShareMode;
|
---|
264 | switch (fOpen & RTFILE_O_DENY_MASK)
|
---|
265 | {
|
---|
266 | case RTFILE_O_DENY_NONE: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
|
---|
267 | case RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_WRITE; break;
|
---|
268 | case RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_READ; break;
|
---|
269 | case RTFILE_O_DENY_READWRITE: dwShareMode = 0; break;
|
---|
270 |
|
---|
271 | case RTFILE_O_DENY_NOT_DELETE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; break;
|
---|
272 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_WRITE; break;
|
---|
273 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE: dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ; break;
|
---|
274 | case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READWRITE:dwShareMode = FILE_SHARE_DELETE; break;
|
---|
275 | default:
|
---|
276 | AssertMsgFailedReturn(("Impossible fOpen=%#llx\n", fOpen), VERR_INVALID_FLAGS);
|
---|
277 | }
|
---|
278 |
|
---|
279 | SECURITY_ATTRIBUTES SecurityAttributes;
|
---|
280 | PSECURITY_ATTRIBUTES pSecurityAttributes = NULL;
|
---|
281 | if (fOpen & RTFILE_O_INHERIT)
|
---|
282 | {
|
---|
283 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
---|
284 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
---|
285 | SecurityAttributes.bInheritHandle = TRUE;
|
---|
286 | pSecurityAttributes = &SecurityAttributes;
|
---|
287 | }
|
---|
288 |
|
---|
289 | DWORD dwFlagsAndAttributes;
|
---|
290 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
|
---|
291 | if (fOpen & RTFILE_O_WRITE_THROUGH)
|
---|
292 | dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
|
---|
293 | if (fOpen & RTFILE_O_ASYNC_IO)
|
---|
294 | dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
|
---|
295 | if (fOpen & RTFILE_O_NO_CACHE)
|
---|
296 | {
|
---|
297 | dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
|
---|
298 | dwDesiredAccess &= ~FILE_APPEND_DATA;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Open/Create the file.
|
---|
303 | */
|
---|
304 | PRTUTF16 pwszFilename;
|
---|
305 | rc = RTPathWinFromUtf8(&pwszFilename, pszFilename, 0 /*fFlags*/);
|
---|
306 | if (RT_SUCCESS(rc))
|
---|
307 | {
|
---|
308 | HANDLE hFile = CreateFileW(pwszFilename,
|
---|
309 | dwDesiredAccess,
|
---|
310 | dwShareMode,
|
---|
311 | pSecurityAttributes,
|
---|
312 | dwCreationDisposition,
|
---|
313 | dwFlagsAndAttributes,
|
---|
314 | NULL);
|
---|
315 | DWORD const dwErr = GetLastError();
|
---|
316 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
317 | {
|
---|
318 | /*
|
---|
319 | * Calculate the action taken value.
|
---|
320 | */
|
---|
321 | RTFILEACTION enmActionTaken;
|
---|
322 | switch (dwCreationDisposition)
|
---|
323 | {
|
---|
324 | case CREATE_NEW:
|
---|
325 | enmActionTaken = RTFILEACTION_CREATED;
|
---|
326 | break;
|
---|
327 | case CREATE_ALWAYS:
|
---|
328 | AssertMsg(dwErr == ERROR_ALREADY_EXISTS || dwErr == NO_ERROR, ("%u\n", dwErr));
|
---|
329 | enmActionTaken = dwErr == ERROR_ALREADY_EXISTS ? RTFILEACTION_REPLACED : RTFILEACTION_CREATED;
|
---|
330 | break;
|
---|
331 | case OPEN_EXISTING:
|
---|
332 | enmActionTaken = RTFILEACTION_OPENED;
|
---|
333 | break;
|
---|
334 | case OPEN_ALWAYS:
|
---|
335 | AssertMsg(dwErr == ERROR_ALREADY_EXISTS || dwErr == NO_ERROR, ("%u\n", dwErr));
|
---|
336 | enmActionTaken = dwErr == ERROR_ALREADY_EXISTS ? RTFILEACTION_OPENED : RTFILEACTION_CREATED;
|
---|
337 | break;
|
---|
338 | case TRUNCATE_EXISTING:
|
---|
339 | enmActionTaken = RTFILEACTION_TRUNCATED;
|
---|
340 | break;
|
---|
341 | default:
|
---|
342 | AssertMsgFailed(("%d %#x\n", dwCreationDisposition, dwCreationDisposition));
|
---|
343 | enmActionTaken = RTFILEACTION_INVALID;
|
---|
344 | break;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * Turn off indexing of directory through Windows Indexing Service if
|
---|
349 | * we created a new file or replaced an existing one.
|
---|
350 | */
|
---|
351 | if ( (fOpen & RTFILE_O_NOT_CONTENT_INDEXED)
|
---|
352 | && ( enmActionTaken == RTFILEACTION_CREATED
|
---|
353 | || enmActionTaken == RTFILEACTION_REPLACED) )
|
---|
354 | {
|
---|
355 | /** @todo there must be a way to do this via the handle! */
|
---|
356 | if (!SetFileAttributesW(pwszFilename, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED))
|
---|
357 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
358 | }
|
---|
359 | /*
|
---|
360 | * If RTFILEACTION_OPENED, we may need to truncate the file.
|
---|
361 | */
|
---|
362 | else if ( (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_ACTION_MASK)) == (RTFILE_O_TRUNCATE | RTFILE_O_OPEN_CREATE)
|
---|
363 | && enmActionTaken == RTFILEACTION_OPENED)
|
---|
364 | {
|
---|
365 | if (SetEndOfFile(hFile))
|
---|
366 | enmActionTaken = RTFILEACTION_TRUNCATED;
|
---|
367 | else
|
---|
368 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
369 | }
|
---|
370 | if (penmActionTaken)
|
---|
371 | *penmActionTaken = enmActionTaken;
|
---|
372 | if (RT_SUCCESS(rc))
|
---|
373 | {
|
---|
374 | *phFile = (RTFILE)hFile;
|
---|
375 | Assert((HANDLE)*phFile == hFile);
|
---|
376 | RTPathWinFree(pwszFilename);
|
---|
377 | return VINF_SUCCESS;
|
---|
378 | }
|
---|
379 |
|
---|
380 | CloseHandle(hFile);
|
---|
381 | }
|
---|
382 | else
|
---|
383 | {
|
---|
384 | if ( penmActionTaken
|
---|
385 | && dwCreationDisposition == CREATE_NEW
|
---|
386 | && dwErr == ERROR_FILE_EXISTS)
|
---|
387 | *penmActionTaken = RTFILEACTION_ALREADY_EXISTS;
|
---|
388 | rc = RTErrConvertFromWin32(dwErr);
|
---|
389 | }
|
---|
390 | RTPathWinFree(pwszFilename);
|
---|
391 | }
|
---|
392 | return rc;
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | RTR3DECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint64_t fAccess)
|
---|
397 | {
|
---|
398 | AssertReturn( fAccess == RTFILE_O_READ
|
---|
399 | || fAccess == RTFILE_O_WRITE
|
---|
400 | || fAccess == RTFILE_O_READWRITE,
|
---|
401 | VERR_INVALID_PARAMETER);
|
---|
402 | return RTFileOpen(phFile, "NUL", fAccess | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | RTR3DECL(int) RTFileClose(RTFILE hFile)
|
---|
407 | {
|
---|
408 | if (hFile == NIL_RTFILE)
|
---|
409 | return VINF_SUCCESS;
|
---|
410 | if (CloseHandle((HANDLE)RTFileToNative(hFile)))
|
---|
411 | return VINF_SUCCESS;
|
---|
412 | return RTErrConvertFromWin32(GetLastError());
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | RTFILE rtFileGetStandard(RTHANDLESTD enmStdHandle)
|
---|
417 | {
|
---|
418 | DWORD dwStdHandle;
|
---|
419 | switch (enmStdHandle)
|
---|
420 | {
|
---|
421 | case RTHANDLESTD_INPUT: dwStdHandle = STD_INPUT_HANDLE; break;
|
---|
422 | case RTHANDLESTD_OUTPUT: dwStdHandle = STD_OUTPUT_HANDLE; break;
|
---|
423 | case RTHANDLESTD_ERROR: dwStdHandle = STD_ERROR_HANDLE; break;
|
---|
424 | default:
|
---|
425 | AssertFailedReturn(NIL_RTFILE);
|
---|
426 | }
|
---|
427 |
|
---|
428 | HANDLE hNative = GetStdHandle(dwStdHandle);
|
---|
429 | if (hNative == INVALID_HANDLE_VALUE)
|
---|
430 | return NIL_RTFILE;
|
---|
431 |
|
---|
432 | RTFILE hFile = (RTFILE)(uintptr_t)hNative;
|
---|
433 | AssertReturn((HANDLE)(uintptr_t)hFile == hNative, NIL_RTFILE);
|
---|
434 | return hFile;
|
---|
435 | }
|
---|
436 |
|
---|
437 |
|
---|
438 | RTR3DECL(int) RTFileSeek(RTFILE hFile, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
|
---|
439 | {
|
---|
440 | static ULONG aulSeekRecode[] =
|
---|
441 | {
|
---|
442 | FILE_BEGIN,
|
---|
443 | FILE_CURRENT,
|
---|
444 | FILE_END,
|
---|
445 | };
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Validate input.
|
---|
449 | */
|
---|
450 | if (uMethod > RTFILE_SEEK_END)
|
---|
451 | {
|
---|
452 | AssertMsgFailed(("Invalid uMethod=%d\n", uMethod));
|
---|
453 | return VERR_INVALID_PARAMETER;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Execute the seek.
|
---|
458 | */
|
---|
459 | if (MySetFilePointer(hFile, offSeek, poffActual, aulSeekRecode[uMethod]))
|
---|
460 | return VINF_SUCCESS;
|
---|
461 | return RTErrConvertFromWin32(GetLastError());
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | RTR3DECL(int) RTFileRead(RTFILE hFile, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
466 | {
|
---|
467 | if (cbToRead <= 0)
|
---|
468 | {
|
---|
469 | if (pcbRead)
|
---|
470 | *pcbRead = 0;
|
---|
471 | return VINF_SUCCESS;
|
---|
472 | }
|
---|
473 | ULONG cbToReadAdj = (ULONG)cbToRead;
|
---|
474 | AssertReturn(cbToReadAdj == cbToRead, VERR_NUMBER_TOO_BIG);
|
---|
475 |
|
---|
476 | ULONG cbRead = 0;
|
---|
477 | if (ReadFile((HANDLE)RTFileToNative(hFile), pvBuf, cbToReadAdj, &cbRead, NULL))
|
---|
478 | {
|
---|
479 | if (pcbRead)
|
---|
480 | /* Caller can handle partial reads. */
|
---|
481 | *pcbRead = cbRead;
|
---|
482 | else
|
---|
483 | {
|
---|
484 | /* Caller expects everything to be read. */
|
---|
485 | while (cbToReadAdj > cbRead)
|
---|
486 | {
|
---|
487 | ULONG cbReadPart = 0;
|
---|
488 | if (!ReadFile((HANDLE)RTFileToNative(hFile), (char*)pvBuf + cbRead, cbToReadAdj - cbRead, &cbReadPart, NULL))
|
---|
489 | return RTErrConvertFromWin32(GetLastError());
|
---|
490 | if (cbReadPart == 0)
|
---|
491 | return VERR_EOF;
|
---|
492 | cbRead += cbReadPart;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | return VINF_SUCCESS;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /*
|
---|
499 | * If it's a console, we might bump into out of memory conditions in the
|
---|
500 | * ReadConsole call.
|
---|
501 | */
|
---|
502 | DWORD dwErr = GetLastError();
|
---|
503 | if (dwErr == ERROR_NOT_ENOUGH_MEMORY)
|
---|
504 | {
|
---|
505 | ULONG cbChunk = cbToReadAdj / 2;
|
---|
506 | if (cbChunk > 16*_1K)
|
---|
507 | cbChunk = 16*_1K;
|
---|
508 | else
|
---|
509 | cbChunk = RT_ALIGN_32(cbChunk, 256);
|
---|
510 |
|
---|
511 | cbRead = 0;
|
---|
512 | while (cbToReadAdj > cbRead)
|
---|
513 | {
|
---|
514 | ULONG cbToReadNow = RT_MIN(cbChunk, cbToReadAdj - cbRead);
|
---|
515 | ULONG cbReadPart = 0;
|
---|
516 | if (!ReadFile((HANDLE)RTFileToNative(hFile), (char *)pvBuf + cbRead, cbToReadNow, &cbReadPart, NULL))
|
---|
517 | {
|
---|
518 | /* If we failed because the buffer is too big, shrink it and
|
---|
519 | try again. */
|
---|
520 | dwErr = GetLastError();
|
---|
521 | if ( dwErr == ERROR_NOT_ENOUGH_MEMORY
|
---|
522 | && cbChunk > 8)
|
---|
523 | {
|
---|
524 | cbChunk /= 2;
|
---|
525 | continue;
|
---|
526 | }
|
---|
527 | return RTErrConvertFromWin32(dwErr);
|
---|
528 | }
|
---|
529 | cbRead += cbReadPart;
|
---|
530 |
|
---|
531 | /* Return if the caller can handle partial reads, otherwise try
|
---|
532 | fill the buffer all the way up. */
|
---|
533 | if (pcbRead)
|
---|
534 | {
|
---|
535 | *pcbRead = cbRead;
|
---|
536 | break;
|
---|
537 | }
|
---|
538 | if (cbReadPart == 0)
|
---|
539 | return VERR_EOF;
|
---|
540 | }
|
---|
541 | return VINF_SUCCESS;
|
---|
542 | }
|
---|
543 |
|
---|
544 | return RTErrConvertFromWin32(dwErr);
|
---|
545 | }
|
---|
546 |
|
---|
547 |
|
---|
548 | RTDECL(int) RTFileReadAt(RTFILE hFile, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
549 | {
|
---|
550 | ULONG cbToReadAdj = (ULONG)cbToRead;
|
---|
551 | AssertReturn(cbToReadAdj == cbToRead, VERR_NUMBER_TOO_BIG);
|
---|
552 |
|
---|
553 | OVERLAPPED Overlapped;
|
---|
554 | Overlapped.Offset = (uint32_t)off;
|
---|
555 | Overlapped.OffsetHigh = (uint32_t)(off >> 32);
|
---|
556 | Overlapped.hEvent = NULL;
|
---|
557 | Overlapped.Internal = 0;
|
---|
558 | Overlapped.InternalHigh = 0;
|
---|
559 |
|
---|
560 | ULONG cbRead = 0;
|
---|
561 | if (ReadFile((HANDLE)RTFileToNative(hFile), pvBuf, cbToReadAdj, &cbRead, &Overlapped))
|
---|
562 | {
|
---|
563 | if (pcbRead)
|
---|
564 | /* Caller can handle partial reads. */
|
---|
565 | *pcbRead = cbRead;
|
---|
566 | else
|
---|
567 | {
|
---|
568 | /* Caller expects everything to be read. */
|
---|
569 | while (cbToReadAdj > cbRead)
|
---|
570 | {
|
---|
571 | Overlapped.Offset = (uint32_t)(off + cbRead);
|
---|
572 | Overlapped.OffsetHigh = (uint32_t)((off + cbRead) >> 32);
|
---|
573 | Overlapped.hEvent = NULL;
|
---|
574 | Overlapped.Internal = 0;
|
---|
575 | Overlapped.InternalHigh = 0;
|
---|
576 |
|
---|
577 | ULONG cbReadPart = 0;
|
---|
578 | if (!ReadFile((HANDLE)RTFileToNative(hFile), (char *)pvBuf + cbRead, cbToReadAdj - cbRead,
|
---|
579 | &cbReadPart, &Overlapped))
|
---|
580 | return RTErrConvertFromWin32(GetLastError());
|
---|
581 | if (cbReadPart == 0)
|
---|
582 | return VERR_EOF;
|
---|
583 | cbRead += cbReadPart;
|
---|
584 | }
|
---|
585 | }
|
---|
586 | return VINF_SUCCESS;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* We will get an EOF error when using overlapped I/O. So, make sure we don't
|
---|
590 | return it when pcbhRead is not NULL. */
|
---|
591 | DWORD dwErr = GetLastError();
|
---|
592 | if (pcbRead && dwErr == ERROR_HANDLE_EOF)
|
---|
593 | {
|
---|
594 | *pcbRead = 0;
|
---|
595 | return VINF_SUCCESS;
|
---|
596 | }
|
---|
597 | return RTErrConvertFromWin32(dwErr);
|
---|
598 | }
|
---|
599 |
|
---|
600 |
|
---|
601 | RTR3DECL(int) RTFileWrite(RTFILE hFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
602 | {
|
---|
603 | if (cbToWrite <= 0)
|
---|
604 | return VINF_SUCCESS;
|
---|
605 | ULONG const cbToWriteAdj = (ULONG)cbToWrite;
|
---|
606 | AssertReturn(cbToWriteAdj == cbToWrite, VERR_NUMBER_TOO_BIG);
|
---|
607 |
|
---|
608 | ULONG cbWritten = 0;
|
---|
609 | if (WriteFile((HANDLE)RTFileToNative(hFile), pvBuf, cbToWriteAdj, &cbWritten, NULL))
|
---|
610 | {
|
---|
611 | if (pcbWritten)
|
---|
612 | /* Caller can handle partial writes. */
|
---|
613 | *pcbWritten = RT_MIN(cbWritten, cbToWriteAdj); /* paranoia^3 */
|
---|
614 | else
|
---|
615 | {
|
---|
616 | /* Caller expects everything to be written. */
|
---|
617 | while (cbWritten < cbToWriteAdj)
|
---|
618 | {
|
---|
619 | ULONG cbWrittenPart = 0;
|
---|
620 | if (!WriteFile((HANDLE)RTFileToNative(hFile), (char*)pvBuf + cbWritten,
|
---|
621 | cbToWriteAdj - cbWritten, &cbWrittenPart, NULL))
|
---|
622 | {
|
---|
623 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
624 | if (rc == VERR_DISK_FULL)
|
---|
625 | rc = rtFileWinCheckIfDiskReallyFull(hFile, RTFileTell(hFile) + cbToWriteAdj - cbWritten);
|
---|
626 | return rc;
|
---|
627 | }
|
---|
628 | if (cbWrittenPart == 0)
|
---|
629 | return VERR_WRITE_ERROR;
|
---|
630 | cbWritten += cbWrittenPart;
|
---|
631 | }
|
---|
632 | }
|
---|
633 | return VINF_SUCCESS;
|
---|
634 | }
|
---|
635 |
|
---|
636 | /*
|
---|
637 | * If it's a console, we might bump into out of memory conditions in the
|
---|
638 | * WriteConsole call.
|
---|
639 | */
|
---|
640 | DWORD dwErr = GetLastError();
|
---|
641 | if (dwErr == ERROR_NOT_ENOUGH_MEMORY)
|
---|
642 | {
|
---|
643 | ULONG cbChunk = cbToWriteAdj / 2;
|
---|
644 | if (cbChunk > _32K)
|
---|
645 | cbChunk = _32K;
|
---|
646 | else
|
---|
647 | cbChunk = RT_ALIGN_32(cbChunk, 256);
|
---|
648 |
|
---|
649 | cbWritten = 0;
|
---|
650 | while (cbWritten < cbToWriteAdj)
|
---|
651 | {
|
---|
652 | ULONG cbToWriteNow = RT_MIN(cbChunk, cbToWriteAdj - cbWritten);
|
---|
653 | ULONG cbWrittenPart = 0;
|
---|
654 | if (!WriteFile((HANDLE)RTFileToNative(hFile), (const char *)pvBuf + cbWritten, cbToWriteNow, &cbWrittenPart, NULL))
|
---|
655 | {
|
---|
656 | /* If we failed because the buffer is too big, shrink it and
|
---|
657 | try again. */
|
---|
658 | dwErr = GetLastError();
|
---|
659 | if ( dwErr == ERROR_NOT_ENOUGH_MEMORY
|
---|
660 | && cbChunk > 8)
|
---|
661 | {
|
---|
662 | cbChunk /= 2;
|
---|
663 | continue;
|
---|
664 | }
|
---|
665 | int rc = RTErrConvertFromWin32(dwErr);
|
---|
666 | if (rc == VERR_DISK_FULL)
|
---|
667 | rc = rtFileWinCheckIfDiskReallyFull(hFile, RTFileTell(hFile) + cbToWriteNow);
|
---|
668 | return rc;
|
---|
669 | }
|
---|
670 | cbWritten += cbWrittenPart;
|
---|
671 |
|
---|
672 | /* Return if the caller can handle partial writes, otherwise try
|
---|
673 | write out everything. */
|
---|
674 | if (pcbWritten)
|
---|
675 | {
|
---|
676 | *pcbWritten = RT_MIN(cbWritten, cbToWriteAdj); /* paranoia^3 */
|
---|
677 | break;
|
---|
678 | }
|
---|
679 | if (cbWrittenPart == 0)
|
---|
680 | return VERR_WRITE_ERROR;
|
---|
681 | }
|
---|
682 | return VINF_SUCCESS;
|
---|
683 | }
|
---|
684 |
|
---|
685 | int rc = RTErrConvertFromWin32(dwErr);
|
---|
686 | if (rc == VERR_DISK_FULL)
|
---|
687 | rc = rtFileWinCheckIfDiskReallyFull(hFile, RTFileTell(hFile) + cbToWriteAdj);
|
---|
688 | return rc;
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | RTDECL(int) RTFileWriteAt(RTFILE hFile, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
693 | {
|
---|
694 | ULONG const cbToWriteAdj = (ULONG)cbToWrite;
|
---|
695 | AssertReturn(cbToWriteAdj == cbToWrite, VERR_NUMBER_TOO_BIG);
|
---|
696 |
|
---|
697 | OVERLAPPED Overlapped;
|
---|
698 | Overlapped.Offset = (uint32_t)off;
|
---|
699 | Overlapped.OffsetHigh = (uint32_t)(off >> 32);
|
---|
700 | Overlapped.hEvent = NULL;
|
---|
701 | Overlapped.Internal = 0;
|
---|
702 | Overlapped.InternalHigh = 0;
|
---|
703 |
|
---|
704 | ULONG cbWritten = 0;
|
---|
705 | if (WriteFile((HANDLE)RTFileToNative(hFile), pvBuf, cbToWriteAdj, &cbWritten, &Overlapped))
|
---|
706 | {
|
---|
707 | if (pcbWritten)
|
---|
708 | /* Caller can handle partial writes. */
|
---|
709 | *pcbWritten = RT_MIN(cbWritten, cbToWriteAdj); /* paranoia^3 */
|
---|
710 | else
|
---|
711 | {
|
---|
712 | /* Caller expects everything to be written. */
|
---|
713 | while (cbWritten < cbToWriteAdj)
|
---|
714 | {
|
---|
715 | Overlapped.Offset = (uint32_t)(off + cbWritten);
|
---|
716 | Overlapped.OffsetHigh = (uint32_t)((off + cbWritten) >> 32);
|
---|
717 | Overlapped.hEvent = NULL;
|
---|
718 | Overlapped.Internal = 0;
|
---|
719 | Overlapped.InternalHigh = 0;
|
---|
720 |
|
---|
721 | ULONG cbWrittenPart = 0;
|
---|
722 | if (!WriteFile((HANDLE)RTFileToNative(hFile), (char*)pvBuf + cbWritten,
|
---|
723 | cbToWriteAdj - cbWritten, &cbWrittenPart, &Overlapped))
|
---|
724 | {
|
---|
725 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
726 | if (rc == VERR_DISK_FULL)
|
---|
727 | rc = rtFileWinCheckIfDiskReallyFull(hFile, off + cbToWriteAdj);
|
---|
728 | return rc;
|
---|
729 | }
|
---|
730 | if (cbWrittenPart == 0)
|
---|
731 | return VERR_WRITE_ERROR;
|
---|
732 | cbWritten += cbWrittenPart;
|
---|
733 | }
|
---|
734 | }
|
---|
735 | return VINF_SUCCESS;
|
---|
736 | }
|
---|
737 |
|
---|
738 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
739 | if (rc == VERR_DISK_FULL)
|
---|
740 | rc = rtFileWinCheckIfDiskReallyFull(hFile, off + cbToWriteAdj);
|
---|
741 | return rc;
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | RTR3DECL(int) RTFileFlush(RTFILE hFile)
|
---|
746 | {
|
---|
747 | if (!FlushFileBuffers((HANDLE)RTFileToNative(hFile)))
|
---|
748 | {
|
---|
749 | int rc = GetLastError();
|
---|
750 | Log(("FlushFileBuffers failed with %d\n", rc));
|
---|
751 | return RTErrConvertFromWin32(rc);
|
---|
752 | }
|
---|
753 | return VINF_SUCCESS;
|
---|
754 | }
|
---|
755 |
|
---|
756 | #if 1
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * Checks the the two handles refers to the same file.
|
---|
760 | *
|
---|
761 | * @returns true if the same file, false if different ones or invalid handles.
|
---|
762 | * @param hFile1 Handle \#1.
|
---|
763 | * @param hFile2 Handle \#2.
|
---|
764 | */
|
---|
765 | static bool rtFileIsSame(HANDLE hFile1, HANDLE hFile2)
|
---|
766 | {
|
---|
767 | /*
|
---|
768 | * We retry in case CreationTime or the Object ID is being modified and there
|
---|
769 | * aren't any IndexNumber (file ID) on this kind of file system.
|
---|
770 | */
|
---|
771 | for (uint32_t iTries = 0; iTries < 3; iTries++)
|
---|
772 | {
|
---|
773 | /*
|
---|
774 | * Fetch data to compare (being a little lazy here).
|
---|
775 | */
|
---|
776 | struct
|
---|
777 | {
|
---|
778 | HANDLE hFile;
|
---|
779 | NTSTATUS rcObjId;
|
---|
780 | FILE_OBJECTID_INFORMATION ObjId;
|
---|
781 | FILE_ALL_INFORMATION All;
|
---|
782 | FILE_FS_VOLUME_INFORMATION Vol;
|
---|
783 | } auData[2];
|
---|
784 | auData[0].hFile = hFile1;
|
---|
785 | auData[1].hFile = hFile2;
|
---|
786 |
|
---|
787 | for (uintptr_t i = 0; i < RT_ELEMENTS(auData); i++)
|
---|
788 | {
|
---|
789 | RT_ZERO(auData[i].ObjId);
|
---|
790 | IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
791 | auData[i].rcObjId = NtQueryInformationFile(auData[i].hFile, &Ios, &auData[i].ObjId, sizeof(auData[i].ObjId),
|
---|
792 | FileObjectIdInformation);
|
---|
793 |
|
---|
794 | RT_ZERO(auData[i].All);
|
---|
795 | RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
|
---|
796 | NTSTATUS rcNt = NtQueryInformationFile(auData[i].hFile, &Ios, &auData[i].All, sizeof(auData[i].All),
|
---|
797 | FileAllInformation);
|
---|
798 | AssertReturn(rcNt == STATUS_BUFFER_OVERFLOW /* insufficient space for name info */ || NT_SUCCESS(rcNt), false);
|
---|
799 |
|
---|
800 | union
|
---|
801 | {
|
---|
802 | FILE_FS_VOLUME_INFORMATION Info;
|
---|
803 | uint8_t abBuf[sizeof(FILE_FS_VOLUME_INFORMATION) + 4096];
|
---|
804 | } uVol;
|
---|
805 | RT_ZERO(uVol.Info);
|
---|
806 | RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
|
---|
807 | rcNt = NtQueryVolumeInformationFile(auData[i].hFile, &Ios, &uVol, sizeof(uVol), FileFsVolumeInformation);
|
---|
808 | if (NT_SUCCESS(rcNt))
|
---|
809 | auData[i].Vol = uVol.Info;
|
---|
810 | else
|
---|
811 | RT_ZERO(auData[i].Vol);
|
---|
812 | }
|
---|
813 |
|
---|
814 | /*
|
---|
815 | * Compare it.
|
---|
816 | */
|
---|
817 | if ( auData[0].All.StandardInformation.Directory
|
---|
818 | == auData[1].All.StandardInformation.Directory)
|
---|
819 | { /* likely */ }
|
---|
820 | else
|
---|
821 | break;
|
---|
822 |
|
---|
823 | if ( (auData[0].All.BasicInformation.FileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_REPARSE_POINT))
|
---|
824 | == (auData[1].All.BasicInformation.FileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_REPARSE_POINT)))
|
---|
825 | { /* likely */ }
|
---|
826 | else
|
---|
827 | break;
|
---|
828 |
|
---|
829 | if ( auData[0].Vol.VolumeSerialNumber
|
---|
830 | == auData[1].Vol.VolumeSerialNumber)
|
---|
831 | { /* likely */ }
|
---|
832 | else
|
---|
833 | break;
|
---|
834 |
|
---|
835 | if ( auData[0].All.InternalInformation.IndexNumber.QuadPart
|
---|
836 | == auData[1].All.InternalInformation.IndexNumber.QuadPart)
|
---|
837 | { /* likely */ }
|
---|
838 | else
|
---|
839 | break;
|
---|
840 |
|
---|
841 | if ( !NT_SUCCESS(auData[0].rcObjId)
|
---|
842 | || memcmp(&auData[0].ObjId, &auData[1].ObjId, RT_UOFFSETOF(FILE_OBJECTID_INFORMATION, ExtendedInfo)) == 0)
|
---|
843 | {
|
---|
844 | if ( auData[0].All.BasicInformation.CreationTime.QuadPart
|
---|
845 | == auData[1].All.BasicInformation.CreationTime.QuadPart)
|
---|
846 | return true;
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | return false;
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * If @a hFile is opened in append mode, try return a handle with
|
---|
856 | * FILE_WRITE_DATA permissions.
|
---|
857 | *
|
---|
858 | * @returns Duplicate handle.
|
---|
859 | * @param hFile The NT handle to check & duplicate.
|
---|
860 | *
|
---|
861 | * @todo It would be much easier to implement this by not dropping the
|
---|
862 | * FILE_WRITE_DATA access and instead have the RTFileWrite APIs
|
---|
863 | * enforce the appending. That will require keeping additional
|
---|
864 | * information along side the handle (instance structure). However, on
|
---|
865 | * windows you can grant append permissions w/o giving people access to
|
---|
866 | * overwrite existing data, so the RTFileOpenEx code would have to deal
|
---|
867 | * with those kinds of STATUS_ACCESS_DENIED too then.
|
---|
868 | */
|
---|
869 | static HANDLE rtFileReOpenAppendOnlyWithFullWriteAccess(HANDLE hFile)
|
---|
870 | {
|
---|
871 | OBJECT_BASIC_INFORMATION BasicInfo = {0};
|
---|
872 | ULONG cbActual = 0;
|
---|
873 | NTSTATUS rcNt = NtQueryObject(hFile, ObjectBasicInformation, &BasicInfo, sizeof(BasicInfo), &cbActual);
|
---|
874 | if (NT_SUCCESS(rcNt))
|
---|
875 | {
|
---|
876 | if ((BasicInfo.GrantedAccess & (FILE_APPEND_DATA | FILE_WRITE_DATA)) == FILE_APPEND_DATA)
|
---|
877 | {
|
---|
878 | /*
|
---|
879 | * We cannot use NtDuplicateObject here as it is not possible to
|
---|
880 | * upgrade the access on files, only making it more strict. So,
|
---|
881 | * query the path and re-open it (we could do by file/object/whatever
|
---|
882 | * id too, but that may not work with all file systems).
|
---|
883 | */
|
---|
884 | for (uint32_t i = 0; i < 16; i++)
|
---|
885 | {
|
---|
886 | UNICODE_STRING NtName;
|
---|
887 | int rc = RTNtPathFromHandle(&NtName, hFile, 0);
|
---|
888 | AssertRCReturn(rc, INVALID_HANDLE_VALUE);
|
---|
889 |
|
---|
890 | HANDLE hDupFile = RTNT_INVALID_HANDLE_VALUE;
|
---|
891 | IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
892 | OBJECT_ATTRIBUTES ObjAttr;
|
---|
893 | InitializeObjectAttributes(&ObjAttr, &NtName, BasicInfo.Attributes & ~OBJ_INHERIT, NULL, NULL);
|
---|
894 |
|
---|
895 | rcNt = NtCreateFile(&hDupFile,
|
---|
896 | BasicInfo.GrantedAccess | FILE_WRITE_DATA,
|
---|
897 | &ObjAttr,
|
---|
898 | &Ios,
|
---|
899 | NULL /* AllocationSize*/,
|
---|
900 | FILE_ATTRIBUTE_NORMAL,
|
---|
901 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
902 | FILE_OPEN,
|
---|
903 | FILE_OPEN_FOR_BACKUP_INTENT /*??*/,
|
---|
904 | NULL /*EaBuffer*/,
|
---|
905 | 0 /*EaLength*/);
|
---|
906 | RTUtf16Free(NtName.Buffer);
|
---|
907 | if (NT_SUCCESS(rcNt))
|
---|
908 | {
|
---|
909 | /*
|
---|
910 | * Check that we've opened the same file.
|
---|
911 | */
|
---|
912 | if (rtFileIsSame(hFile, hDupFile))
|
---|
913 | return hDupFile;
|
---|
914 | NtClose(hDupFile);
|
---|
915 | }
|
---|
916 | }
|
---|
917 | AssertFailed();
|
---|
918 | }
|
---|
919 | }
|
---|
920 | return INVALID_HANDLE_VALUE;
|
---|
921 | }
|
---|
922 |
|
---|
923 | #endif
|
---|
924 |
|
---|
925 | RTR3DECL(int) RTFileSetSize(RTFILE hFile, uint64_t cbSize)
|
---|
926 | {
|
---|
927 | #if 1
|
---|
928 | HANDLE hNtFile = (HANDLE)RTFileToNative(hFile);
|
---|
929 | HANDLE hDupFile = INVALID_HANDLE_VALUE;
|
---|
930 | union
|
---|
931 | {
|
---|
932 | FILE_END_OF_FILE_INFORMATION Eof;
|
---|
933 | FILE_ALLOCATION_INFORMATION Alloc;
|
---|
934 | } uInfo;
|
---|
935 |
|
---|
936 | /*
|
---|
937 | * Change the EOF marker.
|
---|
938 | *
|
---|
939 | * HACK ALERT! If the file was opened in RTFILE_O_APPEND mode, we will have
|
---|
940 | * to re-open it with FILE_WRITE_DATA access to get the job done.
|
---|
941 | * This how ftruncate on a unixy system would work but not how
|
---|
942 | * it is done on Windows where appending is a separate permission
|
---|
943 | * rather than just a write modifier, making this hack totally wrong.
|
---|
944 | */
|
---|
945 | /** @todo The right way to fix this is either to add a RTFileSetSizeEx function
|
---|
946 | * for specifically requesting the unixy behaviour, or add an additional
|
---|
947 | * flag to RTFileOpen[Ex] to request the unixy append behaviour there.
|
---|
948 | * The latter would require saving the open flags in a instance data
|
---|
949 | * structure, which is a bit of a risky move, though something we should
|
---|
950 | * do in 6.2 (or later).
|
---|
951 | *
|
---|
952 | * Note! Because handle interitance, it is not realyan option to
|
---|
953 | * always use FILE_WRITE_DATA and implement the RTFILE_O_APPEND
|
---|
954 | * bits in RTFileWrite and friends. Besides, it's not like
|
---|
955 | * RTFILE_O_APPEND is so clearly defined anyway - see
|
---|
956 | * RTFileWriteAt.
|
---|
957 | */
|
---|
958 | IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
959 | uInfo.Eof.EndOfFile.QuadPart = cbSize;
|
---|
960 | NTSTATUS rcNt = NtSetInformationFile(hNtFile, &Ios, &uInfo.Eof, sizeof(uInfo.Eof), FileEndOfFileInformation);
|
---|
961 | if (rcNt == STATUS_ACCESS_DENIED)
|
---|
962 | {
|
---|
963 | hDupFile = rtFileReOpenAppendOnlyWithFullWriteAccess(hNtFile);
|
---|
964 | if (hDupFile != INVALID_HANDLE_VALUE)
|
---|
965 | {
|
---|
966 | hNtFile = hDupFile;
|
---|
967 | uInfo.Eof.EndOfFile.QuadPart = cbSize;
|
---|
968 | rcNt = NtSetInformationFile(hNtFile, &Ios, &uInfo.Eof, sizeof(uInfo.Eof), FileEndOfFileInformation);
|
---|
969 | }
|
---|
970 | }
|
---|
971 |
|
---|
972 | if (NT_SUCCESS(rcNt))
|
---|
973 | {
|
---|
974 | /*
|
---|
975 | * Change the allocation.
|
---|
976 | */
|
---|
977 | uInfo.Alloc.AllocationSize.QuadPart = cbSize;
|
---|
978 | rcNt = NtSetInformationFile(hNtFile, &Ios, &uInfo.Eof, sizeof(uInfo.Alloc), FileAllocationInformation);
|
---|
979 | }
|
---|
980 |
|
---|
981 | /*
|
---|
982 | * Close the temporary file handle:
|
---|
983 | */
|
---|
984 | if (hDupFile != INVALID_HANDLE_VALUE)
|
---|
985 | NtClose(hDupFile);
|
---|
986 |
|
---|
987 | if (RT_SUCCESS(rcNt))
|
---|
988 | return VINF_SUCCESS;
|
---|
989 | return RTErrConvertFromNtStatus(rcNt);
|
---|
990 |
|
---|
991 | #else /* this version of the code will fail to truncate files when RTFILE_O_APPEND is in effect, which isn't what we want... */
|
---|
992 | /*
|
---|
993 | * Get current file pointer.
|
---|
994 | */
|
---|
995 | int rc;
|
---|
996 | uint64_t offCurrent;
|
---|
997 | if (MySetFilePointer(hFile, 0, &offCurrent, FILE_CURRENT))
|
---|
998 | {
|
---|
999 | /*
|
---|
1000 | * Set new file pointer.
|
---|
1001 | */
|
---|
1002 | if (MySetFilePointer(hFile, cbSize, NULL, FILE_BEGIN))
|
---|
1003 | {
|
---|
1004 | /* set file pointer */
|
---|
1005 | if (SetEndOfFile((HANDLE)RTFileToNative(hFile)))
|
---|
1006 | {
|
---|
1007 | /*
|
---|
1008 | * Restore file pointer and return.
|
---|
1009 | * If the old pointer was beyond the new file end, ignore failure.
|
---|
1010 | */
|
---|
1011 | if ( MySetFilePointer(hFile, offCurrent, NULL, FILE_BEGIN)
|
---|
1012 | || offCurrent > cbSize)
|
---|
1013 | return VINF_SUCCESS;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /*
|
---|
1017 | * Failed, try restoring the file pointer.
|
---|
1018 | */
|
---|
1019 | rc = GetLastError();
|
---|
1020 | MySetFilePointer(hFile, offCurrent, NULL, FILE_BEGIN);
|
---|
1021 |
|
---|
1022 | if (rc == ERROR_DISK_FULL)
|
---|
1023 | return rtFileWinCheckIfDiskReallyFull(hFile, cbSize);
|
---|
1024 | }
|
---|
1025 | else
|
---|
1026 | rc = GetLastError();
|
---|
1027 | }
|
---|
1028 | else
|
---|
1029 | rc = GetLastError();
|
---|
1030 |
|
---|
1031 | return RTErrConvertFromWin32(rc);
|
---|
1032 | #endif
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 |
|
---|
1036 | RTR3DECL(int) RTFileQuerySize(RTFILE hFile, uint64_t *pcbSize)
|
---|
1037 | {
|
---|
1038 | /*
|
---|
1039 | * GetFileSize works for most handles.
|
---|
1040 | */
|
---|
1041 | ULARGE_INTEGER Size;
|
---|
1042 | Size.LowPart = GetFileSize((HANDLE)RTFileToNative(hFile), &Size.HighPart);
|
---|
1043 | if (Size.LowPart != INVALID_FILE_SIZE)
|
---|
1044 | {
|
---|
1045 | *pcbSize = Size.QuadPart;
|
---|
1046 | return VINF_SUCCESS;
|
---|
1047 | }
|
---|
1048 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
1049 |
|
---|
1050 | /*
|
---|
1051 | * Could it be a volume or a disk?
|
---|
1052 | */
|
---|
1053 | DISK_GEOMETRY DriveGeo;
|
---|
1054 | DWORD cbDriveGeo;
|
---|
1055 | if (DeviceIoControl((HANDLE)RTFileToNative(hFile),
|
---|
1056 | IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
|
---|
1057 | &DriveGeo, sizeof(DriveGeo), &cbDriveGeo, NULL))
|
---|
1058 | {
|
---|
1059 | if ( DriveGeo.MediaType == FixedMedia
|
---|
1060 | || DriveGeo.MediaType == RemovableMedia)
|
---|
1061 | {
|
---|
1062 | *pcbSize = DriveGeo.Cylinders.QuadPart
|
---|
1063 | * DriveGeo.TracksPerCylinder
|
---|
1064 | * DriveGeo.SectorsPerTrack
|
---|
1065 | * DriveGeo.BytesPerSector;
|
---|
1066 |
|
---|
1067 | GET_LENGTH_INFORMATION DiskLenInfo;
|
---|
1068 | DWORD Ignored;
|
---|
1069 | if (DeviceIoControl((HANDLE)RTFileToNative(hFile),
|
---|
1070 | IOCTL_DISK_GET_LENGTH_INFO, NULL, 0,
|
---|
1071 | &DiskLenInfo, sizeof(DiskLenInfo), &Ignored, (LPOVERLAPPED)NULL))
|
---|
1072 | {
|
---|
1073 | /* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
|
---|
1074 | *pcbSize = DiskLenInfo.Length.QuadPart;
|
---|
1075 | }
|
---|
1076 | return VINF_SUCCESS;
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /*
|
---|
1081 | * Return the GetFileSize result if not a volume/disk.
|
---|
1082 | */
|
---|
1083 | return rc;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 |
|
---|
1087 | RTR3DECL(int) RTFileQueryMaxSizeEx(RTFILE hFile, PRTFOFF pcbMax)
|
---|
1088 | {
|
---|
1089 | /** @todo r=bird:
|
---|
1090 | * We might have to make this code OS version specific... In the worse
|
---|
1091 | * case, we'll have to try GetVolumeInformationByHandle on vista and fall
|
---|
1092 | * back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation)
|
---|
1093 | * else where, and check for known file system names. (For LAN shares we'll
|
---|
1094 | * have to figure out the remote file system.) */
|
---|
1095 | RT_NOREF_PV(hFile); RT_NOREF_PV(pcbMax);
|
---|
1096 | return VERR_NOT_IMPLEMENTED;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 |
|
---|
1100 | RTR3DECL(bool) RTFileIsValid(RTFILE hFile)
|
---|
1101 | {
|
---|
1102 | if (hFile != NIL_RTFILE)
|
---|
1103 | {
|
---|
1104 | DWORD dwType = GetFileType((HANDLE)RTFileToNative(hFile));
|
---|
1105 | switch (dwType)
|
---|
1106 | {
|
---|
1107 | case FILE_TYPE_CHAR:
|
---|
1108 | case FILE_TYPE_DISK:
|
---|
1109 | case FILE_TYPE_PIPE:
|
---|
1110 | case FILE_TYPE_REMOTE:
|
---|
1111 | return true;
|
---|
1112 |
|
---|
1113 | case FILE_TYPE_UNKNOWN:
|
---|
1114 | if (GetLastError() == NO_ERROR)
|
---|
1115 | return true;
|
---|
1116 | break;
|
---|
1117 |
|
---|
1118 | default:
|
---|
1119 | break;
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | return false;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 |
|
---|
1126 | #define LOW_DWORD(u64) ((DWORD)u64)
|
---|
1127 | #define HIGH_DWORD(u64) (((DWORD *)&u64)[1])
|
---|
1128 |
|
---|
1129 | RTR3DECL(int) RTFileLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
1130 | {
|
---|
1131 | Assert(offLock >= 0);
|
---|
1132 |
|
---|
1133 | /* Check arguments. */
|
---|
1134 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
1135 | {
|
---|
1136 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
1137 | return VERR_INVALID_PARAMETER;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /* Prepare flags. */
|
---|
1141 | Assert(RTFILE_LOCK_WRITE);
|
---|
1142 | DWORD dwFlags = (fLock & RTFILE_LOCK_WRITE) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
|
---|
1143 | Assert(RTFILE_LOCK_WAIT);
|
---|
1144 | if (!(fLock & RTFILE_LOCK_WAIT))
|
---|
1145 | dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
|
---|
1146 |
|
---|
1147 | /* Windows structure. */
|
---|
1148 | OVERLAPPED Overlapped;
|
---|
1149 | memset(&Overlapped, 0, sizeof(Overlapped));
|
---|
1150 | Overlapped.Offset = LOW_DWORD(offLock);
|
---|
1151 | Overlapped.OffsetHigh = HIGH_DWORD(offLock);
|
---|
1152 |
|
---|
1153 | /* Note: according to Microsoft, LockFileEx API call is available starting from NT 3.5 */
|
---|
1154 | if (LockFileEx((HANDLE)RTFileToNative(hFile), dwFlags, 0, LOW_DWORD(cbLock), HIGH_DWORD(cbLock), &Overlapped))
|
---|
1155 | return VINF_SUCCESS;
|
---|
1156 |
|
---|
1157 | return RTErrConvertFromWin32(GetLastError());
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 |
|
---|
1161 | RTR3DECL(int) RTFileChangeLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
1162 | {
|
---|
1163 | Assert(offLock >= 0);
|
---|
1164 |
|
---|
1165 | /* Check arguments. */
|
---|
1166 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
1167 | {
|
---|
1168 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
1169 | return VERR_INVALID_PARAMETER;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | /* Remove old lock. */
|
---|
1173 | int rc = RTFileUnlock(hFile, offLock, cbLock);
|
---|
1174 | if (RT_FAILURE(rc))
|
---|
1175 | return rc;
|
---|
1176 |
|
---|
1177 | /* Set new lock. */
|
---|
1178 | rc = RTFileLock(hFile, fLock, offLock, cbLock);
|
---|
1179 | if (RT_SUCCESS(rc))
|
---|
1180 | return rc;
|
---|
1181 |
|
---|
1182 | /* Try to restore old lock. */
|
---|
1183 | unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
|
---|
1184 | rc = RTFileLock(hFile, fLockOld, offLock, cbLock);
|
---|
1185 | if (RT_SUCCESS(rc))
|
---|
1186 | return VERR_FILE_LOCK_VIOLATION;
|
---|
1187 | else
|
---|
1188 | return VERR_FILE_LOCK_LOST;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | RTR3DECL(int) RTFileUnlock(RTFILE hFile, int64_t offLock, uint64_t cbLock)
|
---|
1193 | {
|
---|
1194 | Assert(offLock >= 0);
|
---|
1195 |
|
---|
1196 | if (UnlockFile((HANDLE)RTFileToNative(hFile),
|
---|
1197 | LOW_DWORD(offLock), HIGH_DWORD(offLock),
|
---|
1198 | LOW_DWORD(cbLock), HIGH_DWORD(cbLock)))
|
---|
1199 | return VINF_SUCCESS;
|
---|
1200 |
|
---|
1201 | return RTErrConvertFromWin32(GetLastError());
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 |
|
---|
1205 |
|
---|
1206 | RTR3DECL(int) RTFileQueryInfo(RTFILE hFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
1207 | {
|
---|
1208 | /*
|
---|
1209 | * Validate input.
|
---|
1210 | */
|
---|
1211 | if (hFile == NIL_RTFILE)
|
---|
1212 | {
|
---|
1213 | AssertMsgFailed(("Invalid hFile=%RTfile\n", hFile));
|
---|
1214 | return VERR_INVALID_PARAMETER;
|
---|
1215 | }
|
---|
1216 | if (!pObjInfo)
|
---|
1217 | {
|
---|
1218 | AssertMsgFailed(("Invalid pObjInfo=%p\n", pObjInfo));
|
---|
1219 | return VERR_INVALID_PARAMETER;
|
---|
1220 | }
|
---|
1221 | if ( enmAdditionalAttribs < RTFSOBJATTRADD_NOTHING
|
---|
1222 | || enmAdditionalAttribs > RTFSOBJATTRADD_LAST)
|
---|
1223 | {
|
---|
1224 | AssertMsgFailed(("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs));
|
---|
1225 | return VERR_INVALID_PARAMETER;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | /*
|
---|
1229 | * Query file info.
|
---|
1230 | */
|
---|
1231 | HANDLE hHandle = (HANDLE)RTFileToNative(hFile);
|
---|
1232 | #if 1
|
---|
1233 | uint64_t auBuf[168 / sizeof(uint64_t)]; /* Missing FILE_ALL_INFORMATION here. */
|
---|
1234 | int rc = rtPathNtQueryInfoFromHandle(hFile, auBuf, sizeof(auBuf), pObjInfo, enmAdditionalAttribs, NULL, 0);
|
---|
1235 | if (RT_SUCCESS(rc))
|
---|
1236 | return rc;
|
---|
1237 |
|
---|
1238 | /*
|
---|
1239 | * Console I/O handles make trouble here. On older windows versions they
|
---|
1240 | * end up with ERROR_INVALID_HANDLE when handed to the above API, while on
|
---|
1241 | * more recent ones they cause different errors to appear.
|
---|
1242 | *
|
---|
1243 | * Thus, we must ignore the latter and doubly verify invalid handle claims.
|
---|
1244 | * We use the undocumented VerifyConsoleIoHandle to do this, falling back on
|
---|
1245 | * GetFileType should it not be there.
|
---|
1246 | */
|
---|
1247 | if ( rc == VERR_INVALID_HANDLE
|
---|
1248 | || rc == VERR_ACCESS_DENIED
|
---|
1249 | || rc == VERR_UNEXPECTED_FS_OBJ_TYPE)
|
---|
1250 | {
|
---|
1251 | static PFNVERIFYCONSOLEIOHANDLE s_pfnVerifyConsoleIoHandle = NULL;
|
---|
1252 | static bool volatile s_fInitialized = false;
|
---|
1253 | PFNVERIFYCONSOLEIOHANDLE pfnVerifyConsoleIoHandle;
|
---|
1254 | if (s_fInitialized)
|
---|
1255 | pfnVerifyConsoleIoHandle = s_pfnVerifyConsoleIoHandle;
|
---|
1256 | else
|
---|
1257 | {
|
---|
1258 | pfnVerifyConsoleIoHandle = (PFNVERIFYCONSOLEIOHANDLE)RTLdrGetSystemSymbol("kernel32.dll", "VerifyConsoleIoHandle");
|
---|
1259 | ASMAtomicWriteBool(&s_fInitialized, true);
|
---|
1260 | }
|
---|
1261 | if ( pfnVerifyConsoleIoHandle
|
---|
1262 | ? !pfnVerifyConsoleIoHandle(hHandle)
|
---|
1263 | : GetFileType(hHandle) == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR)
|
---|
1264 | return VERR_INVALID_HANDLE;
|
---|
1265 | }
|
---|
1266 | /*
|
---|
1267 | * On Windows 10 and (hopefully) 8.1 we get ERROR_INVALID_FUNCTION with console
|
---|
1268 | * I/O handles and null device handles. We must ignore these just like the
|
---|
1269 | * above invalid handle error.
|
---|
1270 | */
|
---|
1271 | else if (rc != VERR_INVALID_FUNCTION && rc != VERR_IO_BAD_COMMAND)
|
---|
1272 | return rc;
|
---|
1273 |
|
---|
1274 | RT_ZERO(*pObjInfo);
|
---|
1275 | pObjInfo->Attr.enmAdditional = enmAdditionalAttribs;
|
---|
1276 | pObjInfo->Attr.fMode = rtFsModeFromDos(RTFS_DOS_NT_DEVICE, "", 0, 0, 0);
|
---|
1277 | return VINF_SUCCESS;
|
---|
1278 | #else
|
---|
1279 |
|
---|
1280 | BY_HANDLE_FILE_INFORMATION Data;
|
---|
1281 | if (!GetFileInformationByHandle(hHandle, &Data))
|
---|
1282 | {
|
---|
1283 | /*
|
---|
1284 | * Console I/O handles make trouble here. On older windows versions they
|
---|
1285 | * end up with ERROR_INVALID_HANDLE when handed to the above API, while on
|
---|
1286 | * more recent ones they cause different errors to appear.
|
---|
1287 | *
|
---|
1288 | * Thus, we must ignore the latter and doubly verify invalid handle claims.
|
---|
1289 | * We use the undocumented VerifyConsoleIoHandle to do this, falling back on
|
---|
1290 | * GetFileType should it not be there.
|
---|
1291 | */
|
---|
1292 | DWORD dwErr = GetLastError();
|
---|
1293 | if (dwErr == ERROR_INVALID_HANDLE)
|
---|
1294 | {
|
---|
1295 | static PFNVERIFYCONSOLEIOHANDLE s_pfnVerifyConsoleIoHandle = NULL;
|
---|
1296 | static bool volatile s_fInitialized = false;
|
---|
1297 | PFNVERIFYCONSOLEIOHANDLE pfnVerifyConsoleIoHandle;
|
---|
1298 | if (s_fInitialized)
|
---|
1299 | pfnVerifyConsoleIoHandle = s_pfnVerifyConsoleIoHandle;
|
---|
1300 | else
|
---|
1301 | {
|
---|
1302 | pfnVerifyConsoleIoHandle = (PFNVERIFYCONSOLEIOHANDLE)RTLdrGetSystemSymbol("kernel32.dll", "VerifyConsoleIoHandle");
|
---|
1303 | ASMAtomicWriteBool(&s_fInitialized, true);
|
---|
1304 | }
|
---|
1305 | if ( pfnVerifyConsoleIoHandle
|
---|
1306 | ? !pfnVerifyConsoleIoHandle(hHandle)
|
---|
1307 | : GetFileType(hHandle) == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR)
|
---|
1308 | return VERR_INVALID_HANDLE;
|
---|
1309 | }
|
---|
1310 | /*
|
---|
1311 | * On Windows 10 and (hopefully) 8.1 we get ERROR_INVALID_FUNCTION with console I/O
|
---|
1312 | * handles. We must ignore these just like the above invalid handle error.
|
---|
1313 | */
|
---|
1314 | else if (dwErr != ERROR_INVALID_FUNCTION)
|
---|
1315 | return RTErrConvertFromWin32(dwErr);
|
---|
1316 |
|
---|
1317 | RT_ZERO(Data);
|
---|
1318 | Data.dwFileAttributes = RTFS_DOS_NT_DEVICE;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /*
|
---|
1322 | * Setup the returned data.
|
---|
1323 | */
|
---|
1324 | pObjInfo->cbObject = ((uint64_t)Data.nFileSizeHigh << 32)
|
---|
1325 | | (uint64_t)Data.nFileSizeLow;
|
---|
1326 | pObjInfo->cbAllocated = pObjInfo->cbObject;
|
---|
1327 |
|
---|
1328 | Assert(sizeof(uint64_t) == sizeof(Data.ftCreationTime));
|
---|
1329 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, *(uint64_t *)&Data.ftCreationTime);
|
---|
1330 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, *(uint64_t *)&Data.ftLastAccessTime);
|
---|
1331 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, *(uint64_t *)&Data.ftLastWriteTime);
|
---|
1332 | pObjInfo->ChangeTime = pObjInfo->ModificationTime;
|
---|
1333 |
|
---|
1334 | pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT, "", 0,
|
---|
1335 | RTFSMODE_SYMLINK_REPARSE_TAG /* (symlink or not, doesn't usually matter here) */);
|
---|
1336 |
|
---|
1337 | /*
|
---|
1338 | * Requested attributes (we cannot provide anything actually).
|
---|
1339 | */
|
---|
1340 | switch (enmAdditionalAttribs)
|
---|
1341 | {
|
---|
1342 | case RTFSOBJATTRADD_NOTHING:
|
---|
1343 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
1344 | break;
|
---|
1345 |
|
---|
1346 | case RTFSOBJATTRADD_UNIX:
|
---|
1347 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
|
---|
1348 | pObjInfo->Attr.u.Unix.uid = ~0U;
|
---|
1349 | pObjInfo->Attr.u.Unix.gid = ~0U;
|
---|
1350 | pObjInfo->Attr.u.Unix.cHardlinks = Data.nNumberOfLinks ? Data.nNumberOfLinks : 1;
|
---|
1351 | pObjInfo->Attr.u.Unix.INodeIdDevice = Data.dwVolumeSerialNumber;
|
---|
1352 | pObjInfo->Attr.u.Unix.INodeId = RT_MAKE_U64(Data.nFileIndexLow, Data.nFileIndexHigh);
|
---|
1353 | pObjInfo->Attr.u.Unix.fFlags = 0;
|
---|
1354 | pObjInfo->Attr.u.Unix.GenerationId = 0;
|
---|
1355 | pObjInfo->Attr.u.Unix.Device = 0;
|
---|
1356 | break;
|
---|
1357 |
|
---|
1358 | case RTFSOBJATTRADD_UNIX_OWNER:
|
---|
1359 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
|
---|
1360 | pObjInfo->Attr.u.UnixOwner.uid = ~0U;
|
---|
1361 | pObjInfo->Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
|
---|
1362 | break;
|
---|
1363 |
|
---|
1364 | case RTFSOBJATTRADD_UNIX_GROUP:
|
---|
1365 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
|
---|
1366 | pObjInfo->Attr.u.UnixGroup.gid = ~0U;
|
---|
1367 | pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
|
---|
1368 | break;
|
---|
1369 |
|
---|
1370 | case RTFSOBJATTRADD_EASIZE:
|
---|
1371 | pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
|
---|
1372 | pObjInfo->Attr.u.EASize.cb = 0;
|
---|
1373 | break;
|
---|
1374 |
|
---|
1375 | default:
|
---|
1376 | AssertMsgFailed(("Impossible!\n"));
|
---|
1377 | return VERR_INTERNAL_ERROR;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | return VINF_SUCCESS;
|
---|
1381 | #endif
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 |
|
---|
1385 | RTR3DECL(int) RTFileSetTimes(RTFILE hFile, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
|
---|
1386 | PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
|
---|
1387 | {
|
---|
1388 | RT_NOREF_PV(pChangeTime); /* Not exposed thru the windows API we're using. */
|
---|
1389 |
|
---|
1390 | if (!pAccessTime && !pModificationTime && !pBirthTime)
|
---|
1391 | return VINF_SUCCESS; /* NOP */
|
---|
1392 |
|
---|
1393 | FILETIME CreationTimeFT;
|
---|
1394 | PFILETIME pCreationTimeFT = NULL;
|
---|
1395 | if (pBirthTime)
|
---|
1396 | pCreationTimeFT = RTTimeSpecGetNtFileTime(pBirthTime, &CreationTimeFT);
|
---|
1397 |
|
---|
1398 | FILETIME LastAccessTimeFT;
|
---|
1399 | PFILETIME pLastAccessTimeFT = NULL;
|
---|
1400 | if (pAccessTime)
|
---|
1401 | pLastAccessTimeFT = RTTimeSpecGetNtFileTime(pAccessTime, &LastAccessTimeFT);
|
---|
1402 |
|
---|
1403 | FILETIME LastWriteTimeFT;
|
---|
1404 | PFILETIME pLastWriteTimeFT = NULL;
|
---|
1405 | if (pModificationTime)
|
---|
1406 | pLastWriteTimeFT = RTTimeSpecGetNtFileTime(pModificationTime, &LastWriteTimeFT);
|
---|
1407 |
|
---|
1408 | int rc = VINF_SUCCESS;
|
---|
1409 | if (!SetFileTime((HANDLE)RTFileToNative(hFile), pCreationTimeFT, pLastAccessTimeFT, pLastWriteTimeFT))
|
---|
1410 | {
|
---|
1411 | DWORD Err = GetLastError();
|
---|
1412 | rc = RTErrConvertFromWin32(Err);
|
---|
1413 | Log(("RTFileSetTimes(%RTfile, %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
|
---|
1414 | hFile, pAccessTime, pModificationTime, pChangeTime, pBirthTime, Err, rc));
|
---|
1415 | }
|
---|
1416 | return rc;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 |
|
---|
1420 | #if 0 /* RTFileSetMode is implemented by RTFileSetMode-r3-nt.cpp */
|
---|
1421 | /* This comes from a source file with a different set of system headers (DDK)
|
---|
1422 | * so it can't be declared in a common header, like internal/file.h.
|
---|
1423 | */
|
---|
1424 | extern int rtFileNativeSetAttributes(HANDLE FileHandle, ULONG FileAttributes);
|
---|
1425 |
|
---|
1426 |
|
---|
1427 | RTR3DECL(int) RTFileSetMode(RTFILE hFile, RTFMODE fMode)
|
---|
1428 | {
|
---|
1429 | /*
|
---|
1430 | * Normalize the mode and call the API.
|
---|
1431 | */
|
---|
1432 | fMode = rtFsModeNormalize(fMode, NULL, 0);
|
---|
1433 | if (!rtFsModeIsValid(fMode))
|
---|
1434 | return VERR_INVALID_PARAMETER;
|
---|
1435 |
|
---|
1436 | ULONG FileAttributes = (fMode & RTFS_DOS_MASK) >> RTFS_DOS_SHIFT;
|
---|
1437 | int Err = rtFileNativeSetAttributes((HANDLE)hFile, FileAttributes);
|
---|
1438 | if (Err != ERROR_SUCCESS)
|
---|
1439 | {
|
---|
1440 | int rc = RTErrConvertFromWin32(Err);
|
---|
1441 | Log(("RTFileSetMode(%RTfile, %RTfmode): rtFileNativeSetAttributes (0x%08X) failed with err %d (%Rrc)\n",
|
---|
1442 | hFile, fMode, FileAttributes, Err, rc));
|
---|
1443 | return rc;
|
---|
1444 | }
|
---|
1445 | return VINF_SUCCESS;
|
---|
1446 | }
|
---|
1447 | #endif
|
---|
1448 |
|
---|
1449 |
|
---|
1450 | /* RTFileQueryFsSizes is implemented by ../nt/RTFileQueryFsSizes-nt.cpp */
|
---|
1451 |
|
---|
1452 |
|
---|
1453 | RTR3DECL(int) RTFileDelete(const char *pszFilename)
|
---|
1454 | {
|
---|
1455 | PRTUTF16 pwszFilename;
|
---|
1456 | int rc = RTPathWinFromUtf8(&pwszFilename, pszFilename, 0 /*fFlags*/);
|
---|
1457 | if (RT_SUCCESS(rc))
|
---|
1458 | {
|
---|
1459 | if (!DeleteFileW(pwszFilename))
|
---|
1460 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
1461 | RTPathWinFree(pwszFilename);
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | return rc;
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 |
|
---|
1468 | RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
1469 | {
|
---|
1470 | /*
|
---|
1471 | * Validate input.
|
---|
1472 | */
|
---|
1473 | AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
|
---|
1474 | AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
|
---|
1475 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
1476 |
|
---|
1477 | /*
|
---|
1478 | * Hand it on to the worker.
|
---|
1479 | */
|
---|
1480 | int rc = rtPathWin32MoveRename(pszSrc, pszDst,
|
---|
1481 | fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
|
---|
1482 | RTFS_TYPE_FILE);
|
---|
1483 |
|
---|
1484 | LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
1485 | pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
1486 | return rc;
|
---|
1487 |
|
---|
1488 | }
|
---|
1489 |
|
---|
1490 |
|
---|
1491 | RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
|
---|
1492 | {
|
---|
1493 | /*
|
---|
1494 | * Validate input.
|
---|
1495 | */
|
---|
1496 | AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
|
---|
1497 | AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
|
---|
1498 | AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
|
---|
1499 |
|
---|
1500 | /*
|
---|
1501 | * Hand it on to the worker.
|
---|
1502 | */
|
---|
1503 | int rc = rtPathWin32MoveRename(pszSrc, pszDst,
|
---|
1504 | fMove & RTFILEMOVE_FLAGS_REPLACE
|
---|
1505 | ? MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING
|
---|
1506 | : MOVEFILE_COPY_ALLOWED,
|
---|
1507 | RTFS_TYPE_FILE);
|
---|
1508 |
|
---|
1509 | LogFlow(("RTFileMove(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
1510 | pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
|
---|
1511 | return rc;
|
---|
1512 | }
|
---|
1513 |
|
---|