VirtualBox

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

Last change on this file since 44529 was 43363, checked in by vboxsync, 12 years ago

Haiku Additions.

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