1 | /* $Id: dir-posix.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Directory manipulation, POSIX.
|
---|
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 <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 <stdio.h>
|
---|
39 |
|
---|
40 | #include <iprt/dir.h>
|
---|
41 | #include "internal/iprt.h"
|
---|
42 |
|
---|
43 | #include <iprt/alloca.h>
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/log.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/param.h>
|
---|
49 | #include <iprt/path.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include "internal/dir.h"
|
---|
52 | #include "internal/fs.h"
|
---|
53 | #include "internal/path.h"
|
---|
54 |
|
---|
55 | #if !defined(RT_OS_SOLARIS) && !defined(RT_OS_HAIKU)
|
---|
56 | # define HAVE_DIRENT_D_TYPE 1
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 | RTDECL(bool) RTDirExists(const char *pszPath)
|
---|
61 | {
|
---|
62 | bool fRc = false;
|
---|
63 | char const *pszNativePath;
|
---|
64 | int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
65 | if (RT_SUCCESS(rc))
|
---|
66 | {
|
---|
67 | struct stat s;
|
---|
68 | fRc = !stat(pszNativePath, &s)
|
---|
69 | && S_ISDIR(s.st_mode);
|
---|
70 |
|
---|
71 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
72 | }
|
---|
73 |
|
---|
74 | LogFlow(("RTDirExists(%p={%s}): returns %RTbool\n", pszPath, pszPath, fRc));
|
---|
75 | return fRc;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode, uint32_t fCreate)
|
---|
80 | {
|
---|
81 | RT_NOREF_PV(fCreate);
|
---|
82 |
|
---|
83 | int rc;
|
---|
84 | fMode = rtFsModeNormalize(fMode, pszPath, 0);
|
---|
85 | if (rtFsModeIsValidPermissions(fMode))
|
---|
86 | {
|
---|
87 | char const *pszNativePath;
|
---|
88 | rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
89 | if (RT_SUCCESS(rc))
|
---|
90 | {
|
---|
91 | if (mkdir(pszNativePath, fMode & RTFS_UNIX_MASK))
|
---|
92 | {
|
---|
93 | rc = errno;
|
---|
94 | bool fVerifyIsDir = true;
|
---|
95 | #ifdef RT_OS_SOLARIS
|
---|
96 | /*
|
---|
97 | * mkdir on nfs mount points has been/is busted in various
|
---|
98 | * during the Nevada development cycle. We've observed:
|
---|
99 | * - Build 111b (2009.06) returns EACCES.
|
---|
100 | * - Build ca. 70-80 returns ENOSYS.
|
---|
101 | */
|
---|
102 | if ( rc == ENOSYS
|
---|
103 | || rc == EACCES)
|
---|
104 | {
|
---|
105 | rc = RTErrConvertFromErrno(rc);
|
---|
106 | fVerifyIsDir = false; /* We'll check if it's a dir ourselves since we're going to stat() anyway. */
|
---|
107 | struct stat st;
|
---|
108 | if (!stat(pszNativePath, &st))
|
---|
109 | {
|
---|
110 | rc = VERR_ALREADY_EXISTS;
|
---|
111 | if (!S_ISDIR(st.st_mode))
|
---|
112 | rc = VERR_IS_A_FILE;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else
|
---|
116 | rc = RTErrConvertFromErrno(rc);
|
---|
117 | #else
|
---|
118 | rc = RTErrConvertFromErrno(rc);
|
---|
119 | #endif
|
---|
120 | if ( rc == VERR_ALREADY_EXISTS
|
---|
121 | && fVerifyIsDir == true)
|
---|
122 | {
|
---|
123 | /*
|
---|
124 | * Verify that it really exists as a directory.
|
---|
125 | */
|
---|
126 | struct stat st;
|
---|
127 | if (!stat(pszNativePath, &st) && !S_ISDIR(st.st_mode))
|
---|
128 | rc = VERR_IS_A_FILE;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | AssertMsgFailed(("Invalid file mode! %RTfmode\n", fMode));
|
---|
138 | rc = VERR_INVALID_FMODE;
|
---|
139 | }
|
---|
140 | LogFlow(("RTDirCreate(%p={%s}, %RTfmode): returns %Rrc\n", pszPath, pszPath, fMode, rc));
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(int) RTDirRemove(const char *pszPath)
|
---|
146 | {
|
---|
147 | char const *pszNativePath;
|
---|
148 | int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | {
|
---|
151 | if (rmdir(pszNativePath))
|
---|
152 | rc = RTErrConvertFromErrno(errno);
|
---|
153 |
|
---|
154 | rtPathFreeNative(pszNativePath, pszPath);
|
---|
155 | }
|
---|
156 |
|
---|
157 | LogFlow(("RTDirRemove(%p={%s}): returns %Rrc\n", pszPath, pszPath, rc));
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(int) RTDirFlush(const char *pszPath)
|
---|
163 | {
|
---|
164 | /*
|
---|
165 | * Linux: The fsync() man page hints at this being required for ensuring
|
---|
166 | * consistency between directory and file in case of a crash.
|
---|
167 | *
|
---|
168 | * Solaris: No mentioned is made of directories on the fsync man page.
|
---|
169 | * While rename+fsync will do what we want on ZFS, the code needs more
|
---|
170 | * careful studying wrt whether the directory entry of a new file is
|
---|
171 | * implicitly synced when the file is synced (it's very likely for ZFS).
|
---|
172 | *
|
---|
173 | * FreeBSD: The FFS fsync code seems to flush the directory entry as well
|
---|
174 | * in some cases. Don't know exactly what's up with rename, but from the
|
---|
175 | * look of things fsync(dir) should work.
|
---|
176 | */
|
---|
177 | int rc;
|
---|
178 | #ifdef O_DIRECTORY
|
---|
179 | int fd = open(pszPath, O_RDONLY | O_DIRECTORY, 0);
|
---|
180 | #else
|
---|
181 | int fd = open(pszPath, O_RDONLY, 0);
|
---|
182 | #endif
|
---|
183 | if (fd >= 0)
|
---|
184 | {
|
---|
185 | if (fsync(fd) == 0)
|
---|
186 | rc = VINF_SUCCESS;
|
---|
187 | else
|
---|
188 | {
|
---|
189 | /* Linux fsync(2) man page documents both errors as an indication
|
---|
190 | * that the file descriptor can't be flushed (seen EINVAL for usual
|
---|
191 | * directories on CIFS). BSD (OS X) fsync(2) documents only the
|
---|
192 | * latter, and Solaris fsync(3C) pretends there is no problem. */
|
---|
193 | if (errno == EROFS || errno == EINVAL)
|
---|
194 | rc = VERR_NOT_SUPPORTED;
|
---|
195 | else
|
---|
196 | rc = RTErrConvertFromErrno(errno);
|
---|
197 | }
|
---|
198 | close(fd);
|
---|
199 | }
|
---|
200 | else
|
---|
201 | rc = RTErrConvertFromErrno(errno);
|
---|
202 | return rc;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | size_t rtDirNativeGetStructSize(const char *pszPath)
|
---|
207 | {
|
---|
208 | long cbNameMax = pathconf(pszPath, _PC_NAME_MAX);
|
---|
209 | # ifdef NAME_MAX
|
---|
210 | if (cbNameMax < NAME_MAX) /* This is plain paranoia, but it doesn't hurt. */
|
---|
211 | cbNameMax = NAME_MAX;
|
---|
212 | # endif
|
---|
213 | # ifdef _XOPEN_NAME_MAX
|
---|
214 | if (cbNameMax < _XOPEN_NAME_MAX) /* Ditto. */
|
---|
215 | cbNameMax = _XOPEN_NAME_MAX;
|
---|
216 | # endif
|
---|
217 | size_t cbDir = RT_OFFSETOF(RTDIR, Data.d_name[cbNameMax + 1]);
|
---|
218 | if (cbDir < sizeof(RTDIR)) /* Ditto. */
|
---|
219 | cbDir = sizeof(RTDIR);
|
---|
220 | cbDir = RT_ALIGN_Z(cbDir, 8);
|
---|
221 |
|
---|
222 | return cbDir;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | int rtDirNativeOpen(PRTDIR pDir, char *pszPathBuf)
|
---|
227 | {
|
---|
228 | NOREF(pszPathBuf); /* only used on windows */
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Convert to a native path and try opendir.
|
---|
232 | */
|
---|
233 | char const *pszNativePath;
|
---|
234 | int rc = rtPathToNative(&pszNativePath, pDir->pszPath, NULL);
|
---|
235 | if (RT_SUCCESS(rc))
|
---|
236 | {
|
---|
237 | pDir->pDir = opendir(pszNativePath);
|
---|
238 | if (pDir->pDir)
|
---|
239 | {
|
---|
240 | /*
|
---|
241 | * Init data (allocated as all zeros).
|
---|
242 | */
|
---|
243 | pDir->fDataUnread = false; /* spelling it out */
|
---|
244 | }
|
---|
245 | else
|
---|
246 | rc = RTErrConvertFromErrno(errno);
|
---|
247 |
|
---|
248 | rtPathFreeNative(pszNativePath, pDir->pszPath);
|
---|
249 | }
|
---|
250 |
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | RTDECL(int) RTDirClose(PRTDIR pDir)
|
---|
256 | {
|
---|
257 | /*
|
---|
258 | * Validate input.
|
---|
259 | */
|
---|
260 | if (!pDir)
|
---|
261 | return VERR_INVALID_PARAMETER;
|
---|
262 | if (pDir->u32Magic != RTDIR_MAGIC)
|
---|
263 | {
|
---|
264 | AssertMsgFailed(("Invalid pDir=%p\n", pDir));
|
---|
265 | return VERR_INVALID_PARAMETER;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Close the handle.
|
---|
270 | */
|
---|
271 | int rc = VINF_SUCCESS;
|
---|
272 | pDir->u32Magic = RTDIR_MAGIC_DEAD;
|
---|
273 | if (closedir(pDir->pDir))
|
---|
274 | {
|
---|
275 | rc = RTErrConvertFromErrno(errno);
|
---|
276 | AssertMsgFailed(("closedir(%p) -> errno=%d (%Rrc)\n", pDir->pDir, errno, rc));
|
---|
277 | }
|
---|
278 |
|
---|
279 | RTMemFree(pDir);
|
---|
280 | return rc;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Ensure that there is unread data in the buffer
|
---|
286 | * and that there is a converted filename hanging around.
|
---|
287 | *
|
---|
288 | * @returns IPRT status code.
|
---|
289 | * @param pDir the open directory. Fully validated.
|
---|
290 | */
|
---|
291 | static int rtDirReadMore(PRTDIR pDir)
|
---|
292 | {
|
---|
293 | /** @todo try avoid the rematching on buffer overflow errors. */
|
---|
294 | for (;;)
|
---|
295 | {
|
---|
296 | /*
|
---|
297 | * Fetch data?
|
---|
298 | */
|
---|
299 | if (!pDir->fDataUnread)
|
---|
300 | {
|
---|
301 | struct dirent *pResult = NULL;
|
---|
302 | #if RT_GNUC_PREREQ(4, 6)
|
---|
303 | # pragma GCC diagnostic push
|
---|
304 | # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
---|
305 | #endif
|
---|
306 | int rc = readdir_r(pDir->pDir, &pDir->Data, &pResult);
|
---|
307 | #if RT_GNUC_PREREQ(4, 6)
|
---|
308 | # pragma GCC diagnostic pop
|
---|
309 | #endif
|
---|
310 | if (rc)
|
---|
311 | {
|
---|
312 | rc = RTErrConvertFromErrno(rc);
|
---|
313 | /** @todo Consider translating ENOENT (The current
|
---|
314 | * position of the directory stream is invalid)
|
---|
315 | * differently. */
|
---|
316 | AssertMsg(rc == VERR_FILE_NOT_FOUND, ("%Rrc\n", rc));
|
---|
317 | return rc;
|
---|
318 | }
|
---|
319 | if (!pResult)
|
---|
320 | return VERR_NO_MORE_FILES;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * Convert the filename to UTF-8.
|
---|
325 | */
|
---|
326 | if (!pDir->pszName)
|
---|
327 | {
|
---|
328 | int rc = rtPathFromNative(&pDir->pszName, pDir->Data.d_name, pDir->pszPath);
|
---|
329 | if (RT_FAILURE(rc))
|
---|
330 | {
|
---|
331 | pDir->pszName = NULL;
|
---|
332 | return rc;
|
---|
333 | }
|
---|
334 | pDir->cchName = strlen(pDir->pszName);
|
---|
335 | }
|
---|
336 | if ( !pDir->pfnFilter
|
---|
337 | || pDir->pfnFilter(pDir, pDir->pszName))
|
---|
338 | break;
|
---|
339 | rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
|
---|
340 | pDir->pszName = NULL;
|
---|
341 | pDir->fDataUnread = false;
|
---|
342 | }
|
---|
343 |
|
---|
344 | pDir->fDataUnread = true;
|
---|
345 | return VINF_SUCCESS;
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | #ifdef HAVE_DIRENT_D_TYPE
|
---|
350 | /**
|
---|
351 | * Converts the d_type field to IPRT directory entry type.
|
---|
352 | *
|
---|
353 | * @returns IPRT directory entry type.
|
---|
354 | * @param Unix
|
---|
355 | */
|
---|
356 | static RTDIRENTRYTYPE rtDirType(int iType)
|
---|
357 | {
|
---|
358 | switch (iType)
|
---|
359 | {
|
---|
360 | case DT_UNKNOWN: return RTDIRENTRYTYPE_UNKNOWN;
|
---|
361 | case DT_FIFO: return RTDIRENTRYTYPE_FIFO;
|
---|
362 | case DT_CHR: return RTDIRENTRYTYPE_DEV_CHAR;
|
---|
363 | case DT_DIR: return RTDIRENTRYTYPE_DIRECTORY;
|
---|
364 | case DT_BLK: return RTDIRENTRYTYPE_DEV_BLOCK;
|
---|
365 | case DT_REG: return RTDIRENTRYTYPE_FILE;
|
---|
366 | case DT_LNK: return RTDIRENTRYTYPE_SYMLINK;
|
---|
367 | case DT_SOCK: return RTDIRENTRYTYPE_SOCKET;
|
---|
368 | case DT_WHT: return RTDIRENTRYTYPE_WHITEOUT;
|
---|
369 | default:
|
---|
370 | AssertMsgFailed(("iType=%d\n", iType));
|
---|
371 | return RTDIRENTRYTYPE_UNKNOWN;
|
---|
372 | }
|
---|
373 | }
|
---|
374 | #endif /*HAVE_DIRENT_D_TYPE */
|
---|
375 |
|
---|
376 |
|
---|
377 | RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
|
---|
378 | {
|
---|
379 | /*
|
---|
380 | * Validate and digest input.
|
---|
381 | */
|
---|
382 | if (!rtDirValidHandle(pDir))
|
---|
383 | return VERR_INVALID_PARAMETER;
|
---|
384 | AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
|
---|
385 |
|
---|
386 | size_t cbDirEntry = sizeof(*pDirEntry);
|
---|
387 | if (pcbDirEntry)
|
---|
388 | {
|
---|
389 | AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
|
---|
390 | cbDirEntry = *pcbDirEntry;
|
---|
391 | AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
|
---|
392 | ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
|
---|
393 | VERR_INVALID_PARAMETER);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * Fetch more data if necessary and/or convert the name.
|
---|
398 | */
|
---|
399 | int rc = rtDirReadMore(pDir);
|
---|
400 | if (RT_SUCCESS(rc))
|
---|
401 | {
|
---|
402 | /*
|
---|
403 | * Check if we've got enough space to return the data.
|
---|
404 | */
|
---|
405 | const char *pszName = pDir->pszName;
|
---|
406 | const size_t cchName = pDir->cchName;
|
---|
407 | const size_t cbRequired = RT_OFFSETOF(RTDIRENTRY, szName[1]) + cchName;
|
---|
408 | if (pcbDirEntry)
|
---|
409 | *pcbDirEntry = cbRequired;
|
---|
410 | if (cbRequired <= cbDirEntry)
|
---|
411 | {
|
---|
412 | /*
|
---|
413 | * Setup the returned data.
|
---|
414 | */
|
---|
415 | pDirEntry->INodeId = pDir->Data.d_ino; /* may need #ifdefing later */
|
---|
416 | #ifdef HAVE_DIRENT_D_TYPE
|
---|
417 | pDirEntry->enmType = rtDirType(pDir->Data.d_type);
|
---|
418 | #else
|
---|
419 | pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
|
---|
420 | #endif
|
---|
421 | pDirEntry->cbName = (uint16_t)cchName;
|
---|
422 | Assert(pDirEntry->cbName == cchName);
|
---|
423 | memcpy(pDirEntry->szName, pszName, cchName + 1);
|
---|
424 |
|
---|
425 | /* free cached data */
|
---|
426 | pDir->fDataUnread = false;
|
---|
427 | rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
|
---|
428 | pDir->pszName = NULL;
|
---|
429 | }
|
---|
430 | else
|
---|
431 | rc = VERR_BUFFER_OVERFLOW;
|
---|
432 | }
|
---|
433 |
|
---|
434 | LogFlow(("RTDirRead(%p:{%s}, %p:{%s}, %p:{%u}): returns %Rrc\n",
|
---|
435 | pDir, pDir->pszPath, pDirEntry, RT_SUCCESS(rc) ? pDirEntry->szName : "<failed>",
|
---|
436 | pcbDirEntry, pcbDirEntry ? *pcbDirEntry : 0, rc));
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * Fills dummy info into the info structure.
|
---|
443 | * This function is called if we cannot stat the file.
|
---|
444 | *
|
---|
445 | * @param pInfo The struct in question.
|
---|
446 | * @param
|
---|
447 | */
|
---|
448 | static void rtDirSetDummyInfo(PRTFSOBJINFO pInfo, RTDIRENTRYTYPE enmType)
|
---|
449 | {
|
---|
450 | pInfo->cbObject = 0;
|
---|
451 | pInfo->cbAllocated = 0;
|
---|
452 | RTTimeSpecSetNano(&pInfo->AccessTime, 0);
|
---|
453 | RTTimeSpecSetNano(&pInfo->ModificationTime, 0);
|
---|
454 | RTTimeSpecSetNano(&pInfo->ChangeTime, 0);
|
---|
455 | RTTimeSpecSetNano(&pInfo->BirthTime, 0);
|
---|
456 | memset(&pInfo->Attr, 0, sizeof(pInfo->Attr));
|
---|
457 | pInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
458 | switch (enmType)
|
---|
459 | {
|
---|
460 | default:
|
---|
461 | case RTDIRENTRYTYPE_UNKNOWN: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL; break;
|
---|
462 | case RTDIRENTRYTYPE_FIFO: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FIFO; break;
|
---|
463 | case RTDIRENTRYTYPE_DEV_CHAR: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_CHAR; break;
|
---|
464 | case RTDIRENTRYTYPE_DIRECTORY: pInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY; break;
|
---|
465 | case RTDIRENTRYTYPE_DEV_BLOCK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_BLOCK; break;
|
---|
466 | case RTDIRENTRYTYPE_FILE: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE; break;
|
---|
467 | case RTDIRENTRYTYPE_SYMLINK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SYMLINK; break;
|
---|
468 | case RTDIRENTRYTYPE_SOCKET: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SOCKET; break;
|
---|
469 | case RTDIRENTRYTYPE_WHITEOUT: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_WHITEOUT; break;
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
|
---|
475 | {
|
---|
476 | /*
|
---|
477 | * Validate and digest input.
|
---|
478 | */
|
---|
479 | if (!rtDirValidHandle(pDir))
|
---|
480 | return VERR_INVALID_PARAMETER;
|
---|
481 | AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
|
---|
482 | AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
|
---|
483 | && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
|
---|
484 | ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
|
---|
485 | VERR_INVALID_PARAMETER);
|
---|
486 | AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
487 | size_t cbDirEntry = sizeof(*pDirEntry);
|
---|
488 | if (pcbDirEntry)
|
---|
489 | {
|
---|
490 | AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
|
---|
491 | cbDirEntry = *pcbDirEntry;
|
---|
492 | AssertMsgReturn(cbDirEntry >= (unsigned)RT_OFFSETOF(RTDIRENTRYEX, szName[2]),
|
---|
493 | ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
|
---|
494 | VERR_INVALID_PARAMETER);
|
---|
495 | }
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * Fetch more data if necessary and/or convert the name.
|
---|
499 | */
|
---|
500 | int rc = rtDirReadMore(pDir);
|
---|
501 | if (RT_SUCCESS(rc))
|
---|
502 | {
|
---|
503 | /*
|
---|
504 | * Check if we've got enough space to return the data.
|
---|
505 | */
|
---|
506 | const char *pszName = pDir->pszName;
|
---|
507 | const size_t cchName = pDir->cchName;
|
---|
508 | const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
|
---|
509 | if (pcbDirEntry)
|
---|
510 | *pcbDirEntry = cbRequired;
|
---|
511 | if (cbRequired <= cbDirEntry)
|
---|
512 | {
|
---|
513 | /*
|
---|
514 | * Setup the returned data.
|
---|
515 | */
|
---|
516 | pDirEntry->cwcShortName = 0;
|
---|
517 | pDirEntry->wszShortName[0] = 0;
|
---|
518 | pDirEntry->cbName = (uint16_t)cchName;
|
---|
519 | Assert(pDirEntry->cbName == cchName);
|
---|
520 | memcpy(pDirEntry->szName, pszName, cchName + 1);
|
---|
521 |
|
---|
522 | /* get the info data */
|
---|
523 | size_t cch = cchName + pDir->cchPath + 1;
|
---|
524 | char *pszNamePath = (char *)alloca(cch);
|
---|
525 | if (pszNamePath)
|
---|
526 | {
|
---|
527 | memcpy(pszNamePath, pDir->pszPath, pDir->cchPath);
|
---|
528 | memcpy(pszNamePath + pDir->cchPath, pszName, cchName + 1);
|
---|
529 | rc = RTPathQueryInfoEx(pszNamePath, &pDirEntry->Info, enmAdditionalAttribs, fFlags);
|
---|
530 | }
|
---|
531 | else
|
---|
532 | rc = VERR_NO_MEMORY;
|
---|
533 | if (RT_FAILURE(rc))
|
---|
534 | {
|
---|
535 | #ifdef HAVE_DIRENT_D_TYPE
|
---|
536 | rtDirSetDummyInfo(&pDirEntry->Info, rtDirType(pDir->Data.d_type));
|
---|
537 | #else
|
---|
538 | rtDirSetDummyInfo(&pDirEntry->Info, RTDIRENTRYTYPE_UNKNOWN);
|
---|
539 | #endif
|
---|
540 | rc = VWRN_NO_DIRENT_INFO;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /* free cached data */
|
---|
544 | pDir->fDataUnread = false;
|
---|
545 | rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
|
---|
546 | pDir->pszName = NULL;
|
---|
547 | }
|
---|
548 | else
|
---|
549 | rc = VERR_BUFFER_OVERFLOW;
|
---|
550 | }
|
---|
551 |
|
---|
552 | return rc;
|
---|
553 | }
|
---|
554 |
|
---|
555 |
|
---|
556 | RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
557 | {
|
---|
558 | /*
|
---|
559 | * Validate input.
|
---|
560 | */
|
---|
561 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
562 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
563 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
564 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
565 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
566 |
|
---|
567 | /*
|
---|
568 | * Take common cause with RTPathRename.
|
---|
569 | */
|
---|
570 | int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_DIRECTORY);
|
---|
571 |
|
---|
572 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}): returns %Rrc\n",
|
---|
573 | pszSrc, pszSrc, pszDst, pszDst, rc));
|
---|
574 | return rc;
|
---|
575 | }
|
---|
576 |
|
---|