VirtualBox

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

Last change on this file since 8765 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

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