VirtualBox

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

Last change on this file since 28690 was 28690, checked in by vboxsync, 15 years ago

oops.

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