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