VirtualBox

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

最後變更 在這個檔案從35935是 35935,由 vboxsync 提交於 14 年 前

Renabled the rtDirReadMore assertion, but made it ignore ENOENT.

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

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