VirtualBox

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

Last change on this file since 20102 was 20102, checked in by vboxsync, 16 years ago

space

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette