VirtualBox

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

Last change on this file since 75839 was 75652, checked in by vboxsync, 6 years ago

IPRT/dir: Adding RTDirRewind for use with shared folders.

  • 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 75652 2018-11-21 21:00:31Z 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 pThis->fRestartScan,
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 pThis->fRestartScan);
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 pThis->fRestartScan);
489 if (NT_SUCCESS(rcNt))
490 { /* likely */ }
491 else
492 {
493 bool fRestartScan = pThis->fRestartScan;
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 pThis->fRestartScan = false;
554 AssertMsg( Ios.Information
555 > (pThis->enmInfoClass == FileMaximumInformation ? sizeof(*pThis->uCurData.pObjDir) : sizeof(*pThis->uCurData.pBoth)),
556 ("Ios.Information=%#x\n", Ios.Information));
557
558 /*
559 * Set up the data members.
560 */
561 pThis->uCurData.u = (uintptr_t)pThis->pabBuffer;
562 pThis->cbBuffer = Ios.Information;
563
564 int rc = rtDirNtCheckRecord(pThis);
565 pThis->fDataUnread = RT_SUCCESS(rc);
566
567 return rc;
568}
569
570
571/**
572 * Converts the name from UTF-16 to UTF-8.
573 *
574 * Fortunately, the names are relative to the directory, so we won't have to do
575 * any sweaty path style coversion. :-)
576 *
577 * @returns IPRT status code
578 * @param pThis The directory instance data.
579 * @param cbName The file name length in bytes.
580 * @param pwsName The file name, not terminated.
581 */
582static int rtDirNtConvertName(PRTDIRINTERNAL pThis, uint32_t cbName, PCRTUTF16 pwsName)
583{
584 int rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
585 if (RT_SUCCESS(rc))
586 {
587 if (!pThis->cbNameAlloc)
588 pThis->cbNameAlloc = pThis->cchName + 1;
589 }
590 else if (rc == VERR_BUFFER_OVERFLOW)
591 {
592 RTStrFree(pThis->pszName);
593 pThis->pszName = NULL;
594 pThis->cbNameAlloc = 0;
595
596 rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
597 if (RT_SUCCESS(rc))
598 pThis->cbNameAlloc = pThis->cchName + 1;
599 }
600 Assert(RT_SUCCESS(rc) ? pThis->pszName != NULL : pThis->pszName == NULL);
601 return rc;
602}
603
604
605/**
606 * Converts the name of the current record.
607 *
608 * @returns IPRT status code.
609 * @param pThis The directory instance data.
610 */
611static int rtDirNtConvertCurName(PRTDIRINTERNAL pThis)
612{
613 switch (pThis->enmInfoClass)
614 {
615 case FileIdBothDirectoryInformation:
616 return rtDirNtConvertName(pThis, pThis->uCurData.pBothId->FileNameLength, pThis->uCurData.pBothId->FileName);
617 case FileBothDirectoryInformation:
618 return rtDirNtConvertName(pThis, pThis->uCurData.pBoth->FileNameLength, pThis->uCurData.pBoth->FileName);
619#ifdef IPRT_WITH_NT_PATH_PASSTHRU
620 case FileMaximumInformation:
621 return rtDirNtConvertName(pThis, pThis->uCurData.pObjDir->Name.Length, pThis->uCurData.pObjDir->Name.Buffer);
622#endif
623
624 default:
625 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
626 }
627}
628
629
630RTDECL(int) RTDirRead(RTDIR hDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
631{
632 PRTDIRINTERNAL pDir = hDir;
633 int rc;
634
635 /*
636 * Validate input.
637 */
638 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
639 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
640 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
641 size_t cbDirEntry = sizeof(*pDirEntry);
642 if (pcbDirEntry)
643 {
644 cbDirEntry = *pcbDirEntry;
645 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
646 ("Invalid *pcbDirEntry=%zu (min %zu)\n", *pcbDirEntry, RT_UOFFSETOF(RTDIRENTRY, szName[2])),
647 VERR_INVALID_PARAMETER);
648 }
649
650 /*
651 * Fetch data?
652 */
653 if (!pDir->fDataUnread)
654 {
655 rc = rtDirNtFetchMore(pDir);
656 if (RT_FAILURE(rc))
657 return rc;
658 }
659
660 /*
661 * Convert the filename to UTF-8.
662 */
663 rc = rtDirNtConvertCurName(pDir);
664 if (RT_FAILURE(rc))
665 return rc;
666
667 /*
668 * Check if we've got enough space to return the data.
669 */
670 const char *pszName = pDir->pszName;
671 const size_t cchName = pDir->cchName;
672 const size_t cbRequired = RT_UOFFSETOF(RTDIRENTRY, szName[1]) + cchName;
673 if (pcbDirEntry)
674 *pcbDirEntry = cbRequired;
675 if (cbRequired > cbDirEntry)
676 return VERR_BUFFER_OVERFLOW;
677
678 /*
679 * Setup the returned data.
680 */
681 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
682 memcpy(pDirEntry->szName, pszName, cchName + 1);
683
684 pDirEntry->INodeId = pDir->enmInfoClass == FileIdBothDirectoryInformation
685 ? pDir->uCurData.pBothId->FileId.QuadPart : 0;
686
687#ifdef IPRT_WITH_NT_PATH_PASSTHRU
688 if (pDir->enmInfoClass != FileMaximumInformation)
689#endif
690 {
691 switch ( pDir->uCurData.pBoth->FileAttributes
692 & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
693 {
694 default:
695 AssertFailed();
696 case 0:
697 pDirEntry->enmType = RTDIRENTRYTYPE_FILE;
698 break;
699
700 case FILE_ATTRIBUTE_DIRECTORY:
701 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
702 break;
703
704 case FILE_ATTRIBUTE_REPARSE_POINT:
705 case FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY:
706 /* EaSize is here reused for returning the repharse tag value. */
707 if (pDir->uCurData.pBoth->EaSize == IO_REPARSE_TAG_SYMLINK)
708 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
709 break;
710 }
711 }
712#ifdef IPRT_WITH_NT_PATH_PASSTHRU
713 else
714 {
715 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
716 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
717 RT_STR_TUPLE("Directory")))
718 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
719 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
720 RT_STR_TUPLE("SymbolicLink")))
721 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
722 }
723#endif
724
725 return rtDirNtAdvanceBuffer(pDir);
726}
727
728
729RTDECL(int) RTDirReadEx(RTDIR hDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
730 RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
731{
732 PRTDIRINTERNAL pDir = hDir;
733 int rc;
734
735 /*
736 * Validate input.
737 */
738 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
739 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
740 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
741
742 AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
743 VERR_INVALID_PARAMETER);
744 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
745
746 size_t cbDirEntry = sizeof(*pDirEntry);
747 if (pcbDirEntry)
748 {
749 cbDirEntry = *pcbDirEntry;
750 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
751 ("Invalid *pcbDirEntry=%zu (min %zu)\n", *pcbDirEntry, RT_UOFFSETOF(RTDIRENTRYEX, szName[2])),
752 VERR_INVALID_PARAMETER);
753 }
754
755 /*
756 * Fetch data?
757 */
758 if (!pDir->fDataUnread)
759 {
760 rc = rtDirNtFetchMore(pDir);
761 if (RT_FAILURE(rc))
762 return rc;
763 }
764
765 /*
766 * Convert the filename to UTF-8.
767 */
768 rc = rtDirNtConvertCurName(pDir);
769 if (RT_FAILURE(rc))
770 return rc;
771
772 /*
773 * Check if we've got enough space to return the data.
774 */
775 const char *pszName = pDir->pszName;
776 const size_t cchName = pDir->cchName;
777 const size_t cbRequired = RT_UOFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
778 if (pcbDirEntry)
779 *pcbDirEntry = cbRequired;
780 if (cbRequired > cbDirEntry)
781 return VERR_BUFFER_OVERFLOW;
782
783 /*
784 * Setup the returned data.
785 */
786 PFILE_BOTH_DIR_INFORMATION pBoth = pDir->uCurData.pBoth;
787
788 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
789 memcpy(pDirEntry->szName, pszName, cchName + 1);
790 memset(pDirEntry->wszShortName, 0, sizeof(pDirEntry->wszShortName));
791#ifdef IPRT_WITH_NT_PATH_PASSTHRU
792 if (pDir->enmInfoClass != FileMaximumInformation)
793#endif
794 {
795 uint8_t cbShort = pBoth->ShortNameLength;
796 if (cbShort > 0)
797 {
798 AssertStmt(cbShort < sizeof(pDirEntry->wszShortName), cbShort = sizeof(pDirEntry->wszShortName) - 2);
799 memcpy(pDirEntry->wszShortName, pBoth->ShortName, cbShort);
800 pDirEntry->cwcShortName = cbShort / 2;
801 }
802 else
803 pDirEntry->cwcShortName = 0;
804
805 pDirEntry->Info.cbObject = pBoth->EndOfFile.QuadPart;
806 pDirEntry->Info.cbAllocated = pBoth->AllocationSize.QuadPart;
807
808 Assert(sizeof(uint64_t) == sizeof(pBoth->CreationTime));
809 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, pBoth->CreationTime.QuadPart);
810 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, pBoth->LastAccessTime.QuadPart);
811 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, pBoth->LastWriteTime.QuadPart);
812 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, pBoth->ChangeTime.QuadPart);
813
814 pDirEntry->Info.Attr.fMode = rtFsModeFromDos((pBoth->FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
815 pszName, cchName, pBoth->EaSize);
816 }
817#ifdef IPRT_WITH_NT_PATH_PASSTHRU
818 else
819 {
820 pDirEntry->cwcShortName = 0;
821 pDirEntry->Info.cbObject = 0;
822 pDirEntry->Info.cbAllocated = 0;
823 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, 0);
824 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, 0);
825 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, 0);
826 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, 0);
827
828 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
829 RT_STR_TUPLE("Directory")))
830 pDirEntry->Info.Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
831 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
832 RT_STR_TUPLE("SymbolicLink")))
833 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_REPARSE_POINT | RTFS_TYPE_SYMLINK | 0777;
834 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
835 RT_STR_TUPLE("Device")))
836 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_DEVICE | RTFS_TYPE_DEV_CHAR | 0666;
837 else
838 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | 0666;
839 }
840#endif
841
842 /*
843 * Requested attributes (we cannot provide anything actually).
844 */
845 switch (enmAdditionalAttribs)
846 {
847 case RTFSOBJATTRADD_EASIZE:
848 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
849#ifdef IPRT_WITH_NT_PATH_PASSTHRU
850 if (pDir->enmInfoClass == FileMaximumInformation)
851 pDirEntry->Info.Attr.u.EASize.cb = 0;
852 else
853#endif
854 pDirEntry->Info.Attr.u.EASize.cb = pBoth->EaSize;
855 break;
856
857 case RTFSOBJATTRADD_UNIX:
858 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
859 pDirEntry->Info.Attr.u.Unix.uid = NIL_RTUID;
860 pDirEntry->Info.Attr.u.Unix.gid = NIL_RTGID;
861 pDirEntry->Info.Attr.u.Unix.cHardlinks = 1;
862 pDirEntry->Info.Attr.u.Unix.INodeIdDevice = pDir->uDirDev;
863 pDirEntry->Info.Attr.u.Unix.INodeId = 0;
864 if ( pDir->enmInfoClass == FileIdBothDirectoryInformation
865 && pDir->uCurData.pBothId->FileId.QuadPart != UINT64_MAX)
866 pDirEntry->Info.Attr.u.Unix.INodeId = pDir->uCurData.pBothId->FileId.QuadPart;
867 pDirEntry->Info.Attr.u.Unix.fFlags = 0;
868 pDirEntry->Info.Attr.u.Unix.GenerationId = 0;
869 pDirEntry->Info.Attr.u.Unix.Device = 0;
870 break;
871
872 case RTFSOBJATTRADD_NOTHING:
873 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
874 break;
875
876 case RTFSOBJATTRADD_UNIX_OWNER:
877 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
878 pDirEntry->Info.Attr.u.UnixOwner.uid = NIL_RTUID;
879 pDirEntry->Info.Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
880 break;
881
882 case RTFSOBJATTRADD_UNIX_GROUP:
883 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
884 pDirEntry->Info.Attr.u.UnixGroup.gid = NIL_RTGID;
885 pDirEntry->Info.Attr.u.UnixGroup.szName[0] = '\0';
886 break;
887
888 default:
889 AssertMsgFailed(("Impossible!\n"));
890 return VERR_INTERNAL_ERROR;
891 }
892
893 /*
894 * Follow links if requested.
895 */
896 if ( (fFlags & RTPATH_F_FOLLOW_LINK)
897 && RTFS_IS_SYMLINK(fFlags))
898 {
899 /** @todo Symlinks: Find[First|Next]FileW will return info about
900 the link, so RTPATH_F_FOLLOW_LINK is not handled correctly. */
901 }
902
903 /*
904 * Finally advance the buffer.
905 */
906 return rtDirNtAdvanceBuffer(pDir);
907}
908
909
910RTDECL(int) RTDirRewind(RTDIR hDir)
911{
912 /*
913 * Validate and digest input.
914 */
915 PRTDIRINTERNAL pThis = hDir;
916 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
917 AssertReturn(pThis->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
918
919 /*
920 * The work is done on the next call to rtDirNtFetchMore.
921 */
922 pThis->fRestartScan = true;
923 pThis->fDataUnread = false;
924
925 return VINF_SUCCESS;
926}
927
928
929RTR3DECL(int) RTDirQueryInfo(RTDIR hDir, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
930{
931 PRTDIRINTERNAL pDir = hDir;
932 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
933 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
934 AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
935 VERR_INVALID_PARAMETER);
936
937 if (pDir->enmInfoClass == FileMaximumInformation)
938 {
939 /*
940 * Directory object (see similar code above and rtPathNtQueryInfoInDirectoryObject).
941 */
942 pObjInfo->cbObject = 0;
943 pObjInfo->cbAllocated = 0;
944 RTTimeSpecSetNtTime(&pObjInfo->BirthTime, 0);
945 RTTimeSpecSetNtTime(&pObjInfo->AccessTime, 0);
946 RTTimeSpecSetNtTime(&pObjInfo->ModificationTime, 0);
947 RTTimeSpecSetNtTime(&pObjInfo->ChangeTime, 0);
948 pObjInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
949 pObjInfo->Attr.enmAdditional = enmAdditionalAttribs;
950 switch (enmAdditionalAttribs)
951 {
952 case RTFSOBJATTRADD_NOTHING:
953 case RTFSOBJATTRADD_UNIX:
954 pObjInfo->Attr.u.Unix.uid = NIL_RTUID;
955 pObjInfo->Attr.u.Unix.gid = NIL_RTGID;
956 pObjInfo->Attr.u.Unix.cHardlinks = 1;
957 pObjInfo->Attr.u.Unix.INodeIdDevice = pDir->uDirDev;
958 pObjInfo->Attr.u.Unix.INodeId = 0;
959 pObjInfo->Attr.u.Unix.fFlags = 0;
960 pObjInfo->Attr.u.Unix.GenerationId = 0;
961 pObjInfo->Attr.u.Unix.Device = 0;
962 break;
963
964 case RTFSOBJATTRADD_EASIZE:
965 pObjInfo->Attr.u.EASize.cb = 0;
966 break;
967
968 case RTFSOBJATTRADD_UNIX_OWNER:
969 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
970 pObjInfo->Attr.u.UnixOwner.uid = NIL_RTUID;
971 pObjInfo->Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
972 break;
973
974 case RTFSOBJATTRADD_UNIX_GROUP:
975 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
976 pObjInfo->Attr.u.UnixGroup.gid = NIL_RTGID;
977 pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
978 break;
979
980 default:
981 AssertMsgFailed(("Impossible!\n"));
982 return VERR_INTERNAL_ERROR_2;
983 }
984 return VINF_SUCCESS;
985 }
986
987 /*
988 * Regular directory file.
989 */
990 uint8_t abBuf[_2K];
991 return rtPathNtQueryInfoFromHandle(pDir->hDir, abBuf, sizeof(abBuf), pObjInfo, enmAdditionalAttribs, "", 0);
992}
993
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