VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/nt/RTPathQueryInfo-nt.cpp@ 70940

Last change on this file since 70940 was 70940, checked in by vboxsync, 7 years ago

IPRT/RTPathQueryInfo-nt.cpp: Adjustments for statting \Device\xxx files in the NT namespace.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.0 KB
Line 
1/* $Id: RTPathQueryInfo-nt.cpp 70940 2018-02-09 19:40:22Z vboxsync $ */
2/** @file
3 * IPRT - RTPathQueryInfo[Ex], Native NT.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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_FILE
32#include "internal-r3-nt.h"
33
34#include <iprt/path.h>
35#include <iprt/err.h>
36#include <iprt/time.h>
37#include "internal/fs.h"
38#include "internal/path.h"
39
40
41/*********************************************************************************************************************************
42* Defined Constants And Macros *
43*********************************************************************************************************************************/
44/** Helper for comparing a UNICODE_STRING with a string litteral. */
45#define ARE_UNICODE_STRINGS_EQUAL(a_UniStr, a_wszType) \
46 ( (a_UniStr)->Length == sizeof(a_wszType) - sizeof(RTUTF16) \
47 && memcmp((a_UniStr)->Buffer, a_wszType, sizeof(a_wszType) - sizeof(RTUTF16)) == 0)
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53extern decltype(NtQueryFullAttributesFile) *g_pfnNtQueryFullAttributesFile; /* init-win.cpp */
54
55
56/* ASSUMES FileID comes after ShortName and the structs are identical up to that point. */
57AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, NextEntryOffset, FILE_ID_BOTH_DIR_INFORMATION, NextEntryOffset);
58AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileIndex , FILE_ID_BOTH_DIR_INFORMATION, FileIndex );
59AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, CreationTime , FILE_ID_BOTH_DIR_INFORMATION, CreationTime );
60AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastAccessTime , FILE_ID_BOTH_DIR_INFORMATION, LastAccessTime );
61AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastWriteTime , FILE_ID_BOTH_DIR_INFORMATION, LastWriteTime );
62AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ChangeTime , FILE_ID_BOTH_DIR_INFORMATION, ChangeTime );
63AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EndOfFile , FILE_ID_BOTH_DIR_INFORMATION, EndOfFile );
64AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, AllocationSize , FILE_ID_BOTH_DIR_INFORMATION, AllocationSize );
65AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileAttributes , FILE_ID_BOTH_DIR_INFORMATION, FileAttributes );
66AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileNameLength , FILE_ID_BOTH_DIR_INFORMATION, FileNameLength );
67AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EaSize , FILE_ID_BOTH_DIR_INFORMATION, EaSize );
68AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortNameLength, FILE_ID_BOTH_DIR_INFORMATION, ShortNameLength);
69AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortName , FILE_ID_BOTH_DIR_INFORMATION, ShortName );
70
71
72
73/**
74 * Splits up an NT path into directory and filename.
75 *
76 * @param pNtName The path to split.
77 * @param pNtParent Where to return the directory path.
78 * @param pNtFilename Where to return the filename part.
79 * @param fNoParentDirSlash Whether to make sure the directory path doesn't
80 * end with a slash (except root).
81 */
82static void ntPathNtSplitName(UNICODE_STRING const *pNtName, UNICODE_STRING *pNtParent, UNICODE_STRING *pNtFilename,
83 bool fNoParentDirSlash)
84{
85 PRTUTF16 pwszBuffer = pNtName->Buffer;
86 size_t off = pNtName->Length / sizeof(RTUTF16);
87
88 /* Skip trailing slash if present. */
89 if ( off > 0
90 && pwszBuffer[off - 1] == '\\')
91 off--;
92
93 /* Find the slash before that. */
94 RTUTF16 wc;
95 while ( off > 0
96 && (wc = pwszBuffer[off - 1]) != '\\'
97 && wc != '/')
98 off--;
99 if (off != 0)
100 {
101 pNtParent->Buffer = pwszBuffer;
102 pNtParent->MaximumLength = pNtParent->Length = (USHORT)(off * sizeof(RTUTF16));
103 }
104 else
105 {
106 AssertFailed(); /* This is impossible and won't work (NT doesn't know '.' or '..'). */
107 /** @todo query the whole path as it is possible relative. Use the buffer for
108 * temporary name storage. */
109 pNtParent->Buffer = L".";
110 pNtParent->Length = 1 * sizeof(RTUTF16);
111 pNtParent->MaximumLength = 2 * sizeof(RTUTF16);
112 }
113
114 pNtFilename->Buffer = &pwszBuffer[off];
115 pNtFilename->Length = pNtName->Length - (USHORT)(off * sizeof(RTUTF16));
116 pNtFilename->MaximumLength = pNtName->MaximumLength - (USHORT)(off * sizeof(RTUTF16));
117
118 while ( fNoParentDirSlash
119 && pNtParent->Length > sizeof(RTUTF16)
120 && pNtParent->Buffer[pNtParent->Length / sizeof(RTUTF16) - 1] == '\\')
121 pNtParent->Length -= sizeof(RTUTF16);
122}
123
124
125/**
126 * Deals with enmAddAttr != RTFSOBJATTRADD_UNIX.
127 *
128 * @returns IPRT status code (usually @a rc).
129 * @param rc The return code.
130 * @param pObjInfo The info to complete.
131 * @param enmAddAttr What to complete it with. Caller should fill in
132 * RTFSOBJATTRADD_UNIX.
133 */
134static int rtPathNtQueryInfoFillInDummyData(int rc, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
135{
136 switch (enmAddAttr)
137 {
138 case RTFSOBJATTRADD_UNIX:
139 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
140 break;
141
142 case RTFSOBJATTRADD_NOTHING:
143 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
144 break;
145
146 case RTFSOBJATTRADD_UNIX_OWNER:
147 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
148 pObjInfo->Attr.u.UnixOwner.uid = NIL_RTUID;
149 pObjInfo->Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
150 break;
151
152 case RTFSOBJATTRADD_UNIX_GROUP:
153 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
154 pObjInfo->Attr.u.UnixGroup.gid = NIL_RTGID;
155 pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
156 break;
157
158 case RTFSOBJATTRADD_EASIZE:
159 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
160 pObjInfo->Attr.u.EASize.cb = 0;
161 break;
162
163 default:
164 AssertMsgFailed(("Impossible!\n"));
165 rc = VERR_INTERNAL_ERROR;
166 }
167 return rc;
168}
169
170
171/**
172 * Deal with getting info about something that could be in a directory object.
173 *
174 * @returns IPRT status code
175 * @param pObjAttr The NT object attribute.
176 * @param pObjInfo Where to return the info.
177 * @param enmAddAttr Which extra attributes to get (/fake).
178 * @param fFlags The flags.
179 * @param pvBuf Query buffer space.
180 * @param cbBuf Size of the buffer. ASSUMES lots of space.
181 */
182static int rtPathNtQueryInfoInDirectoryObject(OBJECT_ATTRIBUTES *pObjAttr, PRTFSOBJINFO pObjInfo,
183 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags,
184 void *pvBuf, size_t cbBuf)
185{
186 RT_NOREF(fFlags);
187
188 /*
189 * Special case: Root dir.
190 */
191 if ( pObjAttr->RootDirectory == NULL
192 && pObjAttr->ObjectName->Length == sizeof(RTUTF16)
193 && pObjAttr->ObjectName->Buffer[0] == '\\')
194 {
195 pObjInfo->cbObject = 0;
196 pObjInfo->cbAllocated = 0;
197 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, 0);
198 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, 0);
199 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, 0);
200 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, 0);
201 pObjInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
202 return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
203 }
204
205 /*
206 * We must open and scan the parent directory object.
207 */
208 UNICODE_STRING NtDirName;
209 UNICODE_STRING NtDirEntry;
210 ntPathNtSplitName(pObjAttr->ObjectName, &NtDirName, &NtDirEntry, true /*fNoParentDirSlash*/);
211
212 while ( NtDirEntry.Length > sizeof(RTUTF16)
213 && NtDirEntry.Buffer[NtDirEntry.Length / sizeof(RTUTF16) - 1] == '\\')
214 NtDirEntry.Length -= sizeof(RTUTF16);
215
216 pObjAttr->ObjectName = &NtDirName;
217 HANDLE hDir = RTNT_INVALID_HANDLE_VALUE;
218 NTSTATUS rcNt = NtOpenDirectoryObject(&hDir, DIRECTORY_QUERY | DIRECTORY_TRAVERSE, pObjAttr);
219 if (NT_SUCCESS(rcNt))
220 {
221 ULONG uObjDirCtx = 0;
222 for (;;)
223 {
224 ULONG cbReturned = 0;
225 rcNt = NtQueryDirectoryObject(hDir,
226 pvBuf,
227 (ULONG)cbBuf,
228 FALSE /*ReturnSingleEntry */,
229 FALSE /*RestartScan*/,
230 &uObjDirCtx,
231 &cbReturned);
232 if (!NT_SUCCESS(rcNt))
233 break;
234
235 for (POBJECT_DIRECTORY_INFORMATION pObjDir = (POBJECT_DIRECTORY_INFORMATION)pvBuf;
236 pObjDir->Name.Length != 0;
237 pObjDir++)
238 {
239 if ( pObjDir->Name.Length == NtDirEntry.Length
240 && memcmp(pObjDir->Name.Buffer, NtDirEntry.Buffer, NtDirEntry.Length) == 0)
241 {
242 /*
243 * Find it. Fill in the info we've got and return (see similar code in direnum-r3-nt.cpp).
244 */
245 NtClose(hDir);
246
247 pObjInfo->cbObject = 0;
248 pObjInfo->cbAllocated = 0;
249 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, 0);
250 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, 0);
251 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, 0);
252 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, 0);
253
254 if (ARE_UNICODE_STRINGS_EQUAL(&pObjDir->TypeName, L"Directory"))
255 pObjInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
256 else if (ARE_UNICODE_STRINGS_EQUAL(&pObjDir->TypeName, L"SymbolicLink"))
257 pObjInfo->Attr.fMode = RTFS_DOS_NT_REPARSE_POINT | RTFS_TYPE_SYMLINK | 0777;
258 else if (ARE_UNICODE_STRINGS_EQUAL(&pObjDir->TypeName, L"Device"))
259 pObjInfo->Attr.fMode = RTFS_DOS_NT_DEVICE | RTFS_TYPE_DEV_CHAR | 0666;
260 else
261 pObjInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | 0666;
262
263 pObjInfo->Attr.enmAdditional = enmAddAttr;
264 return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
265 }
266 }
267 }
268
269 NtClose(hDir);
270 if (rcNt == STATUS_NO_MORE_FILES || rcNt == STATUS_NO_MORE_ENTRIES || rcNt == STATUS_NO_SUCH_FILE)
271 return VERR_FILE_NOT_FOUND;
272 }
273 return RTErrConvertFromNtStatus(rcNt);
274}
275
276
277/**
278 * Queries information from a file or directory handle.
279 *
280 * This is shared between the RTPathQueryInfo, RTFileQueryInfo and
281 * RTDirQueryInfo code.
282 *
283 * @returns IPRT status code.
284 * @param hFile The handle to query information from. Must have
285 * the necessary privileges.
286 * @param pvBuf Pointer to a scratch buffer.
287 * @param cbBuf The size of the buffer. This must be large
288 * enough to hold a FILE_ALL_INFORMATION struct.
289 * @param pObjInfo Where to return information about the handle.
290 * @param enmAddAttr What extra info to return.
291 * @param pszPath The path if this is a file (for exe detect).
292 * @param uReparseTag The reparse tag number (0 if not applicable) for
293 * symlink detection/whatnot.
294 */
295DECLHIDDEN(int) rtPathNtQueryInfoFromHandle(HANDLE hFile, void *pvBuf, size_t cbBuf, PRTFSOBJINFO pObjInfo,
296 RTFSOBJATTRADD enmAddAttr, const char *pszPath, ULONG uReparseTag)
297{
298 Assert(cbBuf >= sizeof(FILE_ALL_INFORMATION));
299
300 /** @todo Try optimize this for when RTFSOBJATTRADD_UNIX isn't set? */
301 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
302 NTSTATUS rcNt = NtQueryInformationFile(hFile, &Ios, pvBuf, sizeof(FILE_ALL_INFORMATION), FileAllInformation);
303 if ( NT_SUCCESS(rcNt)
304 || rcNt == STATUS_BUFFER_OVERFLOW)
305 {
306 FILE_ALL_INFORMATION *pAllInfo = (FILE_ALL_INFORMATION *)pvBuf;
307 pObjInfo->cbObject = pAllInfo->StandardInformation.EndOfFile.QuadPart;
308 pObjInfo->cbAllocated = pAllInfo->StandardInformation.AllocationSize.QuadPart;
309 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, pAllInfo->BasicInformation.CreationTime.QuadPart);
310 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, pAllInfo->BasicInformation.LastAccessTime.QuadPart);
311 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, pAllInfo->BasicInformation.LastWriteTime.QuadPart);
312 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, pAllInfo->BasicInformation.ChangeTime.QuadPart);
313 pObjInfo->Attr.fMode = rtFsModeFromDos( (pAllInfo->BasicInformation.FileAttributes << RTFS_DOS_SHIFT)
314 & RTFS_DOS_MASK_NT,
315 pszPath, pszPath ? strlen(pszPath) : 0, uReparseTag);
316 pObjInfo->Attr.enmAdditional = enmAddAttr;
317 if (enmAddAttr == RTFSOBJATTRADD_UNIX)
318 {
319 pObjInfo->Attr.u.Unix.uid = ~0U;
320 pObjInfo->Attr.u.Unix.gid = ~0U;
321 pObjInfo->Attr.u.Unix.cHardlinks = RT_MAX(1, pAllInfo->StandardInformation.NumberOfLinks);
322 pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /* below */
323 pObjInfo->Attr.u.Unix.INodeId = pAllInfo->InternalInformation.IndexNumber.QuadPart;
324 pObjInfo->Attr.u.Unix.fFlags = 0;
325 pObjInfo->Attr.u.Unix.GenerationId = 0;
326 pObjInfo->Attr.u.Unix.Device = 0;
327
328 /* Get the serial number. */
329 rcNt = NtQueryVolumeInformationFile(hFile, &Ios, pvBuf, (ULONG)RT_MIN(cbBuf, _2K), FileFsVolumeInformation);
330 if (NT_SUCCESS(rcNt) || rcNt == STATUS_BUFFER_OVERFLOW)
331 {
332 FILE_FS_VOLUME_INFORMATION *pVolInfo = (FILE_FS_VOLUME_INFORMATION *)pvBuf;
333 pObjInfo->Attr.u.Unix.INodeIdDevice = pVolInfo->VolumeSerialNumber;
334 }
335 }
336
337 return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
338 }
339 return RTErrConvertFromNtStatus(rcNt);
340}
341
342
343/**
344 * Worker for RTPathQueryInfoEx and RTDirRelPathQueryInfo.
345 *
346 * @returns IPRT status code.
347 * @param hRootDir The root directory that pNtName is relative to.
348 * @param pNtName The NT path which we want to query info for.
349 * @param pObjInfo Where to return the info.
350 * @param enmAddAttr What additional info to get/fake.
351 * @param fFlags Query flags (RTPATH_F_XXX).
352 * @param pszPath The path for detecting executables and such.
353 * Pass empty string if not applicable/available.
354 */
355DECLHIDDEN(int) rtPathNtQueryInfoWorker(HANDLE hRootDir, UNICODE_STRING *pNtName, PRTFSOBJINFO pObjInfo,
356 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags, const char *pszPath)
357{
358 /*
359 * There are a three different ways of doing this:
360 * 1. Use NtQueryFullAttributesFile to the get basic file info.
361 * 2. Open whatever the path points to and use NtQueryInformationFile.
362 * 3. Open the parent directory and use NtQueryDirectoryFile like RTDirReadEx.
363 *
364 * The first two options may fail with sharing violations or access denied,
365 * in which case we must use the last one as fallback.
366 */
367 HANDLE hFile = RTNT_INVALID_HANDLE_VALUE;
368 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
369 NTSTATUS rcNt;
370 OBJECT_ATTRIBUTES ObjAttr;
371 union
372 {
373 FILE_NETWORK_OPEN_INFORMATION NetOpenInfo;
374 FILE_ALL_INFORMATION AllInfo;
375 FILE_FS_VOLUME_INFORMATION VolInfo;
376 FILE_BOTH_DIR_INFORMATION Both;
377 FILE_ID_BOTH_DIR_INFORMATION BothId;
378 uint8_t abPadding[sizeof(FILE_ID_BOTH_DIR_INFORMATION) + RTPATH_MAX * sizeof(wchar_t)];
379 } uBuf;
380
381 /*
382 * We can only use the first option if no additional UNIX attribs are
383 * requested and it isn't a symbolic link. NT directory object
384 */
385 int rc = VINF_TRY_AGAIN;
386 if ( enmAddAttr != RTFSOBJATTRADD_UNIX
387 && g_pfnNtQueryFullAttributesFile)
388 {
389 InitializeObjectAttributes(&ObjAttr, pNtName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
390 rcNt = g_pfnNtQueryFullAttributesFile(&ObjAttr, &uBuf.NetOpenInfo);
391 if (NT_SUCCESS(rcNt))
392 {
393 if (!(uBuf.NetOpenInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
394 {
395 pObjInfo->cbObject = uBuf.NetOpenInfo.EndOfFile.QuadPart;
396 pObjInfo->cbAllocated = uBuf.NetOpenInfo.AllocationSize.QuadPart;
397 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, uBuf.NetOpenInfo.CreationTime.QuadPart);
398 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, uBuf.NetOpenInfo.LastAccessTime.QuadPart);
399 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, uBuf.NetOpenInfo.LastWriteTime.QuadPart);
400 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, uBuf.NetOpenInfo.ChangeTime.QuadPart);
401 pObjInfo->Attr.fMode = rtFsModeFromDos((uBuf.NetOpenInfo.FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
402 pszPath, strlen(pszPath), 0 /*uReparseTag*/);
403 pObjInfo->Attr.enmAdditional = enmAddAttr;
404
405 return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
406 }
407 }
408 else if ( rcNt == STATUS_OBJECT_TYPE_MISMATCH
409 || rcNt == STATUS_OBJECT_NAME_INVALID
410 || rcNt == STATUS_INVALID_PARAMETER)
411 {
412 rc = rtPathNtQueryInfoInDirectoryObject(&ObjAttr, pObjInfo, enmAddAttr, fFlags, &uBuf, sizeof(uBuf));
413 if (RT_SUCCESS(rc))
414 return rc;
415 }
416 else if ( rcNt != STATUS_ACCESS_DENIED
417 && rcNt != STATUS_SHARING_VIOLATION)
418 rc = RTErrConvertFromNtStatus(rcNt);
419 else
420 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
421 }
422
423 /*
424 * Try the 2nd option. We might have to redo this if not following symbolic
425 * links and the reparse point isn't a symbolic link but a mount point or similar.
426 * We want to return information about the mounted root directory if we can, not
427 * the directory in which it was mounted.
428 */
429 if (rc == VINF_TRY_AGAIN)
430 {
431 static int volatile g_fReparsePoints = -1;
432 uint32_t fOptions = FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT;
433 int fReparsePoints = g_fReparsePoints;
434 if (fReparsePoints != 0 && !(fFlags & RTPATH_F_FOLLOW_LINK))
435 fOptions |= FILE_OPEN_REPARSE_POINT;
436
437 InitializeObjectAttributes(&ObjAttr, pNtName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
438 rcNt = NtCreateFile(&hFile,
439 FILE_READ_ATTRIBUTES | SYNCHRONIZE,
440 &ObjAttr,
441 &Ios,
442 NULL /*pcbFile*/,
443 FILE_ATTRIBUTE_NORMAL,
444 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
445 FILE_OPEN,
446 fOptions,
447 NULL /*pvEaBuffer*/,
448 0 /*cbEa*/);
449 if ( ( rcNt == STATUS_INVALID_PARAMETER
450 || rcNt == STATUS_INVALID_PARAMETER_9)
451 && fReparsePoints == -1
452 && (fOptions & FILE_OPEN_REPARSE_POINT))
453 {
454 fOptions &= ~FILE_OPEN_REPARSE_POINT;
455 rcNt = NtCreateFile(&hFile,
456 FILE_READ_ATTRIBUTES | SYNCHRONIZE,
457 &ObjAttr,
458 &Ios,
459 NULL /*pcbFile*/,
460 FILE_ATTRIBUTE_NORMAL,
461 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
462 FILE_OPEN,
463 fOptions,
464 NULL /*pvEaBuffer*/,
465 0 /*cbEa*/);
466 if (rcNt != STATUS_INVALID_PARAMETER)
467 g_fReparsePoints = fReparsePoints = 0;
468 }
469 if (NT_SUCCESS(rcNt))
470 {
471 /* Query tag information first in order to try re-open non-symlink reparse points. */
472 FILE_ATTRIBUTE_TAG_INFORMATION TagInfo;
473 rcNt = NtQueryInformationFile(hFile, &Ios, &TagInfo, sizeof(TagInfo), FileAttributeTagInformation);
474 if (!NT_SUCCESS(rcNt))
475 TagInfo.FileAttributes = TagInfo.ReparseTag = 0;
476 if ( !(TagInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
477 || TagInfo.ReparseTag == IO_REPARSE_TAG_SYMLINK
478 || (fFlags & RTPATH_F_FOLLOW_LINK))
479 { /* likely */ }
480 else
481 {
482 /* Reparse point that isn't a symbolic link, try follow the reparsing. */
483 HANDLE hFile2;
484 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
485 rcNt = NtCreateFile(&hFile2,
486 FILE_READ_ATTRIBUTES | SYNCHRONIZE,
487 &ObjAttr,
488 &Ios,
489 NULL /*pcbFile*/,
490 FILE_ATTRIBUTE_NORMAL,
491 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
492 FILE_OPEN,
493 FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
494 NULL /*pvEaBuffer*/,
495 0 /*cbEa*/);
496 if (NT_SUCCESS(rcNt))
497 {
498 NtClose(hFile);
499 hFile = hFile2;
500 TagInfo.FileAttributes = TagInfo.ReparseTag = 0;
501 }
502 }
503
504 /*
505 * Get the information we need and convert it.
506 */
507 rc = rtPathNtQueryInfoFromHandle(hFile, &uBuf, sizeof(uBuf), pObjInfo, enmAddAttr, pszPath, TagInfo.ReparseTag);
508 NtClose(hFile);
509 if (RT_SUCCESS(rc))
510 return rc;
511
512 if (RT_FAILURE(rc))
513 rc = VINF_TRY_AGAIN;
514 }
515 else if ( rcNt == STATUS_OBJECT_TYPE_MISMATCH
516 || rcNt == STATUS_OBJECT_NAME_INVALID
517 /*|| rcNt == STATUS_INVALID_PARAMETER*/)
518 {
519 rc = rtPathNtQueryInfoInDirectoryObject(&ObjAttr, pObjInfo, enmAddAttr, fFlags, &uBuf, sizeof(uBuf));
520 if (RT_SUCCESS(rc))
521 return rc;
522 }
523 else if ( rcNt != STATUS_ACCESS_DENIED
524 && rcNt != STATUS_SHARING_VIOLATION)
525 rc = RTErrConvertFromNtStatus(rcNt);
526 else
527 RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
528 }
529
530 /*
531 * Try the 3rd option if none of the other worked.
532 * If none of the above worked, try open the directory and enumerate
533 * the file we're after. This
534 */
535 if (rc == VINF_TRY_AGAIN)
536 {
537 /* Split up the name into parent directory path and filename. */
538 UNICODE_STRING NtDirName;
539 UNICODE_STRING NtFilter;
540 ntPathNtSplitName(pNtName, &NtDirName, &NtFilter, false /*fNoParentDirSlash*/);
541
542 /* Try open the directory. */
543 InitializeObjectAttributes(&ObjAttr, &NtDirName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
544 rcNt = NtCreateFile(&hFile,
545 FILE_LIST_DIRECTORY | SYNCHRONIZE,
546 &ObjAttr,
547 &Ios,
548 NULL /*pcbFile*/,
549 FILE_ATTRIBUTE_NORMAL,
550 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
551 FILE_OPEN,
552 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
553 NULL /*pvEaBuffer*/,
554 0 /*cbEa*/);
555 if (NT_SUCCESS(rcNt))
556 {
557 FILE_INFORMATION_CLASS enmInfoClass;
558 if (RT_MAKE_U64(RTNtCurrentPeb()->OSMinorVersion, RTNtCurrentPeb()->OSMajorVersion) > RT_MAKE_U64(0,5) /* > W2K */)
559 enmInfoClass = FileIdBothDirectoryInformation; /* Introduced in XP, from I can tell. */
560 else
561 enmInfoClass = FileBothDirectoryInformation;
562 rcNt = NtQueryDirectoryFile(hFile,
563 NULL /* Event */,
564 NULL /* ApcRoutine */,
565 NULL /* ApcContext */,
566 &Ios,
567 &uBuf,
568 RT_MIN(sizeof(uBuf), 0xfff0),
569 enmInfoClass,
570 TRUE /*ReturnSingleEntry */,
571 &NtFilter,
572 FALSE /*RestartScan */);
573 if (NT_SUCCESS(rcNt))
574 {
575 pObjInfo->cbObject = uBuf.Both.EndOfFile.QuadPart;
576 pObjInfo->cbAllocated = uBuf.Both.AllocationSize.QuadPart;
577
578 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, uBuf.Both.CreationTime.QuadPart);
579 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, uBuf.Both.LastAccessTime.QuadPart);
580 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, uBuf.Both.LastWriteTime.QuadPart);
581 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, uBuf.Both.ChangeTime.QuadPart);
582
583 pObjInfo->Attr.fMode = rtFsModeFromDos((uBuf.Both.FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
584 pszPath, strlen(pszPath), uBuf.Both.EaSize);
585
586 pObjInfo->Attr.enmAdditional = enmAddAttr;
587 if (enmAddAttr == RTFSOBJATTRADD_UNIX)
588 {
589 pObjInfo->Attr.u.Unix.uid = ~0U;
590 pObjInfo->Attr.u.Unix.gid = ~0U;
591 pObjInfo->Attr.u.Unix.cHardlinks = 1;
592 pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /* below */
593 pObjInfo->Attr.u.Unix.INodeId = enmInfoClass == FileIdBothDirectoryInformation
594 ? uBuf.BothId.FileId.QuadPart : 0;
595 pObjInfo->Attr.u.Unix.fFlags = 0;
596 pObjInfo->Attr.u.Unix.GenerationId = 0;
597 pObjInfo->Attr.u.Unix.Device = 0;
598
599 /* Get the serial number. */
600 rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &uBuf, RT_MIN(sizeof(uBuf), _2K),
601 FileFsVolumeInformation);
602 if (NT_SUCCESS(rcNt))
603 pObjInfo->Attr.u.Unix.INodeIdDevice = uBuf.VolInfo.VolumeSerialNumber;
604 }
605
606 rc = rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
607 }
608 else
609 rc = RTErrConvertFromNtStatus(rcNt);
610
611 NtClose(hFile);
612 }
613 /*
614 * Quite possibly a object directory.
615 */
616 else if ( rcNt == STATUS_OBJECT_NAME_INVALID /* with trailing slash */
617 || rcNt == STATUS_OBJECT_TYPE_MISMATCH /* without trailing slash */ )
618 {
619 InitializeObjectAttributes(&ObjAttr, pNtName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
620 rc = rtPathNtQueryInfoInDirectoryObject(&ObjAttr, pObjInfo, enmAddAttr, fFlags, &uBuf, sizeof(uBuf));
621 if (RT_FAILURE(rc))
622 rc = RTErrConvertFromNtStatus(rcNt);
623 }
624 else
625 rc = RTErrConvertFromNtStatus(rcNt);
626 }
627
628 return rc;
629}
630
631
632RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
633{
634 /*
635 * Validate input.
636 */
637 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
638 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
639 AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
640 AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
641 && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
642 ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
643 VERR_INVALID_PARAMETER);
644 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
645
646
647 /*
648 * Convert the input path and call common worker.
649 */
650 HANDLE hRootDir;
651 UNICODE_STRING NtName;
652 int rc = RTNtPathFromWinUtf8(&NtName, &hRootDir, pszPath);
653 if (RT_SUCCESS(rc))
654 {
655 rc = rtPathNtQueryInfoWorker(hRootDir, &NtName, pObjInfo, enmAdditionalAttribs, fFlags, pszPath);
656 RTNtPathFree(&NtName, &hRootDir);
657 }
658 return rc;
659}
660
661
662RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
663{
664 return RTPathQueryInfoEx(pszPath, pObjInfo, enmAdditionalAttribs, RTPATH_F_ON_LINK);
665}
666
Note: See TracBrowser for help on using the repository browser.

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