VirtualBox

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

Last change on this file since 4456 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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