VirtualBox

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

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

IPRT/RTDirRemove/posix: Correction to r131730 that makes the kludge work on OS/2 too. bugref:9172

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