VirtualBox

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

Last change on this file since 34147 was 28918, checked in by vboxsync, 14 years ago

iprt: dropped RT_DONT_CONVERT_FILENAMES, we've never needed it so far...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.1 KB
Line 
1/* $Id: dir-posix.cpp 28918 2010-04-29 18:30:09Z 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)
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#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 struct stat st;
104 if (!stat(pszNativePath, &st))
105 rc = EEXIST;
106 }
107#endif
108 rc = RTErrConvertFromErrno(rc);
109 }
110 }
111
112 rtPathFreeNative(pszNativePath, pszPath);
113 }
114 else
115 {
116 AssertMsgFailed(("Invalid file mode! %RTfmode\n", fMode));
117 rc = VERR_INVALID_FMODE;
118 }
119 LogFlow(("RTDirCreate(%p={%s}, %RTfmode): returns %Rrc\n", pszPath, pszPath, fMode, rc));
120 return rc;
121}
122
123
124RTDECL(int) RTDirRemove(const char *pszPath)
125{
126 char const *pszNativePath;
127 int rc = rtPathToNative(&pszNativePath, pszPath, NULL);
128 if (RT_SUCCESS(rc))
129 {
130 if (rmdir(pszNativePath))
131 rc = RTErrConvertFromErrno(errno);
132
133 rtPathFreeNative(pszNativePath, pszPath);
134 }
135
136 LogFlow(("RTDirRemove(%p={%s}): returns %Rrc\n", pszPath, pszPath, rc));
137 return rc;
138}
139
140
141RTDECL(int) RTDirFlush(const char *pszPath)
142{
143 /*
144 * Linux: The fsync() man page hints at this being required for ensuring
145 * consistency between directory and file in case of a crash.
146 *
147 * Solaris: No mentioned is made of directories on the fsync man page.
148 * While rename+fsync will do what we want on ZFS, the code needs more
149 * careful studying wrt whether the directory entry of a new file is
150 * implicitly synced when the file is synced (it's very likely for ZFS).
151 *
152 * FreeBSD: The FFS fsync code seems to flush the directory entry as well
153 * in some cases. Don't know exactly what's up with rename, but from the
154 * look of things fsync(dir) should work.
155 */
156 int rc;
157#ifdef O_DIRECTORY
158 int fd = open(pszPath, O_RDONLY | O_DIRECTORY, 0);
159#else
160 int fd = open(pszPath, O_RDONLY, 0);
161#endif
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 rtDirNativeOpen(PRTDIR pDir, char *pszPathBuf)
177{
178 /*
179 * Convert to a native path and try opendir.
180 */
181 char const *pszNativePath;
182 int rc = rtPathToNative(&pszNativePath, pDir->pszPath, NULL);
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, pDir->pszPath);
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 /*
264 * Convert the filename to UTF-8.
265 */
266 if (!pDir->pszName)
267 {
268 int rc = rtPathFromNative(&pDir->pszName, pDir->Data.d_name, pDir->pszPath);
269 if (RT_FAILURE(rc))
270 {
271 pDir->pszName = NULL;
272 return rc;
273 }
274 pDir->cchName = strlen(pDir->pszName);
275 }
276 if ( !pDir->pfnFilter
277 || pDir->pfnFilter(pDir, pDir->pszName))
278 break;
279 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
280 pDir->pszName = NULL;
281 pDir->fDataUnread = false;
282 }
283
284 pDir->fDataUnread = true;
285 return VINF_SUCCESS;
286}
287
288
289#ifdef HAVE_DIRENT_D_TYPE
290/**
291 * Converts the d_type field to IPRT directory entry type.
292 *
293 * @returns IPRT directory entry type.
294 * @param Unix
295 */
296static RTDIRENTRYTYPE rtDirType(int iType)
297{
298 switch (iType)
299 {
300 case DT_UNKNOWN: return RTDIRENTRYTYPE_UNKNOWN;
301 case DT_FIFO: return RTDIRENTRYTYPE_FIFO;
302 case DT_CHR: return RTDIRENTRYTYPE_DEV_CHAR;
303 case DT_DIR: return RTDIRENTRYTYPE_DIRECTORY;
304 case DT_BLK: return RTDIRENTRYTYPE_DEV_BLOCK;
305 case DT_REG: return RTDIRENTRYTYPE_FILE;
306 case DT_LNK: return RTDIRENTRYTYPE_SYMLINK;
307 case DT_SOCK: return RTDIRENTRYTYPE_SOCKET;
308 case DT_WHT: return RTDIRENTRYTYPE_WHITEOUT;
309 default:
310 AssertMsgFailed(("iType=%d\n", iType));
311 return RTDIRENTRYTYPE_UNKNOWN;
312 }
313}
314#endif /*HAVE_DIRENT_D_TYPE */
315
316
317RTDECL(int) RTDirRead(PRTDIR pDir, PRTDIRENTRY pDirEntry, size_t *pcbDirEntry)
318{
319 /*
320 * Validate and digest input.
321 */
322 if (!rtDirValidHandle(pDir))
323 return VERR_INVALID_PARAMETER;
324 AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
325
326 size_t cbDirEntry = sizeof(*pDirEntry);
327 if (pcbDirEntry)
328 {
329 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
330 cbDirEntry = *pcbDirEntry;
331 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRY, szName[2]),
332 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
333 VERR_INVALID_PARAMETER);
334 }
335
336 /*
337 * Fetch more data if necessary and/or convert the name.
338 */
339 int rc = rtDirReadMore(pDir);
340 if (RT_SUCCESS(rc))
341 {
342 /*
343 * Check if we've got enough space to return the data.
344 */
345 const char *pszName = pDir->pszName;
346 const size_t cchName = pDir->cchName;
347 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRY, szName[1]) + cchName;
348 if (pcbDirEntry)
349 *pcbDirEntry = cbRequired;
350 if (cbRequired <= cbDirEntry)
351 {
352 /*
353 * Setup the returned data.
354 */
355 pDirEntry->INodeId = pDir->Data.d_ino; /* may need #ifdefing later */
356#ifdef HAVE_DIRENT_D_TYPE
357 pDirEntry->enmType = rtDirType(pDir->Data.d_type);
358#else
359 pDirEntry->enmType = RTDIRENTRYTYPE_UNKNOWN;
360#endif
361 pDirEntry->cbName = (uint16_t)cchName;
362 Assert(pDirEntry->cbName == cchName);
363 memcpy(pDirEntry->szName, pszName, cchName + 1);
364
365 /* free cached data */
366 pDir->fDataUnread = false;
367 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
368 pDir->pszName = NULL;
369 }
370 else
371 rc = VERR_BUFFER_OVERFLOW;
372 }
373
374 LogFlow(("RTDirRead(%p:{%s}, %p:{%s}, %p:{%u}): returns %Rrc\n",
375 pDir, pDir->pszPath, pDirEntry, RT_SUCCESS(rc) ? pDirEntry->szName : "<failed>",
376 pcbDirEntry, pcbDirEntry ? *pcbDirEntry : 0, rc));
377 return rc;
378}
379
380
381/**
382 * Fills dummy info into the info structure.
383 * This function is called if we cannot stat the file.
384 *
385 * @param pInfo The struct in question.
386 * @param
387 */
388static void rtDirSetDummyInfo(PRTFSOBJINFO pInfo, RTDIRENTRYTYPE enmType)
389{
390 pInfo->cbObject = 0;
391 pInfo->cbAllocated = 0;
392 RTTimeSpecSetNano(&pInfo->AccessTime, 0);
393 RTTimeSpecSetNano(&pInfo->ModificationTime, 0);
394 RTTimeSpecSetNano(&pInfo->ChangeTime, 0);
395 RTTimeSpecSetNano(&pInfo->BirthTime, 0);
396 memset(&pInfo->Attr, 0, sizeof(pInfo->Attr));
397 pInfo->Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
398 switch (enmType)
399 {
400 default:
401 case RTDIRENTRYTYPE_UNKNOWN: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL;
402 case RTDIRENTRYTYPE_FIFO: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FIFO;
403 case RTDIRENTRYTYPE_DEV_CHAR: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_CHAR;
404 case RTDIRENTRYTYPE_DIRECTORY: pInfo->Attr.fMode = RTFS_DOS_DIRECTORY | RTFS_TYPE_DIRECTORY;
405 case RTDIRENTRYTYPE_DEV_BLOCK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_DEV_BLOCK;
406 case RTDIRENTRYTYPE_FILE: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE;
407 case RTDIRENTRYTYPE_SYMLINK: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SYMLINK;
408 case RTDIRENTRYTYPE_SOCKET: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_SOCKET;
409 case RTDIRENTRYTYPE_WHITEOUT: pInfo->Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_WHITEOUT;
410 }
411}
412
413
414RTDECL(int) RTDirReadEx(PRTDIR pDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
415{
416 /*
417 * Validate and digest input.
418 */
419 if (!rtDirValidHandle(pDir))
420 return VERR_INVALID_PARAMETER;
421 AssertMsgReturn(VALID_PTR(pDirEntry), ("%p\n", pDirEntry), VERR_INVALID_POINTER);
422 AssertMsgReturn( enmAdditionalAttribs >= RTFSOBJATTRADD_NOTHING
423 && enmAdditionalAttribs <= RTFSOBJATTRADD_LAST,
424 ("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
425 VERR_INVALID_PARAMETER);
426 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
427 size_t cbDirEntry = sizeof(*pDirEntry);
428 if (pcbDirEntry)
429 {
430 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
431 cbDirEntry = *pcbDirEntry;
432 AssertMsgReturn(cbDirEntry >= (unsigned)RT_OFFSETOF(RTDIRENTRYEX, szName[2]),
433 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
434 VERR_INVALID_PARAMETER);
435 }
436
437 /*
438 * Fetch more data if necessary and/or convert the name.
439 */
440 int rc = rtDirReadMore(pDir);
441 if (RT_SUCCESS(rc))
442 {
443 /*
444 * Check if we've got enough space to return the data.
445 */
446 const char *pszName = pDir->pszName;
447 const size_t cchName = pDir->cchName;
448 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
449 if (pcbDirEntry)
450 *pcbDirEntry = cbRequired;
451 if (cbRequired <= cbDirEntry)
452 {
453 /*
454 * Setup the returned data.
455 */
456 pDirEntry->cwcShortName = 0;
457 pDirEntry->wszShortName[0] = 0;
458 pDirEntry->cbName = (uint16_t)cchName;
459 Assert(pDirEntry->cbName == cchName);
460 memcpy(pDirEntry->szName, pszName, cchName + 1);
461
462 /* get the info data */
463 size_t cch = cchName + pDir->cchPath + 1;
464 char *pszNamePath = (char *)alloca(cch);
465 if (pszNamePath)
466 {
467 memcpy(pszNamePath, pDir->pszPath, pDir->cchPath);
468 memcpy(pszNamePath + pDir->cchPath, pszName, cchName + 1);
469 rc = RTPathQueryInfoEx(pszNamePath, &pDirEntry->Info, enmAdditionalAttribs, fFlags);
470 }
471 else
472 rc = VERR_NO_MEMORY;
473 if (RT_FAILURE(rc))
474 {
475#ifdef HAVE_DIRENT_D_TYPE
476 rtDirSetDummyInfo(&pDirEntry->Info, rtDirType(pDir->Data.d_type));
477#else
478 rtDirSetDummyInfo(&pDirEntry->Info, RTDIRENTRYTYPE_UNKNOWN);
479#endif
480 rc = VWRN_NO_DIRENT_INFO;
481 }
482
483 /* free cached data */
484 pDir->fDataUnread = false;
485 rtPathFreeIprt(pDir->pszName, pDir->Data.d_name);
486 pDir->pszName = NULL;
487 }
488 else
489 rc = VERR_BUFFER_OVERFLOW;
490 }
491
492 return rc;
493}
494
495
496RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
497{
498 /*
499 * Validate input.
500 */
501 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
502 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
503 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
504 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
505 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
506
507 /*
508 * Take common cause with RTPathRename.
509 */
510 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_DIRECTORY);
511
512 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}): returns %Rrc\n",
513 pszSrc, pszSrc, pszDst, pszDst, rc));
514 return rc;
515}
516
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