1 | /* $Id: dir.cpp 78178 2019-04-17 18:32:29Z 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 |
|
---|
50 | static DECLCALLBACK(bool) rtDirFilterWinNtMatch(PRTDIRINTERNAL pDir, const char *pszName);
|
---|
51 | static DECLCALLBACK(bool) rtDirFilterWinNtMatchNoWildcards(PRTDIRINTERNAL pDir, const char *pszName);
|
---|
52 | DECLINLINE(bool) rtDirFilterWinNtMatchEon(PCRTUNICP puszFilter);
|
---|
53 | static bool rtDirFilterWinNtMatchDosStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter);
|
---|
54 | static bool rtDirFilterWinNtMatchStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter);
|
---|
55 | static bool rtDirFilterWinNtMatchBase(unsigned iDepth, const char *pszName, PCRTUNICP puszFilter);
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | RTDECL(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 | */
|
---|
120 | static 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 | */
|
---|
144 | DECLINLINE(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 | */
|
---|
164 | static 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 | */
|
---|
243 | static 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 | */
|
---|
376 | static 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 | */
|
---|
450 | static 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 | */
|
---|
463 | static 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 | */
|
---|
520 | static 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 | /* Note! RTPathAbs currently strips trailing slashes, so we have
|
---|
537 | to inspect pszPath to figure it out. */
|
---|
538 | if (*pszPath != '\0')
|
---|
539 | {
|
---|
540 | const char *pszLast = strchr(pszPath, '\0') - 1;
|
---|
541 | if (RTPATH_IS_SLASH(*pszLast))
|
---|
542 | fDirSlash = true;
|
---|
543 | }
|
---|
544 |
|
---|
545 | cbFilter = cucFilter0 = 0;
|
---|
546 | pszAbsPath = RTPathAbsExDup(NULL, pszPath, RTPATHABS_F_ENSURE_TRAILING_SLASH);
|
---|
547 | }
|
---|
548 | else
|
---|
549 | {
|
---|
550 | cbFilter = strlen(pszFilter) + 1;
|
---|
551 | cucFilter0 = RTStrUniLen(pszFilter) + 1;
|
---|
552 |
|
---|
553 | if (pszFilter != pszPath)
|
---|
554 | {
|
---|
555 | /* yea, I'm lazy. sue me. */
|
---|
556 | char *pszTmp = RTStrDup(pszPath);
|
---|
557 | if (!pszTmp)
|
---|
558 | return VERR_NO_MEMORY;
|
---|
559 | pszTmp[pszFilter - pszPath] = '\0';
|
---|
560 | pszAbsPath = RTPathAbsExDup(NULL, pszTmp, RTPATHABS_F_ENSURE_TRAILING_SLASH);
|
---|
561 | RTStrFree(pszTmp);
|
---|
562 | }
|
---|
563 | else
|
---|
564 | pszAbsPath = RTPathAbsExDup(NULL, ".", RTPATHABS_F_ENSURE_TRAILING_SLASH);
|
---|
565 | fDirSlash = true;
|
---|
566 | }
|
---|
567 | if (!pszAbsPath)
|
---|
568 | return VERR_NO_MEMORY;
|
---|
569 | Assert(strchr(pszAbsPath, '\0')[-1] == RTPATH_SLASH);
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * Allocate and initialize the directory handle.
|
---|
573 | *
|
---|
574 | * The posix definition of Data.d_name allows it to be < NAME_MAX + 1,
|
---|
575 | * thus the horrible ugliness here. Solaris uses d_name[1] for instance.
|
---|
576 | */
|
---|
577 | size_t const cchAbsPath = strlen(pszAbsPath);
|
---|
578 | size_t const cbDir = rtDirNativeGetStructSize(pszAbsPath);
|
---|
579 | size_t const cbAllocated = cbDir
|
---|
580 | + cucFilter0 * sizeof(RTUNICP)
|
---|
581 | + cbFilter
|
---|
582 | + cchAbsPath + 1 + 4;
|
---|
583 | PRTDIRINTERNAL pDir = (PRTDIRINTERNAL)RTMemAllocZ(cbAllocated);
|
---|
584 | if (!pDir)
|
---|
585 | {
|
---|
586 | RTStrFree(pszAbsPath);
|
---|
587 | return VERR_NO_MEMORY;
|
---|
588 | }
|
---|
589 | uint8_t *pb = (uint8_t *)pDir + cbDir;
|
---|
590 |
|
---|
591 | /* initialize it */
|
---|
592 | pDir->u32Magic = RTDIR_MAGIC;
|
---|
593 | pDir->cbSelf = cbDir;
|
---|
594 | if (cbFilter)
|
---|
595 | {
|
---|
596 | pDir->puszFilter = (PRTUNICP)pb;
|
---|
597 | int rc2 = RTStrToUniEx(pszFilter, RTSTR_MAX, &pDir->puszFilter, cucFilter0, &pDir->cucFilter);
|
---|
598 | AssertRC(rc2);
|
---|
599 | pb += cucFilter0 * sizeof(RTUNICP);
|
---|
600 | pDir->pszFilter = (char *)memcpy(pb, pszFilter, cbFilter);
|
---|
601 | pDir->cchFilter = cbFilter - 1;
|
---|
602 | pb += cbFilter;
|
---|
603 | }
|
---|
604 | else
|
---|
605 | {
|
---|
606 | pDir->puszFilter = NULL;
|
---|
607 | pDir->cucFilter = 0;
|
---|
608 | pDir->pszFilter = NULL;
|
---|
609 | pDir->cchFilter = 0;
|
---|
610 | }
|
---|
611 | pDir->enmFilter = enmFilter;
|
---|
612 | switch (enmFilter)
|
---|
613 | {
|
---|
614 | default:
|
---|
615 | case RTDIRFILTER_NONE:
|
---|
616 | pDir->pfnFilter = NULL;
|
---|
617 | break;
|
---|
618 | case RTDIRFILTER_WINNT:
|
---|
619 | pDir->pfnFilter = rtDirFilterWinNtInit(pDir);
|
---|
620 | break;
|
---|
621 | case RTDIRFILTER_UNIX:
|
---|
622 | pDir->pfnFilter = NULL;
|
---|
623 | break;
|
---|
624 | case RTDIRFILTER_UNIX_UPCASED:
|
---|
625 | pDir->pfnFilter = NULL;
|
---|
626 | break;
|
---|
627 | }
|
---|
628 | pDir->cchPath = cchAbsPath;
|
---|
629 | pDir->pszPath = (char *)memcpy(pb, pszAbsPath, cchAbsPath);
|
---|
630 | pb[cchAbsPath] = '\0';
|
---|
631 | Assert(pb - (uint8_t *)pDir + cchAbsPath + 1 <= cbAllocated);
|
---|
632 | pDir->pszName = NULL;
|
---|
633 | pDir->cchName = 0;
|
---|
634 | pDir->fFlags = fFlags;
|
---|
635 | pDir->fDirSlash = fDirSlash;
|
---|
636 | pDir->fDataUnread = false;
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * Hand it over to the native part.
|
---|
640 | */
|
---|
641 | int rc = rtDirNativeOpen(pDir, hRelativeDir, pvNativeRelative);
|
---|
642 | if (RT_SUCCESS(rc))
|
---|
643 | *phDir = pDir;
|
---|
644 | else
|
---|
645 | RTMemFree(pDir);
|
---|
646 | RTStrFree(pszAbsPath);
|
---|
647 | return rc;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | RTDECL(int) RTDirOpen(RTDIR *phDir, const char *pszPath)
|
---|
652 | {
|
---|
653 | /*
|
---|
654 | * Validate input.
|
---|
655 | */
|
---|
656 | AssertMsgReturn(VALID_PTR(phDir), ("%p\n", phDir), VERR_INVALID_POINTER);
|
---|
657 | AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
|
---|
658 |
|
---|
659 | /*
|
---|
660 | * Take common cause with RTDirOpenFiltered().
|
---|
661 | */
|
---|
662 | int rc = rtDirOpenCommon(phDir, pszPath, NULL, RTDIRFILTER_NONE, 0 /*fFlags*/, ~(uintptr_t)0, NULL);
|
---|
663 | LogFlow(("RTDirOpen(%p:{%p}, %p:{%s}): return %Rrc\n", phDir, *phDir, pszPath, pszPath, rc));
|
---|
664 | return rc;
|
---|
665 | }
|
---|
666 |
|
---|
667 |
|
---|
668 | DECLHIDDEN(int) rtDirOpenRelativeOrHandle(RTDIR *phDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fFlags,
|
---|
669 | uintptr_t hRelativeDir, void *pvNativeRelative)
|
---|
670 | {
|
---|
671 | /*
|
---|
672 | * Validate input.
|
---|
673 | */
|
---|
674 | AssertMsgReturn(VALID_PTR(phDir), ("%p\n", phDir), VERR_INVALID_POINTER);
|
---|
675 | AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
|
---|
676 | AssertReturn(!(fFlags & ~RTDIR_F_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
677 | switch (enmFilter)
|
---|
678 | {
|
---|
679 | case RTDIRFILTER_UNIX:
|
---|
680 | case RTDIRFILTER_UNIX_UPCASED:
|
---|
681 | AssertMsgFailed(("%d is not implemented!\n", enmFilter));
|
---|
682 | return VERR_NOT_IMPLEMENTED;
|
---|
683 | case RTDIRFILTER_NONE:
|
---|
684 | case RTDIRFILTER_WINNT:
|
---|
685 | break;
|
---|
686 | default:
|
---|
687 | AssertMsgFailedReturn(("%d\n", enmFilter), VERR_INVALID_PARAMETER);
|
---|
688 | }
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * Find the last component, i.e. where the filter criteria starts and the dir name ends.
|
---|
692 | */
|
---|
693 | const char *pszFilter;
|
---|
694 | if (enmFilter == RTDIRFILTER_NONE)
|
---|
695 | pszFilter = NULL;
|
---|
696 | else
|
---|
697 | {
|
---|
698 | pszFilter = RTPathFilename(pszPath);
|
---|
699 | if (!pszFilter) /* trailing slash => directory to read => no filter. */
|
---|
700 | enmFilter = RTDIRFILTER_NONE;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * Call worker common with RTDirOpen which will verify the path, allocate
|
---|
705 | * and initialize the handle, and finally call the backend.
|
---|
706 | */
|
---|
707 | int rc = rtDirOpenCommon(phDir, pszPath, pszFilter, enmFilter, fFlags, hRelativeDir, pvNativeRelative);
|
---|
708 |
|
---|
709 | LogFlow(("RTDirOpenFiltered(%p:{%p}, %p:{%s}, %d, %#x, %p, %p): return %Rrc\n",
|
---|
710 | phDir,*phDir, pszPath, pszPath, enmFilter, fFlags, hRelativeDir, pvNativeRelative, rc));
|
---|
711 | return rc;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | RTDECL(int) RTDirOpenFiltered(RTDIR *phDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fFlags)
|
---|
716 | {
|
---|
717 | return rtDirOpenRelativeOrHandle(phDir, pszPath, enmFilter, fFlags, ~(uintptr_t)0, NULL);
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | RTDECL(bool) RTDirIsValid(RTDIR hDir)
|
---|
722 | {
|
---|
723 | return RT_VALID_PTR(hDir)
|
---|
724 | && hDir->u32Magic == RTDIR_MAGIC;
|
---|
725 | }
|
---|
726 |
|
---|
727 |
|
---|
728 | RTDECL(int) RTDirFlushParent(const char *pszChild)
|
---|
729 | {
|
---|
730 | char *pszPath;
|
---|
731 | char *pszPathFree = NULL;
|
---|
732 | size_t const cchChild = strlen(pszChild);
|
---|
733 | if (cchChild < RTPATH_MAX)
|
---|
734 | pszPath = (char *)alloca(cchChild);
|
---|
735 | else
|
---|
736 | {
|
---|
737 | pszPathFree = pszPath = (char *)RTMemTmpAlloc(cchChild + 1);
|
---|
738 | if (!pszPath)
|
---|
739 | return VERR_NO_TMP_MEMORY;
|
---|
740 | }
|
---|
741 | memcpy(pszPath, pszChild, cchChild);
|
---|
742 | pszPath[cchChild] = '\0';
|
---|
743 | RTPathStripFilename(pszPath);
|
---|
744 |
|
---|
745 | int rc = RTDirFlush(pszPath);
|
---|
746 |
|
---|
747 | if (pszPathFree)
|
---|
748 | RTMemTmpFree(pszPathFree);
|
---|
749 | return rc;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | RTDECL(int) RTDirQueryUnknownTypeEx(const char *pszComposedName, bool fFollowSymlinks,
|
---|
754 | RTDIRENTRYTYPE *penmType, PRTFSOBJINFO pObjInfo)
|
---|
755 | {
|
---|
756 | int rc = RTPathQueryInfoEx(pszComposedName, pObjInfo, RTFSOBJATTRADD_NOTHING,
|
---|
757 | fFollowSymlinks ? RTPATH_F_FOLLOW_LINK : RTPATH_F_ON_LINK);
|
---|
758 | if (RT_FAILURE(rc))
|
---|
759 | return rc;
|
---|
760 |
|
---|
761 | if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
|
---|
762 | *penmType = RTDIRENTRYTYPE_DIRECTORY;
|
---|
763 | else if (RTFS_IS_FILE(pObjInfo->Attr.fMode))
|
---|
764 | *penmType = RTDIRENTRYTYPE_FILE;
|
---|
765 | else if (RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
|
---|
766 | *penmType = RTDIRENTRYTYPE_SYMLINK;
|
---|
767 | else if (RTFS_IS_FIFO(pObjInfo->Attr.fMode))
|
---|
768 | *penmType = RTDIRENTRYTYPE_FIFO;
|
---|
769 | else if (RTFS_IS_DEV_CHAR(pObjInfo->Attr.fMode))
|
---|
770 | *penmType = RTDIRENTRYTYPE_DEV_CHAR;
|
---|
771 | else if (RTFS_IS_DEV_BLOCK(pObjInfo->Attr.fMode))
|
---|
772 | *penmType = RTDIRENTRYTYPE_DEV_BLOCK;
|
---|
773 | else if (RTFS_IS_SOCKET(pObjInfo->Attr.fMode))
|
---|
774 | *penmType = RTDIRENTRYTYPE_SOCKET;
|
---|
775 | else if (RTFS_IS_WHITEOUT(pObjInfo->Attr.fMode))
|
---|
776 | *penmType = RTDIRENTRYTYPE_WHITEOUT;
|
---|
777 | else
|
---|
778 | *penmType = RTDIRENTRYTYPE_UNKNOWN;
|
---|
779 |
|
---|
780 | return VINF_SUCCESS;
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | RTDECL(int) RTDirQueryUnknownType(const char *pszComposedName, bool fFollowSymlinks, RTDIRENTRYTYPE *penmType)
|
---|
785 | {
|
---|
786 | if ( *penmType != RTDIRENTRYTYPE_UNKNOWN
|
---|
787 | && ( !fFollowSymlinks
|
---|
788 | || *penmType != RTDIRENTRYTYPE_SYMLINK))
|
---|
789 | return VINF_SUCCESS;
|
---|
790 |
|
---|
791 | RTFSOBJINFO ObjInfo;
|
---|
792 | return RTDirQueryUnknownTypeEx(pszComposedName, fFollowSymlinks, penmType, &ObjInfo);
|
---|
793 | }
|
---|
794 |
|
---|
795 |
|
---|
796 | RTDECL(bool) RTDirEntryIsStdDotLink(PRTDIRENTRY pDirEntry)
|
---|
797 | {
|
---|
798 | if (pDirEntry->szName[0] != '.')
|
---|
799 | return false;
|
---|
800 | if (pDirEntry->cbName == 1)
|
---|
801 | return true;
|
---|
802 | if (pDirEntry->cbName != 2)
|
---|
803 | return false;
|
---|
804 | return pDirEntry->szName[1] == '.';
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | RTDECL(bool) RTDirEntryExIsStdDotLink(PCRTDIRENTRYEX pDirEntryEx)
|
---|
809 | {
|
---|
810 | if (pDirEntryEx->szName[0] != '.')
|
---|
811 | return false;
|
---|
812 | if (pDirEntryEx->cbName == 1)
|
---|
813 | return true;
|
---|
814 | if (pDirEntryEx->cbName != 2)
|
---|
815 | return false;
|
---|
816 | return pDirEntryEx->szName[1] == '.';
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | RTDECL(int) RTDirReadExA(RTDIR hDir, PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
|
---|
821 | {
|
---|
822 | PRTDIRENTRYEX pDirEntry = *ppDirEntry;
|
---|
823 | size_t cbDirEntry = *pcbDirEntry;
|
---|
824 | if (pDirEntry != NULL && cbDirEntry >= sizeof(RTDIRENTRYEX))
|
---|
825 | { /* likely */ }
|
---|
826 | else
|
---|
827 | {
|
---|
828 | Assert(pDirEntry == NULL);
|
---|
829 | Assert(cbDirEntry == 0);
|
---|
830 |
|
---|
831 | cbDirEntry = RT_ALIGN_Z(sizeof(RTDIRENTRYEX), 16);
|
---|
832 | *ppDirEntry = pDirEntry = (PRTDIRENTRYEX)RTMemTmpAlloc(cbDirEntry);
|
---|
833 | if (pDirEntry)
|
---|
834 | *pcbDirEntry = cbDirEntry;
|
---|
835 | else
|
---|
836 | {
|
---|
837 | *pcbDirEntry = 0;
|
---|
838 | return VERR_NO_TMP_MEMORY;
|
---|
839 | }
|
---|
840 | }
|
---|
841 |
|
---|
842 | for (;;)
|
---|
843 | {
|
---|
844 | int rc = RTDirReadEx(hDir, pDirEntry, &cbDirEntry, enmAddAttr, fFlags);
|
---|
845 | if (rc != VERR_BUFFER_OVERFLOW)
|
---|
846 | return rc;
|
---|
847 |
|
---|
848 | /* Grow the buffer. */
|
---|
849 | RTMemTmpFree(pDirEntry);
|
---|
850 | cbDirEntry = RT_MAX(RT_ALIGN_Z(cbDirEntry, 64), *pcbDirEntry + 64);
|
---|
851 | *ppDirEntry = pDirEntry = (PRTDIRENTRYEX)RTMemTmpAlloc(cbDirEntry);
|
---|
852 | if (pDirEntry)
|
---|
853 | *pcbDirEntry = cbDirEntry;
|
---|
854 | else
|
---|
855 | {
|
---|
856 | *pcbDirEntry = 0;
|
---|
857 | return VERR_NO_TMP_MEMORY;
|
---|
858 | }
|
---|
859 | }
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | RTDECL(void) RTDirReadExAFree(PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry)
|
---|
864 | {
|
---|
865 | PRTDIRENTRYEX pDirEntry = *ppDirEntry;
|
---|
866 | if (pDirEntry != NULL && *pcbDirEntry >= sizeof(*pcbDirEntry))
|
---|
867 | RTMemTmpFree(pDirEntry);
|
---|
868 | else
|
---|
869 | {
|
---|
870 | Assert(pDirEntry == NULL);
|
---|
871 | Assert(*pcbDirEntry == 0);
|
---|
872 | }
|
---|
873 | *ppDirEntry = NULL;
|
---|
874 | *pcbDirEntry = 0;
|
---|
875 | }
|
---|
876 |
|
---|