VirtualBox

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

Last change on this file since 58591 was 57919, checked in by vboxsync, 9 years ago

direnum-r3-nt.cpp: Don't try FileIdBothDirectoryInformation on W2K and NT4, the cifs file system driver / server may get upset.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 29.1 KB
Line 
1/* $Id: direnum-r3-nt.cpp 57919 2015-09-27 23:39:09Z vboxsync $ */
2/** @file
3 * IPRT - Directory Enumeration, Native NT.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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
45
46/*********************************************************************************************************************************
47* Defined Constants And Macros *
48*********************************************************************************************************************************/
49/** Whether to return a single record (TRUE) or multiple (FALSE)o. */
50#define RTDIR_NT_SINGLE_RECORD FALSE
51
52/** Go hard on record chaining (has slight performance impact). */
53#ifdef RT_STRICT
54# define RTDIR_NT_STRICT
55#endif
56
57
58/* ASSUMES FileID comes after ShortName and the structus are identical up to that point. */
59AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, NextEntryOffset, FILE_ID_BOTH_DIR_INFORMATION, NextEntryOffset);
60AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileIndex , FILE_ID_BOTH_DIR_INFORMATION, FileIndex );
61AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, CreationTime , FILE_ID_BOTH_DIR_INFORMATION, CreationTime );
62AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastAccessTime , FILE_ID_BOTH_DIR_INFORMATION, LastAccessTime );
63AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, LastWriteTime , FILE_ID_BOTH_DIR_INFORMATION, LastWriteTime );
64AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ChangeTime , FILE_ID_BOTH_DIR_INFORMATION, ChangeTime );
65AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EndOfFile , FILE_ID_BOTH_DIR_INFORMATION, EndOfFile );
66AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, AllocationSize , FILE_ID_BOTH_DIR_INFORMATION, AllocationSize );
67AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileAttributes , FILE_ID_BOTH_DIR_INFORMATION, FileAttributes );
68AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, FileNameLength , FILE_ID_BOTH_DIR_INFORMATION, FileNameLength );
69AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, EaSize , FILE_ID_BOTH_DIR_INFORMATION, EaSize );
70AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortNameLength, FILE_ID_BOTH_DIR_INFORMATION, ShortNameLength);
71AssertCompileMembersSameSizeAndOffset(FILE_BOTH_DIR_INFORMATION, ShortName , FILE_ID_BOTH_DIR_INFORMATION, ShortName );
72
73
74
75size_t rtDirNativeGetStructSize(const char *pszPath)
76{
77 NOREF(pszPath);
78 return sizeof(RTDIR);
79}
80
81
82int rtDirNativeOpen(PRTDIR pDir, char *pszPathBuf)
83{
84 /*
85 * Convert the filter to UTF-16.
86 */
87 int rc;
88 pDir->pNtFilterStr = NULL;
89 if ( pDir->cchFilter > 0
90 && pDir->enmFilter == RTDIRFILTER_WINNT)
91 {
92 PRTUTF16 pwszTmp;
93 rc = RTStrToUtf16(pDir->pszFilter, &pwszTmp);
94 if (RT_FAILURE(rc))
95 return rc;
96 pDir->NtFilterStr.Buffer = pwszTmp;
97 pDir->NtFilterStr.Length = pDir->NtFilterStr.MaximumLength = (uint16_t)(RTUtf16Len(pwszTmp) * sizeof(RTUTF16));
98 pDir->pNtFilterStr = &pDir->NtFilterStr;
99 }
100
101 /*
102 * Try open the directory
103 */
104#ifdef IPRT_WITH_NT_PATH_PASSTHRU
105 bool fObjDir;
106#endif
107 rc = RTNtPathOpenDir(pszPathBuf,
108 FILE_READ_DATA | SYNCHRONIZE,
109 FILE_SHARE_READ | FILE_SHARE_WRITE,
110 FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
111 OBJ_CASE_INSENSITIVE,
112 &pDir->hDir,
113#ifdef IPRT_WITH_NT_PATH_PASSTHRU
114 &fObjDir
115#else
116 NULL
117#endif
118 );
119 if (RT_SUCCESS(rc))
120 {
121 /*
122 * Init data.
123 */
124 pDir->fDataUnread = false; /* spelling it out */
125#ifdef IPRT_WITH_NT_PATH_PASSTHRU
126 if (fObjDir)
127 pDir->enmInfoClass = FileMaximumInformation; /* object directory. */
128#endif
129 }
130 return rc;
131}
132
133
134RTDECL(int) RTDirClose(PRTDIR pDir)
135{
136 /*
137 * Validate input.
138 */
139 if (!pDir)
140 return VERR_INVALID_PARAMETER;
141 if (pDir->u32Magic != RTDIR_MAGIC)
142 {
143 AssertMsgFailed(("Invalid pDir=%p\n", pDir));
144 return VERR_INVALID_PARAMETER;
145 }
146
147 /*
148 * Close the handle.
149 */
150 pDir->u32Magic = ~RTDIR_MAGIC;
151 if (pDir->hDir != RTNT_INVALID_HANDLE_VALUE)
152 {
153 int rc = RTNtPathClose(pDir->hDir);
154 AssertRC(rc);
155 pDir->hDir = RTNT_INVALID_HANDLE_VALUE;
156 }
157 RTStrFree(pDir->pszName);
158 pDir->pszName = NULL;
159 RTUtf16Free(pDir->NtFilterStr.Buffer);
160 pDir->NtFilterStr.Buffer = NULL;
161 RTMemFree(pDir->pabBuffer);
162 pDir->pabBuffer = NULL;
163 RTMemFree(pDir);
164
165 return VINF_SUCCESS;
166}
167
168
169/**
170 * Checks the validity of the current record.
171 *
172 * @returns IPRT status code
173 * @param pThis The directory instance data.
174 */
175static int rtDirNtCheckRecord(PRTDIR pThis)
176{
177#ifdef RTDIR_NT_STRICT
178# ifdef IPRT_WITH_NT_PATH_PASSTHRU
179 if (pThis->enmInfoClass != FileMaximumInformation)
180# endif
181 {
182 uintptr_t uEndAddr;
183 if (pThis->enmInfoClass == FileIdBothDirectoryInformation)
184 uEndAddr = (uintptr_t)&pThis->uCurData.pBothId->FileName[0];
185 else
186 uEndAddr = (uintptr_t)&pThis->uCurData.pBoth->FileName[0];
187 AssertReturn(uEndAddr < (uintptr_t)&pThis->pabBuffer[pThis->cbBuffer], VERR_IO_GEN_FAILURE);
188
189 AssertReturn(pThis->uCurData.pBoth->FileNameLength < _64K, VERR_FILENAME_TOO_LONG);
190 AssertReturn((pThis->uCurData.pBoth->FileNameLength & 1) == 0, VERR_IO_GEN_FAILURE);
191
192 uEndAddr += pThis->uCurData.pBoth->FileNameLength;
193 AssertReturn(uEndAddr <= (uintptr_t)&pThis->pabBuffer[pThis->cbBuffer], VERR_IO_GEN_FAILURE);
194
195 AssertReturn((unsigned)pThis->uCurData.pBoth->ShortNameLength <= sizeof(pThis->uCurData.pBoth->ShortName),
196 VERR_IO_GEN_FAILURE);
197 }
198#endif
199
200 return VINF_SUCCESS;
201}
202
203
204/**
205 * Advances the buffer pointer.
206 *
207 * @param pThis The directory instance data.
208 */
209static int rtDirNtAdvanceBuffer(PRTDIR pThis)
210{
211 int rc;
212
213#ifdef IPRT_WITH_NT_PATH_PASSTHRU
214 if (pThis->enmInfoClass == FileMaximumInformation)
215 {
216 pThis->uCurData.pObjDir++;
217 pThis->fDataUnread = pThis->uCurData.pObjDir->Name.Length != 0;
218 return VINF_SUCCESS;
219 }
220#endif
221
222 pThis->fDataUnread = false;
223
224 uint32_t const offNext = pThis->uCurData.pBoth->NextEntryOffset;
225 if (offNext == 0)
226 return VINF_SUCCESS;
227
228#ifdef RTDIR_NT_STRICT
229 /* Make sure the next-record offset is beyond the current record. */
230 size_t cbRec;
231 if (pThis->enmInfoClass == FileIdBothDirectoryInformation)
232 cbRec = RT_UOFFSETOF(FILE_ID_BOTH_DIR_INFORMATION, FileName);
233 else
234 cbRec = RT_UOFFSETOF(FILE_BOTH_DIR_INFORMATION, FileName);
235 cbRec += pThis->uCurData.pBoth->FileNameLength;
236 AssertReturn(offNext >= cbRec, VERR_IO_GEN_FAILURE);
237#endif
238 pThis->uCurData.u += offNext;
239
240 rc = rtDirNtCheckRecord(pThis);
241 pThis->fDataUnread = RT_SUCCESS(rc);
242 return rc;
243}
244
245
246/**
247 * Fetches more data from the file system.
248 *
249 * @returns IPRT status code
250 * @param pThis The directory instance data.
251 */
252static int rtDirNtFetchMore(PRTDIR pThis)
253{
254 Assert(!pThis->fDataUnread);
255
256 /*
257 * Allocate the buffer the first time around.
258 * We do this in lazy fashion as some users of RTDirOpen will not actually
259 * list any files, just open it for various reasons.
260 *
261 * We also reduce the buffer size for networked devices as the windows 7-8.1,
262 * server 2012, ++ CIFS servers or/and IFSes screws up buffers larger than 64KB.
263 * There is an alternative hack below, btw. We'll leave both in for now.
264 */
265 bool fFirst = false;
266 if (!pThis->pabBuffer)
267 {
268 pThis->cbBufferAlloc = _256K;
269 if (true) /** @todo skip for known local devices, like the boot device? */
270 {
271 IO_STATUS_BLOCK Ios2 = RTNT_IO_STATUS_BLOCK_INITIALIZER;
272 FILE_FS_DEVICE_INFORMATION Info = { 0, 0 };
273 NTSTATUS rcNt2 = NtQueryVolumeInformationFile(pThis->hDir, &Ios2, &Info, sizeof(Info), FileFsDeviceInformation);
274 if ( !NT_SUCCESS(rcNt2)
275 || (Info.Characteristics & FILE_REMOTE_DEVICE)
276 || Info.DeviceType == FILE_DEVICE_NETWORK
277 || Info.DeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM
278 || Info.DeviceType == FILE_DEVICE_NETWORK_REDIRECTOR
279 || Info.DeviceType == FILE_DEVICE_SMB)
280 pThis->cbBufferAlloc = _64K;
281 }
282
283 fFirst = false;
284 pThis->pabBuffer = (uint8_t *)RTMemAlloc(pThis->cbBufferAlloc);
285 if (!pThis->pabBuffer)
286 {
287 do
288 {
289 pThis->cbBufferAlloc /= 4;
290 pThis->pabBuffer = (uint8_t *)RTMemAlloc(pThis->cbBufferAlloc);
291 } while (pThis->pabBuffer == NULL && pThis->cbBufferAlloc > _4K);
292 if (!pThis->pabBuffer)
293 return VERR_NO_MEMORY;
294 }
295 }
296
297 /*
298 * Read more.
299 */
300 NTSTATUS rcNt;
301 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
302 if (pThis->enmInfoClass != (FILE_INFORMATION_CLASS)0)
303 {
304#ifdef IPRT_WITH_NT_PATH_PASSTHRU
305 if (pThis->enmInfoClass == FileMaximumInformation)
306 {
307 Ios.Information = 0;
308 Ios.Status = rcNt = NtQueryDirectoryObject(pThis->hDir,
309 pThis->pabBuffer,
310 pThis->cbBufferAlloc,
311 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
312 FALSE /*RestartScan*/,
313 &pThis->uObjDirCtx,
314 (PULONG)&Ios.Information);
315 }
316 else
317#endif
318 rcNt = NtQueryDirectoryFile(pThis->hDir,
319 NULL /* Event */,
320 NULL /* ApcRoutine */,
321 NULL /* ApcContext */,
322 &Ios,
323 pThis->pabBuffer,
324 pThis->cbBufferAlloc,
325 pThis->enmInfoClass,
326 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
327 pThis->pNtFilterStr,
328 FALSE /*RestartScan */);
329 }
330 else
331 {
332 /*
333 * The first time around we have to figure which info class we can use
334 * as well as the right buffer size. We prefer an info class which
335 * gives us file IDs (Vista+ IIRC) and we prefer large buffers (for long
336 * ReFS file names and such), but we'll settle for whatever works...
337 *
338 * The windows 7 thru 8.1 CIFS servers have been observed to have
339 * trouble with large buffers, but weirdly only when listing large
340 * directories. Seems 0x10000 is the max. (Samba does not exhibit
341 * these problems, of course.)
342 *
343 * This complicates things. The buffer size issues causes an
344 * STATUS_INVALID_PARAMETER error. Now, you would expect the lack of
345 * FileIdBothDirectoryInformation support to return
346 * STATUS_INVALID_INFO_CLASS, but I'm not entirely sure if we can 100%
347 * depend on third IFSs to get that right. Nor, am I entirely confident
348 * that we can depend on them to check the class before the buffer size.
349 *
350 * Thus the mess.
351 */
352 if (RT_MAKE_U64(RTNtCurrentPeb()->OSMinorVersion, RTNtCurrentPeb()->OSMajorVersion) > RT_MAKE_U64(0,5) /* > W2K */)
353 pThis->enmInfoClass = FileIdBothDirectoryInformation; /* Introduced in XP, from I can tell. */
354 else
355 pThis->enmInfoClass = FileBothDirectoryInformation;
356 rcNt = NtQueryDirectoryFile(pThis->hDir,
357 NULL /* Event */,
358 NULL /* ApcRoutine */,
359 NULL /* ApcContext */,
360 &Ios,
361 pThis->pabBuffer,
362 pThis->cbBufferAlloc,
363 pThis->enmInfoClass,
364 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
365 pThis->pNtFilterStr,
366 FALSE /*RestartScan */);
367 if (NT_SUCCESS(rcNt))
368 { /* likely */ }
369 else
370 {
371 bool fRestartScan = false;
372 for (unsigned iRetry = 0; iRetry < 2; iRetry++)
373 {
374 if ( rcNt == STATUS_INVALID_INFO_CLASS
375 || rcNt == STATUS_INVALID_PARAMETER_8
376 || iRetry != 0)
377 pThis->enmInfoClass = FileBothDirectoryInformation;
378
379 uint32_t cbBuffer = pThis->cbBufferAlloc;
380 if ( rcNt == STATUS_INVALID_PARAMETER
381 || rcNt == STATUS_INVALID_PARAMETER_7
382 || rcNt == STATUS_INVALID_NETWORK_RESPONSE
383 || iRetry != 0)
384 {
385 cbBuffer = RT_MIN(cbBuffer / 2, _64K);
386 fRestartScan = true;
387 }
388
389 for (;;)
390 {
391 rcNt = NtQueryDirectoryFile(pThis->hDir,
392 NULL /* Event */,
393 NULL /* ApcRoutine */,
394 NULL /* ApcContext */,
395 &Ios,
396 pThis->pabBuffer,
397 cbBuffer,
398 pThis->enmInfoClass,
399 RTDIR_NT_SINGLE_RECORD /*ReturnSingleEntry */,
400 pThis->pNtFilterStr,
401 fRestartScan);
402 if ( NT_SUCCESS(rcNt)
403 || cbBuffer == pThis->cbBufferAlloc
404 || cbBuffer <= sizeof(*pThis->uCurData.pBothId) + sizeof(WCHAR) * 260)
405 break;
406
407 /* Reduce the buffer size agressivly and try again. We fall back to
408 FindFirstFile values for the final lap. This means we'll do 4 rounds
409 with the current initial buffer size (64KB, 8KB, 1KB, 0x278/0x268). */
410 cbBuffer /= 8;
411 if (cbBuffer < 1024)
412 cbBuffer = pThis->enmInfoClass == FileIdBothDirectoryInformation
413 ? sizeof(*pThis->uCurData.pBothId) + sizeof(WCHAR) * 260
414 : sizeof(*pThis->uCurData.pBoth) + sizeof(WCHAR) * 260;
415 }
416 if (NT_SUCCESS(rcNt))
417 {
418 pThis->cbBufferAlloc = cbBuffer;
419 break;
420 }
421 }
422 }
423 }
424 if (!NT_SUCCESS(rcNt))
425 {
426 if (rcNt == STATUS_NO_MORE_FILES || rcNt == STATUS_NO_MORE_ENTRIES)
427 return VERR_NO_MORE_FILES;
428 return RTErrConvertFromNtStatus(rcNt);
429 }
430 Assert(Ios.Information > sizeof(*pThis->uCurData.pBoth));
431
432 /*
433 * Set up the data members.
434 */
435 pThis->uCurData.u = (uintptr_t)pThis->pabBuffer;
436 pThis->cbBuffer = Ios.Information;
437
438 int rc = rtDirNtCheckRecord(pThis);
439 pThis->fDataUnread = RT_SUCCESS(rc);
440
441 return rc;
442}
443
444
445/**
446 * Converts the name from UTF-16 to UTF-8.
447 *
448 * Fortunately, the names are relative to the directory, so we won't have to do
449 * any sweaty path style coversion. :-)
450 *
451 * @returns IPRT status code
452 * @param pThis The directory instance data.
453 * @param cbName The file name length in bytes.
454 * @param pwsName The file name, not terminated.
455 */
456static int rtDirNtConvertName(PRTDIR pThis, uint32_t cbName, PCRTUTF16 pwsName)
457{
458 int rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
459 if (RT_SUCCESS(rc))
460 {
461 if (!pThis->cbNameAlloc)
462 pThis->cbNameAlloc = pThis->cchName + 1;
463 }
464 else if (rc == VERR_BUFFER_OVERFLOW)
465 {
466 RTStrFree(pThis->pszName);
467 pThis->pszName = NULL;
468 pThis->cbNameAlloc = 0;
469
470 rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
471 if (RT_SUCCESS(rc))
472 pThis->cbNameAlloc = pThis->cchName + 1;
473 }
474 Assert(RT_SUCCESS(rc) ? pThis->pszName != NULL : pThis->pszName == NULL);
475 return rc;
476}
477
478
479/**
480 * Converts the name of the current record.
481 *
482 * @returns IPRT status code.
483 * @param pThis The directory instance data.
484 */
485static int rtDirNtConvertCurName(PRTDIR pThis)
486{
487 switch (pThis->enmInfoClass)
488 {
489 case FileIdBothDirectoryInformation:
490 return rtDirNtConvertName(pThis, pThis->uCurData.pBothId->FileNameLength, pThis->uCurData.pBothId->FileName);
491 case FileBothDirectoryInformation:
492 return rtDirNtConvertName(pThis, pThis->uCurData.pBoth->FileNameLength, pThis->uCurData.pBoth->FileName);
493#ifdef IPRT_WITH_NT_PATH_PASSTHRU
494 case FileMaximumInformation:
495 return rtDirNtConvertName(pThis, pThis->uCurData.pObjDir->Name.Length, pThis->uCurData.pObjDir->Name.Buffer);
496#endif
497
498 default:
499 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
500 }
501}
502
503
504RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
505{
506 int rc;
507
508 /*
509 * Validate input.
510 */
511 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
512 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
513 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
514 size_t cbDirEntry = sizeof(*pDirEntry);
515 if (pcbDirEntry)
516 {
517 cbDirEntry = *pcbDirEntry;
518 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
519 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRY, szName[2])),
520 VERR_INVALID_PARAMETER);
521 }
522
523 /*
524 * Fetch data?
525 */
526 if (!pDir->fDataUnread)
527 {
528 rc = rtDirNtFetchMore(pDir);
529 if (RT_FAILURE(rc))
530 return rc;
531 }
532
533 /*
534 * Convert the filename to UTF-8.
535 */
536 rc = rtDirNtConvertCurName(pDir);
537 if (RT_FAILURE(rc))
538 return rc;
539
540 /*
541 * Check if we've got enough space to return the data.
542 */
543 const char *pszName = pDir->pszName;
544 const size_t cchName = pDir->cchName;
545 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRY, szName[1]) + cchName;
546 if (pcbDirEntry)
547 *pcbDirEntry = cbRequired;
548 if (cbRequired > cbDirEntry)
549 return VERR_BUFFER_OVERFLOW;
550
551 /*
552 * Setup the returned data.
553 */
554 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
555 memcpy(pDirEntry->szName, pszName, cchName + 1);
556
557 pDirEntry->INodeId = pDir->enmInfoClass == FileIdBothDirectoryInformation
558 ? pDir->uCurData.pBothId->FileId.QuadPart : 0;
559
560#ifdef IPRT_WITH_NT_PATH_PASSTHRU
561 if (pDir->enmInfoClass != FileMaximumInformation)
562#endif
563 {
564 switch ( pDir->uCurData.pBoth->FileAttributes
565 & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
566 {
567 default:
568 AssertFailed();
569 case 0:
570 pDirEntry->enmType = RTDIRENTRYTYPE_FILE;
571 break;
572
573 case FILE_ATTRIBUTE_DIRECTORY:
574 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
575 break;
576
577 case FILE_ATTRIBUTE_REPARSE_POINT:
578 case FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY:
579 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
580 break;
581 }
582 }
583#ifdef IPRT_WITH_NT_PATH_PASSTHRU
584 else
585 {
586 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
587 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
588 RT_STR_TUPLE("Directory")))
589 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
590 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
591 RT_STR_TUPLE("SymbolicLink")))
592 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
593 }
594#endif
595
596 return rtDirNtAdvanceBuffer(pDir);
597}
598
599
600RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
601 RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
602{
603 int rc;
604
605 /*
606 * Validate input.
607 */
608 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
609 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
610 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
611
612 AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
613 VERR_INVALID_PARAMETER);
614 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
615
616 size_t cbDirEntry = sizeof(*pDirEntry);
617 if (pcbDirEntry)
618 {
619 cbDirEntry = *pcbDirEntry;
620 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
621 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
622 VERR_INVALID_PARAMETER);
623 }
624
625 /*
626 * Fetch data?
627 */
628 if (!pDir->fDataUnread)
629 {
630 rc = rtDirNtFetchMore(pDir);
631 if (RT_FAILURE(rc))
632 return rc;
633 }
634
635 /*
636 * Convert the filename to UTF-8.
637 */
638 rc = rtDirNtConvertCurName(pDir);
639 if (RT_FAILURE(rc))
640 return rc;
641
642 /*
643 * Check if we've got enough space to return the data.
644 */
645 const char *pszName = pDir->pszName;
646 const size_t cchName = pDir->cchName;
647 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
648 if (pcbDirEntry)
649 *pcbDirEntry = cbRequired;
650 if (cbRequired > cbDirEntry)
651 return VERR_BUFFER_OVERFLOW;
652
653 /*
654 * Setup the returned data.
655 */
656 PFILE_BOTH_DIR_INFORMATION pBoth = pDir->uCurData.pBoth;
657
658 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
659 memcpy(pDirEntry->szName, pszName, cchName + 1);
660 memset(pDirEntry->wszShortName, 0, sizeof(pDirEntry->wszShortName));
661#ifdef IPRT_WITH_NT_PATH_PASSTHRU
662 if (pDir->enmInfoClass != FileMaximumInformation)
663#endif
664 {
665 uint8_t cbShort = pBoth->ShortNameLength;
666 if (cbShort > 0)
667 {
668 AssertStmt(cbShort < sizeof(pDirEntry->wszShortName), cbShort = sizeof(pDirEntry->wszShortName) - 2);
669 memcpy(pDirEntry->wszShortName, pBoth->ShortName, cbShort);
670 pDirEntry->cwcShortName = cbShort / 2;
671 }
672 else
673 pDirEntry->cwcShortName = 0;
674
675 pDirEntry->Info.cbObject = pBoth->EndOfFile.QuadPart;
676 pDirEntry->Info.cbAllocated = pBoth->AllocationSize.QuadPart;
677
678 Assert(sizeof(uint64_t) == sizeof(pBoth->CreationTime));
679 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, pBoth->CreationTime.QuadPart);
680 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, pBoth->LastAccessTime.QuadPart);
681 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, pBoth->LastWriteTime.QuadPart);
682 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, pBoth->ChangeTime.QuadPart);
683
684 pDirEntry->Info.Attr.fMode = rtFsModeFromDos((pBoth->FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
685 pszName, cchName);
686 }
687#ifdef IPRT_WITH_NT_PATH_PASSTHRU
688 else
689 {
690 pDirEntry->cwcShortName = 0;
691 pDirEntry->Info.cbObject = 0;
692 pDirEntry->Info.cbAllocated = 0;
693 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, 0);
694 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, 0);
695 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, 0);
696 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, 0);
697
698 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
699 RT_STR_TUPLE("Directory")))
700 pDirEntry->Info.Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
701 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
702 RT_STR_TUPLE("SymbolicLink")))
703 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_REPARSE_POINT | RTFS_TYPE_SYMLINK | 0777;
704 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
705 RT_STR_TUPLE("Device")))
706 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_DEVICE | RTFS_TYPE_DEV_CHAR | 0666;
707 else
708 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | 0666;
709 }
710#endif
711
712 /*
713 * Requested attributes (we cannot provide anything actually).
714 */
715 switch (enmAdditionalAttribs)
716 {
717 case RTFSOBJATTRADD_EASIZE:
718 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
719#ifdef IPRT_WITH_NT_PATH_PASSTHRU
720 if (pDir->enmInfoClass == FileMaximumInformation)
721 pDirEntry->Info.Attr.u.EASize.cb = 0;
722 else
723#endif
724 pDirEntry->Info.Attr.u.EASize.cb = pBoth->EaSize;
725 break;
726
727 case RTFSOBJATTRADD_UNIX:
728 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
729 pDirEntry->Info.Attr.u.Unix.uid = ~0U;
730 pDirEntry->Info.Attr.u.Unix.gid = ~0U;
731 pDirEntry->Info.Attr.u.Unix.cHardlinks = 1;
732 pDirEntry->Info.Attr.u.Unix.INodeIdDevice = 0; /** @todo Use the volume serial number (see GetFileInformationByHandle). */
733 pDirEntry->Info.Attr.u.Unix.INodeId = 0; /** @todo Use the fileid (see GetFileInformationByHandle). */
734 pDirEntry->Info.Attr.u.Unix.fFlags = 0;
735 pDirEntry->Info.Attr.u.Unix.GenerationId = 0;
736 pDirEntry->Info.Attr.u.Unix.Device = 0;
737 break;
738
739 case RTFSOBJATTRADD_NOTHING:
740 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
741 break;
742
743 case RTFSOBJATTRADD_UNIX_OWNER:
744 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
745 pDirEntry->Info.Attr.u.UnixOwner.uid = ~0U;
746 pDirEntry->Info.Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
747 break;
748
749 case RTFSOBJATTRADD_UNIX_GROUP:
750 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
751 pDirEntry->Info.Attr.u.UnixGroup.gid = ~0U;
752 pDirEntry->Info.Attr.u.UnixGroup.szName[0] = '\0';
753 break;
754
755 default:
756 AssertMsgFailed(("Impossible!\n"));
757 return VERR_INTERNAL_ERROR;
758 }
759
760 /*
761 * Follow links if requested.
762 */
763 if ( (fFlags & RTPATH_F_FOLLOW_LINK)
764 && RTFS_IS_SYMLINK(fFlags))
765 {
766 /** @todo Symlinks: Find[First|Next]FileW will return info about
767 the link, so RTPATH_F_FOLLOW_LINK is not handled correctly. */
768 }
769
770 /*
771 * Finally advance the buffer.
772 */
773 return rtDirNtAdvanceBuffer(pDir);
774}
775
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