1 | /* $Id: dir-posix.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Directory manipulation, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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 <errno.h>
|
---|
33 | #include <unistd.h>
|
---|
34 | #include <sys/types.h>
|
---|
35 | #include <sys/stat.h>
|
---|
36 | #include <fcntl.h>
|
---|
37 | #include <dirent.h>
|
---|
38 | #include <dlfcn.h>
|
---|
39 | #include <stdio.h>
|
---|
40 |
|
---|
41 | #include <iprt/dir.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/alloca.h>
|
---|
45 | #include <iprt/asm.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/err.h>
|
---|
48 | #include <iprt/log.h>
|
---|
49 | #include <iprt/mem.h>
|
---|
50 | #include <iprt/param.h>
|
---|
51 | #include <iprt/path.h>
|
---|
52 | #include <iprt/string.h>
|
---|
53 | #include "internal/dir.h"
|
---|
54 | #include "internal/fs.h"
|
---|
55 | #include "internal/path.h"
|
---|
56 |
|
---|
57 | #if !defined(RT_OS_SOLARIS) && !defined(RT_OS_HAIKU)
|
---|
58 | # define HAVE_DIRENT_D_TYPE 1
|
---|
59 | #endif
|
---|
60 |
|
---|
61 |
|
---|
62 | RTDECL(bool) RTDirExists(const char *pszPath)
|
---|
63 | {
|
---|
64 | bool fRc = false;
|
---|
65 | char const *pszNativePath;
|
---|
66 | int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | {
|
---|
69 | struct stat s;
|
---|
70 | fRc = !stat(pszNativePath, &s)
|
---|
71 | && S_ISDIR(s.st_mode);
|
---|
72 |
|
---|
73 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
74 | }
|
---|
75 |
|
---|
76 | LogFlow(("RTDirExists(%p={%s}): returns %RTbool\n", pszPath, pszPath, fRc));
|
---|
77 | return fRc;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode, uint32_t fCreate)
|
---|
82 | {
|
---|
83 | RT_NOREF_PV(fCreate);
|
---|
84 |
|
---|
85 | int rc;
|
---|
86 | fMode = rtFsModeNormalize(fMode, pszPath, 0, RTFS_TYPE_DIRECTORY);
|
---|
87 | if (rtFsModeIsValidPermissions(fMode))
|
---|
88 | {
|
---|
89 | char const *pszNativePath;
|
---|
90 | rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
91 | if (RT_SUCCESS(rc))
|
---|
92 | {
|
---|
93 | struct stat st;
|
---|
94 | if (mkdir(pszNativePath, fMode & RTFS_UNIX_MASK) == 0)
|
---|
95 | {
|
---|
96 | /* If requested, we try make use the permission bits are set
|
---|
97 | correctly when asked. For now, we'll just ignore errors here. */
|
---|
98 | if (fCreate & RTDIRCREATE_FLAGS_IGNORE_UMASK)
|
---|
99 | {
|
---|
100 | if ( stat(pszNativePath, &st)
|
---|
101 | || (st.st_mode & 07777) != (fMode & 07777) )
|
---|
102 | chmod(pszNativePath, fMode & RTFS_UNIX_MASK);
|
---|
103 | }
|
---|
104 | rc = VINF_SUCCESS;
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | rc = errno;
|
---|
109 | /*bool fVerifyIsDir = true; - Windows returns VERR_ALREADY_EXISTS, so why bother with this. */
|
---|
110 | #ifdef RT_OS_SOLARIS
|
---|
111 | /*
|
---|
112 | * mkdir on nfs mount points has been/is busted in various
|
---|
113 | * during the Nevada development cycle. We've observed:
|
---|
114 | * - Build 111b (2009.06) returns EACCES.
|
---|
115 | * - Build ca. 70-80 returns ENOSYS.
|
---|
116 | */
|
---|
117 | if ( rc == ENOSYS
|
---|
118 | || rc == EACCES)
|
---|
119 | {
|
---|
120 | rc = RTErrConvertFromErrno(rc);
|
---|
121 | /*fVerifyIsDir = false; We'll check if it's a dir ourselves since we're going to stat() anyway. */
|
---|
122 | if (!stat(pszNativePath, &st))
|
---|
123 | {
|
---|
124 | rc = VERR_ALREADY_EXISTS;
|
---|
125 | /* Windows returns VERR_ALREADY_EXISTS, so why bother with this:
|
---|
126 | if (!S_ISDIR(st.st_mode))
|
---|
127 | rc = VERR_IS_A_FILE; */
|
---|
128 | }
|
---|
129 | }
|
---|
130 | else
|
---|
131 | rc = RTErrConvertFromErrno(rc);
|
---|
132 | #else
|
---|
133 | rc = RTErrConvertFromErrno(rc);
|
---|
134 | #endif
|
---|
135 | #if 0 /* Windows returns VERR_ALREADY_EXISTS, so why bother with this. */
|
---|
136 | if ( rc == VERR_ALREADY_EXISTS
|
---|
137 | /*&& fVerifyIsDir == true*/)
|
---|
138 | {
|
---|
139 | /*
|
---|
140 | * Verify that it really exists as a directory.
|
---|
141 | */
|
---|
142 | struct stat st;
|
---|
143 | if (!stat(pszNativePath, &st) && !S_ISDIR(st.st_mode))
|
---|
144 | rc = VERR_IS_A_FILE;
|
---|
145 | }
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | AssertMsgFailed(("Invalid file mode! %RTfmode\n", fMode));
|
---|
155 | rc = VERR_INVALID_FMODE;
|
---|
156 | }
|
---|
157 | LogFlow(("RTDirCreate(%p={%s}, %RTfmode): returns %Rrc\n", pszPath, pszPath, fMode, rc));
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(int) RTDirRemove(const char *pszPath)
|
---|
163 | {
|
---|
164 | char const *pszNativePath;
|
---|
165 | int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
166 | if (RT_SUCCESS(rc))
|
---|
167 | {
|
---|
168 | if (rmdir(pszNativePath))
|
---|
169 | {
|
---|
170 | rc = errno;
|
---|
171 | if (rc == EEXIST) /* Solaris returns this, the rest have ENOTEMPTY. */
|
---|
172 | rc = VERR_DIR_NOT_EMPTY;
|
---|
173 | else if (rc != ENOTDIR)
|
---|
174 | rc = RTErrConvertFromErrno(rc);
|
---|
175 | else
|
---|
176 | {
|
---|
177 | /*
|
---|
178 | * This may be a valid path-not-found or it may be a non-directory in
|
---|
179 | * the final component. FsPerf want us to distinguish between the two,
|
---|
180 | * and trailing slash shouldn't matter because it doesn't on windows...
|
---|
181 | */
|
---|
182 | char *pszFree = NULL;
|
---|
183 | const char *pszStat = pszNativePath;
|
---|
184 | size_t cch = strlen(pszNativePath);
|
---|
185 | if (cch > 2 && pszNativePath[cch - 1] == '/')
|
---|
186 | {
|
---|
187 | pszStat = pszFree = (char *)RTMemTmpAlloc(cch);
|
---|
188 | memcpy(pszFree, pszNativePath, cch);
|
---|
189 | do
|
---|
190 | pszFree[--cch] = '\0';
|
---|
191 | while (cch > 2 && pszFree[cch - 1] == '/');
|
---|
192 | }
|
---|
193 |
|
---|
194 | struct stat st;
|
---|
195 | if (!stat(pszStat, &st) && !S_ISDIR(st.st_mode))
|
---|
196 | rc = VERR_NOT_A_DIRECTORY;
|
---|
197 | else
|
---|
198 | rc = VERR_PATH_NOT_FOUND;
|
---|
199 |
|
---|
200 | if (pszFree)
|
---|
201 | RTMemTmpFree(pszFree);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
206 | }
|
---|
207 |
|
---|
208 | LogFlow(("RTDirRemove(%p={%s}): returns %Rrc\n", pszPath, pszPath, rc));
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | RTDECL(int) RTDirFlush(const char *pszPath)
|
---|
214 | {
|
---|
215 | /*
|
---|
216 | * Linux: The fsync() man page hints at this being required for ensuring
|
---|
217 | * consistency between directory and file in case of a crash.
|
---|
218 | *
|
---|
219 | * Solaris: No mentioned is made of directories on the fsync man page.
|
---|
220 | * While rename+fsync will do what we want on ZFS, the code needs more
|
---|
221 | * careful studying wrt whether the directory entry of a new file is
|
---|
222 | * implicitly synced when the file is synced (it's very likely for ZFS).
|
---|
223 | *
|
---|
224 | * FreeBSD: The FFS fsync code seems to flush the directory entry as well
|
---|
225 | * in some cases. Don't know exactly what's up with rename, but from the
|
---|
226 | * look of things fsync(dir) should work.
|
---|
227 | */
|
---|
228 | int rc;
|
---|
229 | #ifdef O_DIRECTORY
|
---|
230 | int fd = open(pszPath, O_RDONLY | O_DIRECTORY, 0);
|
---|
231 | #else
|
---|
232 | int fd = open(pszPath, O_RDONLY, 0);
|
---|
233 | #endif
|
---|
234 | if (fd >= 0)
|
---|
235 | {
|
---|
236 | if (fsync(fd) == 0)
|
---|
237 | rc = VINF_SUCCESS;
|
---|
238 | else
|
---|
239 | {
|
---|
240 | /* Linux fsync(2) man page documents both errors as an indication
|
---|
241 | * that the file descriptor can't be flushed (seen EINVAL for usual
|
---|
242 | * directories on CIFS). BSD (OS X) fsync(2) documents only the
|
---|
243 | * latter, and Solaris fsync(3C) pretends there is no problem. */
|
---|
244 | if (errno == EROFS || errno == EINVAL)
|
---|
245 | rc = VERR_NOT_SUPPORTED;
|
---|
246 | else
|
---|
247 | rc = RTErrConvertFromErrno(errno);
|
---|
248 | }
|
---|
249 | close(fd);
|
---|
250 | }
|
---|
251 | else
|
---|
252 | rc = RTErrConvertFromErrno(errno);
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | size_t rtDirNativeGetStructSize(const char *pszPath)
|
---|
258 | {
|
---|
259 | long cbNameMax = pathconf(pszPath, _PC_NAME_MAX);
|
---|
260 | # ifdef NAME_MAX
|
---|
261 | if (cbNameMax < NAME_MAX) /* This is plain paranoia, but it doesn't hurt. */
|
---|
262 | cbNameMax = NAME_MAX;
|
---|
263 | # endif
|
---|
264 | # ifdef _XOPEN_NAME_MAX
|
---|
265 | if (cbNameMax < _XOPEN_NAME_MAX) /* Ditto. */
|
---|
266 | cbNameMax = _XOPEN_NAME_MAX;
|
---|
267 | # endif
|
---|
268 | size_t cbDir = RT_UOFFSETOF_DYN(RTDIRINTERNAL, Data.d_name[cbNameMax + 1]);
|
---|
269 | if (cbDir < sizeof(RTDIRINTERNAL)) /* Ditto. */
|
---|
270 | cbDir = sizeof(RTDIRINTERNAL);
|
---|
271 | cbDir = RT_ALIGN_Z(cbDir, 8);
|
---|
272 |
|
---|
273 | return cbDir;
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | int rtDirNativeOpen(PRTDIRINTERNAL pDir, uintptr_t hRelativeDir, void *pvNativeRelative)
|
---|
278 | {
|
---|
279 | NOREF(hRelativeDir);
|
---|
280 | NOREF(pvNativeRelative);
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * Convert to a native path and try opendir.
|
---|
284 | */
|
---|
285 | char *pszSlash = NULL;
|
---|
286 | char const *pszNativePath;
|
---|
287 | int rc;
|
---|
288 | if ( !(pDir->fFlags & RTDIR_F_NO_FOLLOW)
|
---|
289 | || pDir->fDirSlash
|
---|
290 | || pDir->cchPath <= 1)
|
---|
291 | rc = rtPathToNative(&pszNativePath, pDir->pszPath, NULL);
|
---|
292 | else
|
---|
293 | {
|
---|
294 | pszSlash = (char *)&pDir->pszPath[pDir->cchPath - 1];
|
---|
295 | *pszSlash = '\0';
|
---|
296 | rc = rtPathToNative(&pszNativePath, pDir->pszPath, NULL);
|
---|
297 | }
|
---|
298 | if (RT_SUCCESS(rc))
|
---|
299 | {
|
---|
300 | if ( !(pDir->fFlags & RTDIR_F_NO_FOLLOW)
|
---|
301 | || pDir->fDirSlash)
|
---|
302 | pDir->pDir = opendir(pszNativePath);
|
---|
303 | else
|
---|
304 | {
|
---|
305 | /*
|
---|
306 | * If we can get fdopendir() and have both O_NOFOLLOW and O_DIRECTORY,
|
---|
307 | * we will use open() to safely open the directory without following
|
---|
308 | * symlinks in the final component, and then use fdopendir to get a DIR
|
---|
309 | * from the file descriptor.
|
---|
310 | *
|
---|
311 | * If we cannot get that, we will use lstat() + opendir() as a fallback.
|
---|
312 | *
|
---|
313 | * We ASSUME that support for the O_NOFOLLOW and O_DIRECTORY flags is
|
---|
314 | * older than fdopendir().
|
---|
315 | */
|
---|
316 | #if defined(O_NOFOLLOW) && defined(O_DIRECTORY)
|
---|
317 | /* Need to resolve fdopendir dynamically. */
|
---|
318 | typedef DIR * (*PFNFDOPENDIR)(int);
|
---|
319 | static PFNFDOPENDIR s_pfnFdOpenDir = NULL;
|
---|
320 | static bool volatile s_fInitalized = false;
|
---|
321 |
|
---|
322 | PFNFDOPENDIR pfnFdOpenDir = s_pfnFdOpenDir;
|
---|
323 | ASMCompilerBarrier();
|
---|
324 | if (s_fInitalized)
|
---|
325 | { /* likely */ }
|
---|
326 | else
|
---|
327 | {
|
---|
328 | pfnFdOpenDir = (PFNFDOPENDIR)(uintptr_t)dlsym(RTLD_DEFAULT, "fdopendir");
|
---|
329 | s_pfnFdOpenDir = pfnFdOpenDir;
|
---|
330 | ASMAtomicWriteBool(&s_fInitalized, true);
|
---|
331 | }
|
---|
332 |
|
---|
333 | if (pfnFdOpenDir)
|
---|
334 | {
|
---|
335 | int fd = open(pszNativePath, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
|
---|
336 | if (fd >= 0)
|
---|
337 | {
|
---|
338 | pDir->pDir = pfnFdOpenDir(fd);
|
---|
339 | if (RT_UNLIKELY(!pDir->pDir))
|
---|
340 | {
|
---|
341 | rc = RTErrConvertFromErrno(errno);
|
---|
342 | close(fd);
|
---|
343 | }
|
---|
344 | }
|
---|
345 | else
|
---|
346 | {
|
---|
347 | /* WSL returns ELOOP here, but we take no chances that O_NOFOLLOW
|
---|
348 | takes precedence over O_DIRECTORY everywhere. */
|
---|
349 | int iErr = errno;
|
---|
350 | if (iErr == ELOOP || iErr == ENOTDIR)
|
---|
351 | {
|
---|
352 | struct stat St;
|
---|
353 | if ( lstat(pszNativePath, &St) == 0
|
---|
354 | && S_ISLNK(St.st_mode))
|
---|
355 | rc = VERR_IS_A_SYMLINK;
|
---|
356 | else
|
---|
357 | rc = RTErrConvertFromErrno(iErr);
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|
361 | else
|
---|
362 | #endif
|
---|
363 | {
|
---|
364 | /* Fallback. This contains a race condition. */
|
---|
365 | struct stat St;
|
---|
366 | if ( lstat(pszNativePath, &St) != 0
|
---|
367 | || !S_ISLNK(St.st_mode))
|
---|
368 | pDir->pDir = opendir(pszNativePath);
|
---|
369 | else
|
---|
370 | rc = VERR_IS_A_SYMLINK;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | if (pDir->pDir)
|
---|
374 | {
|
---|
375 | /*
|
---|
376 | * Init data (allocated as all zeros).
|
---|
377 | */
|
---|
378 | pDir->fDataUnread = false; /* spelling it out */
|
---|
379 | }
|
---|
380 | else if (RT_SUCCESS_NP(rc))
|
---|
381 | rc = RTErrConvertFromErrno(errno);
|
---|
382 |
|
---|
383 | rtPathFreeNative(pszNativePath, pDir->pszPath);
|
---|
384 | }
|
---|
385 | if (pszSlash)
|
---|
386 | *pszSlash = RTPATH_SLASH;
|
---|
387 | return rc;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | RTDECL(int) RTDirClose(RTDIR hDir)
|
---|
392 | {
|
---|
393 | PRTDIRINTERNAL pDir = hDir;
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Validate input.
|
---|
397 | */
|
---|
398 | if (!pDir)
|
---|
399 | return VERR_INVALID_PARAMETER;
|
---|
400 | if (pDir->u32Magic != RTDIR_MAGIC)
|
---|
401 | {
|
---|
402 | AssertMsgFailed(("Invalid pDir=%p\n", pDir));
|
---|
403 | return VERR_INVALID_PARAMETER;
|
---|
404 | }
|
---|
405 |
|
---|
406 | /*
|
---|
407 | * Close the handle.
|
---|
408 | */
|
---|
409 | int rc = VINF_SUCCESS;
|
---|
410 | pDir->u32Magic = RTDIR_MAGIC_DEAD;
|
---|
411 | if (closedir(pDir->pDir))
|
---|
412 | {
|
---|
413 | rc = RTErrConvertFromErrno(errno);
|
---|
414 | AssertMsgFailed(("closedir(%p) -> errno=%d (%Rrc)\n", pDir->pDir, errno, rc));
|
---|
415 | }
|
---|
416 |
|
---|
417 | RTMemFree(pDir);
|
---|
418 | return rc;
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Ensure that there is unread data in the buffer
|
---|
424 | * and that there is a converted filename hanging around.
|
---|
425 | *
|
---|
426 | * @returns IPRT status code.
|
---|
427 | * @param pDir the open directory. Fully validated.
|
---|
428 | */
|
---|
429 | static int rtDirReadMore(PRTDIRINTERNAL pDir)
|
---|
430 | {
|
---|
431 | /** @todo try avoid the rematching on buffer overflow errors. */
|
---|
432 | for (;;)
|
---|
433 | {
|
---|
434 | /*
|
---|
435 | * Fetch data?
|
---|
436 | */
|
---|
437 | if (!pDir->fDataUnread)
|
---|
438 | {
|
---|
439 | struct dirent *pResult = NULL;
|
---|
440 | #if RT_GNUC_PREREQ(4, 6)
|
---|
441 | # pragma GCC diagnostic push
|
---|
442 | # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
---|
443 | #endif
|
---|
444 | int rc = readdir_r(pDir->pDir, &pDir->Data, &pResult);
|
---|
445 | #if RT_GNUC_PREREQ(4, 6)
|
---|
446 | # pragma GCC diagnostic pop
|
---|
447 | #endif
|
---|
448 | if (rc)
|
---|
449 | {
|
---|
450 | rc = RTErrConvertFromErrno(rc);
|
---|
451 | /** @todo Consider translating ENOENT (The current
|
---|
452 | * position of the directory stream is invalid)
|
---|
453 | * differently. */
|
---|
454 | AssertMsg(rc == VERR_FILE_NOT_FOUND, ("%Rrc\n", rc));
|
---|
455 | return rc;
|
---|
456 | }
|
---|
457 | if (!pResult)
|
---|
458 | return VERR_NO_MORE_FILES;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Convert the filename to UTF-8.
|
---|
463 | */
|
---|
464 | if (!pDir->pszName)
|
---|
465 | {
|
---|
466 | int rc = rtPathFromNative(&pDir->pszName, pDir->Data.d_name, pDir->pszPath);
|
---|
467 | if (RT_FAILURE(rc))
|
---|
468 | {
|
---|
469 | pDir->pszName = NULL;
|
---|
470 | return rc;
|
---|
471 | }
|
---|
472 | pDir->cchName = strlen(pDir->pszName);
|
---|
473 | }
|
---|
474 | if ( !pDir->pfnFilter
|
---|
475 | || pDir->pfnFilter(pDir, pDir->pszName))
|
---|
476 | break;
|
---|
477 | rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
|
---|
478 | pDir->pszName = NULL;
|
---|
479 | pDir->fDataUnread = false;
|
---|
480 | }
|
---|
481 |
|
---|
482 | pDir->fDataUnread = true;
|
---|
483 | return VINF_SUCCESS;
|
---|
484 | }
|
---|
485 |
|
---|
486 |
|
---|
487 | #ifdef HAVE_DIRENT_D_TYPE
|
---|
488 | /**
|
---|
489 | * Converts the d_type field to IPRT directory entry type.
|
---|
490 | *
|
---|
491 | * @returns IPRT directory entry type.
|
---|
492 | * @param Unix
|
---|
493 | */
|
---|
494 | static RTDIRENTRYTYPE rtDirType(int iType)
|
---|
495 | {
|
---|
496 | switch (iType)
|
---|
497 | {
|
---|
498 | case DT_UNKNOWN: return RTDIRENTRYTYPE_UNKNOWN;
|
---|
499 | case DT_FIFO: return RTDIRENTRYTYPE_FIFO;
|
---|
500 | case DT_CHR: return RTDIRENTRYTYPE_DEV_CHAR;
|
---|
501 | case DT_DIR: return RTDIRENTRYTYPE_DIRECTORY;
|
---|
502 | case DT_BLK: return RTDIRENTRYTYPE_DEV_BLOCK;
|
---|
503 | case DT_REG: return RTDIRENTRYTYPE_FILE;
|
---|
504 | case DT_LNK: return RTDIRENTRYTYPE_SYMLINK;
|
---|
505 | case DT_SOCK: return RTDIRENTRYTYPE_SOCKET;
|
---|
506 | case DT_WHT: return RTDIRENTRYTYPE_WHITEOUT;
|
---|
507 | default:
|
---|
508 | AssertMsgFailed(("iType=%d\n", iType));
|
---|
509 | return RTDIRENTRYTYPE_UNKNOWN;
|
---|
510 | }
|
---|
511 | }
|
---|
512 | #endif /*HAVE_DIRENT_D_TYPE */
|
---|
513 |
|
---|
514 |
|
---|
515 | RTDECL(int) RTDirRead(RTDIR hDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
|
---|
516 | {
|
---|
517 | PRTDIRINTERNAL pDir = hDir;
|
---|
518 |
|
---|
519 | /*
|
---|
520 | * Validate and digest input.
|
---|
521 | */
|
---|
522 | if (!rtDirValidHandle(pDir))
|
---|
523 | return VERR_INVALID_PARAMETER;
|
---|
524 | AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
|
---|
525 |
|
---|
526 | size_t cbDirEntry = sizeof(*pDirEntry);
|
---|
527 | if (pcbDirEntry)
|
---|
528 | {
|
---|
529 | AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
|
---|
530 | cbDirEntry = *pcbDirEntry;
|
---|
531 | AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
|
---|
532 | ("Invalid *pcbDirEntry=%d (min %zu)\n", *pcbDirEntry, RT_UOFFSETOF(RTDIRENTRYEX, szName[2])),
|
---|
533 | VERR_INVALID_PARAMETER);
|
---|
534 | }
|
---|
535 |
|
---|
536 | /*
|
---|
537 | * Fetch more data if necessary and/or convert the name.
|
---|
538 | */
|
---|
539 | int rc = rtDirReadMore(pDir);
|
---|
540 | if (RT_SUCCESS(rc))
|
---|
541 | {
|
---|
542 | /*
|
---|
543 | * Check if we've got enough space to return the data.
|
---|
544 | */
|
---|
545 | const char *pszName = pDir->pszName;
|
---|
546 | const size_t cchName = pDir->cchName;
|
---|
547 | const size_t cbRequired = RT_UOFFSETOF(RTDIRENTRY, szName[1]) + cchName;
|
---|
548 | if (pcbDirEntry)
|
---|
549 | *pcbDirEntry = cbRequired;
|
---|
550 | if (cbRequired <= cbDirEntry)
|
---|
551 | {
|
---|
552 | /*
|
---|
553 | * Setup the returned data.
|
---|
554 | */
|
---|
555 | pDirEntry->INodeId = pDir->Data.d_ino; /* may need #ifdefing later */
|
---|
556 | #ifdef HAVE_DIRENT_D_TYPE
|
---|
557 | pDirEntry->enmType = rtDirType(pDir->Data.d_type);
|
---|
558 | #else
|
---|
559 | pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
|
---|
560 | #endif
|
---|
561 | pDirEntry->cbName = (uint16_t)cchName;
|
---|
562 | Assert(pDirEntry->cbName == cchName);
|
---|
563 | memcpy(pDirEntry->szName, pszName, cchName + 1);
|
---|
564 |
|
---|
565 | /* free cached data */
|
---|
566 | pDir->fDataUnread = false;
|
---|
567 | rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
|
---|
568 | pDir->pszName = NULL;
|
---|
569 | }
|
---|
570 | else
|
---|
571 | rc = VERR_BUFFER_OVERFLOW;
|
---|
572 | }
|
---|
573 |
|
---|
574 | LogFlow(("RTDirRead(%p:{%s}, %p:{%s}, %p:{%u}): returns %Rrc\n",
|
---|
575 | pDir, pDir->pszPath, pDirEntry, RT_SUCCESS(rc) ? pDirEntry->szName : "<failed>",
|
---|
576 | pcbDirEntry, pcbDirEntry ? *pcbDirEntry : 0, rc));
|
---|
577 | return rc;
|
---|
578 | }
|
---|
579 |
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Fills dummy info into the info structure.
|
---|
583 | * This function is called if we cannot stat the file.
|
---|
584 | *
|
---|
585 | * @param pInfo The struct in question.
|
---|
586 | * @param
|
---|
587 | */
|
---|
588 | static void rtDirSetDummyInfo(PRTFSOBJINFO pInfo, RTDIRENTRYTYPE enmType)
|
---|
589 | {
|
---|
590 | pInfo->cbObject = 0;
|
---|
591 | pInfo->cbAllocated = 0;
|
---|
592 | RTTimeSpecSetNano(&pInfo->AccessTime, 0);
|
---|
593 | RTTimeSpecSetNano(&pInfo->ModificationTime, 0);
|
---|
594 | RTTimeSpecSetNano(&pInfo->ChangeTime, 0);
|
---|
595 | RTTimeSpecSetNano(&pInfo->BirthTime, 0);
|
---|
596 | memset(&pInfo->Attr, 0, sizeof(pInfo->Attr));
|
---|
597 | pInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
598 | switch (enmType)
|
---|
599 | {
|
---|
600 | default:
|
---|
601 | case RTDIRENTRYTYPE_UNKNOWN: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL; break;
|
---|
602 | case RTDIRENTRYTYPE_FIFO: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FIFO; break;
|
---|
603 | case RTDIRENTRYTYPE_DEV_CHAR: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_CHAR; break;
|
---|
604 | case RTDIRENTRYTYPE_DIRECTORY: pInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY; break;
|
---|
605 | case RTDIRENTRYTYPE_DEV_BLOCK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_BLOCK; break;
|
---|
606 | case RTDIRENTRYTYPE_FILE: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE; break;
|
---|
607 | case RTDIRENTRYTYPE_SYMLINK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SYMLINK; break;
|
---|
608 | case RTDIRENTRYTYPE_SOCKET: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SOCKET; break;
|
---|
609 | case RTDIRENTRYTYPE_WHITEOUT: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_WHITEOUT; break;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | RTDECL(int) RTDirReadEx(RTDIR hDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
|
---|
615 | RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
|
---|
616 | {
|
---|
617 | PRTDIRINTERNAL pDir = hDir;
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * Validate and digest input.
|
---|
621 | */
|
---|
622 | if (!rtDirValidHandle(pDir))
|
---|
623 | return VERR_INVALID_PARAMETER;
|
---|
624 | AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
|
---|
625 | AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
|
---|
626 | && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
|
---|
627 | ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
|
---|
628 | VERR_INVALID_PARAMETER);
|
---|
629 | AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
630 | size_t cbDirEntry = sizeof(*pDirEntry);
|
---|
631 | if (pcbDirEntry)
|
---|
632 | {
|
---|
633 | AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
|
---|
634 | cbDirEntry = *pcbDirEntry;
|
---|
635 | AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
|
---|
636 | ("Invalid *pcbDirEntry=%zu (min %zu)\n", *pcbDirEntry, RT_UOFFSETOF(RTDIRENTRYEX, szName[2])),
|
---|
637 | VERR_INVALID_PARAMETER);
|
---|
638 | }
|
---|
639 |
|
---|
640 | /*
|
---|
641 | * Fetch more data if necessary and/or convert the name.
|
---|
642 | */
|
---|
643 | int rc = rtDirReadMore(pDir);
|
---|
644 | if (RT_SUCCESS(rc))
|
---|
645 | {
|
---|
646 | /*
|
---|
647 | * Check if we've got enough space to return the data.
|
---|
648 | */
|
---|
649 | const char *pszName = pDir->pszName;
|
---|
650 | const size_t cchName = pDir->cchName;
|
---|
651 | const size_t cbRequired = RT_UOFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
|
---|
652 | if (pcbDirEntry)
|
---|
653 | *pcbDirEntry = cbRequired;
|
---|
654 | if (cbRequired <= cbDirEntry)
|
---|
655 | {
|
---|
656 | /*
|
---|
657 | * Setup the returned data.
|
---|
658 | */
|
---|
659 | pDirEntry->cwcShortName = 0;
|
---|
660 | pDirEntry->wszShortName[0] = 0;
|
---|
661 | pDirEntry->cbName = (uint16_t)cchName;
|
---|
662 | Assert(pDirEntry->cbName == cchName);
|
---|
663 | memcpy(pDirEntry->szName, pszName, cchName + 1);
|
---|
664 |
|
---|
665 | /* get the info data */
|
---|
666 | size_t cch = cchName + pDir->cchPath + 1;
|
---|
667 | char *pszNamePath = (char *)alloca(cch);
|
---|
668 | if (pszNamePath)
|
---|
669 | {
|
---|
670 | memcpy(pszNamePath, pDir->pszPath, pDir->cchPath);
|
---|
671 | memcpy(pszNamePath + pDir->cchPath, pszName, cchName + 1);
|
---|
672 | rc = RTPathQueryInfoEx(pszNamePath, &pDirEntry->Info, enmAdditionalAttribs, fFlags);
|
---|
673 | }
|
---|
674 | else
|
---|
675 | rc = VERR_NO_MEMORY;
|
---|
676 | if (RT_FAILURE(rc))
|
---|
677 | {
|
---|
678 | #ifdef HAVE_DIRENT_D_TYPE
|
---|
679 | rtDirSetDummyInfo(&pDirEntry->Info, rtDirType(pDir->Data.d_type));
|
---|
680 | #else
|
---|
681 | rtDirSetDummyInfo(&pDirEntry->Info, RTDIRENTRYTYPE_UNKNOWN);
|
---|
682 | #endif
|
---|
683 | rc = VWRN_NO_DIRENT_INFO;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /* free cached data */
|
---|
687 | pDir->fDataUnread = false;
|
---|
688 | rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
|
---|
689 | pDir->pszName = NULL;
|
---|
690 | }
|
---|
691 | else
|
---|
692 | rc = VERR_BUFFER_OVERFLOW;
|
---|
693 | }
|
---|
694 |
|
---|
695 | return rc;
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | RTDECL(int) RTDirRewind(RTDIR hDir)
|
---|
700 | {
|
---|
701 | PRTDIRINTERNAL pDir = hDir;
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * Validate and digest input.
|
---|
705 | */
|
---|
706 | if (!rtDirValidHandle(pDir))
|
---|
707 | return VERR_INVALID_PARAMETER;
|
---|
708 |
|
---|
709 | /*
|
---|
710 | * Do the rewinding.
|
---|
711 | */
|
---|
712 | /** @todo OS/2 does not rescan the directory as it should. */
|
---|
713 | rewinddir(pDir->pDir);
|
---|
714 | pDir->fDataUnread = false;
|
---|
715 |
|
---|
716 | return VINF_SUCCESS;
|
---|
717 | }
|
---|
718 |
|
---|
719 |
|
---|
720 | RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
721 | {
|
---|
722 | /*
|
---|
723 | * Validate input.
|
---|
724 | */
|
---|
725 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
726 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
727 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
728 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
729 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
730 |
|
---|
731 | /*
|
---|
732 | * Take common cause with RTPathRename.
|
---|
733 | */
|
---|
734 | int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_DIRECTORY);
|
---|
735 |
|
---|
736 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}): returns %Rrc\n",
|
---|
737 | pszSrc, pszSrc, pszDst, pszDst, rc));
|
---|
738 | return rc;
|
---|
739 | }
|
---|
740 |
|
---|