VirtualBox

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

Last change on this file since 69691 was 69691, checked in by vboxsync, 7 years ago

iprt: Started on RTDirRel for NT.

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