VirtualBox

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

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

RTDirReadEx parameter to resolve symlinks.

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