VirtualBox

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

Last change on this file since 62514 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 29.3 KB
Line 
1/* $Id: direnum-r3-nt.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Directory Enumeration, Native NT.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 /* Note! VBoxSVR and CIFS file systems both ends up with STATUS_NO_SUCH_FILE here instead of STATUS_NO_MORE_FILES. */
427 if (rcNt == STATUS_NO_MORE_FILES || rcNt == STATUS_NO_MORE_ENTRIES || rcNt == STATUS_NO_SUCH_FILE)
428 return VERR_NO_MORE_FILES;
429 return RTErrConvertFromNtStatus(rcNt);
430 }
431 Assert(Ios.Information > sizeof(*pThis->uCurData.pBoth));
432
433 /*
434 * Set up the data members.
435 */
436 pThis->uCurData.u = (uintptr_t)pThis->pabBuffer;
437 pThis->cbBuffer = Ios.Information;
438
439 int rc = rtDirNtCheckRecord(pThis);
440 pThis->fDataUnread = RT_SUCCESS(rc);
441
442 return rc;
443}
444
445
446/**
447 * Converts the name from UTF-16 to UTF-8.
448 *
449 * Fortunately, the names are relative to the directory, so we won't have to do
450 * any sweaty path style coversion. :-)
451 *
452 * @returns IPRT status code
453 * @param pThis The directory instance data.
454 * @param cbName The file name length in bytes.
455 * @param pwsName The file name, not terminated.
456 */
457static int rtDirNtConvertName(PRTDIR pThis, uint32_t cbName, PCRTUTF16 pwsName)
458{
459 int rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
460 if (RT_SUCCESS(rc))
461 {
462 if (!pThis->cbNameAlloc)
463 pThis->cbNameAlloc = pThis->cchName + 1;
464 }
465 else if (rc == VERR_BUFFER_OVERFLOW)
466 {
467 RTStrFree(pThis->pszName);
468 pThis->pszName = NULL;
469 pThis->cbNameAlloc = 0;
470
471 rc = RTUtf16ToUtf8Ex(pwsName, cbName / 2, &pThis->pszName, pThis->cbNameAlloc, &pThis->cchName);
472 if (RT_SUCCESS(rc))
473 pThis->cbNameAlloc = pThis->cchName + 1;
474 }
475 Assert(RT_SUCCESS(rc) ? pThis->pszName != NULL : pThis->pszName == NULL);
476 return rc;
477}
478
479
480/**
481 * Converts the name of the current record.
482 *
483 * @returns IPRT status code.
484 * @param pThis The directory instance data.
485 */
486static int rtDirNtConvertCurName(PRTDIR pThis)
487{
488 switch (pThis->enmInfoClass)
489 {
490 case FileIdBothDirectoryInformation:
491 return rtDirNtConvertName(pThis, pThis->uCurData.pBothId->FileNameLength, pThis->uCurData.pBothId->FileName);
492 case FileBothDirectoryInformation:
493 return rtDirNtConvertName(pThis, pThis->uCurData.pBoth->FileNameLength, pThis->uCurData.pBoth->FileName);
494#ifdef IPRT_WITH_NT_PATH_PASSTHRU
495 case FileMaximumInformation:
496 return rtDirNtConvertName(pThis, pThis->uCurData.pObjDir->Name.Length, pThis->uCurData.pObjDir->Name.Buffer);
497#endif
498
499 default:
500 AssertFailedReturn(VERR_INTERNAL_ERROR_3);
501 }
502}
503
504
505RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
506{
507 int rc;
508
509 /*
510 * Validate input.
511 */
512 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
513 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
514 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
515 size_t cbDirEntry = sizeof(*pDirEntry);
516 if (pcbDirEntry)
517 {
518 cbDirEntry = *pcbDirEntry;
519 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
520 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRY, szName[2])),
521 VERR_INVALID_PARAMETER);
522 }
523
524 /*
525 * Fetch data?
526 */
527 if (!pDir->fDataUnread)
528 {
529 rc = rtDirNtFetchMore(pDir);
530 if (RT_FAILURE(rc))
531 return rc;
532 }
533
534 /*
535 * Convert the filename to UTF-8.
536 */
537 rc = rtDirNtConvertCurName(pDir);
538 if (RT_FAILURE(rc))
539 return rc;
540
541 /*
542 * Check if we've got enough space to return the data.
543 */
544 const char *pszName = pDir->pszName;
545 const size_t cchName = pDir->cchName;
546 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRY, szName[1]) + cchName;
547 if (pcbDirEntry)
548 *pcbDirEntry = cbRequired;
549 if (cbRequired > cbDirEntry)
550 return VERR_BUFFER_OVERFLOW;
551
552 /*
553 * Setup the returned data.
554 */
555 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
556 memcpy(pDirEntry->szName, pszName, cchName + 1);
557
558 pDirEntry->INodeId = pDir->enmInfoClass == FileIdBothDirectoryInformation
559 ? pDir->uCurData.pBothId->FileId.QuadPart : 0;
560
561#ifdef IPRT_WITH_NT_PATH_PASSTHRU
562 if (pDir->enmInfoClass != FileMaximumInformation)
563#endif
564 {
565 switch ( pDir->uCurData.pBoth->FileAttributes
566 & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
567 {
568 default:
569 AssertFailed();
570 case 0:
571 pDirEntry->enmType = RTDIRENTRYTYPE_FILE;
572 break;
573
574 case FILE_ATTRIBUTE_DIRECTORY:
575 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
576 break;
577
578 case FILE_ATTRIBUTE_REPARSE_POINT:
579 case FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY:
580 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
581 break;
582 }
583 }
584#ifdef IPRT_WITH_NT_PATH_PASSTHRU
585 else
586 {
587 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
588 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
589 RT_STR_TUPLE("Directory")))
590 pDirEntry->enmType = RTDIRENTRYTYPE_DIRECTORY;
591 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
592 RT_STR_TUPLE("SymbolicLink")))
593 pDirEntry->enmType = RTDIRENTRYTYPE_SYMLINK;
594 }
595#endif
596
597 return rtDirNtAdvanceBuffer(pDir);
598}
599
600
601RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
602 RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
603{
604 int rc;
605
606 /*
607 * Validate input.
608 */
609 AssertPtrReturn(pDir, VERR_INVALID_POINTER);
610 AssertReturn(pDir->u32Magic == RTDIR_MAGIC, VERR_INVALID_HANDLE);
611 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
612
613 AssertReturn(enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
614 VERR_INVALID_PARAMETER);
615 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
616
617 size_t cbDirEntry = sizeof(*pDirEntry);
618 if (pcbDirEntry)
619 {
620 cbDirEntry = *pcbDirEntry;
621 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
622 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
623 VERR_INVALID_PARAMETER);
624 }
625
626 /*
627 * Fetch data?
628 */
629 if (!pDir->fDataUnread)
630 {
631 rc = rtDirNtFetchMore(pDir);
632 if (RT_FAILURE(rc))
633 return rc;
634 }
635
636 /*
637 * Convert the filename to UTF-8.
638 */
639 rc = rtDirNtConvertCurName(pDir);
640 if (RT_FAILURE(rc))
641 return rc;
642
643 /*
644 * Check if we've got enough space to return the data.
645 */
646 const char *pszName = pDir->pszName;
647 const size_t cchName = pDir->cchName;
648 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
649 if (pcbDirEntry)
650 *pcbDirEntry = cbRequired;
651 if (cbRequired > cbDirEntry)
652 return VERR_BUFFER_OVERFLOW;
653
654 /*
655 * Setup the returned data.
656 */
657 PFILE_BOTH_DIR_INFORMATION pBoth = pDir->uCurData.pBoth;
658
659 pDirEntry->cbName = (uint16_t)cchName; Assert(pDirEntry->cbName == cchName);
660 memcpy(pDirEntry->szName, pszName, cchName + 1);
661 memset(pDirEntry->wszShortName, 0, sizeof(pDirEntry->wszShortName));
662#ifdef IPRT_WITH_NT_PATH_PASSTHRU
663 if (pDir->enmInfoClass != FileMaximumInformation)
664#endif
665 {
666 uint8_t cbShort = pBoth->ShortNameLength;
667 if (cbShort > 0)
668 {
669 AssertStmt(cbShort < sizeof(pDirEntry->wszShortName), cbShort = sizeof(pDirEntry->wszShortName) - 2);
670 memcpy(pDirEntry->wszShortName, pBoth->ShortName, cbShort);
671 pDirEntry->cwcShortName = cbShort / 2;
672 }
673 else
674 pDirEntry->cwcShortName = 0;
675
676 pDirEntry->Info.cbObject = pBoth->EndOfFile.QuadPart;
677 pDirEntry->Info.cbAllocated = pBoth->AllocationSize.QuadPart;
678
679 Assert(sizeof(uint64_t) == sizeof(pBoth->CreationTime));
680 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, pBoth->CreationTime.QuadPart);
681 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, pBoth->LastAccessTime.QuadPart);
682 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, pBoth->LastWriteTime.QuadPart);
683 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, pBoth->ChangeTime.QuadPart);
684
685 pDirEntry->Info.Attr.fMode = rtFsModeFromDos((pBoth->FileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
686 pszName, cchName);
687 }
688#ifdef IPRT_WITH_NT_PATH_PASSTHRU
689 else
690 {
691 pDirEntry->cwcShortName = 0;
692 pDirEntry->Info.cbObject = 0;
693 pDirEntry->Info.cbAllocated = 0;
694 RTTimeSpecSetNtTime(&pDirEntry->Info.BirthTime, 0);
695 RTTimeSpecSetNtTime(&pDirEntry->Info.AccessTime, 0);
696 RTTimeSpecSetNtTime(&pDirEntry->Info.ModificationTime, 0);
697 RTTimeSpecSetNtTime(&pDirEntry->Info.ChangeTime, 0);
698
699 if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
700 RT_STR_TUPLE("Directory")))
701 pDirEntry->Info.Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY | 0777;
702 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
703 RT_STR_TUPLE("SymbolicLink")))
704 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_REPARSE_POINT | RTFS_TYPE_SYMLINK | 0777;
705 else if (rtNtCompWideStrAndAscii(pDir->uCurData.pObjDir->TypeName.Buffer, pDir->uCurData.pObjDir->TypeName.Length,
706 RT_STR_TUPLE("Device")))
707 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_DEVICE | RTFS_TYPE_DEV_CHAR | 0666;
708 else
709 pDirEntry->Info.Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | 0666;
710 }
711#endif
712
713 /*
714 * Requested attributes (we cannot provide anything actually).
715 */
716 switch (enmAdditionalAttribs)
717 {
718 case RTFSOBJATTRADD_EASIZE:
719 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
720#ifdef IPRT_WITH_NT_PATH_PASSTHRU
721 if (pDir->enmInfoClass == FileMaximumInformation)
722 pDirEntry->Info.Attr.u.EASize.cb = 0;
723 else
724#endif
725 pDirEntry->Info.Attr.u.EASize.cb = pBoth->EaSize;
726 break;
727
728 case RTFSOBJATTRADD_UNIX:
729 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
730 pDirEntry->Info.Attr.u.Unix.uid = ~0U;
731 pDirEntry->Info.Attr.u.Unix.gid = ~0U;
732 pDirEntry->Info.Attr.u.Unix.cHardlinks = 1;
733 pDirEntry->Info.Attr.u.Unix.INodeIdDevice = 0; /** @todo Use the volume serial number (see GetFileInformationByHandle). */
734 pDirEntry->Info.Attr.u.Unix.INodeId = 0; /** @todo Use the fileid (see GetFileInformationByHandle). */
735 pDirEntry->Info.Attr.u.Unix.fFlags = 0;
736 pDirEntry->Info.Attr.u.Unix.GenerationId = 0;
737 pDirEntry->Info.Attr.u.Unix.Device = 0;
738 break;
739
740 case RTFSOBJATTRADD_NOTHING:
741 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
742 break;
743
744 case RTFSOBJATTRADD_UNIX_OWNER:
745 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
746 pDirEntry->Info.Attr.u.UnixOwner.uid = ~0U;
747 pDirEntry->Info.Attr.u.UnixOwner.szName[0] = '\0'; /** @todo return something sensible here. */
748 break;
749
750 case RTFSOBJATTRADD_UNIX_GROUP:
751 pDirEntry->Info.Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
752 pDirEntry->Info.Attr.u.UnixGroup.gid = ~0U;
753 pDirEntry->Info.Attr.u.UnixGroup.szName[0] = '\0';
754 break;
755
756 default:
757 AssertMsgFailed(("Impossible!\n"));
758 return VERR_INTERNAL_ERROR;
759 }
760
761 /*
762 * Follow links if requested.
763 */
764 if ( (fFlags & RTPATH_F_FOLLOW_LINK)
765 && RTFS_IS_SYMLINK(fFlags))
766 {
767 /** @todo Symlinks: Find[First|Next]FileW will return info about
768 the link, so RTPATH_F_FOLLOW_LINK is not handled correctly. */
769 }
770
771 /*
772 * Finally advance the buffer.
773 */
774 return rtDirNtAdvanceBuffer(pDir);
775}
776
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