VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/dir-posix.cpp@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette