VirtualBox

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

Last change on this file since 78253 was 78050, checked in by vboxsync, 6 years ago

IPRT/dir: Lifting RTPATH_MAX restriction on RTDirOpen*. bugref:9172

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