VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/dir.cpp@ 78185

Last change on this file since 78185 was 78184, checked in by vboxsync, 6 years ago

IPRT/RTPathGetCurrent/win: Upper case the drive letter so it is consistent with RTPathAbsEx. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 27.2 KB
Line 
1/* $Id: dir.cpp 78184 2019-04-17 20:26:41Z vboxsync $ */
2/** @file
3 * IPRT - Directory Manipulation, Part 1.
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 <iprt/dir.h>
33#include "internal/iprt.h"
34
35#include <iprt/alloca.h>
36#include <iprt/assert.h>
37#include <iprt/file.h>
38#include <iprt/err.h>
39#include <iprt/log.h>
40#include <iprt/mem.h>
41#include <iprt/param.h>
42#include <iprt/path.h>
43#include <iprt/string.h>
44#include <iprt/uni.h>
45#define RTDIR_AGNOSTIC
46#include "internal/dir.h"
47#include "internal/path.h"
48
49
50static DECLCALLBACK(bool) rtDirFilterWinNtMatch(PRTDIRINTERNAL pDir, const char *pszName);
51static DECLCALLBACK(bool) rtDirFilterWinNtMatchNoWildcards(PRTDIRINTERNAL pDir, const char *pszName);
52DECLINLINE(bool) rtDirFilterWinNtMatchEon(PCRTUNICP puszFilter);
53static bool rtDirFilterWinNtMatchDosStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter);
54static bool rtDirFilterWinNtMatchStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter);
55static bool rtDirFilterWinNtMatchBase(unsigned iDepth, const char *pszName, PCRTUNICP puszFilter);
56
57
58
59RTDECL(int) RTDirCreateFullPath(const char *pszPath, RTFMODE fMode)
60{
61 /*
62 * Resolve the path.
63 */
64 char szAbsPath[RTPATH_MAX];
65 int rc = RTPathAbs(pszPath, szAbsPath, sizeof(szAbsPath));
66 if (RT_FAILURE(rc))
67 return rc;
68
69 /*
70 * Iterate the path components making sure each of them exists.
71 */
72 /* skip volume name */
73 char *psz = &szAbsPath[rtPathVolumeSpecLen(szAbsPath)];
74
75 /* skip the root slash if any */
76 if ( psz[0] == '/'
77#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
78 || psz[0] == '\\'
79#endif
80 )
81 psz++;
82
83 /* iterate over path components. */
84 do
85 {
86 /* the next component is NULL, stop iterating */
87 if (!*psz)
88 break;
89#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
90 psz = strpbrk(psz, "\\/");
91#else
92 psz = strchr(psz, '/');
93#endif
94 if (psz)
95 *psz = '\0';
96 /*
97 * ASSUME that RTDirCreate will return VERR_ALREADY_EXISTS and not VERR_ACCESS_DENIED in those cases
98 * where the directory exists but we don't have write access to the parent directory.
99 */
100 rc = RTDirCreate(szAbsPath, fMode, 0);
101 if (rc == VERR_ALREADY_EXISTS)
102 rc = VINF_SUCCESS;
103 if (!psz)
104 break;
105 *psz++ = RTPATH_DELIMITER;
106 } while (RT_SUCCESS(rc));
107
108 return rc;
109}
110
111
112/**
113 * Filter a the filename in the against a filter.
114 *
115 * @returns true if the name matches the filter.
116 * @returns false if the name doesn't match filter.
117 * @param pDir The directory handle.
118 * @param pszName The path to match to the filter.
119 */
120static DECLCALLBACK(bool) rtDirFilterWinNtMatchNoWildcards(PRTDIRINTERNAL pDir, const char *pszName)
121{
122 /*
123 * Walk the string and compare.
124 */
125 PCRTUNICP pucFilter = pDir->puszFilter;
126 const char *psz = pszName;
127 RTUNICP uc;
128 do
129 {
130 int rc = RTStrGetCpEx(&psz, &uc);
131 AssertRCReturn(rc, false);
132 RTUNICP ucFilter = *pucFilter++;
133 if ( uc != ucFilter
134 && RTUniCpToUpper(uc) != ucFilter)
135 return false;
136 } while (uc);
137 return true;
138}
139
140
141/**
142 * Matches end of name.
143 */
144DECLINLINE(bool) rtDirFilterWinNtMatchEon(PCRTUNICP puszFilter)
145{
146 RTUNICP ucFilter;
147 while ( (ucFilter = *puszFilter) == '>'
148 || ucFilter == '<'
149 || ucFilter == '*'
150 || ucFilter == '"')
151 puszFilter++;
152 return !ucFilter;
153}
154
155
156/**
157 * Recursive star matching.
158 * Practically the same as normal star, except that the dos star stops
159 * when hitting the last dot.
160 *
161 * @returns true on match.
162 * @returns false on miss.
163 */
164static bool rtDirFilterWinNtMatchDosStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter)
165{
166 AssertReturn(iDepth++ < 256, false);
167
168 /*
169 * If there is no dos star, we should work just like the NT star.
170 * Since that's generally faster algorithms, we jump down to there if we can.
171 */
172 const char *pszDosDot = strrchr(pszNext, '.');
173 if (!pszDosDot && uc == '.')
174 pszDosDot = pszNext - 1;
175 if (!pszDosDot)
176 return rtDirFilterWinNtMatchStar(iDepth, uc, pszNext, puszFilter);
177
178 /*
179 * Inspect the next filter char(s) until we find something to work on.
180 */
181 RTUNICP ucFilter = *puszFilter++;
182 switch (ucFilter)
183 {
184 /*
185 * The star expression is the last in the pattern.
186 * We're fine if the name ends with a dot.
187 */
188 case '\0':
189 return !pszDosDot[1];
190
191 /*
192 * Simplified by brute force.
193 */
194 case '>': /* dos question mark */
195 case '?':
196 case '*':
197 case '<': /* dos star */
198 case '"': /* dos dot */
199 {
200 puszFilter--;
201 const char *pszStart = pszNext;
202 do
203 {
204 if (rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter))
205 return true;
206 int rc = RTStrGetCpEx(&pszNext, &uc); AssertRCReturn(rc, false);
207 } while ((intptr_t)pszDosDot - (intptr_t)pszNext >= -1);
208
209 /* backtrack and do the current char. */
210 pszNext = RTStrPrevCp(NULL, pszStart); AssertReturn(pszNext, false);
211 return rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter);
212 }
213
214 /*
215 * Ok, we've got zero or more characters.
216 * We'll try match starting at each occurrence of this character.
217 */
218 default:
219 {
220 if ( RTUniCpToUpper(uc) == ucFilter
221 && rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter))
222 return true;
223 do
224 {
225 int rc = RTStrGetCpEx(&pszNext, &uc); AssertRCReturn(rc, false);
226 if ( RTUniCpToUpper(uc) == ucFilter
227 && rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter))
228 return true;
229 } while ((intptr_t)pszDosDot - (intptr_t)pszNext >= -1);
230 return false;
231 }
232 }
233 /* won't ever get here! */
234}
235
236
237/**
238 * Recursive star matching.
239 *
240 * @returns true on match.
241 * @returns false on miss.
242 */
243static bool rtDirFilterWinNtMatchStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter)
244{
245 AssertReturn(iDepth++ < 256, false);
246
247 /*
248 * Inspect the next filter char(s) until we find something to work on.
249 */
250 for (;;)
251 {
252 RTUNICP ucFilter = *puszFilter++;
253 switch (ucFilter)
254 {
255 /*
256 * The star expression is the last in the pattern.
257 * Cool, that means we're done!
258 */
259 case '\0':
260 return true;
261
262 /*
263 * Just in case (doubt we ever get here), just merge it with the current one.
264 */
265 case '*':
266 break;
267
268 /*
269 * Skip a fixed number of chars.
270 * Figure out how many by walking the filter ignoring '*'s.
271 */
272 case '?':
273 {
274 unsigned cQms = 1;
275 while ((ucFilter = *puszFilter) == '*' || ucFilter == '?')
276 {
277 cQms += ucFilter == '?';
278 puszFilter++;
279 }
280 do
281 {
282 if (!uc)
283 return false;
284 int rc = RTStrGetCpEx(&pszNext, &uc); AssertRCReturn(rc, false);
285 } while (--cQms > 0);
286 /* done? */
287 if (!ucFilter)
288 return true;
289 break;
290 }
291
292 /*
293 * The simple way is to try char by char and match the remaining
294 * expression. If it's trailing we're done.
295 */
296 case '>': /* dos question mark */
297 {
298 if (rtDirFilterWinNtMatchEon(puszFilter))
299 return true;
300 const char *pszStart = pszNext;
301 do
302 {
303 if (rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter))
304 return true;
305 int rc = RTStrGetCpEx(&pszNext, &uc); AssertRCReturn(rc, false);
306 } while (uc);
307
308 /* backtrack and do the current char. */
309 pszNext = RTStrPrevCp(NULL, pszStart); AssertReturn(pszNext, false);
310 return rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter);
311 }
312
313 /*
314 * This bugger is interesting.
315 * Time for brute force. Iterate the name char by char.
316 */
317 case '<':
318 {
319 do
320 {
321 if (rtDirFilterWinNtMatchDosStar(iDepth, uc, pszNext, puszFilter))
322 return true;
323 int rc = RTStrGetCpEx(&pszNext, &uc); AssertRCReturn(rc, false);
324 } while (uc);
325 return false;
326 }
327
328 /*
329 * This guy matches a '.' or the end of the name.
330 * It's very simple if the rest of the filter expression also matches eon.
331 */
332 case '"':
333 if (rtDirFilterWinNtMatchEon(puszFilter))
334 return true;
335 ucFilter = '.';
336 RT_FALL_THRU();
337
338 /*
339 * Ok, we've got zero or more characters.
340 * We'll try match starting at each occurrence of this character.
341 */
342 default:
343 {
344 do
345 {
346 if ( RTUniCpToUpper(uc) == ucFilter
347 && rtDirFilterWinNtMatchBase(iDepth, pszNext, puszFilter))
348 return true;
349 int rc = RTStrGetCpEx(&pszNext, &uc); AssertRCReturn(rc, false);
350 } while (uc);
351 return false;
352 }
353 }
354 } /* for (;;) */
355
356 /* won't ever get here! */
357}
358
359
360/**
361 * Filter a the filename in the against a filter.
362 *
363 * The rules are as follows:
364 * '?' Matches exactly one char.
365 * '*' Matches zero or more chars.
366 * '<' The dos star, matches zero or more chars except the DOS dot.
367 * '>' The dos question mark, matches one char, but dots and end-of-name eats them.
368 * '"' The dos dot, matches a dot or end-of-name.
369 *
370 * @returns true if the name matches the filter.
371 * @returns false if the name doesn't match filter.
372 * @param iDepth The recursion depth.
373 * @param pszName The path to match to the filter.
374 * @param puszFilter The filter string.
375 */
376static bool rtDirFilterWinNtMatchBase(unsigned iDepth, const char *pszName, PCRTUNICP puszFilter)
377{
378 AssertReturn(iDepth++ < 256, false);
379
380 /*
381 * Walk the string and match it up char by char.
382 */
383 RTUNICP uc;
384 do
385 {
386 RTUNICP ucFilter = *puszFilter++;
387 int rc = RTStrGetCpEx(&pszName, &uc); AssertRCReturn(rc, false);
388 switch (ucFilter)
389 {
390 /* Exactly one char. */
391 case '?':
392 if (!uc)
393 return false;
394 break;
395
396 /* One char, but the dos dot and end-of-name eats '>' and '<'. */
397 case '>': /* dos ? */
398 if (!uc)
399 return rtDirFilterWinNtMatchEon(puszFilter);
400 if (uc == '.')
401 {
402 while ((ucFilter = *puszFilter) == '>' || ucFilter == '<')
403 puszFilter++;
404 if (ucFilter == '"' || ucFilter == '.') /* not 100% sure about the last dot */
405 ++puszFilter;
406 else /* the does question mark doesn't match '.'s, so backtrack. */
407 pszName = RTStrPrevCp(NULL, pszName);
408 }
409 break;
410
411 /* Match a dot or the end-of-name. */
412 case '"': /* dos '.' */
413 if (uc != '.')
414 {
415 if (uc)
416 return false;
417 return rtDirFilterWinNtMatchEon(puszFilter);
418 }
419 break;
420
421 /* zero or more */
422 case '*':
423 return rtDirFilterWinNtMatchStar(iDepth, uc, pszName, puszFilter);
424 case '<': /* dos '*' */
425 return rtDirFilterWinNtMatchDosStar(iDepth, uc, pszName, puszFilter);
426
427
428 /* uppercased match */
429 default:
430 {
431 if (RTUniCpToUpper(uc) != ucFilter)
432 return false;
433 break;
434 }
435 }
436 } while (uc);
437
438 return true;
439}
440
441
442/**
443 * Filter a the filename in the against a filter.
444 *
445 * @returns true if the name matches the filter.
446 * @returns false if the name doesn't match filter.
447 * @param pDir The directory handle.
448 * @param pszName The path to match to the filter.
449 */
450static DECLCALLBACK(bool) rtDirFilterWinNtMatch(PRTDIRINTERNAL pDir, const char *pszName)
451{
452 return rtDirFilterWinNtMatchBase(0, pszName, pDir->puszFilter);
453}
454
455
456/**
457 * Initializes a WinNt like wildcard filter.
458 *
459 * @returns Pointer to the filter function.
460 * @returns NULL if the filter doesn't filter out anything.
461 * @param pDir The directory handle (not yet opened).
462 */
463static PFNRTDIRFILTER rtDirFilterWinNtInit(PRTDIRINTERNAL pDir)
464{
465 /*
466 * Check for the usual * and <"< (*.* in DOS language) patterns.
467 */
468 if ( (pDir->cchFilter == 1 && pDir->pszFilter[0] == '*')
469 || (pDir->cchFilter == 3 && !memcmp(pDir->pszFilter, "<\".>", 3))
470 )
471 return NULL;
472
473 /*
474 * Uppercase the expression, also do a little optimizations when possible.
475 */
476 bool fHaveWildcards = false;
477 unsigned iRead = 0;
478 unsigned iWrite = 0;
479 while (iRead < pDir->cucFilter)
480 {
481 RTUNICP uc = pDir->puszFilter[iRead++];
482 if (uc == '*')
483 {
484 fHaveWildcards = true;
485 /* remove extra stars. */
486 RTUNICP uc2;
487 while ((uc2 = pDir->puszFilter[iRead + 1]) == '*')
488 iRead++;
489 }
490 else if (uc == '?' || uc == '>' || uc == '<' || uc == '"')
491 fHaveWildcards = true;
492 else
493 uc = RTUniCpToUpper(uc);
494 pDir->puszFilter[iWrite++] = uc;
495 }
496 pDir->puszFilter[iWrite] = 0;
497 pDir->cucFilter = iWrite;
498
499 return fHaveWildcards
500 ? rtDirFilterWinNtMatch
501 : rtDirFilterWinNtMatchNoWildcards;
502}
503
504
505/**
506 * Common worker for opening a directory.
507 *
508 * @returns IPRT status code.
509 * @param phDir Where to store the directory handle.
510 * @param pszPath The specified path.
511 * @param pszFilter Pointer to where the filter start in the path.
512 * NULL if no filter.
513 * @param enmFilter The type of filter to apply.
514 * @param fFlags RTDIR_F_XXX.
515 * @param hRelativeDir The directory @a pvNativeRelative is relative
516 * to, ~(uintptr_t)0 if absolute.
517 * @param pvNativeRelative The native relative path. NULL if absolute or
518 * we're to use (consume) hRelativeDir.
519 */
520static int rtDirOpenCommon(RTDIR *phDir, const char *pszPath, const char *pszFilter, RTDIRFILTER enmFilter,
521 uint32_t fFlags, uintptr_t hRelativeDir, void *pvNativeRelative)
522{
523 /*
524 * Expand the path.
525 *
526 * The purpose of this exercise to have the abs path around
527 * for querying extra information about the objects we list.
528 * As a sideeffect we also validate the path here.
529 */
530 char *pszAbsPath;
531 size_t cbFilter; /* includes '\0' (thus cb and not cch). */
532 size_t cucFilter0; /* includes U+0. */
533 bool fDirSlash = false;
534 if (!pszFilter)
535 {
536 if (*pszPath != '\0')
537 {
538 const char *pszLast = strchr(pszPath, '\0') - 1;
539 if (RTPATH_IS_SLASH(*pszLast))
540 fDirSlash = true;
541 }
542
543 cbFilter = cucFilter0 = 0;
544 pszAbsPath = RTPathAbsExDup(NULL, pszPath, RTPATHABS_F_ENSURE_TRAILING_SLASH);
545 }
546 else
547 {
548 cbFilter = strlen(pszFilter) + 1;
549 cucFilter0 = RTStrUniLen(pszFilter) + 1;
550
551 if (pszFilter != pszPath)
552 {
553 /* yea, I'm lazy. sue me. */
554 char *pszTmp = RTStrDup(pszPath);
555 if (!pszTmp)
556 return VERR_NO_MEMORY;
557 pszTmp[pszFilter - pszPath] = '\0';
558 pszAbsPath = RTPathAbsExDup(NULL, pszTmp, RTPATHABS_F_ENSURE_TRAILING_SLASH);
559 RTStrFree(pszTmp);
560 }
561 else
562 pszAbsPath = RTPathAbsExDup(NULL, ".", RTPATHABS_F_ENSURE_TRAILING_SLASH);
563 fDirSlash = true;
564 }
565 if (!pszAbsPath)
566 return VERR_NO_MEMORY;
567 Assert(strchr(pszAbsPath, '\0')[-1] == RTPATH_SLASH);
568
569 /*
570 * Allocate and initialize the directory handle.
571 *
572 * The posix definition of Data.d_name allows it to be < NAME_MAX + 1,
573 * thus the horrible ugliness here. Solaris uses d_name[1] for instance.
574 */
575 size_t const cchAbsPath = strlen(pszAbsPath);
576 size_t const cbDir = rtDirNativeGetStructSize(pszAbsPath);
577 size_t const cbAllocated = cbDir
578 + cucFilter0 * sizeof(RTUNICP)
579 + cbFilter
580 + cchAbsPath + 1 + 4;
581 PRTDIRINTERNAL pDir = (PRTDIRINTERNAL)RTMemAllocZ(cbAllocated);
582 if (!pDir)
583 {
584 RTStrFree(pszAbsPath);
585 return VERR_NO_MEMORY;
586 }
587 uint8_t *pb = (uint8_t *)pDir + cbDir;
588
589 /* initialize it */
590 pDir->u32Magic = RTDIR_MAGIC;
591 pDir->cbSelf = cbDir;
592 if (cbFilter)
593 {
594 pDir->puszFilter = (PRTUNICP)pb;
595 int rc2 = RTStrToUniEx(pszFilter, RTSTR_MAX, &pDir->puszFilter, cucFilter0, &pDir->cucFilter);
596 AssertRC(rc2);
597 pb += cucFilter0 * sizeof(RTUNICP);
598 pDir->pszFilter = (char *)memcpy(pb, pszFilter, cbFilter);
599 pDir->cchFilter = cbFilter - 1;
600 pb += cbFilter;
601 }
602 else
603 {
604 pDir->puszFilter = NULL;
605 pDir->cucFilter = 0;
606 pDir->pszFilter = NULL;
607 pDir->cchFilter = 0;
608 }
609 pDir->enmFilter = enmFilter;
610 switch (enmFilter)
611 {
612 default:
613 case RTDIRFILTER_NONE:
614 pDir->pfnFilter = NULL;
615 break;
616 case RTDIRFILTER_WINNT:
617 pDir->pfnFilter = rtDirFilterWinNtInit(pDir);
618 break;
619 case RTDIRFILTER_UNIX:
620 pDir->pfnFilter = NULL;
621 break;
622 case RTDIRFILTER_UNIX_UPCASED:
623 pDir->pfnFilter = NULL;
624 break;
625 }
626 pDir->cchPath = cchAbsPath;
627 pDir->pszPath = (char *)memcpy(pb, pszAbsPath, cchAbsPath);
628 pb[cchAbsPath] = '\0';
629 Assert(pb - (uint8_t *)pDir + cchAbsPath + 1 <= cbAllocated);
630 pDir->pszName = NULL;
631 pDir->cchName = 0;
632 pDir->fFlags = fFlags;
633 pDir->fDirSlash = fDirSlash;
634 pDir->fDataUnread = false;
635
636 /*
637 * Hand it over to the native part.
638 */
639 int rc = rtDirNativeOpen(pDir, hRelativeDir, pvNativeRelative);
640 if (RT_SUCCESS(rc))
641 *phDir = pDir;
642 else
643 RTMemFree(pDir);
644 RTStrFree(pszAbsPath);
645 return rc;
646}
647
648
649RTDECL(int) RTDirOpen(RTDIR *phDir, const char *pszPath)
650{
651 /*
652 * Validate input.
653 */
654 AssertMsgReturn(VALID_PTR(phDir), ("%p\n", phDir), VERR_INVALID_POINTER);
655 AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
656
657 /*
658 * Take common cause with RTDirOpenFiltered().
659 */
660 int rc = rtDirOpenCommon(phDir, pszPath, NULL, RTDIRFILTER_NONE, 0 /*fFlags*/, ~(uintptr_t)0, NULL);
661 LogFlow(("RTDirOpen(%p:{%p}, %p:{%s}): return %Rrc\n", phDir, *phDir, pszPath, pszPath, rc));
662 return rc;
663}
664
665
666DECLHIDDEN(int) rtDirOpenRelativeOrHandle(RTDIR *phDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fFlags,
667 uintptr_t hRelativeDir, void *pvNativeRelative)
668{
669 /*
670 * Validate input.
671 */
672 AssertMsgReturn(VALID_PTR(phDir), ("%p\n", phDir), VERR_INVALID_POINTER);
673 AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
674 AssertReturn(!(fFlags & ~RTDIR_F_VALID_MASK), VERR_INVALID_FLAGS);
675 switch (enmFilter)
676 {
677 case RTDIRFILTER_UNIX:
678 case RTDIRFILTER_UNIX_UPCASED:
679 AssertMsgFailed(("%d is not implemented!\n", enmFilter));
680 return VERR_NOT_IMPLEMENTED;
681 case RTDIRFILTER_NONE:
682 case RTDIRFILTER_WINNT:
683 break;
684 default:
685 AssertMsgFailedReturn(("%d\n", enmFilter), VERR_INVALID_PARAMETER);
686 }
687
688 /*
689 * Find the last component, i.e. where the filter criteria starts and the dir name ends.
690 */
691 const char *pszFilter;
692 if (enmFilter == RTDIRFILTER_NONE)
693 pszFilter = NULL;
694 else
695 {
696 pszFilter = RTPathFilename(pszPath);
697 if (!pszFilter) /* trailing slash => directory to read => no filter. */
698 enmFilter = RTDIRFILTER_NONE;
699 }
700
701 /*
702 * Call worker common with RTDirOpen which will verify the path, allocate
703 * and initialize the handle, and finally call the backend.
704 */
705 int rc = rtDirOpenCommon(phDir, pszPath, pszFilter, enmFilter, fFlags, hRelativeDir, pvNativeRelative);
706
707 LogFlow(("RTDirOpenFiltered(%p:{%p}, %p:{%s}, %d, %#x, %p, %p): return %Rrc\n",
708 phDir,*phDir, pszPath, pszPath, enmFilter, fFlags, hRelativeDir, pvNativeRelative, rc));
709 return rc;
710}
711
712
713RTDECL(int) RTDirOpenFiltered(RTDIR *phDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fFlags)
714{
715 return rtDirOpenRelativeOrHandle(phDir, pszPath, enmFilter, fFlags, ~(uintptr_t)0, NULL);
716}
717
718
719RTDECL(bool) RTDirIsValid(RTDIR hDir)
720{
721 return RT_VALID_PTR(hDir)
722 && hDir->u32Magic == RTDIR_MAGIC;
723}
724
725
726RTDECL(int) RTDirFlushParent(const char *pszChild)
727{
728 char *pszPath;
729 char *pszPathFree = NULL;
730 size_t const cchChild = strlen(pszChild);
731 if (cchChild < RTPATH_MAX)
732 pszPath = (char *)alloca(cchChild);
733 else
734 {
735 pszPathFree = pszPath = (char *)RTMemTmpAlloc(cchChild + 1);
736 if (!pszPath)
737 return VERR_NO_TMP_MEMORY;
738 }
739 memcpy(pszPath, pszChild, cchChild);
740 pszPath[cchChild] = '\0';
741 RTPathStripFilename(pszPath);
742
743 int rc = RTDirFlush(pszPath);
744
745 if (pszPathFree)
746 RTMemTmpFree(pszPathFree);
747 return rc;
748}
749
750
751RTDECL(int) RTDirQueryUnknownTypeEx(const char *pszComposedName, bool fFollowSymlinks,
752 RTDIRENTRYTYPE *penmType, PRTFSOBJINFO pObjInfo)
753{
754 int rc = RTPathQueryInfoEx(pszComposedName, pObjInfo, RTFSOBJATTRADD_NOTHING,
755 fFollowSymlinks ? RTPATH_F_FOLLOW_LINK : RTPATH_F_ON_LINK);
756 if (RT_FAILURE(rc))
757 return rc;
758
759 if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
760 *penmType = RTDIRENTRYTYPE_DIRECTORY;
761 else if (RTFS_IS_FILE(pObjInfo->Attr.fMode))
762 *penmType = RTDIRENTRYTYPE_FILE;
763 else if (RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
764 *penmType = RTDIRENTRYTYPE_SYMLINK;
765 else if (RTFS_IS_FIFO(pObjInfo->Attr.fMode))
766 *penmType = RTDIRENTRYTYPE_FIFO;
767 else if (RTFS_IS_DEV_CHAR(pObjInfo->Attr.fMode))
768 *penmType = RTDIRENTRYTYPE_DEV_CHAR;
769 else if (RTFS_IS_DEV_BLOCK(pObjInfo->Attr.fMode))
770 *penmType = RTDIRENTRYTYPE_DEV_BLOCK;
771 else if (RTFS_IS_SOCKET(pObjInfo->Attr.fMode))
772 *penmType = RTDIRENTRYTYPE_SOCKET;
773 else if (RTFS_IS_WHITEOUT(pObjInfo->Attr.fMode))
774 *penmType = RTDIRENTRYTYPE_WHITEOUT;
775 else
776 *penmType = RTDIRENTRYTYPE_UNKNOWN;
777
778 return VINF_SUCCESS;
779}
780
781
782RTDECL(int) RTDirQueryUnknownType(const char *pszComposedName, bool fFollowSymlinks, RTDIRENTRYTYPE *penmType)
783{
784 if ( *penmType != RTDIRENTRYTYPE_UNKNOWN
785 && ( !fFollowSymlinks
786 || *penmType != RTDIRENTRYTYPE_SYMLINK))
787 return VINF_SUCCESS;
788
789 RTFSOBJINFO ObjInfo;
790 return RTDirQueryUnknownTypeEx(pszComposedName, fFollowSymlinks, penmType, &ObjInfo);
791}
792
793
794RTDECL(bool) RTDirEntryIsStdDotLink(PRTDIRENTRY pDirEntry)
795{
796 if (pDirEntry->szName[0] != '.')
797 return false;
798 if (pDirEntry->cbName == 1)
799 return true;
800 if (pDirEntry->cbName != 2)
801 return false;
802 return pDirEntry->szName[1] == '.';
803}
804
805
806RTDECL(bool) RTDirEntryExIsStdDotLink(PCRTDIRENTRYEX pDirEntryEx)
807{
808 if (pDirEntryEx->szName[0] != '.')
809 return false;
810 if (pDirEntryEx->cbName == 1)
811 return true;
812 if (pDirEntryEx->cbName != 2)
813 return false;
814 return pDirEntryEx->szName[1] == '.';
815}
816
817
818RTDECL(int) RTDirReadExA(RTDIR hDir, PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
819{
820 PRTDIRENTRYEX pDirEntry = *ppDirEntry;
821 size_t cbDirEntry = *pcbDirEntry;
822 if (pDirEntry != NULL && cbDirEntry >= sizeof(RTDIRENTRYEX))
823 { /* likely */ }
824 else
825 {
826 Assert(pDirEntry == NULL);
827 Assert(cbDirEntry == 0);
828
829 cbDirEntry = RT_ALIGN_Z(sizeof(RTDIRENTRYEX), 16);
830 *ppDirEntry = pDirEntry = (PRTDIRENTRYEX)RTMemTmpAlloc(cbDirEntry);
831 if (pDirEntry)
832 *pcbDirEntry = cbDirEntry;
833 else
834 {
835 *pcbDirEntry = 0;
836 return VERR_NO_TMP_MEMORY;
837 }
838 }
839
840 for (;;)
841 {
842 int rc = RTDirReadEx(hDir, pDirEntry, &cbDirEntry, enmAddAttr, fFlags);
843 if (rc != VERR_BUFFER_OVERFLOW)
844 return rc;
845
846 /* Grow the buffer. */
847 RTMemTmpFree(pDirEntry);
848 cbDirEntry = RT_MAX(RT_ALIGN_Z(cbDirEntry, 64), *pcbDirEntry + 64);
849 *ppDirEntry = pDirEntry = (PRTDIRENTRYEX)RTMemTmpAlloc(cbDirEntry);
850 if (pDirEntry)
851 *pcbDirEntry = cbDirEntry;
852 else
853 {
854 *pcbDirEntry = 0;
855 return VERR_NO_TMP_MEMORY;
856 }
857 }
858}
859
860
861RTDECL(void) RTDirReadExAFree(PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry)
862{
863 PRTDIRENTRYEX pDirEntry = *ppDirEntry;
864 if (pDirEntry != NULL && *pcbDirEntry >= sizeof(*pcbDirEntry))
865 RTMemTmpFree(pDirEntry);
866 else
867 {
868 Assert(pDirEntry == NULL);
869 Assert(*pcbDirEntry == 0);
870 }
871 *ppDirEntry = NULL;
872 *pcbDirEntry = 0;
873}
874
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette