VirtualBox

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

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

RTDirCreate: recent nevadas (111b at least) returns EACCES when mkdir(szNfsMountPoint), causing RTDirCreateFullPath to bust again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.3 KB
Line 
1/* $Id: dir-posix.cpp 21672 2009-07-17 12:07:16Z 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)
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 size_t cbDirEntry = sizeof(*pDirEntry);
404 if (pcbDirEntry)
405 {
406 AssertMsgReturn(VALID_PTR(pcbDirEntry), ("%p\n", pcbDirEntry), VERR_INVALID_POINTER);
407 cbDirEntry = *pcbDirEntry;
408 AssertMsgReturn(cbDirEntry >= (unsigned)RT_OFFSETOF(RTDIRENTRYEX, szName[2]),
409 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
410 VERR_INVALID_PARAMETER);
411 }
412
413 /*
414 * Fetch more data if necessary and/or convert the name.
415 */
416 int rc = rtDirReadMore(pDir);
417 if (RT_SUCCESS(rc))
418 {
419 /*
420 * Check if we've got enough space to return the data.
421 */
422#ifdef RT_DONT_CONVERT_FILENAMES
423 const char *pszName = pDir->Data.d_name;
424 const size_t cchName = strlen(pszName);
425#else
426 const char *pszName = pDir->pszName;
427 const size_t cchName = pDir->cchName;
428#endif
429 const size_t cbRequired = RT_OFFSETOF(RTDIRENTRYEX, szName[1]) + cchName;
430 if (pcbDirEntry)
431 *pcbDirEntry = cbRequired;
432 if (cbRequired <= cbDirEntry)
433 {
434 /*
435 * Setup the returned data.
436 */
437 pDirEntry->cwcShortName = 0;
438 pDirEntry->wszShortName[0] = 0;
439 pDirEntry->cbName = (uint16_t)cchName;
440 Assert(pDirEntry->cbName == cchName);
441 memcpy(pDirEntry->szName, pszName, cchName + 1);
442
443 /* get the info data */
444 size_t cch = cchName + pDir->cchPath + 1;
445 char *pszNamePath = (char *)alloca(cch);
446 if (pszNamePath)
447 {
448 memcpy(pszNamePath, pDir->pszPath, pDir->cchPath);
449 memcpy(pszNamePath + pDir->cchPath, pszName, cchName + 1);
450 rc = RTPathQueryInfo(pszNamePath, &pDirEntry->Info, enmAdditionalAttribs);
451 }
452 else
453 rc = VERR_NO_MEMORY;
454 if (RT_FAILURE(rc))
455 {
456#ifdef HAVE_DIRENT_D_TYPE
457 rtDirSetDummyInfo(&pDirEntry->Info, rtDirType(pDir->Data.d_type));
458#else
459 rtDirSetDummyInfo(&pDirEntry->Info, RTDIRENTRYTYPE_UNKNOWN);
460#endif
461 rc = VWRN_NO_DIRENT_INFO;
462 }
463
464 /* free cached data */
465 pDir->fDataUnread = false;
466#ifndef RT_DONT_CONVERT_FILENAMES
467 RTStrFree(pDir->pszName);
468 pDir->pszName = NULL;
469#endif
470 }
471 else
472 rc = VERR_BUFFER_OVERFLOW;
473 }
474
475 return rc;
476}
477
478
479RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
480{
481 /*
482 * Validate input.
483 */
484 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
485 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
486 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
487 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
488 AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
489
490 /*
491 * Take common cause with RTPathRename.
492 */
493 int rc = rtPathPosixRename(pszSrc, pszDst, fRename, RTFS_TYPE_DIRECTORY);
494
495 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}): returns %Rrc\n",
496 pszSrc, pszSrc, pszDst, pszDst, rc));
497 return rc;
498}
499
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