VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/nt/direnum-r3-nt.cpp@ 73531

Last change on this file since 73531 was 73097, checked in by vboxsync, 6 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 38.0 KB
Line 
1/* $Id: direnum-r3-nt.cpp 73097 2018-07-12 21:06:33Z vboxsync $ */
2/** @file
3 * IPRT - Directory Enumeration, 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_DIR
32#include "internal-r3-nt.h"
33
34#include <iprt/dir.h>
35#include <iprt/path.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38#include <iprt/assert.h>
39#include <iprt/err.h>
40#include <iprt/file.h>
41#include <iprt/log.h>
42#include "internal/fs.h"
43#include "internal/dir.h"
44#include "internal/path.h"
45#include "../win/internal-r3-win.h"
46
47
48/*********************************************************************************************************************************
49* Defined Constants And Macros *
50*********************************************************************************************************************************/
51/** Whether to return a single record (TRUE) or multiple (FALSE). */
52#define RTDIR_NT_SINGLE_RECORD FALSE
53
54/** Go hard on record chaining (has slight performance impact). */
55#ifdef RT_STRICT
56# define RTDIR_NT_STRICT
57#endif
58
59
60/* ASSUMES FileID comes after ShortName and the structs are identical up to that point. */
61AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, NextEntryOffset, FILE_ID_BOTH_DIR_INFORMATION, NextEntryOffset);
62AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileIndex , FILE_ID_BOTH_DIR_INFORMATION, FileIndex );
63AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, CreationTime , FILE_ID_BOTH_DIR_INFORMATION, CreationTime );
64AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastAccessTime , FILE_ID_BOTH_DIR_INFORMATION, LastAccessTime );
65AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastWriteTime , FILE_ID_BOTH_DIR_INFORMATION, LastWriteTime );
66AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ChangeTime , FILE_ID_BOTH_DIR_INFORMATION, ChangeTime );
67AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EndOfFile , FILE_ID_BOTH_DIR_INFORMATION, EndOfFile );
68AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, AllocationSize , FILE_ID_BOTH_DIR_INFORMATION, AllocationSize );
69AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileAttributes , FILE_ID_BOTH_DIR_INFORMATION, FileAttributes );
70AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileNameLength , FILE_ID_BOTH_DIR_INFORMATION, FileNameLength );
71AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EaSize , FILE_ID_BOTH_DIR_INFORMATION, EaSize );
72AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortNameLength, FILE_ID_BOTH_DIR_INFORMATION, ShortNameLength);
73AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortName , FILE_ID_BOTH_DIR_INFORMATION, ShortName );
74
75
76
77size_t rtDirNativeGetStructSize(const char *pszPath)
78{
79 NOREF(pszPath);
80 return sizeof(RTDIRINTERNAL);
81}
82
83
84int rtDirNativeOpen(PRTDIRINTERNAL pDir, char *pszPathBuf, uintptr_t hRelativeDir, void *pvNativeRelative)
85{
86 /*
87 * Convert the filter to UTF-16.
88 */
89 int rc;
90 pDir->pNtFilterStr = NULL;
91 if ( pDir->cchFilter > 0
92 && pDir->enmFilter == RTDIRFILTER_WINNT)
93 {
94 PRTUTF16 pwszTmp;
95 rc = RTStrToUtf16(pDir->pszFilter, &pwszTmp);
96 if (RT_FAILURE(rc))
97 return rc;
98 pDir->NtFilterStr.Buffer = pwszTmp;
99 pDir->NtFilterStr.Length = pDir->NtFilterStr.MaximumLength = (uint16_t)(RTUtf16Len(pwszTmp) * sizeof(RTUTF16));
100 pDir->pNtFilterStr = &pDir->NtFilterStr;
101 }
102
103 /*
104 * Try open the directory
105 */
106#ifdef IPRT_WITH_NT_PATH_PASSTHRU
107 bool fObjDir = false;
108#endif
109 if (hRelativeDir != ~(uintptr_t)0 && pvNativeRelative == NULL)
110 {
111 /* Caller already opened it, easy! */
112 pDir->hDir = (HANDLE)hRelativeDir;
113 rc = VINF_SUCCESS;
114 }
115 else
116 {
117 /*
118 * If we have to check for reparse points, this gets complicated!
119 */
120 static int volatile g_fReparsePoints = -1;
121 uint32_t fOptions = FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT;
122 int fReparsePoints = g_fReparsePoints;
123 if ( fReparsePoints != 0
124 && (pDir->fFlags & RTDIR_F_NO_FOLLOW)
125 && !pDir->fDirSlash)
126 fOptions |= FILE_OPEN_REPARSE_POINT;
127
128 for (;;)
129 {
130 if (pvNativeRelative == NULL)
131 rc = RTNtPathOpenDir(pszPathBuf,
132 FILE_LIST_DIRECTORY | FILE_READ_ATTRIBUTES | FILE_TRAVERSE | SYNCHRONIZE,
133 FILE_SHARE_READ | FILE_SHARE_WRITE,
134 fOptions,
135 OBJ_CASE_INSENSITIVE,
136 &pDir->hDir,
137#ifdef IPRT_WITH_NT_PATH_PASSTHRU
138 &fObjDir
139#else
140 NULL
141#endif
142 );
143 else
144 rc = RTNtPathOpenDirEx((HANDLE)hRelativeDir,
145 (struct _UNICODE_STRING *)pvNativeRelative,
146 FILE_LIST_DIRECTORY | FILE_READ_ATTRIBUTES | FILE_TRAVERSE | SYNCHRONIZE,
147 FILE_SHARE_READ | FILE_SHARE_WRITE,
148 fOptions,
149 OBJ_CASE_INSENSITIVE,
150 &pDir->hDir,
151#ifdef IPRT_WITH_NT_PATH_PASSTHRU
152 &fObjDir
153#else
154 NULL
155#endif
156 );
157 if ( !(fOptions & FILE_OPEN_REPARSE_POINT)
158 || (rc != VINF_SUCCESS && rc != VERR_INVALID_PARAMETER) )
159 break;
160 if (rc == VINF_SUCCESS)
161 {
162 if (fReparsePoints == -1)
163 g_fReparsePoints = 1;
164
165 /*
166 * We now need to check if we opened a symbolic directory link.
167 * (These can be enumerated, but contains only '.' and '..'.)
168 */
169 FILE_ATTRIBUTE_TAG_INFORMATION TagInfo = { 0, 0 };
170 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
171 NTSTATUS rcNt = NtQueryInformationFile(pDir->hDir, &Ios, &TagInfo, sizeof(TagInfo), FileAttributeTagInformation);
172 AssertMsg(NT_SUCCESS(rcNt), ("%#x\n", rcNt));
173 if (!NT_SUCCESS(rcNt))
174 TagInfo.FileAttributes = TagInfo.ReparseTag = 0;
175 if (!(TagInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
176 break;
177
178 NtClose(pDir->hDir);
179 pDir->hDir = RTNT_INVALID_HANDLE_VALUE;
180
181 if (TagInfo.ReparseTag == IO_REPARSE_TAG_SYMLINK)
182 {
183 rc = VERR_IS_A_SYMLINK;
184 break;
185 }
186
187 /* Reparse point that isn't a symbolic link, try follow the reparsing. */
188 }
189 else if (fReparsePoints == -1)
190 g_fReparsePoints = fReparsePoints = 0;
191 fOptions &= ~FILE_OPEN_REPARSE_POINT;
192 }
193
194 }
195 if (RT_SUCCESS(rc))
196 {
197 /*
198 * Init data.
199 */
200 pDir->fDataUnread = false; /* spelling it out */
201 pDir->uDirDev = 0;
202#ifdef IPRT_WITH_NT_PATH_PASSTHRU
203 if (fObjDir)
204 pDir->enmInfoClass = FileMaximumInformation; /* object directory. */
205#endif
206 }
207 return rc;
208}
209
210
211RTDECL(int) RTDirClose(RTDIR hDir)
212{
213 PRTDIRINTERNAL pDir = hDir;
214
215 /*
216 * Validate input.
217 */
218 if (!pDir)
219 return VERR_INVALID_PARAMETER;
220 if (pDir->u32Magic != RTDIR_MAGIC)
221 {
222 AssertMsgFailed(("Invalid pDir=%p\n", pDir));
223 return VERR_INVALID_PARAMETER;
224 }
225
226 /*
227 * Close the handle.
228 */
229 pDir->u32Magic = ~RTDIR_MAGIC;
230 if (pDir->hDir != RTNT_INVALID_HANDLE_VALUE)
231 {
232 int rc = RTNtPathClose(pDir->hDir);
233 AssertRC(rc);
234 pDir->hDir = RTNT_INVALID_HANDLE_VALUE;
235 }
236 RTStrFree(pDir->pszName);
237 pDir->pszName = NULL;
238 RTUtf16Free(pDir->NtFilterStr.Buffer);
239 pDir->NtFilterStr.Buffer = NULL;
240 RTMemFree(pDir->pabBuffer);
241 pDir->pabBuffer = NULL;
242 RTMemFree(pDir);
243
244 return VINF_SUCCESS;
245}
246
247
248/**
249 * Checks the validity of the current record.
250 *
251 * @returns IPRT status code
252 * @param pThis The directory instance data.
253 */
254static int rtDirNtCheckRecord(PRTDIRINTERNAL pThis)
255{
256#if defined(RTDIR_NT_STRICT) || defined(RT_ARCH_X86)
257# ifdef IPRT_WITH_NT_PATH_PASSTHRU
258 if (pThis->enmInfoClass != FileMaximumInformation)
259# endif
260 {
261 uintptr_t uEndAddr;
262 if (pThis->enmInfoClass == FileIdBothDirectoryInformation)
263 uEndAddr = (uintptr_t)&pThis->uCurData.pBothId->FileName[0];
264 else
265 uEndAddr = (uintptr_t)&pThis->uCurData.pBoth->FileName[0];
266
267# ifdef RT_ARCH_X86
268 /* Workaround for NT 3.1 bug where FAT returns a too short buffer length.
269 Including all NT 3.x versions in case it bug was fixed till NT 4. */
270 uintptr_t const uEndBuffer = (uintptr_t)&pThis->pabBuffer[pThis->cbBuffer];
271 if ( uEndAddr < uEndBuffer
272 && uEndAddr + pThis->uCurData.pBoth->FileNameLength <= uEndBuffer)
273 { /* likely */ }
274 else if ( ( g_enmWinVer == kRTWinOSType_NT310
275 || g_enmWinVer == kRTWinOSType_NT350 // not sure when it was fixed...
276 || g_enmWinVer == kRTWinOSType_NT351)
277 && pThis->enmInfoClass == FileBothDirectoryInformation)
278 {
279 size_t cbLeft = (uintptr_t)&pThis->pabBuffer[pThis->cbBufferAlloc] - (uintptr_t)pThis->uCurData.pBoth;
280 if ( cbLeft >= RT_UOFFSETOF(FILE_BOTH_DIR_INFORMATION, FileName)
281 && pThis->uCurData.pBoth->FileNameLength > 0
282 && cbLeft >= RT_UOFFSETOF(FILE_BOTH_DIR_INFORMATION, FileName) + pThis->uCurData.pBoth->FileNameLength)
283 {
284 pThis->cbBuffer = ((uintptr_t)&pThis->uCurData.pBoth->FileName[0] + pThis->uCurData.pBoth->FileNameLength)
285 - (uintptr_t)&pThis->pabBuffer[0];
286 }
287 }
288# endif
289
290# ifdef RTDIR_NT_STRICT
291 AssertReturn(uEndAddr < (uintptr_t)&pThis->pabBuffer[pThis->cbBuffer], VERR_IO_GEN_FAILURE);
292 AssertReturn(pThis->uCurData.pBoth->FileNameLength < _64K, VERR_FILENAME_TOO_LONG);
293 AssertReturn((pThis->uCurData.pBoth->FileNameLength & 1) == 0, VERR_IO_GEN_FAILURE);
294
295 uEndAddr += pThis->uCurData.pBoth->FileNameLength;
296 AssertReturn(uEndAddr <= (uintptr_t)&pThis->pabBuffer[pThis->cbBuffer], VERR_IO_GEN_FAILURE);
297
298 AssertReturn((unsigned)pThis->uCurData.pBoth->ShortNameLength <= sizeof(pThis->uCurData.pBoth->ShortName),
299 VERR_IO_GEN_FAILURE);
300# endif
301 }
302#else
303 RT_NOREF_PV(pThis);
304#endif
305
306 return VINF_SUCCESS;
307}
308
309
310/**
311 * Advances the buffer pointer.
312 *
313 * @param pThis The directory instance data.
314 */
315static int rtDirNtAdvanceBuffer(PRTDIRINTERNAL pThis)
316{
317 int rc;
318
319#ifdef IPRT_WITH_NT_PATH_PASSTHRU
320 if (pThis->enmInfoClass == FileMaximumInformation)
321 {
322 pThis->uCurData.pObjDir++;
323 pThis->fDataUnread = pThis->uCurData.pObjDir->Name.Length != 0;
324 return VINF_SUCCESS;
325 }
326#endif
327
328 pThis->fDataUnread = false;
329
330 uint32_t const offNext = pThis->uCurData.pBoth->NextEntryOffset;
331 if (offNext == 0)
332 return VINF_SUCCESS;
333
334#ifdef RTDIR_NT_STRICT
335 /* Make sure the next-record offset is beyond the current record. */
336 size_t cbRec;
337 if (pThis->enmInfoClass == FileIdBothDirectoryInformation)
338 cbRec = RT_UOFFSETOF(FILE_ID_BOTH_DIR_INFORMATION, FileName);
339 else
340 cbRec = RT_UOFFSETOF(FILE_BOTH_DIR_INFORMATION, FileName);
341 cbRec += pThis->uCurData.pBoth->FileNameLength;
342 AssertReturn(offNext >= cbRec, VERR_IO_GEN_FAILURE);
343#endif
344 pThis->uCurData.u += offNext;
345
346 rc = rtDirNtCheckRecord(pThis);
347 pThis->fDataUnread = RT_SUCCESS(rc);
348 return rc;
349}
350
351
352/**
353 * Fetches more data from the file system.
354 *
355 * @returns IPRT status code
356 * @param pThis The directory instance data.
357 */
358static int rtDirNtFetchMore(PRTDIRINTERNAL pThis)
359{
360 Assert(!pThis->fDataUnread);
361
362 /*
363 * Allocate the buffer the first time around.
364 * We do this in lazy fashion as some users of RTDirOpen will not actually
365 * list any files, just open it for various reasons.
366 *
367 * We also reduce the buffer size for networked devices as the windows 7-8.1,
368 * server 2012, ++ CIFS servers or/and IFSes screws up buffers larger than 64KB.
369 * There is an alternative hack below, btw. We'll leave both in for now.
370 */
371 bool fFirst = false;
372 if (!pThis->pabBuffer)
373 {
374 pThis->cbBufferAlloc = _256K;
375 if (true) /** @todo skip for known local devices, like the boot device? */
376 {
377 IO_STATUS_BLOCK Ios2 = RTNT_IO_STATUS_BLOCK_INITIALIZER;
378 FILE_FS_DEVICE_INFORMATION Info = { 0, 0 };
379 NTSTATUS rcNt2 = NtQueryVolumeInformationFile(pThis->hDir, &Ios2, &Info, sizeof(Info), FileFsDeviceInformation);
380 if ( !NT_SUCCESS(rcNt2)
381 || (Info.Characteristics & FILE_REMOTE_DEVICE)
382 || Info.DeviceType == FILE_DEVICE_NETWORK
383 || Info.DeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM
384 || Info.DeviceType == FILE_DEVICE_NETWORK_REDIRECTOR
385 || Info.DeviceType == FILE_DEVICE_SMB)
386 pThis->cbBufferAlloc = _64K;
387 }
388
389 fFirst = false;
390 pThis->pabBuffer = (uint8_t *)RTMemAlloc(pThis->cbBufferAlloc);
391 if (!pThis->pabBuffer)
392 {
393 do
394 {
395 pThis->cbBufferAlloc /= 4;
396 pThis->pabBuffer = (uint8_t *)RTMemAlloc(pThis->cbBufferAlloc);
397 } while (pThis->pabBuffer == NULL && pThis->cbBufferAlloc > _4K);
398 if (!pThis->pabBuffer)
399 return VERR_NO_MEMORY;
400 }
401
402 /*
403 * Also try determining the device number.
404 */
405 PFILE_FS_VOLUME_INFORMATION pVolInfo = (PFILE_FS_VOLUME_INFORMATION)pThis->pabBuffer;
406 pVolInfo->VolumeSerialNumber = 0;
407 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
408 NTSTATUS rcNt = NtQueryVolumeInformationFile(pThis->hDir, &Ios,
409 pVolInfo, RT_MIN(_2K, pThis->cbBufferAlloc),
410 FileFsVolumeInformation);
411 if (NT_SUCCESS(rcNt) && NT_SUCCESS(Ios.Status))
412 pThis->uDirDev = pVolInfo->VolumeSerialNumber;
413 else
414 pThis->uDirDev = 0;
415 AssertCompile(sizeof(pThis->uDirDev) == sizeof(pVolInfo->VolumeSerialNumber));
416 /** @todo Grow RTDEV to 64-bit and add low dword of VolumeCreationTime to the top of uDirDev. */
417 }
418
419 /*
420 * Read more.
421 */
422 NTSTATUS rcNt;
423 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
424 if (pThis->enmInfoClass != (FILE_INFORMATION_CLASS)0)
425 {
426#ifdef IPRT_WITH_NT_PATH_PASSTHRU
427 if (pThis->enmInfoClass == FileMaximumInformation)
428 {
429 Ios.Information = 0;
430 Ios.Status = rcNt = NtQueryDirectoryObject(pThis->hDir,
431 pThis->pabBuffer,
432 pThis->cbBufferAlloc,
433 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
434 FALSE /*RestartScan*/,
435 &pThis->uObjDirCtx,
436 (PULONG)&Ios.Information);
437 }
438 else
439#endif
440 rcNt = NtQueryDirectoryFile(pThis->hDir,
441 NULL /* Event */,
442 NULL /* ApcRoutine */,
443 NULL /* ApcContext */,
444 &Ios,
445 pThis->pabBuffer,
446 pThis->cbBufferAlloc,
447 pThis->enmInfoClass,
448 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
449 pThis->pNtFilterStr,
450 FALSE /*RestartScan */);
451 }
452 else
453 {
454 /*
455 * The first time around we have to figure which info class we can use
456 * as well as the right buffer size. We prefer an info class which
457 * gives us file IDs (Vista+ IIRC) and we prefer large buffers (for long
458 * ReFS file names and such), but we'll settle for whatever works...
459 *
460 * The windows 7 thru 8.1 CIFS servers have been observed to have
461 * trouble with large buffers, but weirdly only when listing large
462 * directories. Seems 0x10000 is the max. (Samba does not exhibit
463 * these problems, of course.)
464 *
465 * This complicates things. The buffer size issues causes an
466 * STATUS_INVALID_PARAMETER error. Now, you would expect the lack of
467 * FileIdBothDirectoryInformation support to return
468 * STATUS_INVALID_INFO_CLASS, but I'm not entirely sure if we can 100%
469 * depend on third IFSs to get that right. Nor, am I entirely confident
470 * that we can depend on them to check the class before the buffer size.
471 *
472 * Thus the mess.
473 */
474 if (RT_MAKE_U64(RTNtCurrentPeb()->OSMinorVersion, RTNtCurrentPeb()->OSMajorVersion) > RT_MAKE_U64(0,5) /* > W2K */)
475 pThis->enmInfoClass = FileIdBothDirectoryInformation; /* Introduced in XP, from I can tell. */
476 else
477 pThis->enmInfoClass = FileBothDirectoryInformation;
478 rcNt = NtQueryDirectoryFile(pThis->hDir,
479 NULL /* Event */,
480 NULL /* ApcRoutine */,
481 NULL /* ApcContext */,
482 &Ios,
483 pThis->pabBuffer,
484 pThis->cbBufferAlloc,
485 pThis->enmInfoClass,
486 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
487 pThis->pNtFilterStr,
488 FALSE /*RestartScan */);
489 if (NT_SUCCESS(rcNt))
490 { /* likely */ }
491 else
492 {
493 bool fRestartScan = false;
494 for (unsigned iRetry = 0; iRetry < 2; iRetry++)
495 {
496 if ( rcNt == STATUS_INVALID_INFO_CLASS
497 || rcNt == STATUS_INVALID_PARAMETER_8
498 || iRetry != 0)
499 pThis->enmInfoClass = FileBothDirectoryInformation;
500
501 uint32_t cbBuffer = pThis->cbBufferAlloc;
502 if ( rcNt == STATUS_INVALID_PARAMETER
503 || rcNt == STATUS_INVALID_PARAMETER_7
504 || rcNt == STATUS_INVALID_NETWORK_RESPONSE
505 || iRetry != 0)
506 {
507 cbBuffer = RT_MIN(cbBuffer / 2, _64K);
508 fRestartScan = true;
509 }
510
511 for (;;)
512 {
513 rcNt = NtQueryDirectoryFile(pThis->hDir,
514 NULL /* Event */,
515 NULL /* ApcRoutine */,
516 NULL /* ApcContext */,
517 &Ios,
518 pThis->pabBuffer,
519 cbBuffer,
520 pThis->enmInfoClass,
521 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
522 pThis->pNtFilterStr,
523 fRestartScan);
524 if ( NT_SUCCESS(rcNt)
525 || cbBuffer == pThis->cbBufferAlloc
526 || cbBuffer <= sizeof(*pThis->uCurData.pBothId) + sizeof(WCHAR) * 260)
527 break;
528
529 /* Reduce the buffer size agressivly and try again. We fall back to
530 FindFirstFile values for the final lap. This means we'll do 4 rounds
531 with the current initial buffer size (64KB, 8KB, 1KB, 0x278/0x268). */
532 cbBuffer /= 8;
533 if (cbBuffer < 1024)
534 cbBuffer = pThis->enmInfoClass == FileIdBothDirectoryInformation
535 ? sizeof(*pThis->uCurData.pBothId) + sizeof(WCHAR) * 260
536 : sizeof(*pThis->uCurData.pBoth) + sizeof(WCHAR) * 260;
537 }
538 if (NT_SUCCESS(rcNt))
539 {
540 pThis->cbBufferAlloc = cbBuffer;
541 break;
542 }
543 }
544 }
545 }
546 if (!NT_SUCCESS(rcNt))
547 {
548 /* Note! VBoxSVR and CIFS file systems both ends up with STATUS_NO_SUCH_FILE here instead of STATUS_NO_MORE_FILES. */
549 if (rcNt == STATUS_NO_MORE_FILES || rcNt == STATUS_NO_MORE_ENTRIES || rcNt == STATUS_NO_SUCH_FILE)
550 return VERR_NO_MORE_FILES;
551 return RTErrConvertFromNtStatus(rcNt);
552 }
553 AssertMsg( Ios.Information
554 > (pThis->enmInfoClass == FileMaximumInformation ? sizeof(*pThis->uCurData.pObjDir) : sizeof(*pThis->uCurData.pBoth)),
555 ("Ios.Information=%#x\n", Ios.Information));
556
557 /*
558 * Set up the data members.
559 */
560 pThis->uCurData.u = (uintptr_t)pThis->pabBuffer;
561 pThis->cbBuffer = Ios.Information;
562
563 int rc = rtDirNtCheckRecord(pThis);
564 pThis->fDataUnread = RT_SUCCESS(rc);
565
566 return rc;
567}
568
569
570/**
571 * Converts the name from UTF-16 to UTF-8.
572 *
573 * Fortunately, the names are relative to the directory, so we won't have to do
574 * any sweaty path style coversion. :-)
575 *
576 * @returns IPRT status code
577 * @param pThis The directory instance data.
578 * @param cbName The file name length in bytes.
579 * @param pwsName The file name, not terminated.
580 */
581static int rtDirNtConvertName(PRTDIRINTERNAL pThis, uint32_t cbName, PCRTUTF16 pwsName)
582{
583 int rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
584 if (RT_SUCCESS(rc))
585 {
586 if (!pThis->cbNameAlloc)
587 pThis->cbNameAlloc = pThis->cchName + 1;
588 }
589 else if (rc == VERR_BUFFER_OVERFLOW)
590 {
591 RTStrFree(pThis->pszName);
592 pThis->pszName = NULL;
593 pThis->cbNameAlloc = 0;
594
595 rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
596 if (RT_SUCCESS(rc))
597 pThis->cbNameAlloc = pThis->cchName + 1;
598 }
599 Assert(RT_SUCCESS(rc) ? pThis->pszName != NULL : pThis->pszName == NULL);
600 return rc;
601}
602
603
604/**
605 * Converts the name of the current record.
606 *
607 * @returns IPRT status code.
608 * @param pThis The directory instance data.
609 */
610static int rtDirNtConvertCurName(PRTDIRINTERNAL pThis)
611{
612 switch (pThis->enmInfoClass)
613 {
614 case FileIdBothDirectoryInformation:
615 return rtDirNtConvertName(pThis, pThis->uCurData.pBothId->FileNameLength, pThis->uCurData.pBothId->FileName);
616 case FileBothDirectoryInformation:
617 return rtDirNtConvertName(pThis, pThis->uCurData.pBoth->FileNameLength, pThis->uCurData.pBoth->FileName);
618#ifdef IPRT_WITH_NT_PATH_PASSTHRU
619 case FileMaximumInformation:
620 return rtDirNtConvertName(pThis, pThis->uCurData.pObjDir->Name.Length, pThis->uCurData.pObjDir->Name.Buffer);
621#endif
622
623 default:
624 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
625 }
626}
627
628
629RTDECL(int) RTDirRead(RTDIR hDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
630{
631 PRTDIRINTERNAL pDir = hDir;
632 int rc;
633
634 /*
635 * Validate input.
636 */
637 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
638 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
639 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
640 size_t cbDirEntry = sizeof(*pDirEntry);
641 if (pcbDirEntry)
642 {
643 cbDirEntry = *pcbDirEntry;
644 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
645 ("Invalid *pcbDirEntry=%zu (min %zu)\n", *pcbDirEntry, RT_UOFFSETOF(RTDIRENTRY, szName[2])),
646 VERR_INVALID_PARAMETER);
647 }
648
649 /*
650 * Fetch data?
651 */
652 if (!pDir->fDataUnread)
653 {
654 rc = rtDirNtFetchMore(pDir);
655 if (RT_FAILURE(rc))
656 return rc;
657 }
658
659 /*
660 * Convert the filename to UTF-8.
661 */
662 rc = rtDirNtConvertCurName(pDir);
663 if (RT_FAILURE(rc))
664 return rc;
665
666 /*
667 * Check if we've got enough space to return the data.
668 */
669 const char *pszName = pDir->pszName;
670 const size_t cchName = pDir->cchName;
671 const size_t cbRequired = RT_UOFFSETOF(RTDIRENTRY, szName[1]) + cchName;
672 if (pcbDirEntry)
673 *pcbDirEntry = cbRequired;
674 if (cbRequired > cbDirEntry)
675 return VERR_BUFFER_OVERFLOW;
676
677 /*
678 * Setup the returned data.
679 */
680 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
681 memcpy(pDirEntry->szName, pszName, cchName + 1);
682
683 pDirEntry->INodeId = pDir->enmInfoClass == FileIdBothDirectoryInformation
684 ? pDir->uCurData.pBothId->FileId.QuadPart : 0;
685
686#ifdef IPRT_WITH_NT_PATH_PASSTHRU
687 if (pDir->enmInfoClass != FileMaximumInformation)
688#endif
689 {
690 switch ( pDir->uCurData.pBoth->FileAttributes
691 & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
692 {
693 default:
694 AssertFailed();
695 case 0:
696 pDirEntry->enmType = RTDIRENTRYTYPE_FILE;
697 break;
698
699 case FILE_ATTRIBUTE_DIRECTORY:
700 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
701 break;
702
703 case FILE_ATTRIBUTE_REPARSE_POINT:
704 case FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY:
705 /* EaSize is here reused for returning the repharse tag value. */
706 if (pDir->uCurData.pBoth->EaSize == IO_REPARSE_TAG_SYMLINK)
707 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
708 break;
709 }
710 }
711#ifdef IPRT_WITH_NT_PATH_PASSTHRU
712 else
713 {
714 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
715 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
716 RT_STR_TUPLE("Directory")))
717 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
718 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
719 RT_STR_TUPLE("SymbolicLink")))
720 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
721 }
722#endif
723
724 return rtDirNtAdvanceBuffer(pDir);
725}
726
727
728RTDECL(int) RTDirReadEx(RTDIR hDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
729 RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
730{
731 PRTDIRINTERNAL pDir = hDir;
732 int rc;
733
734 /*
735 * Validate input.
736 */
737 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
738 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
739 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
740
741 AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
742 VERR_INVALID_PARAMETER);
743 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
744
745 size_t cbDirEntry = sizeof(*pDirEntry);
746 if (pcbDirEntry)
747 {
748 cbDirEntry = *pcbDirEntry;
749 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
750 ("Invalid *pcbDirEntry=%zu (min %zu)\n", *pcbDirEntry, RT_UOFFSETOF(RTDIRENTRYEX, szName[2])),
751 VERR_INVALID_PARAMETER);
752 }
753
754 /*
755 * Fetch data?
756 */
757 if (!pDir->fDataUnread)
758 {
759 rc = rtDirNtFetchMore(pDir);
760 if (RT_FAILURE(rc))
761 return rc;
762 }
763
764 /*
765 * Convert the filename to UTF-8.
766 */
767 rc = rtDirNtConvertCurName(pDir);
768 if (RT_FAILURE(rc))
769 return rc;
770
771 /*
772 * Check if we've got enough space to return the data.
773 */
774 const char *pszName = pDir->pszName;
775 const size_t cchName = pDir->cchName;
776 const size_t cbRequired = RT_UOFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
777 if (pcbDirEntry)
778 *pcbDirEntry = cbRequired;
779 if (cbRequired > cbDirEntry)
780 return VERR_BUFFER_OVERFLOW;
781
782 /*
783 * Setup the returned data.
784 */
785 PFILE_BOTH_DIR_INFORMATION pBoth = pDir->uCurData.pBoth;
786
787 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
788 memcpy(pDirEntry->szName, pszName, cchName + 1);
789 memset(pDirEntry->wszShortName, 0, sizeof(pDirEntry->wszShortName));
790#ifdef IPRT_WITH_NT_PATH_PASSTHRU
791 if (pDir->enmInfoClass != FileMaximumInformation)
792#endif
793 {
794 uint8_t cbShort = pBoth->ShortNameLength;
795 if (cbShort > 0)
796 {
797 AssertStmt(cbShort < sizeof(pDirEntry->wszShortName), cbShort = sizeof(pDirEntry->wszShortName) - 2);
798 memcpy(pDirEntry->wszShortName, pBoth->ShortName, cbShort);
799 pDirEntry->cwcShortName = cbShort / 2;
800 }
801 else
802 pDirEntry->cwcShortName = 0;
803
804 pDirEntry->Info.cbObject = pBoth->EndOfFile.QuadPart;
805 pDirEntry->Info.cbAllocated = pBoth->AllocationSize.QuadPart;
806
807 Assert(sizeof(uint64_t) == sizeof(pBoth->CreationTime));
808 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, pBoth->CreationTime.QuadPart);
809 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, pBoth->LastAccessTime.QuadPart);
810 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, pBoth->LastWriteTime.QuadPart);
811 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, pBoth->ChangeTime.QuadPart);
812
813 pDirEntry->Info.Attr.fMode = rtFsModeFromDos((pBoth->FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
814 pszName, cchName, pBoth->EaSize);
815 }
816#ifdef IPRT_WITH_NT_PATH_PASSTHRU
817 else
818 {
819 pDirEntry->cwcShortName = 0;
820 pDirEntry->Info.cbObject = 0;
821 pDirEntry->Info.cbAllocated = 0;
822 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, 0);
823 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, 0);
824 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, 0);
825 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, 0);
826
827 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
828 RT_STR_TUPLE("Directory")))
829 pDirEntry->Info.Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
830 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
831 RT_STR_TUPLE("SymbolicLink")))
832 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_REPARSE_POINT | RTFS_TYPE_SYMLINK | 0777;
833 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
834 RT_STR_TUPLE("Device")))
835 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_DEVICE | RTFS_TYPE_DEV_CHAR | 0666;
836 else
837 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | 0666;
838 }
839#endif
840
841 /*
842 * Requested attributes (we cannot provide anything actually).
843 */
844 switch (enmAdditionalAttribs)
845 {
846 case RTFSOBJATTRADD_EASIZE:
847 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
848#ifdef IPRT_WITH_NT_PATH_PASSTHRU
849 if (pDir->enmInfoClass == FileMaximumInformation)
850 pDirEntry->Info.Attr.u.EASize.cb = 0;
851 else
852#endif
853 pDirEntry->Info.Attr.u.EASize.cb = pBoth->EaSize;
854 break;
855
856 case RTFSOBJATTRADD_UNIX:
857 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
858 pDirEntry->Info.Attr.u.Unix.uid = NIL_RTUID;
859 pDirEntry->Info.Attr.u.Unix.gid = NIL_RTGID;
860 pDirEntry->Info.Attr.u.Unix.cHardlinks = 1;
861 pDirEntry->Info.Attr.u.Unix.INodeIdDevice = pDir->uDirDev;
862 pDirEntry->Info.Attr.u.Unix.INodeId = 0;
863 if ( pDir->enmInfoClass == FileIdBothDirectoryInformation
864 && pDir->uCurData.pBothId->FileId.QuadPart != UINT64_MAX)
865 pDirEntry->Info.Attr.u.Unix.INodeId = pDir->uCurData.pBothId->FileId.QuadPart;
866 pDirEntry->Info.Attr.u.Unix.fFlags = 0;
867 pDirEntry->Info.Attr.u.Unix.GenerationId = 0;
868 pDirEntry->Info.Attr.u.Unix.Device = 0;
869 break;
870
871 case RTFSOBJATTRADD_NOTHING:
872 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
873 break;
874
875 case RTFSOBJATTRADD_UNIX_OWNER:
876 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
877 pDirEntry->Info.Attr.u.UnixOwner.uid = NIL_RTUID;
878 pDirEntry->Info.Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
879 break;
880
881 case RTFSOBJATTRADD_UNIX_GROUP:
882 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
883 pDirEntry->Info.Attr.u.UnixGroup.gid = NIL_RTGID;
884 pDirEntry->Info.Attr.u.UnixGroup.szName[0] = '\0';
885 break;
886
887 default:
888 AssertMsgFailed(("Impossible!\n"));
889 return VERR_INTERNAL_ERROR;
890 }
891
892 /*
893 * Follow links if requested.
894 */
895 if ( (fFlags & RTPATH_F_FOLLOW_LINK)
896 && RTFS_IS_SYMLINK(fFlags))
897 {
898 /** @todo Symlinks: Find[First|Next]FileW will return info about
899 the link, so RTPATH_F_FOLLOW_LINK is not handled correctly. */
900 }
901
902 /*
903 * Finally advance the buffer.
904 */
905 return rtDirNtAdvanceBuffer(pDir);
906}
907
908
909
910RTR3DECL(int) RTDirQueryInfo(RTDIR hDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
911{
912 PRTDIRINTERNAL pDir = hDir;
913 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
914 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
915 AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
916 VERR_INVALID_PARAMETER);
917
918 if (pDir->enmInfoClass == FileMaximumInformation)
919 {
920 /*
921 * Directory object (see similar code above and rtPathNtQueryInfoInDirectoryObject).
922 */
923 pObjInfo->cbObject = 0;
924 pObjInfo->cbAllocated = 0;
925 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, 0);
926 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, 0);
927 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, 0);
928 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, 0);
929 pObjInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
930 pObjInfo->Attr.enmAdditional = enmAdditionalAttribs;
931 switch (enmAdditionalAttribs)
932 {
933 case RTFSOBJATTRADD_NOTHING:
934 case RTFSOBJATTRADD_UNIX:
935 pObjInfo->Attr.u.Unix.uid = NIL_RTUID;
936 pObjInfo->Attr.u.Unix.gid = NIL_RTGID;
937 pObjInfo->Attr.u.Unix.cHardlinks = 1;
938 pObjInfo->Attr.u.Unix.INodeIdDevice = pDir->uDirDev;
939 pObjInfo->Attr.u.Unix.INodeId = 0;
940 pObjInfo->Attr.u.Unix.fFlags = 0;
941 pObjInfo->Attr.u.Unix.GenerationId = 0;
942 pObjInfo->Attr.u.Unix.Device = 0;
943 break;
944
945 case RTFSOBJATTRADD_EASIZE:
946 pObjInfo->Attr.u.EASize.cb = 0;
947 break;
948
949 case RTFSOBJATTRADD_UNIX_OWNER:
950 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
951 pObjInfo->Attr.u.UnixOwner.uid = NIL_RTUID;
952 pObjInfo->Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
953 break;
954
955 case RTFSOBJATTRADD_UNIX_GROUP:
956 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
957 pObjInfo->Attr.u.UnixGroup.gid = NIL_RTGID;
958 pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
959 break;
960
961 default:
962 AssertMsgFailed(("Impossible!\n"));
963 return VERR_INTERNAL_ERROR_2;
964 }
965 return VINF_SUCCESS;
966 }
967
968 /*
969 * Regular directory file.
970 */
971 uint8_t abBuf[_2K];
972 return rtPathNtQueryInfoFromHandle(pDir->hDir, abBuf, sizeof(abBuf), pObjInfo, enmAdditionalAttribs, "", 0);
973}
974
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