VirtualBox

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

Last change on this file since 69691 was 69691, checked in by vboxsync, 7 years ago

iprt: Started on RTDirRel for NT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.6 KB
Line 
1/* $Id: dir-posix.cpp 69691 2017-11-14 15:27:52Z 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
60RTDECL(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
79RTDECL(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
145RTDECL(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
162RTDECL(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
206size_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
226int rtDirNativeOpen(PRTDIR pDir, char *pszPathBuf, uintptr_t hRelativeDir, void *pvNativeRelative)
227{
228 NOREF(pszPathBuf); /* only used on windows */
229 NOREF(hRelativeDir);
230 NOREF(pvNativeRelative);
231
232 /*
233 * Convert to a native path and try opendir.
234 */
235 char const *pszNativePath;
236 int rc = rtPathToNative(&pszNativePath, pDir->pszPath, NULL);
237 if (RT_SUCCESS(rc))
238 {
239 pDir->pDir = opendir(pszNativePath);
240 if (pDir->pDir)
241 {
242 /*
243 * Init data (allocated as all zeros).
244 */
245 pDir->fDataUnread = false; /* spelling it out */
246 }
247 else
248 rc = RTErrConvertFromErrno(errno);
249
250 rtPathFreeNative(pszNativePath, pDir->pszPath);
251 }
252
253 return rc;
254}
255
256
257RTDECL(int) RTDirClose(PRTDIR pDir)
258{
259 /*
260 * Validate input.
261 */
262 if (!pDir)
263 return VERR_INVALID_PARAMETER;
264 if (pDir->u32Magic != RTDIR_MAGIC)
265 {
266 AssertMsgFailed(("Invalid pDir=%p\n", pDir));
267 return VERR_INVALID_PARAMETER;
268 }
269
270 /*
271 * Close the handle.
272 */
273 int rc = VINF_SUCCESS;
274 pDir->u32Magic = RTDIR_MAGIC_DEAD;
275 if (closedir(pDir->pDir))
276 {
277 rc = RTErrConvertFromErrno(errno);
278 AssertMsgFailed(("closedir(%p) -> errno=%d (%Rrc)\n", pDir->pDir, errno, rc));
279 }
280
281 RTMemFree(pDir);
282 return rc;
283}
284
285
286/**
287 * Ensure that there is unread data in the buffer
288 * and that there is a converted filename hanging around.
289 *
290 * @returns IPRT status code.
291 * @param pDir the open directory. Fully validated.
292 */
293static int rtDirReadMore(PRTDIR pDir)
294{
295 /** @todo try avoid the rematching on buffer overflow errors. */
296 for (;;)
297 {
298 /*
299 * Fetch data?
300 */
301 if (!pDir->fDataUnread)
302 {
303 struct dirent *pResult = NULL;
304#if RT_GNUC_PREREQ(4, 6)
305# pragma GCC diagnostic push
306# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
307#endif
308 int rc = readdir_r(pDir->pDir, &pDir->Data, &pResult);
309#if RT_GNUC_PREREQ(4, 6)
310# pragma GCC diagnostic pop
311#endif
312 if (rc)
313 {
314 rc = RTErrConvertFromErrno(rc);
315 /** @todo Consider translating ENOENT (The current
316 * position of the directory stream is invalid)
317 * differently. */
318 AssertMsg(rc == VERR_FILE_NOT_FOUND, ("%Rrc\n", rc));
319 return rc;
320 }
321 if (!pResult)
322 return VERR_NO_MORE_FILES;
323 }
324
325 /*
326 * Convert the filename to UTF-8.
327 */
328 if (!pDir->pszName)
329 {
330 int rc = rtPathFromNative(&pDir->pszName, pDir->Data.d_name, pDir->pszPath);
331 if (RT_FAILURE(rc))
332 {
333 pDir->pszName = NULL;
334 return rc;
335 }
336 pDir->cchName = strlen(pDir->pszName);
337 }
338 if ( !pDir->pfnFilter
339 || pDir->pfnFilter(pDir, pDir->pszName))
340 break;
341 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
342 pDir->pszName = NULL;
343 pDir->fDataUnread = false;
344 }
345
346 pDir->fDataUnread = true;
347 return VINF_SUCCESS;
348}
349
350
351#ifdef HAVE_DIRENT_D_TYPE
352/**
353 * Converts the d_type field to IPRT directory entry type.
354 *
355 * @returns IPRT directory entry type.
356 * @param Unix
357 */
358static RTDIRENTRYTYPE rtDirType(int iType)
359{
360 switch (iType)
361 {
362 case DT_UNKNOWN: return RTDIRENTRYTYPE_UNKNOWN;
363 case DT_FIFO: return RTDIRENTRYTYPE_FIFO;
364 case DT_CHR: return RTDIRENTRYTYPE_DEV_CHAR;
365 case DT_DIR: return RTDIRENTRYTYPE_DIRECTORY;
366 case DT_BLK: return RTDIRENTRYTYPE_DEV_BLOCK;
367 case DT_REG: return RTDIRENTRYTYPE_FILE;
368 case DT_LNK: return RTDIRENTRYTYPE_SYMLINK;
369 case DT_SOCK: return RTDIRENTRYTYPE_SOCKET;
370 case DT_WHT: return RTDIRENTRYTYPE_WHITEOUT;
371 default:
372 AssertMsgFailed(("iType=%d\n", iType));
373 return RTDIRENTRYTYPE_UNKNOWN;
374 }
375}
376#endif /*HAVE_DIRENT_D_TYPE */
377
378
379RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
380{
381 /*
382 * Validate and digest input.
383 */
384 if (!rtDirValidHandle(pDir))
385 return VERR_INVALID_PARAMETER;
386 AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
387
388 size_t cbDirEntry = sizeof(*pDirEntry);
389 if (pcbDirEntry)
390 {
391 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
392 cbDirEntry = *pcbDirEntry;
393 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
394 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
395 VERR_INVALID_PARAMETER);
396 }
397
398 /*
399 * Fetch more data if necessary and/or convert the name.
400 */
401 int rc = rtDirReadMore(pDir);
402 if (RT_SUCCESS(rc))
403 {
404 /*
405 * Check if we've got enough space to return the data.
406 */
407 const char *pszName = pDir->pszName;
408 const size_t cchName = pDir->cchName;
409 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRY, szName[1]) + cchName;
410 if (pcbDirEntry)
411 *pcbDirEntry = cbRequired;
412 if (cbRequired <= cbDirEntry)
413 {
414 /*
415 * Setup the returned data.
416 */
417 pDirEntry->INodeId = pDir->Data.d_ino; /* may need #ifdefing later */
418#ifdef HAVE_DIRENT_D_TYPE
419 pDirEntry->enmType = rtDirType(pDir->Data.d_type);
420#else
421 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
422#endif
423 pDirEntry->cbName = (uint16_t)cchName;
424 Assert(pDirEntry->cbName == cchName);
425 memcpy(pDirEntry->szName, pszName, cchName + 1);
426
427 /* free cached data */
428 pDir->fDataUnread = false;
429 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
430 pDir->pszName = NULL;
431 }
432 else
433 rc = VERR_BUFFER_OVERFLOW;
434 }
435
436 LogFlow(("RTDirRead(%p:{%s}, %p:{%s}, %p:{%u}): returns %Rrc\n",
437 pDir, pDir->pszPath, pDirEntry, RT_SUCCESS(rc) ? pDirEntry->szName : "<failed>",
438 pcbDirEntry, pcbDirEntry ? *pcbDirEntry : 0, rc));
439 return rc;
440}
441
442
443/**
444 * Fills dummy info into the info structure.
445 * This function is called if we cannot stat the file.
446 *
447 * @param pInfo The struct in question.
448 * @param
449 */
450static void rtDirSetDummyInfo(PRTFSOBJINFO pInfo, RTDIRENTRYTYPE enmType)
451{
452 pInfo->cbObject = 0;
453 pInfo->cbAllocated = 0;
454 RTTimeSpecSetNano(&pInfo->AccessTime, 0);
455 RTTimeSpecSetNano(&pInfo->ModificationTime, 0);
456 RTTimeSpecSetNano(&pInfo->ChangeTime, 0);
457 RTTimeSpecSetNano(&pInfo->BirthTime, 0);
458 memset(&pInfo->Attr, 0, sizeof(pInfo->Attr));
459 pInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
460 switch (enmType)
461 {
462 default:
463 case RTDIRENTRYTYPE_UNKNOWN: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL; break;
464 case RTDIRENTRYTYPE_FIFO: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FIFO; break;
465 case RTDIRENTRYTYPE_DEV_CHAR: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_CHAR; break;
466 case RTDIRENTRYTYPE_DIRECTORY: pInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY; break;
467 case RTDIRENTRYTYPE_DEV_BLOCK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_BLOCK; break;
468 case RTDIRENTRYTYPE_FILE: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE; break;
469 case RTDIRENTRYTYPE_SYMLINK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SYMLINK; break;
470 case RTDIRENTRYTYPE_SOCKET: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SOCKET; break;
471 case RTDIRENTRYTYPE_WHITEOUT: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_WHITEOUT; break;
472 }
473}
474
475
476RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
477{
478 /*
479 * Validate and digest input.
480 */
481 if (!rtDirValidHandle(pDir))
482 return VERR_INVALID_PARAMETER;
483 AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
484 AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
485 && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
486 ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
487 VERR_INVALID_PARAMETER);
488 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
489 size_t cbDirEntry = sizeof(*pDirEntry);
490 if (pcbDirEntry)
491 {
492 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
493 cbDirEntry = *pcbDirEntry;
494 AssertMsgReturn(cbDirEntry >= (unsigned)RT_OFFSETOF(RTDIRENTRYEX, szName[2]),
495 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
496 VERR_INVALID_PARAMETER);
497 }
498
499 /*
500 * Fetch more data if necessary and/or convert the name.
501 */
502 int rc = rtDirReadMore(pDir);
503 if (RT_SUCCESS(rc))
504 {
505 /*
506 * Check if we've got enough space to return the data.
507 */
508 const char *pszName = pDir->pszName;
509 const size_t cchName = pDir->cchName;
510 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
511 if (pcbDirEntry)
512 *pcbDirEntry = cbRequired;
513 if (cbRequired <= cbDirEntry)
514 {
515 /*
516 * Setup the returned data.
517 */
518 pDirEntry->cwcShortName = 0;
519 pDirEntry->wszShortName[0] = 0;
520 pDirEntry->cbName = (uint16_t)cchName;
521 Assert(pDirEntry->cbName == cchName);
522 memcpy(pDirEntry->szName, pszName, cchName + 1);
523
524 /* get the info data */
525 size_t cch = cchName + pDir->cchPath + 1;
526 char *pszNamePath = (char *)alloca(cch);
527 if (pszNamePath)
528 {
529 memcpy(pszNamePath, pDir->pszPath, pDir->cchPath);
530 memcpy(pszNamePath + pDir->cchPath, pszName, cchName + 1);
531 rc = RTPathQueryInfoEx(pszNamePath, &pDirEntry->Info, enmAdditionalAttribs, fFlags);
532 }
533 else
534 rc = VERR_NO_MEMORY;
535 if (RT_FAILURE(rc))
536 {
537#ifdef HAVE_DIRENT_D_TYPE
538 rtDirSetDummyInfo(&pDirEntry->Info, rtDirType(pDir->Data.d_type));
539#else
540 rtDirSetDummyInfo(&pDirEntry->Info, RTDIRENTRYTYPE_UNKNOWN);
541#endif
542 rc = VWRN_NO_DIRENT_INFO;
543 }
544
545 /* free cached data */
546 pDir->fDataUnread = false;
547 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
548 pDir->pszName = NULL;
549 }
550 else
551 rc = VERR_BUFFER_OVERFLOW;
552 }
553
554 return rc;
555}
556
557
558RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
559{
560 /*
561 * Validate input.
562 */
563 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
564 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
565 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
566 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
567 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
568
569 /*
570 * Take common cause with RTPathRename.
571 */
572 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_DIRECTORY);
573
574 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}): returns %Rrc\n",
575 pszSrc, pszSrc, pszDst, pszDst, rc));
576 return rc;
577}
578
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