VirtualBox

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

Last change on this file since 39627 was 39627, checked in by vboxsync, 13 years ago

backed out previous changeset

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.2 KB
Line 
1/* $Id: dir-posix.cpp 39627 2011-12-15 11:44:19Z vboxsync $ */
2/** @file
3 * IPRT - Directory manipulation, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 <sys/fcntl.h>
37#include <fcntl.h>
38#include <dirent.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/assert.h>
46#include <iprt/err.h>
47#include <iprt/log.h>
48#include <iprt/mem.h>
49#include <iprt/param.h>
50#include <iprt/path.h>
51#include <iprt/string.h>
52#include "internal/dir.h"
53#include "internal/fs.h"
54#include "internal/path.h"
55
56#if !defined(RT_OS_SOLARIS)
57# define HAVE_DIRENT_D_TYPE 1
58#endif
59
60
61RTDECL(bool) RTDirExists(const char *pszPath)
62{
63 bool fRc = false;
64 char const *pszNativePath;
65 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
66 if (RT_SUCCESS(rc))
67 {
68 struct stat s;
69 fRc = !stat(pszNativePath, &s)
70 && S_ISDIR(s.st_mode);
71
72 rtPathFreeNative(pszNativePath, pszPath);
73 }
74
75 LogFlow(("RTDirExists(%p={%s}): returns %RTbool\n", pszPath, pszPath, fRc));
76 return fRc;
77}
78
79
80RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode, uint32_t fCreate)
81{
82 int rc;
83 fMode = rtFsModeNormalize(fMode, pszPath, 0);
84 if (rtFsModeIsValidPermissions(fMode))
85 {
86 char const *pszNativePath;
87 rc = rtPathToNative(&pszNativePath, pszPath, NULL);
88 if (RT_SUCCESS(rc))
89 {
90 if (mkdir(pszNativePath, fMode & RTFS_UNIX_MASK))
91 {
92 rc = errno;
93 bool fVerifyIsDir = true;
94#ifdef RT_OS_SOLARIS
95 /*
96 * mkdir on nfs mount points has been/is busted in various
97 * during the Nevada development cycle. We've observed:
98 * - Build 111b (2009.06) returns EACCES.
99 * - Build ca. 70-80 returns ENOSYS.
100 */
101 if ( rc == ENOSYS
102 || rc == EACCES)
103 {
104 rc = RTErrConvertFromErrno(rc);
105 fVerifyIsDir = false; /* We'll check if it's a dir ourselves since we're going to stat() anyway. */
106 struct stat st;
107 if (!stat(pszNativePath, &st))
108 {
109 rc = VERR_ALREADY_EXISTS;
110 if (!S_ISDIR(st.st_mode))
111 rc = VERR_IS_A_FILE;
112 }
113 }
114 else
115 rc = RTErrConvertFromErrno(rc);
116#else
117 rc = RTErrConvertFromErrno(rc);
118#endif
119 if ( rc == VERR_ALREADY_EXISTS
120 && fVerifyIsDir == true)
121 {
122 /*
123 * Verify that it really exists as a directory.
124 */
125 struct stat st;
126 if (!stat(pszNativePath, &st) && !S_ISDIR(st.st_mode))
127 rc = VERR_IS_A_FILE;
128 }
129 }
130 }
131
132 rtPathFreeNative(pszNativePath, pszPath);
133 }
134 else
135 {
136 AssertMsgFailed(("Invalid file mode! %RTfmode\n", fMode));
137 rc = VERR_INVALID_FMODE;
138 }
139 LogFlow(("RTDirCreate(%p={%s}, %RTfmode): returns %Rrc\n", pszPath, pszPath, fMode, rc));
140 return rc;
141}
142
143
144RTDECL(int) RTDirRemove(const char *pszPath)
145{
146 char const *pszNativePath;
147 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
148 if (RT_SUCCESS(rc))
149 {
150 if (rmdir(pszNativePath))
151 rc = RTErrConvertFromErrno(errno);
152
153 rtPathFreeNative(pszNativePath, pszPath);
154 }
155
156 LogFlow(("RTDirRemove(%p={%s}): returns %Rrc\n", pszPath, pszPath, rc));
157 return rc;
158}
159
160
161RTDECL(int) RTDirFlush(const char *pszPath)
162{
163 /*
164 * Linux: The fsync() man page hints at this being required for ensuring
165 * consistency between directory and file in case of a crash.
166 *
167 * Solaris: No mentioned is made of directories on the fsync man page.
168 * While rename+fsync will do what we want on ZFS, the code needs more
169 * careful studying wrt whether the directory entry of a new file is
170 * implicitly synced when the file is synced (it's very likely for ZFS).
171 *
172 * FreeBSD: The FFS fsync code seems to flush the directory entry as well
173 * in some cases. Don't know exactly what's up with rename, but from the
174 * look of things fsync(dir) should work.
175 */
176 int rc;
177#ifdef O_DIRECTORY
178 int fd = open(pszPath, O_RDONLY | O_DIRECTORY, 0);
179#else
180 int fd = open(pszPath, O_RDONLY, 0);
181#endif
182 if (fd >= 0)
183 {
184 if (fsync(fd) == 0)
185 rc = VINF_SUCCESS;
186 else
187 rc = RTErrConvertFromErrno(errno);
188 close(fd);
189 }
190 else
191 rc = RTErrConvertFromErrno(errno);
192 return rc;
193}
194
195
196int rtDirNativeOpen(PRTDIR pDir, char *pszPathBuf)
197{
198 NOREF(pszPathBuf); /* only used on windows */
199
200 /*
201 * Convert to a native path and try opendir.
202 */
203 char const *pszNativePath;
204 int rc = rtPathToNative(&pszNativePath, pDir->pszPath, NULL);
205 if (RT_SUCCESS(rc))
206 {
207 pDir->pDir = opendir(pszNativePath);
208 if (pDir->pDir)
209 {
210 /*
211 * Init data.
212 */
213 pDir->fDataUnread = false;
214 memset(&pDir->Data, 0, RT_OFFSETOF(RTDIR, Data.d_name)); /* not strictly necessary */
215 memset(&pDir->Data.d_name[0], 0, pDir->cbMaxName);
216 }
217 else
218 rc = RTErrConvertFromErrno(errno);
219
220 rtPathFreeNative(pszNativePath, pDir->pszPath);
221 }
222
223 return rc;
224}
225
226
227RTDECL(int) RTDirClose(PRTDIR pDir)
228{
229 /*
230 * Validate input.
231 */
232 if (!pDir)
233 return VERR_INVALID_PARAMETER;
234 if (pDir->u32Magic != RTDIR_MAGIC)
235 {
236 AssertMsgFailed(("Invalid pDir=%p\n", pDir));
237 return VERR_INVALID_PARAMETER;
238 }
239
240 /*
241 * Close the handle.
242 */
243 int rc = VINF_SUCCESS;
244 pDir->u32Magic = RTDIR_MAGIC_DEAD;
245 if (closedir(pDir->pDir))
246 {
247 rc = RTErrConvertFromErrno(errno);
248 AssertMsgFailed(("closedir(%p) -> errno=%d (%Rrc)\n", pDir->pDir, errno, rc));
249 }
250
251 RTMemFree(pDir);
252 return rc;
253}
254
255
256/**
257 * Ensure that there is unread data in the buffer
258 * and that there is a converted filename hanging around.
259 *
260 * @returns IPRT status code.
261 * @param pDir the open directory. Fully validated.
262 */
263static int rtDirReadMore(PRTDIR pDir)
264{
265 /** @todo try avoid the rematching on buffer overflow errors. */
266 for (;;)
267 {
268 /*
269 * Fetch data?
270 */
271 if (!pDir->fDataUnread)
272 {
273 struct dirent *pResult = NULL;
274 int rc = readdir_r(pDir->pDir, &pDir->Data, &pResult);
275 if (rc)
276 {
277 rc = RTErrConvertFromErrno(rc);
278 /** @todo Consider translating ENOENT (The current
279 * position of the directory stream is invalid)
280 * differently. */
281 AssertMsg(rc == VERR_FILE_NOT_FOUND, ("%Rrc\n", rc));
282 return rc;
283 }
284 if (!pResult)
285 return VERR_NO_MORE_FILES;
286 }
287
288 /*
289 * Convert the filename to UTF-8.
290 */
291 if (!pDir->pszName)
292 {
293 int rc = rtPathFromNative(&pDir->pszName, pDir->Data.d_name, pDir->pszPath);
294 if (RT_FAILURE(rc))
295 {
296 pDir->pszName = NULL;
297 return rc;
298 }
299 pDir->cchName = strlen(pDir->pszName);
300 }
301 if ( !pDir->pfnFilter
302 || pDir->pfnFilter(pDir, pDir->pszName))
303 break;
304 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
305 pDir->pszName = NULL;
306 pDir->fDataUnread = false;
307 }
308
309 pDir->fDataUnread = true;
310 return VINF_SUCCESS;
311}
312
313
314#ifdef HAVE_DIRENT_D_TYPE
315/**
316 * Converts the d_type field to IPRT directory entry type.
317 *
318 * @returns IPRT directory entry type.
319 * @param Unix
320 */
321static RTDIRENTRYTYPE rtDirType(int iType)
322{
323 switch (iType)
324 {
325 case DT_UNKNOWN: return RTDIRENTRYTYPE_UNKNOWN;
326 case DT_FIFO: return RTDIRENTRYTYPE_FIFO;
327 case DT_CHR: return RTDIRENTRYTYPE_DEV_CHAR;
328 case DT_DIR: return RTDIRENTRYTYPE_DIRECTORY;
329 case DT_BLK: return RTDIRENTRYTYPE_DEV_BLOCK;
330 case DT_REG: return RTDIRENTRYTYPE_FILE;
331 case DT_LNK: return RTDIRENTRYTYPE_SYMLINK;
332 case DT_SOCK: return RTDIRENTRYTYPE_SOCKET;
333 case DT_WHT: return RTDIRENTRYTYPE_WHITEOUT;
334 default:
335 AssertMsgFailed(("iType=%d\n", iType));
336 return RTDIRENTRYTYPE_UNKNOWN;
337 }
338}
339#endif /*HAVE_DIRENT_D_TYPE */
340
341
342RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
343{
344 /*
345 * Validate and digest input.
346 */
347 if (!rtDirValidHandle(pDir))
348 return VERR_INVALID_PARAMETER;
349 AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
350
351 size_t cbDirEntry = sizeof(*pDirEntry);
352 if (pcbDirEntry)
353 {
354 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
355 cbDirEntry = *pcbDirEntry;
356 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
357 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
358 VERR_INVALID_PARAMETER);
359 }
360
361 /*
362 * Fetch more data if necessary and/or convert the name.
363 */
364 int rc = rtDirReadMore(pDir);
365 if (RT_SUCCESS(rc))
366 {
367 /*
368 * Check if we've got enough space to return the data.
369 */
370 const char *pszName = pDir->pszName;
371 const size_t cchName = pDir->cchName;
372 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRY, szName[1]) + cchName;
373 if (pcbDirEntry)
374 *pcbDirEntry = cbRequired;
375 if (cbRequired <= cbDirEntry)
376 {
377 /*
378 * Setup the returned data.
379 */
380 pDirEntry->INodeId = pDir->Data.d_ino; /* may need #ifdefing later */
381#ifdef HAVE_DIRENT_D_TYPE
382 pDirEntry->enmType = rtDirType(pDir->Data.d_type);
383#else
384 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
385#endif
386 pDirEntry->cbName = (uint16_t)cchName;
387 Assert(pDirEntry->cbName == cchName);
388 memcpy(pDirEntry->szName, pszName, cchName + 1);
389
390 /* free cached data */
391 pDir->fDataUnread = false;
392 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
393 pDir->pszName = NULL;
394 }
395 else
396 rc = VERR_BUFFER_OVERFLOW;
397 }
398
399 LogFlow(("RTDirRead(%p:{%s}, %p:{%s}, %p:{%u}): returns %Rrc\n",
400 pDir, pDir->pszPath, pDirEntry, RT_SUCCESS(rc) ? pDirEntry->szName : "<failed>",
401 pcbDirEntry, pcbDirEntry ? *pcbDirEntry : 0, rc));
402 return rc;
403}
404
405
406/**
407 * Fills dummy info into the info structure.
408 * This function is called if we cannot stat the file.
409 *
410 * @param pInfo The struct in question.
411 * @param
412 */
413static void rtDirSetDummyInfo(PRTFSOBJINFO pInfo, RTDIRENTRYTYPE enmType)
414{
415 pInfo->cbObject = 0;
416 pInfo->cbAllocated = 0;
417 RTTimeSpecSetNano(&pInfo->AccessTime, 0);
418 RTTimeSpecSetNano(&pInfo->ModificationTime, 0);
419 RTTimeSpecSetNano(&pInfo->ChangeTime, 0);
420 RTTimeSpecSetNano(&pInfo->BirthTime, 0);
421 memset(&pInfo->Attr, 0, sizeof(pInfo->Attr));
422 pInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
423 switch (enmType)
424 {
425 default:
426 case RTDIRENTRYTYPE_UNKNOWN: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL;
427 case RTDIRENTRYTYPE_FIFO: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FIFO;
428 case RTDIRENTRYTYPE_DEV_CHAR: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_CHAR;
429 case RTDIRENTRYTYPE_DIRECTORY: pInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY;
430 case RTDIRENTRYTYPE_DEV_BLOCK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_BLOCK;
431 case RTDIRENTRYTYPE_FILE: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE;
432 case RTDIRENTRYTYPE_SYMLINK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SYMLINK;
433 case RTDIRENTRYTYPE_SOCKET: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SOCKET;
434 case RTDIRENTRYTYPE_WHITEOUT: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_WHITEOUT;
435 }
436}
437
438
439RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
440{
441 /*
442 * Validate and digest input.
443 */
444 if (!rtDirValidHandle(pDir))
445 return VERR_INVALID_PARAMETER;
446 AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
447 AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
448 && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
449 ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
450 VERR_INVALID_PARAMETER);
451 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
452 size_t cbDirEntry = sizeof(*pDirEntry);
453 if (pcbDirEntry)
454 {
455 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
456 cbDirEntry = *pcbDirEntry;
457 AssertMsgReturn(cbDirEntry >= (unsigned)RT_OFFSETOF(RTDIRENTRYEX, szName[2]),
458 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
459 VERR_INVALID_PARAMETER);
460 }
461
462 /*
463 * Fetch more data if necessary and/or convert the name.
464 */
465 int rc = rtDirReadMore(pDir);
466 if (RT_SUCCESS(rc))
467 {
468 /*
469 * Check if we've got enough space to return the data.
470 */
471 const char *pszName = pDir->pszName;
472 const size_t cchName = pDir->cchName;
473 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
474 if (pcbDirEntry)
475 *pcbDirEntry = cbRequired;
476 if (cbRequired <= cbDirEntry)
477 {
478 /*
479 * Setup the returned data.
480 */
481 pDirEntry->cwcShortName = 0;
482 pDirEntry->wszShortName[0] = 0;
483 pDirEntry->cbName = (uint16_t)cchName;
484 Assert(pDirEntry->cbName == cchName);
485 memcpy(pDirEntry->szName, pszName, cchName + 1);
486
487 /* get the info data */
488 size_t cch = cchName + pDir->cchPath + 1;
489 char *pszNamePath = (char *)alloca(cch);
490 if (pszNamePath)
491 {
492 memcpy(pszNamePath, pDir->pszPath, pDir->cchPath);
493 memcpy(pszNamePath + pDir->cchPath, pszName, cchName + 1);
494 rc = RTPathQueryInfoEx(pszNamePath, &pDirEntry->Info, enmAdditionalAttribs, fFlags);
495 }
496 else
497 rc = VERR_NO_MEMORY;
498 if (RT_FAILURE(rc))
499 {
500#ifdef HAVE_DIRENT_D_TYPE
501 rtDirSetDummyInfo(&pDirEntry->Info, rtDirType(pDir->Data.d_type));
502#else
503 rtDirSetDummyInfo(&pDirEntry->Info, RTDIRENTRYTYPE_UNKNOWN);
504#endif
505 rc = VWRN_NO_DIRENT_INFO;
506 }
507
508 /* free cached data */
509 pDir->fDataUnread = false;
510 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
511 pDir->pszName = NULL;
512 }
513 else
514 rc = VERR_BUFFER_OVERFLOW;
515 }
516
517 return rc;
518}
519
520
521RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
522{
523 /*
524 * Validate input.
525 */
526 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
527 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
528 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
529 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
530 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
531
532 /*
533 * Take common cause with RTPathRename.
534 */
535 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_DIRECTORY);
536
537 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}): returns %Rrc\n",
538 pszSrc, pszSrc, pszDst, pszDst, rc));
539 return rc;
540}
541
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