1 | /* $Id: dir.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Directory Manipulation, Part 1.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_DIR
|
---|
32 | #include <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 |
|
---|
49 | static DECLCALLBACK(bool) rtDirFilterWinNtMatch(PRTDIR pDir, const char *pszName);
|
---|
50 | static DECLCALLBACK(bool) rtDirFilterWinNtMatchNoWildcards(PRTDIR pDir, const char *pszName);
|
---|
51 | DECLINLINE(bool) rtDirFilterWinNtMatchEon(PCRTUNICP puszFilter);
|
---|
52 | static bool rtDirFilterWinNtMatchDosStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter);
|
---|
53 | static bool rtDirFilterWinNtMatchStar(unsigned iDepth, RTUNICP uc, const char *pszNext, PCRTUNICP puszFilter);
|
---|
54 | static bool rtDirFilterWinNtMatchBase(unsigned iDepth, const char *pszName, PCRTUNICP puszFilter);
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | RTDECL(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 | */
|
---|
119 | static 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 | */
|
---|
143 | DECLINLINE(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 | */
|
---|
163 | static 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 | */
|
---|
242 | static 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 | /* 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 | */
|
---|
375 | static 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 | */
|
---|
449 | static 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 | */
|
---|
462 | static 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. NULL if no filter.
|
---|
511 | * @param enmFilter The type of filter to apply.
|
---|
512 | */
|
---|
513 | static int rtDirOpenCommon(PRTDIR *ppDir, const char *pszPath, const char *pszFilter, RTDIRFILTER enmFilter)
|
---|
514 | {
|
---|
515 | /*
|
---|
516 | * Expand the path.
|
---|
517 | *
|
---|
518 | * The purpose of this exercise to have the abs path around
|
---|
519 | * for querying extra information about the objects we list.
|
---|
520 | * As a sideeffect we also validate the path here.
|
---|
521 | */
|
---|
522 | char szRealPath[RTPATH_MAX + 1];
|
---|
523 | int rc;
|
---|
524 | size_t cbFilter; /* includes '\0' (thus cb and not cch). */
|
---|
525 | size_t cucFilter0; /* includes U+0. */
|
---|
526 | if (!pszFilter)
|
---|
527 | {
|
---|
528 | cbFilter = cucFilter0 = 0;
|
---|
529 | rc = RTPathAbs(pszPath, szRealPath, sizeof(szRealPath) - 1);
|
---|
530 | }
|
---|
531 | else
|
---|
532 | {
|
---|
533 | cbFilter = strlen(pszFilter) + 1;
|
---|
534 | cucFilter0 = RTStrUniLen(pszFilter) + 1;
|
---|
535 |
|
---|
536 | if (pszFilter != pszPath)
|
---|
537 | {
|
---|
538 | /* yea, I'm lazy. sue me. */
|
---|
539 | char *pszTmp = RTStrDup(pszPath);
|
---|
540 | if (!pszTmp)
|
---|
541 | return VERR_NO_MEMORY;
|
---|
542 | pszTmp[pszFilter - pszPath] = '\0';
|
---|
543 | rc = RTPathAbs(pszTmp, szRealPath, sizeof(szRealPath) - 1);
|
---|
544 | RTStrFree(pszTmp);
|
---|
545 | }
|
---|
546 | else
|
---|
547 | rc = RTPathReal(".", szRealPath, sizeof(szRealPath) - 1);
|
---|
548 | }
|
---|
549 | if (RT_FAILURE(rc))
|
---|
550 | return rc;
|
---|
551 |
|
---|
552 | /* add trailing '/' if missing. */
|
---|
553 | size_t cchRealPath = strlen(szRealPath);
|
---|
554 | if (!RTPATH_IS_SEP(szRealPath[cchRealPath - 1]))
|
---|
555 | {
|
---|
556 | szRealPath[cchRealPath++] = RTPATH_SLASH;
|
---|
557 | szRealPath[cchRealPath] = '\0';
|
---|
558 | }
|
---|
559 |
|
---|
560 | /*
|
---|
561 | * Allocate and initialize the directory handle.
|
---|
562 | *
|
---|
563 | * The posix definition of Data.d_name allows it to be < NAME_MAX + 1,
|
---|
564 | * thus the horrible ugliness here. Solaris uses d_name[1] for instance.
|
---|
565 | */
|
---|
566 | size_t cbDir = rtDirNativeGetStructSize(szRealPath);
|
---|
567 | size_t const cbAllocated = cbDir
|
---|
568 | + cucFilter0 * sizeof(RTUNICP)
|
---|
569 | + cbFilter
|
---|
570 | + cchRealPath + 1 + 4;
|
---|
571 | PRTDIR pDir = (PRTDIR)RTMemAllocZ(cbAllocated);
|
---|
572 | if (!pDir)
|
---|
573 | return VERR_NO_MEMORY;
|
---|
574 | uint8_t *pb = (uint8_t *)pDir + cbDir;
|
---|
575 |
|
---|
576 | /* initialize it */
|
---|
577 | pDir->u32Magic = RTDIR_MAGIC;
|
---|
578 | pDir->cbSelf = cbDir;
|
---|
579 | if (cbFilter)
|
---|
580 | {
|
---|
581 | pDir->puszFilter = (PRTUNICP)pb;
|
---|
582 | rc = RTStrToUniEx(pszFilter, RTSTR_MAX, &pDir->puszFilter, cucFilter0, &pDir->cucFilter);
|
---|
583 | AssertRC(rc);
|
---|
584 | pb += cucFilter0 * sizeof(RTUNICP);
|
---|
585 | pDir->pszFilter = (char *)memcpy(pb, pszFilter, cbFilter);
|
---|
586 | pDir->cchFilter = cbFilter - 1;
|
---|
587 | pb += cbFilter;
|
---|
588 | }
|
---|
589 | else
|
---|
590 | {
|
---|
591 | pDir->puszFilter = NULL;
|
---|
592 | pDir->cucFilter = 0;
|
---|
593 | pDir->pszFilter = NULL;
|
---|
594 | pDir->cchFilter = 0;
|
---|
595 | }
|
---|
596 | pDir->enmFilter = enmFilter;
|
---|
597 | switch (enmFilter)
|
---|
598 | {
|
---|
599 | default:
|
---|
600 | case RTDIRFILTER_NONE:
|
---|
601 | pDir->pfnFilter = NULL;
|
---|
602 | break;
|
---|
603 | case RTDIRFILTER_WINNT:
|
---|
604 | pDir->pfnFilter = rtDirFilterWinNtInit(pDir);
|
---|
605 | break;
|
---|
606 | case RTDIRFILTER_UNIX:
|
---|
607 | pDir->pfnFilter = NULL;
|
---|
608 | break;
|
---|
609 | case RTDIRFILTER_UNIX_UPCASED:
|
---|
610 | pDir->pfnFilter = NULL;
|
---|
611 | break;
|
---|
612 | }
|
---|
613 | pDir->cchPath = cchRealPath;
|
---|
614 | pDir->pszPath = (char *)memcpy(pb, szRealPath, cchRealPath + 1);
|
---|
615 | Assert(pb - (uint8_t *)pDir + cchRealPath + 1 <= cbAllocated);
|
---|
616 | pDir->fDataUnread = false;
|
---|
617 | pDir->pszName = NULL;
|
---|
618 | pDir->cchName = 0;
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * Hand it over to the native part.
|
---|
622 | */
|
---|
623 | rc = rtDirNativeOpen(pDir, szRealPath);
|
---|
624 | if (RT_SUCCESS(rc))
|
---|
625 | *ppDir = pDir;
|
---|
626 | else
|
---|
627 | RTMemFree(pDir);
|
---|
628 |
|
---|
629 | return rc;
|
---|
630 | }
|
---|
631 |
|
---|
632 |
|
---|
633 |
|
---|
634 | RTDECL(int) RTDirOpen(PRTDIR *ppDir, const char *pszPath)
|
---|
635 | {
|
---|
636 | /*
|
---|
637 | * Validate input.
|
---|
638 | */
|
---|
639 | AssertMsgReturn(VALID_PTR(ppDir), ("%p\n", ppDir), VERR_INVALID_POINTER);
|
---|
640 | AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
|
---|
641 |
|
---|
642 | /*
|
---|
643 | * Take common cause with RTDirOpenFiltered().
|
---|
644 | */
|
---|
645 | int rc = rtDirOpenCommon(ppDir, pszPath, NULL, RTDIRFILTER_NONE);
|
---|
646 | LogFlow(("RTDirOpen(%p:{%p}, %p:{%s}): return %Rrc\n", ppDir, *ppDir, pszPath, pszPath, rc));
|
---|
647 | return rc;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | RTDECL(int) RTDirOpenFiltered(PRTDIR *ppDir, const char *pszPath, RTDIRFILTER enmFilter, uint32_t fOpen)
|
---|
652 | {
|
---|
653 | /*
|
---|
654 | * Validate input.
|
---|
655 | */
|
---|
656 | AssertMsgReturn(VALID_PTR(ppDir), ("%p\n", ppDir), VERR_INVALID_POINTER);
|
---|
657 | AssertMsgReturn(VALID_PTR(pszPath), ("%p\n", pszPath), VERR_INVALID_POINTER);
|
---|
658 | switch (enmFilter)
|
---|
659 | {
|
---|
660 | case RTDIRFILTER_UNIX:
|
---|
661 | case RTDIRFILTER_UNIX_UPCASED:
|
---|
662 | AssertMsgFailed(("%d is not implemented!\n", enmFilter));
|
---|
663 | return VERR_NOT_IMPLEMENTED;
|
---|
664 | case RTDIRFILTER_NONE:
|
---|
665 | case RTDIRFILTER_WINNT:
|
---|
666 | break;
|
---|
667 | default:
|
---|
668 | AssertMsgFailedReturn(("%d\n", enmFilter), VERR_INVALID_PARAMETER);
|
---|
669 | }
|
---|
670 |
|
---|
671 | /*
|
---|
672 | * Find the last component, i.e. where the filter criteria starts and the dir name ends.
|
---|
673 | */
|
---|
674 | const char *pszFilter;
|
---|
675 | if (enmFilter == RTDIRFILTER_NONE)
|
---|
676 | pszFilter = NULL;
|
---|
677 | else
|
---|
678 | {
|
---|
679 | pszFilter = RTPathFilename(pszPath);
|
---|
680 | if (!pszFilter) /* trailing slash => directory to read => no filter. */
|
---|
681 | enmFilter = RTDIRFILTER_NONE;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /*
|
---|
685 | * Call worker common with RTDirOpen which will verify the path, allocate
|
---|
686 | * and initialize the handle, and finally call the backend.
|
---|
687 | */
|
---|
688 | int rc = rtDirOpenCommon(ppDir, pszPath, pszFilter, enmFilter);
|
---|
689 |
|
---|
690 | LogFlow(("RTDirOpenFiltered(%p:{%p}, %p:{%s}, %d): return %Rrc\n",
|
---|
691 | ppDir, *ppDir, pszPath, pszPath, enmFilter, rc));
|
---|
692 | return rc;
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | RTDECL(int) RTDirFlushParent(const char *pszChild)
|
---|
697 | {
|
---|
698 | char szPath[RTPATH_MAX];
|
---|
699 | int rc = RTStrCopy(szPath, sizeof(szPath), pszChild);
|
---|
700 | if (RT_SUCCESS(rc))
|
---|
701 | {
|
---|
702 | RTPathStripFilename(szPath);
|
---|
703 | rc = RTDirFlush(szPath);
|
---|
704 | }
|
---|
705 | return rc;
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | RTDECL(int) RTDirQueryUnknownTypeEx(const char *pszComposedName, bool fFollowSymlinks,
|
---|
710 | RTDIRENTRYTYPE *penmType, PRTFSOBJINFO pObjInfo)
|
---|
711 | {
|
---|
712 | int rc = RTPathQueryInfoEx(pszComposedName, pObjInfo, RTFSOBJATTRADD_NOTHING,
|
---|
713 | fFollowSymlinks ? RTPATH_F_FOLLOW_LINK : RTPATH_F_ON_LINK);
|
---|
714 | if (RT_FAILURE(rc))
|
---|
715 | return rc;
|
---|
716 |
|
---|
717 | if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
|
---|
718 | *penmType = RTDIRENTRYTYPE_DIRECTORY;
|
---|
719 | else if (RTFS_IS_FILE(pObjInfo->Attr.fMode))
|
---|
720 | *penmType = RTDIRENTRYTYPE_FILE;
|
---|
721 | else if (RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
|
---|
722 | *penmType = RTDIRENTRYTYPE_SYMLINK;
|
---|
723 | else if (RTFS_IS_FIFO(pObjInfo->Attr.fMode))
|
---|
724 | *penmType = RTDIRENTRYTYPE_FIFO;
|
---|
725 | else if (RTFS_IS_DEV_CHAR(pObjInfo->Attr.fMode))
|
---|
726 | *penmType = RTDIRENTRYTYPE_DEV_CHAR;
|
---|
727 | else if (RTFS_IS_DEV_BLOCK(pObjInfo->Attr.fMode))
|
---|
728 | *penmType = RTDIRENTRYTYPE_DEV_BLOCK;
|
---|
729 | else if (RTFS_IS_SOCKET(pObjInfo->Attr.fMode))
|
---|
730 | *penmType = RTDIRENTRYTYPE_SOCKET;
|
---|
731 | else if (RTFS_IS_WHITEOUT(pObjInfo->Attr.fMode))
|
---|
732 | *penmType = RTDIRENTRYTYPE_WHITEOUT;
|
---|
733 | else
|
---|
734 | *penmType = RTDIRENTRYTYPE_UNKNOWN;
|
---|
735 |
|
---|
736 | return VINF_SUCCESS;
|
---|
737 | }
|
---|
738 |
|
---|
739 |
|
---|
740 | RTDECL(int) RTDirQueryUnknownType(const char *pszComposedName, bool fFollowSymlinks, RTDIRENTRYTYPE *penmType)
|
---|
741 | {
|
---|
742 | if ( *penmType != RTDIRENTRYTYPE_UNKNOWN
|
---|
743 | && ( !fFollowSymlinks
|
---|
744 | || *penmType != RTDIRENTRYTYPE_SYMLINK))
|
---|
745 | return VINF_SUCCESS;
|
---|
746 |
|
---|
747 | RTFSOBJINFO ObjInfo;
|
---|
748 | return RTDirQueryUnknownTypeEx(pszComposedName, fFollowSymlinks, penmType, &ObjInfo);
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | RTDECL(bool) RTDirEntryIsStdDotLink(PRTDIRENTRY pDirEntry)
|
---|
753 | {
|
---|
754 | if (pDirEntry->szName[0] != '.')
|
---|
755 | return false;
|
---|
756 | if (pDirEntry->cbName == 1)
|
---|
757 | return true;
|
---|
758 | if (pDirEntry->cbName != 2)
|
---|
759 | return false;
|
---|
760 | return pDirEntry->szName[1] == '.';
|
---|
761 | }
|
---|
762 |
|
---|
763 |
|
---|
764 | RTDECL(bool) RTDirEntryExIsStdDotLink(PCRTDIRENTRYEX pDirEntryEx)
|
---|
765 | {
|
---|
766 | if (pDirEntryEx->szName[0] != '.')
|
---|
767 | return false;
|
---|
768 | if (pDirEntryEx->cbName == 1)
|
---|
769 | return true;
|
---|
770 | if (pDirEntryEx->cbName != 2)
|
---|
771 | return false;
|
---|
772 | return pDirEntryEx->szName[1] == '.';
|
---|
773 | }
|
---|
774 |
|
---|