VirtualBox

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

Last change on this file since 103795 was 99828, checked in by vboxsync, 19 months ago

*: A bunch of adjustments that allows using /permissive- with Visual C++ (qt 6.x necessity).

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