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