1 | /* $Id: RTPathQueryInfo-nt.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathQueryInfo[Ex], Native NT.
|
---|
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_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 | *********************************************************************************************************************************/
|
---|
53 | extern decltype(NtQueryFullAttributesFile) *g_pfnNtQueryFullAttributesFile; /* init-win.cpp */
|
---|
54 |
|
---|
55 |
|
---|
56 | /* ASSUMES FileID comes after ShortName and the structs are identical up to that point. */
|
---|
57 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, NextEntryOffset, FILE_ID_BOTH_DIR_INFORMATION, NextEntryOffset);
|
---|
58 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileIndex , FILE_ID_BOTH_DIR_INFORMATION, FileIndex );
|
---|
59 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, CreationTime , FILE_ID_BOTH_DIR_INFORMATION, CreationTime );
|
---|
60 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastAccessTime , FILE_ID_BOTH_DIR_INFORMATION, LastAccessTime );
|
---|
61 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastWriteTime , FILE_ID_BOTH_DIR_INFORMATION, LastWriteTime );
|
---|
62 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ChangeTime , FILE_ID_BOTH_DIR_INFORMATION, ChangeTime );
|
---|
63 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EndOfFile , FILE_ID_BOTH_DIR_INFORMATION, EndOfFile );
|
---|
64 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, AllocationSize , FILE_ID_BOTH_DIR_INFORMATION, AllocationSize );
|
---|
65 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileAttributes , FILE_ID_BOTH_DIR_INFORMATION, FileAttributes );
|
---|
66 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileNameLength , FILE_ID_BOTH_DIR_INFORMATION, FileNameLength );
|
---|
67 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EaSize , FILE_ID_BOTH_DIR_INFORMATION, EaSize );
|
---|
68 | AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortNameLength, FILE_ID_BOTH_DIR_INFORMATION, ShortNameLength);
|
---|
69 | AssertCompileMembersSameSizeAndOffset(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 | */
|
---|
82 | static 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 | */
|
---|
134 | static 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 | * @param rcNtCaller The status code that got us here.
|
---|
182 | */
|
---|
183 | static int rtPathNtQueryInfoInDirectoryObject(OBJECT_ATTRIBUTES *pObjAttr, PRTFSOBJINFO pObjInfo,
|
---|
184 | RTFSOBJATTRADD enmAddAttr, uint32_t fFlags,
|
---|
185 | void *pvBuf, size_t cbBuf, NTSTATUS rcNtCaller)
|
---|
186 | {
|
---|
187 | RT_NOREF(fFlags);
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Special case: Root dir.
|
---|
191 | */
|
---|
192 | if ( pObjAttr->RootDirectory == NULL
|
---|
193 | && pObjAttr->ObjectName->Length == sizeof(RTUTF16)
|
---|
194 | && pObjAttr->ObjectName->Buffer[0] == '\\')
|
---|
195 | {
|
---|
196 | pObjInfo->cbObject = 0;
|
---|
197 | pObjInfo->cbAllocated = 0;
|
---|
198 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, 0);
|
---|
199 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, 0);
|
---|
200 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, 0);
|
---|
201 | RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, 0);
|
---|
202 | pObjInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
|
---|
203 | return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * We must open and scan the parent directory object.
|
---|
208 | */
|
---|
209 | UNICODE_STRING NtDirName;
|
---|
210 | UNICODE_STRING NtDirEntry;
|
---|
211 | ntPathNtSplitName(pObjAttr->ObjectName, &NtDirName, &NtDirEntry, true /*fNoParentDirSlash*/);
|
---|
212 |
|
---|
213 | while ( NtDirEntry.Length > sizeof(RTUTF16)
|
---|
214 | && NtDirEntry.Buffer[NtDirEntry.Length / sizeof(RTUTF16) - 1] == '\\')
|
---|
215 | NtDirEntry.Length -= sizeof(RTUTF16);
|
---|
216 |
|
---|
217 | pObjAttr->ObjectName = &NtDirName;
|
---|
218 | HANDLE hDir = RTNT_INVALID_HANDLE_VALUE;
|
---|
219 | NTSTATUS rcNt = NtOpenDirectoryObject(&hDir, DIRECTORY_QUERY | DIRECTORY_TRAVERSE, pObjAttr);
|
---|
220 | if (NT_SUCCESS(rcNt))
|
---|
221 | {
|
---|
222 | ULONG uObjDirCtx = 0;
|
---|
223 | for (;;)
|
---|
224 | {
|
---|
225 | ULONG cbReturned = 0;
|
---|
226 | rcNt = NtQueryDirectoryObject(hDir,
|
---|
227 | pvBuf,
|
---|
228 | (ULONG)cbBuf,
|
---|
229 | FALSE /*ReturnSingleEntry */,
|
---|
230 | FALSE /*RestartScan*/,
|
---|
231 | &uObjDirCtx,
|
---|
232 | &cbReturned);
|
---|
233 | if (!NT_SUCCESS(rcNt))
|
---|
234 | break;
|
---|
235 |
|
---|
236 | for (POBJECT_DIRECTORY_INFORMATION pObjDir = (POBJECT_DIRECTORY_INFORMATION)pvBuf;
|
---|
237 | pObjDir->Name.Length != 0;
|
---|
238 | pObjDir++)
|
---|
239 | {
|
---|
240 | if ( pObjDir->Name.Length == NtDirEntry.Length
|
---|
241 | && memcmp(pObjDir->Name.Buffer, NtDirEntry.Buffer, NtDirEntry.Length) == 0)
|
---|
242 | {
|
---|
243 | /*
|
---|
244 | * Find it. Fill in the info we've got and return (see similar code in direnum-r3-nt.cpp).
|
---|
245 | */
|
---|
246 | NtClose(hDir);
|
---|
247 |
|
---|
248 | pObjInfo->cbObject = 0;
|
---|
249 | pObjInfo->cbAllocated = 0;
|
---|
250 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, 0);
|
---|
251 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, 0);
|
---|
252 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, 0);
|
---|
253 | RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, 0);
|
---|
254 |
|
---|
255 | if (ARE_UNICODE_STRINGS_EQUAL(&pObjDir->TypeName, L"Directory"))
|
---|
256 | pObjInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
|
---|
257 | else if (ARE_UNICODE_STRINGS_EQUAL(&pObjDir->TypeName, L"SymbolicLink"))
|
---|
258 | pObjInfo->Attr.fMode = RTFS_DOS_NT_REPARSE_POINT | RTFS_TYPE_SYMLINK | 0777;
|
---|
259 | else if (ARE_UNICODE_STRINGS_EQUAL(&pObjDir->TypeName, L"Device"))
|
---|
260 | pObjInfo->Attr.fMode = RTFS_DOS_NT_DEVICE | RTFS_TYPE_DEV_CHAR | 0666;
|
---|
261 | else
|
---|
262 | pObjInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | 0666;
|
---|
263 |
|
---|
264 | pObjInfo->Attr.enmAdditional = enmAddAttr;
|
---|
265 | return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
|
---|
266 | }
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | NtClose(hDir);
|
---|
271 | if (rcNt == STATUS_NO_MORE_FILES || rcNt == STATUS_NO_MORE_ENTRIES || rcNt == STATUS_NO_SUCH_FILE)
|
---|
272 | return VERR_FILE_NOT_FOUND;
|
---|
273 | }
|
---|
274 | else
|
---|
275 | return RTErrConvertFromNtStatus(rcNtCaller);
|
---|
276 | return RTErrConvertFromNtStatus(rcNt);
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Queries information from a file or directory handle.
|
---|
282 | *
|
---|
283 | * This is shared between the RTPathQueryInfo, RTFileQueryInfo and
|
---|
284 | * RTDirQueryInfo code.
|
---|
285 | *
|
---|
286 | * @returns IPRT status code.
|
---|
287 | * @param hFile The handle to query information from. Must have
|
---|
288 | * the necessary privileges.
|
---|
289 | * @param pvBuf Pointer to a scratch buffer.
|
---|
290 | * @param cbBuf The size of the buffer. This must be large
|
---|
291 | * enough to hold a FILE_ALL_INFORMATION struct.
|
---|
292 | * @param pObjInfo Where to return information about the handle.
|
---|
293 | * @param enmAddAttr What extra info to return.
|
---|
294 | * @param pszPath The path if this is a file (for exe detect).
|
---|
295 | * @param uReparseTag The reparse tag number (0 if not applicable) for
|
---|
296 | * symlink detection/whatnot.
|
---|
297 | */
|
---|
298 | DECLHIDDEN(int) rtPathNtQueryInfoFromHandle(HANDLE hFile, void *pvBuf, size_t cbBuf, PRTFSOBJINFO pObjInfo,
|
---|
299 | RTFSOBJATTRADD enmAddAttr, const char *pszPath, ULONG uReparseTag)
|
---|
300 | {
|
---|
301 | Assert(cbBuf >= sizeof(FILE_ALL_INFORMATION));
|
---|
302 |
|
---|
303 | /** @todo Try optimize this for when RTFSOBJATTRADD_UNIX isn't set? */
|
---|
304 | IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
305 | NTSTATUS rcNt = NtQueryInformationFile(hFile, &Ios, pvBuf, sizeof(FILE_ALL_INFORMATION), FileAllInformation);
|
---|
306 | if ( NT_SUCCESS(rcNt)
|
---|
307 | || rcNt == STATUS_BUFFER_OVERFLOW)
|
---|
308 | {
|
---|
309 | FILE_ALL_INFORMATION *pAllInfo = (FILE_ALL_INFORMATION *)pvBuf;
|
---|
310 | pObjInfo->cbObject = pAllInfo->StandardInformation.EndOfFile.QuadPart;
|
---|
311 | pObjInfo->cbAllocated = pAllInfo->StandardInformation.AllocationSize.QuadPart;
|
---|
312 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, pAllInfo->BasicInformation.CreationTime.QuadPart);
|
---|
313 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, pAllInfo->BasicInformation.LastAccessTime.QuadPart);
|
---|
314 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, pAllInfo->BasicInformation.LastWriteTime.QuadPart);
|
---|
315 | RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, pAllInfo->BasicInformation.ChangeTime.QuadPart);
|
---|
316 | pObjInfo->Attr.fMode = rtFsModeFromDos( (pAllInfo->BasicInformation.FileAttributes << RTFS_DOS_SHIFT)
|
---|
317 | & RTFS_DOS_MASK_NT,
|
---|
318 | pszPath, pszPath ? strlen(pszPath) : 0, uReparseTag, 0);
|
---|
319 | pObjInfo->Attr.enmAdditional = enmAddAttr;
|
---|
320 | if (enmAddAttr == RTFSOBJATTRADD_UNIX)
|
---|
321 | {
|
---|
322 | pObjInfo->Attr.u.Unix.uid = ~0U;
|
---|
323 | pObjInfo->Attr.u.Unix.gid = ~0U;
|
---|
324 | pObjInfo->Attr.u.Unix.cHardlinks = RT_MAX(1, pAllInfo->StandardInformation.NumberOfLinks);
|
---|
325 | pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /* below */
|
---|
326 | pObjInfo->Attr.u.Unix.INodeId = pAllInfo->InternalInformation.IndexNumber.QuadPart;
|
---|
327 | pObjInfo->Attr.u.Unix.fFlags = 0;
|
---|
328 | pObjInfo->Attr.u.Unix.GenerationId = 0;
|
---|
329 | pObjInfo->Attr.u.Unix.Device = 0;
|
---|
330 |
|
---|
331 | /* Get the serial number. */
|
---|
332 | rcNt = NtQueryVolumeInformationFile(hFile, &Ios, pvBuf, (ULONG)RT_MIN(cbBuf, _2K), FileFsVolumeInformation);
|
---|
333 | if (NT_SUCCESS(rcNt) || rcNt == STATUS_BUFFER_OVERFLOW)
|
---|
334 | {
|
---|
335 | FILE_FS_VOLUME_INFORMATION *pVolInfo = (FILE_FS_VOLUME_INFORMATION *)pvBuf;
|
---|
336 | pObjInfo->Attr.u.Unix.INodeIdDevice = pVolInfo->VolumeSerialNumber;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
|
---|
341 | }
|
---|
342 | return RTErrConvertFromNtStatus(rcNt);
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Worker for RTPathQueryInfoEx and RTDirRelPathQueryInfo.
|
---|
348 | *
|
---|
349 | * @returns IPRT status code.
|
---|
350 | * @param hRootDir The root directory that pNtName is relative to.
|
---|
351 | * @param pNtName The NT path which we want to query info for.
|
---|
352 | * @param pObjInfo Where to return the info.
|
---|
353 | * @param enmAddAttr What additional info to get/fake.
|
---|
354 | * @param fFlags Query flags (RTPATH_F_XXX).
|
---|
355 | * @param pszPath The path for detecting executables and such.
|
---|
356 | * Pass empty string if not applicable/available.
|
---|
357 | */
|
---|
358 | DECLHIDDEN(int) rtPathNtQueryInfoWorker(HANDLE hRootDir, UNICODE_STRING *pNtName, PRTFSOBJINFO pObjInfo,
|
---|
359 | RTFSOBJATTRADD enmAddAttr, uint32_t fFlags, const char *pszPath)
|
---|
360 | {
|
---|
361 | /*
|
---|
362 | * There are a three different ways of doing this:
|
---|
363 | * 1. Use NtQueryFullAttributesFile to the get basic file info.
|
---|
364 | * 2. Open whatever the path points to and use NtQueryInformationFile.
|
---|
365 | * 3. Open the parent directory and use NtQueryDirectoryFile like RTDirReadEx.
|
---|
366 | *
|
---|
367 | * The first two options may fail with sharing violations or access denied,
|
---|
368 | * in which case we must use the last one as fallback.
|
---|
369 | */
|
---|
370 | HANDLE hFile = RTNT_INVALID_HANDLE_VALUE;
|
---|
371 | IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
372 | NTSTATUS rcNt;
|
---|
373 | OBJECT_ATTRIBUTES ObjAttr;
|
---|
374 | union
|
---|
375 | {
|
---|
376 | FILE_NETWORK_OPEN_INFORMATION NetOpenInfo;
|
---|
377 | FILE_ALL_INFORMATION AllInfo;
|
---|
378 | FILE_FS_VOLUME_INFORMATION VolInfo;
|
---|
379 | FILE_BOTH_DIR_INFORMATION Both;
|
---|
380 | FILE_ID_BOTH_DIR_INFORMATION BothId;
|
---|
381 | uint8_t abPadding[sizeof(FILE_ID_BOTH_DIR_INFORMATION) + RTPATH_MAX * sizeof(wchar_t)];
|
---|
382 | } uBuf;
|
---|
383 |
|
---|
384 | /*
|
---|
385 | * We can only use the first option if no additional UNIX attribs are
|
---|
386 | * requested and it isn't a symbolic link. NT directory object
|
---|
387 | */
|
---|
388 | int rc = VINF_TRY_AGAIN;
|
---|
389 | if ( enmAddAttr != RTFSOBJATTRADD_UNIX
|
---|
390 | && g_pfnNtQueryFullAttributesFile)
|
---|
391 | {
|
---|
392 | InitializeObjectAttributes(&ObjAttr, pNtName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
|
---|
393 | rcNt = g_pfnNtQueryFullAttributesFile(&ObjAttr, &uBuf.NetOpenInfo);
|
---|
394 | if (NT_SUCCESS(rcNt))
|
---|
395 | {
|
---|
396 | if (!(uBuf.NetOpenInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
|
---|
397 | {
|
---|
398 | pObjInfo->cbObject = uBuf.NetOpenInfo.EndOfFile.QuadPart;
|
---|
399 | pObjInfo->cbAllocated = uBuf.NetOpenInfo.AllocationSize.QuadPart;
|
---|
400 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, uBuf.NetOpenInfo.CreationTime.QuadPart);
|
---|
401 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, uBuf.NetOpenInfo.LastAccessTime.QuadPart);
|
---|
402 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, uBuf.NetOpenInfo.LastWriteTime.QuadPart);
|
---|
403 | RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, uBuf.NetOpenInfo.ChangeTime.QuadPart);
|
---|
404 | pObjInfo->Attr.fMode = rtFsModeFromDos((uBuf.NetOpenInfo.FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
|
---|
405 | pszPath, strlen(pszPath), 0 /*uReparseTag*/, 0);
|
---|
406 | pObjInfo->Attr.enmAdditional = enmAddAttr;
|
---|
407 |
|
---|
408 | return rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
|
---|
409 | }
|
---|
410 | }
|
---|
411 | else if ( rcNt == STATUS_OBJECT_TYPE_MISMATCH
|
---|
412 | || rcNt == STATUS_OBJECT_NAME_INVALID
|
---|
413 | || rcNt == STATUS_INVALID_PARAMETER)
|
---|
414 | {
|
---|
415 | rc = rtPathNtQueryInfoInDirectoryObject(&ObjAttr, pObjInfo, enmAddAttr, fFlags, &uBuf, sizeof(uBuf), rcNt);
|
---|
416 | if (RT_SUCCESS(rc))
|
---|
417 | return rc;
|
---|
418 | }
|
---|
419 | else if ( rcNt != STATUS_ACCESS_DENIED
|
---|
420 | && rcNt != STATUS_SHARING_VIOLATION)
|
---|
421 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
422 | else
|
---|
423 | RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
|
---|
424 | }
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Try the 2nd option. We might have to redo this if not following symbolic
|
---|
428 | * links and the reparse point isn't a symbolic link but a mount point or similar.
|
---|
429 | * We want to return information about the mounted root directory if we can, not
|
---|
430 | * the directory in which it was mounted.
|
---|
431 | */
|
---|
432 | if (rc == VINF_TRY_AGAIN)
|
---|
433 | {
|
---|
434 | static int volatile g_fReparsePoints = -1;
|
---|
435 | uint32_t fOptions = FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT;
|
---|
436 | int fReparsePoints = g_fReparsePoints;
|
---|
437 | if (fReparsePoints != 0 && !(fFlags & RTPATH_F_FOLLOW_LINK))
|
---|
438 | fOptions |= FILE_OPEN_REPARSE_POINT;
|
---|
439 |
|
---|
440 | InitializeObjectAttributes(&ObjAttr, pNtName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
|
---|
441 | rcNt = NtCreateFile(&hFile,
|
---|
442 | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
|
---|
443 | &ObjAttr,
|
---|
444 | &Ios,
|
---|
445 | NULL /*pcbFile*/,
|
---|
446 | FILE_ATTRIBUTE_NORMAL,
|
---|
447 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
448 | FILE_OPEN,
|
---|
449 | fOptions,
|
---|
450 | NULL /*pvEaBuffer*/,
|
---|
451 | 0 /*cbEa*/);
|
---|
452 | if ( ( rcNt == STATUS_INVALID_PARAMETER
|
---|
453 | || rcNt == STATUS_INVALID_PARAMETER_9)
|
---|
454 | && fReparsePoints == -1
|
---|
455 | && (fOptions & FILE_OPEN_REPARSE_POINT))
|
---|
456 | {
|
---|
457 | fOptions &= ~FILE_OPEN_REPARSE_POINT;
|
---|
458 | rcNt = NtCreateFile(&hFile,
|
---|
459 | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
|
---|
460 | &ObjAttr,
|
---|
461 | &Ios,
|
---|
462 | NULL /*pcbFile*/,
|
---|
463 | FILE_ATTRIBUTE_NORMAL,
|
---|
464 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
465 | FILE_OPEN,
|
---|
466 | fOptions,
|
---|
467 | NULL /*pvEaBuffer*/,
|
---|
468 | 0 /*cbEa*/);
|
---|
469 | if (rcNt != STATUS_INVALID_PARAMETER)
|
---|
470 | g_fReparsePoints = fReparsePoints = 0;
|
---|
471 | }
|
---|
472 | if (NT_SUCCESS(rcNt))
|
---|
473 | {
|
---|
474 | /* Query tag information first in order to try re-open non-symlink reparse points. */
|
---|
475 | FILE_ATTRIBUTE_TAG_INFORMATION TagInfo;
|
---|
476 | rcNt = NtQueryInformationFile(hFile, &Ios, &TagInfo, sizeof(TagInfo), FileAttributeTagInformation);
|
---|
477 | if (!NT_SUCCESS(rcNt))
|
---|
478 | TagInfo.FileAttributes = TagInfo.ReparseTag = 0;
|
---|
479 | if ( !(TagInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
|
---|
480 | || TagInfo.ReparseTag == IO_REPARSE_TAG_SYMLINK
|
---|
481 | || (fFlags & RTPATH_F_FOLLOW_LINK))
|
---|
482 | { /* likely */ }
|
---|
483 | else
|
---|
484 | {
|
---|
485 | /* Reparse point that isn't a symbolic link, try follow the reparsing. */
|
---|
486 | HANDLE hFile2;
|
---|
487 | RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
|
---|
488 | rcNt = NtCreateFile(&hFile2,
|
---|
489 | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
|
---|
490 | &ObjAttr,
|
---|
491 | &Ios,
|
---|
492 | NULL /*pcbFile*/,
|
---|
493 | FILE_ATTRIBUTE_NORMAL,
|
---|
494 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
495 | FILE_OPEN,
|
---|
496 | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
|
---|
497 | NULL /*pvEaBuffer*/,
|
---|
498 | 0 /*cbEa*/);
|
---|
499 | if (NT_SUCCESS(rcNt))
|
---|
500 | {
|
---|
501 | NtClose(hFile);
|
---|
502 | hFile = hFile2;
|
---|
503 | TagInfo.FileAttributes = TagInfo.ReparseTag = 0;
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | /*
|
---|
508 | * Get the information we need and convert it.
|
---|
509 | */
|
---|
510 | rc = rtPathNtQueryInfoFromHandle(hFile, &uBuf, sizeof(uBuf), pObjInfo, enmAddAttr, pszPath, TagInfo.ReparseTag);
|
---|
511 | NtClose(hFile);
|
---|
512 | if (RT_SUCCESS(rc))
|
---|
513 | return rc;
|
---|
514 |
|
---|
515 | if (RT_FAILURE(rc))
|
---|
516 | rc = VINF_TRY_AGAIN;
|
---|
517 | }
|
---|
518 | else if ( rcNt == STATUS_OBJECT_TYPE_MISMATCH
|
---|
519 | || rcNt == STATUS_OBJECT_NAME_INVALID
|
---|
520 | /*|| rcNt == STATUS_INVALID_PARAMETER*/)
|
---|
521 | {
|
---|
522 | rc = rtPathNtQueryInfoInDirectoryObject(&ObjAttr, pObjInfo, enmAddAttr, fFlags, &uBuf, sizeof(uBuf), rcNt);
|
---|
523 | if (RT_SUCCESS(rc))
|
---|
524 | return rc;
|
---|
525 | }
|
---|
526 | else if ( rcNt != STATUS_ACCESS_DENIED
|
---|
527 | && rcNt != STATUS_SHARING_VIOLATION)
|
---|
528 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
529 | else
|
---|
530 | RTNT_IO_STATUS_BLOCK_REINIT(&Ios);
|
---|
531 | }
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Try the 3rd option if none of the other worked.
|
---|
535 | * If none of the above worked, try open the directory and enumerate
|
---|
536 | * the file we're after. This
|
---|
537 | */
|
---|
538 | if (rc == VINF_TRY_AGAIN)
|
---|
539 | {
|
---|
540 | /* Split up the name into parent directory path and filename. */
|
---|
541 | UNICODE_STRING NtDirName;
|
---|
542 | UNICODE_STRING NtFilter;
|
---|
543 | ntPathNtSplitName(pNtName, &NtDirName, &NtFilter, false /*fNoParentDirSlash*/);
|
---|
544 |
|
---|
545 | /* Try open the directory. */
|
---|
546 | InitializeObjectAttributes(&ObjAttr, &NtDirName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
|
---|
547 | rcNt = NtCreateFile(&hFile,
|
---|
548 | FILE_LIST_DIRECTORY | SYNCHRONIZE,
|
---|
549 | &ObjAttr,
|
---|
550 | &Ios,
|
---|
551 | NULL /*pcbFile*/,
|
---|
552 | FILE_ATTRIBUTE_NORMAL,
|
---|
553 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
554 | FILE_OPEN,
|
---|
555 | FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
|
---|
556 | NULL /*pvEaBuffer*/,
|
---|
557 | 0 /*cbEa*/);
|
---|
558 | if (NT_SUCCESS(rcNt))
|
---|
559 | {
|
---|
560 | FILE_INFORMATION_CLASS enmInfoClass;
|
---|
561 | if (RT_MAKE_U64(RTNtCurrentPeb()->OSMinorVersion, RTNtCurrentPeb()->OSMajorVersion) > RT_MAKE_U64(0,5) /* > W2K */)
|
---|
562 | enmInfoClass = FileIdBothDirectoryInformation; /* Introduced in XP, from I can tell. */
|
---|
563 | else
|
---|
564 | enmInfoClass = FileBothDirectoryInformation;
|
---|
565 | rcNt = NtQueryDirectoryFile(hFile,
|
---|
566 | NULL /* Event */,
|
---|
567 | NULL /* ApcRoutine */,
|
---|
568 | NULL /* ApcContext */,
|
---|
569 | &Ios,
|
---|
570 | &uBuf,
|
---|
571 | RT_MIN(sizeof(uBuf), 0xfff0),
|
---|
572 | enmInfoClass,
|
---|
573 | TRUE /*ReturnSingleEntry */,
|
---|
574 | &NtFilter,
|
---|
575 | FALSE /*RestartScan */);
|
---|
576 | if (NT_SUCCESS(rcNt))
|
---|
577 | {
|
---|
578 | pObjInfo->cbObject = uBuf.Both.EndOfFile.QuadPart;
|
---|
579 | pObjInfo->cbAllocated = uBuf.Both.AllocationSize.QuadPart;
|
---|
580 |
|
---|
581 | RTTimeSpecSetNtTime(&pObjInfo->BirthTime, uBuf.Both.CreationTime.QuadPart);
|
---|
582 | RTTimeSpecSetNtTime(&pObjInfo->AccessTime, uBuf.Both.LastAccessTime.QuadPart);
|
---|
583 | RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, uBuf.Both.LastWriteTime.QuadPart);
|
---|
584 | RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, uBuf.Both.ChangeTime.QuadPart);
|
---|
585 |
|
---|
586 | pObjInfo->Attr.fMode = rtFsModeFromDos((uBuf.Both.FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
|
---|
587 | pszPath, strlen(pszPath), uBuf.Both.EaSize, 0);
|
---|
588 |
|
---|
589 | pObjInfo->Attr.enmAdditional = enmAddAttr;
|
---|
590 | if (enmAddAttr == RTFSOBJATTRADD_UNIX)
|
---|
591 | {
|
---|
592 | pObjInfo->Attr.u.Unix.uid = ~0U;
|
---|
593 | pObjInfo->Attr.u.Unix.gid = ~0U;
|
---|
594 | pObjInfo->Attr.u.Unix.cHardlinks = 1;
|
---|
595 | pObjInfo->Attr.u.Unix.INodeIdDevice = 0; /* below */
|
---|
596 | pObjInfo->Attr.u.Unix.INodeId = enmInfoClass == FileIdBothDirectoryInformation
|
---|
597 | ? uBuf.BothId.FileId.QuadPart : 0;
|
---|
598 | pObjInfo->Attr.u.Unix.fFlags = 0;
|
---|
599 | pObjInfo->Attr.u.Unix.GenerationId = 0;
|
---|
600 | pObjInfo->Attr.u.Unix.Device = 0;
|
---|
601 |
|
---|
602 | /* Get the serial number. */
|
---|
603 | rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &uBuf, RT_MIN(sizeof(uBuf), _2K),
|
---|
604 | FileFsVolumeInformation);
|
---|
605 | if (NT_SUCCESS(rcNt))
|
---|
606 | pObjInfo->Attr.u.Unix.INodeIdDevice = uBuf.VolInfo.VolumeSerialNumber;
|
---|
607 | }
|
---|
608 |
|
---|
609 | rc = rtPathNtQueryInfoFillInDummyData(VINF_SUCCESS, pObjInfo, enmAddAttr);
|
---|
610 | }
|
---|
611 | else
|
---|
612 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
613 |
|
---|
614 | NtClose(hFile);
|
---|
615 | }
|
---|
616 | /*
|
---|
617 | * Quite possibly a object directory.
|
---|
618 | */
|
---|
619 | else if ( rcNt == STATUS_OBJECT_NAME_INVALID /* with trailing slash */
|
---|
620 | || rcNt == STATUS_OBJECT_TYPE_MISMATCH /* without trailing slash */ )
|
---|
621 | {
|
---|
622 | InitializeObjectAttributes(&ObjAttr, pNtName, OBJ_CASE_INSENSITIVE, hRootDir, NULL);
|
---|
623 | rc = rtPathNtQueryInfoInDirectoryObject(&ObjAttr, pObjInfo, enmAddAttr, fFlags, &uBuf, sizeof(uBuf), rcNt);
|
---|
624 | if (RT_FAILURE(rc))
|
---|
625 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
626 | }
|
---|
627 | else
|
---|
628 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
629 | }
|
---|
630 |
|
---|
631 | return rc;
|
---|
632 | }
|
---|
633 |
|
---|
634 |
|
---|
635 | RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
|
---|
636 | {
|
---|
637 | /*
|
---|
638 | * Validate input.
|
---|
639 | */
|
---|
640 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
641 | AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
|
---|
642 | AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
|
---|
643 | AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
|
---|
644 | && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
|
---|
645 | ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
|
---|
646 | VERR_INVALID_PARAMETER);
|
---|
647 | AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
648 |
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Convert the input path and call common worker.
|
---|
652 | */
|
---|
653 | HANDLE hRootDir;
|
---|
654 | UNICODE_STRING NtName;
|
---|
655 | int rc = RTNtPathFromWinUtf8(&NtName, &hRootDir, pszPath);
|
---|
656 | if (RT_SUCCESS(rc))
|
---|
657 | {
|
---|
658 | rc = rtPathNtQueryInfoWorker(hRootDir, &NtName, pObjInfo, enmAdditionalAttribs, fFlags, pszPath);
|
---|
659 | RTNtPathFree(&NtName, &hRootDir);
|
---|
660 | }
|
---|
661 | return rc;
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
|
---|
666 | {
|
---|
667 | return RTPathQueryInfoEx(pszPath, pObjInfo, enmAdditionalAttribs, RTPATH_F_ON_LINK);
|
---|
668 | }
|
---|
669 |
|
---|