VirtualBox

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

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

IPRT: Added RTDirFlush and RTDirFlushParent.

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