VirtualBox

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

Last change on this file since 61558 was 60612, checked in by vboxsync, 9 years ago

IPRT/dir-posix.cpp: custom errno mapping for fsync in RTDirFlush, because it otherwise gets rather confusing

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