VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/vfs/vfsbase.cpp@ 67221

Last change on this file since 67221 was 67221, checked in by vboxsync, 8 years ago

IPRT: Adding RTVfsIoStrmGetOpenFlags, RTVfsFileGetOpenFlags, RTVfsCreateProcessForIoStream and RTVfsCreateProcessForFile.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 106.8 KB
Line 
1/* $Id: vfsbase.cpp 67221 2017-06-01 21:44:24Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, Base.
4 */
5
6/*
7 * Copyright (C) 2010-2016 Oracle Corporation
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
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_FS
32#include <iprt/vfs.h>
33#include <iprt/vfslowlevel.h>
34
35#include <iprt/asm.h>
36#include <iprt/err.h>
37#include <iprt/file.h>
38#include <iprt/log.h>
39#include <iprt/mem.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/semaphore.h>
43#include <iprt/thread.h>
44
45#include "internal/file.h"
46#include "internal/fs.h"
47#include "internal/magics.h"
48//#include "internal/vfs.h"
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54/** The instance data alignment. */
55#define RTVFS_INST_ALIGNMENT 16U
56
57/** The max number of symbolic links to resolve in a path. */
58#define RTVFS_MAX_LINKS 20U
59
60
61/** Asserts that the VFS base object vtable is valid. */
62#define RTVFSOBJ_ASSERT_OPS(a_pObjOps, a_enmType) \
63 do \
64 { \
65 Assert((a_pObjOps)->uVersion == RTVFSOBJOPS_VERSION); \
66 Assert((a_pObjOps)->enmType == (a_enmType) || (a_enmType) == RTVFSOBJTYPE_INVALID); \
67 AssertPtr((a_pObjOps)->pszName); \
68 Assert(*(a_pObjOps)->pszName); \
69 AssertPtr((a_pObjOps)->pfnClose); \
70 AssertPtr((a_pObjOps)->pfnQueryInfo); \
71 Assert((a_pObjOps)->uEndMarker == RTVFSOBJOPS_VERSION); \
72 } while (0)
73
74/** Asserts that the VFS set object vtable is valid. */
75#define RTVFSOBJSET_ASSERT_OPS(a_pSetOps, a_offObjOps) \
76 do \
77 { \
78 Assert((a_pSetOps)->uVersion == RTVFSOBJSETOPS_VERSION); \
79 Assert((a_pSetOps)->offObjOps == (a_offObjOps)); \
80 AssertPtr((a_pSetOps)->pfnSetMode); \
81 AssertPtr((a_pSetOps)->pfnSetTimes); \
82 AssertPtr((a_pSetOps)->pfnSetOwner); \
83 Assert((a_pSetOps)->uEndMarker == RTVFSOBJSETOPS_VERSION); \
84 } while (0)
85
86/** Asserts that the VFS directory vtable is valid. */
87#define RTVFSDIR_ASSERT_OPS(pDirOps, a_enmType) \
88 do { \
89 RTVFSOBJ_ASSERT_OPS(&(pDirOps)->Obj, a_enmType); \
90 RTVFSOBJSET_ASSERT_OPS(&(pDirOps)->ObjSet, RT_OFFSETOF(RTVFSDIROPS, Obj) - RT_OFFSETOF(RTVFSDIROPS, ObjSet)); \
91 Assert((pDirOps)->uVersion == RTVFSDIROPS_VERSION); \
92 Assert(!(pDirOps)->fReserved); \
93 AssertPtr((pDirOps)->pfnTraversalOpen); \
94 AssertPtr((pDirOps)->pfnOpenFile); \
95 AssertPtr((pDirOps)->pfnOpenDir); \
96 AssertPtr((pDirOps)->pfnCreateDir); \
97 AssertPtr((pDirOps)->pfnOpenSymlink); \
98 AssertPtr((pDirOps)->pfnCreateSymlink); \
99 AssertPtr((pDirOps)->pfnUnlinkEntry); \
100 AssertPtr((pDirOps)->pfnRewindDir); \
101 AssertPtr((pDirOps)->pfnReadDir); \
102 Assert((pDirOps)->uEndMarker == RTVFSDIROPS_VERSION); \
103 } while (0)
104
105/** Asserts that the VFS I/O stream vtable is valid. */
106#define RTVFSIOSTREAM_ASSERT_OPS(pIoStreamOps, a_enmType) \
107 do { \
108 RTVFSOBJ_ASSERT_OPS(&(pIoStreamOps)->Obj, a_enmType); \
109 Assert((pIoStreamOps)->uVersion == RTVFSIOSTREAMOPS_VERSION); \
110 Assert(!((pIoStreamOps)->fFeatures & ~RTVFSIOSTREAMOPS_FEAT_VALID_MASK)); \
111 AssertPtr((pIoStreamOps)->pfnRead); \
112 AssertPtr((pIoStreamOps)->pfnWrite); \
113 AssertPtr((pIoStreamOps)->pfnFlush); \
114 AssertPtr((pIoStreamOps)->pfnPollOne); \
115 AssertPtr((pIoStreamOps)->pfnTell); \
116 AssertPtrNull((pIoStreamOps)->pfnSkip); \
117 AssertPtrNull((pIoStreamOps)->pfnZeroFill); \
118 Assert((pIoStreamOps)->uEndMarker == RTVFSIOSTREAMOPS_VERSION); \
119 } while (0)
120
121/** Asserts that the VFS symlink vtable is valid. */
122#define RTVFSSYMLINK_ASSERT_OPS(pSymlinkOps, a_enmType) \
123 do { \
124 RTVFSOBJ_ASSERT_OPS(&(pSymlinkOps)->Obj, a_enmType); \
125 RTVFSOBJSET_ASSERT_OPS(&(pSymlinkOps)->ObjSet, \
126 RT_OFFSETOF(RTVFSSYMLINKOPS, Obj) - RT_OFFSETOF(RTVFSSYMLINKOPS, ObjSet)); \
127 Assert((pSymlinkOps)->uVersion == RTVFSSYMLINKOPS_VERSION); \
128 Assert(!(pSymlinkOps)->fReserved); \
129 AssertPtr((pSymlinkOps)->pfnRead); \
130 Assert((pSymlinkOps)->uEndMarker == RTVFSSYMLINKOPS_VERSION); \
131 } while (0)
132
133
134/** Validates a VFS handle and returns @a rcRet if it's invalid. */
135#define RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, rcRet) \
136 do { \
137 if ((hVfs) != NIL_RTVFS) \
138 { \
139 AssertPtrReturn((hVfs), (rcRet)); \
140 AssertReturn((hVfs)->uMagic == RTVFS_MAGIC, (rcRet)); \
141 } \
142 } while (0)
143
144
145/*********************************************************************************************************************************
146* Structures and Typedefs *
147*********************************************************************************************************************************/
148/** @todo Move all this stuff to internal/vfs.h */
149
150
151/**
152 * The VFS internal lock data.
153 */
154typedef struct RTVFSLOCKINTERNAL
155{
156 /** The number of references to the this lock. */
157 uint32_t volatile cRefs;
158 /** The lock type. */
159 RTVFSLOCKTYPE enmType;
160 /** Type specific data. */
161 union
162 {
163 /** Read/Write semaphore handle. */
164 RTSEMRW hSemRW;
165 /** Fast mutex semaphore handle. */
166 RTSEMFASTMUTEX hFastMtx;
167 /** Regular mutex semaphore handle. */
168 RTSEMMUTEX hMtx;
169 } u;
170} RTVFSLOCKINTERNAL;
171
172
173/**
174 * The VFS base object handle data.
175 *
176 * All other VFS handles are derived from this one. The final handle type is
177 * indicated by RTVFSOBJOPS::enmType via the RTVFSOBJINTERNAL::pOps member.
178 */
179typedef struct RTVFSOBJINTERNAL
180{
181 /** The VFS magic (RTVFSOBJ_MAGIC). */
182 uint32_t uMagic : 31;
183 /** Set if we've got no VFS reference but still got a valid hVfs.
184 * This is hack for permanent root directory objects. */
185 uint32_t fNoVfsRef : 1;
186 /** The number of references to this VFS object. */
187 uint32_t volatile cRefs;
188 /** Pointer to the instance data. */
189 void *pvThis;
190 /** The vtable. */
191 PCRTVFSOBJOPS pOps;
192 /** The lock protecting all access to the VFS.
193 * Only valid if RTVFS_C_THREAD_SAFE is set, otherwise it is NIL_RTVFSLOCK. */
194 RTVFSLOCK hLock;
195 /** Reference back to the VFS containing this object. */
196 RTVFS hVfs;
197} RTVFSOBJINTERNAL;
198
199
200/**
201 * The VFS filesystem stream handle data.
202 *
203 * @extends RTVFSOBJINTERNAL
204 */
205typedef struct RTVFSFSSTREAMINTERNAL
206{
207 /** The VFS magic (RTVFSFSTREAM_MAGIC). */
208 uint32_t uMagic;
209 /** File open flags, at a minimum the access mask. */
210 uint32_t fFlags;
211 /** The vtable. */
212 PCRTVFSFSSTREAMOPS pOps;
213 /** The base object handle data. */
214 RTVFSOBJINTERNAL Base;
215} RTVFSFSSTREAMINTERNAL;
216
217
218/**
219 * The VFS handle data.
220 *
221 * @extends RTVFSOBJINTERNAL
222 */
223typedef struct RTVFSINTERNAL
224{
225 /** The VFS magic (RTVFS_MAGIC). */
226 uint32_t uMagic;
227 /** Creation flags (RTVFS_C_XXX). */
228 uint32_t fFlags;
229 /** The vtable. */
230 PCRTVFSOPS pOps;
231 /** The base object handle data. */
232 RTVFSOBJINTERNAL Base;
233} RTVFSINTERNAL;
234
235
236/**
237 * The VFS directory handle data.
238 *
239 * @extends RTVFSOBJINTERNAL
240 */
241typedef struct RTVFSDIRINTERNAL
242{
243 /** The VFS magic (RTVFSDIR_MAGIC). */
244 uint32_t uMagic;
245 /** Reserved for flags or something. */
246 uint32_t fReserved;
247 /** The vtable. */
248 PCRTVFSDIROPS pOps;
249 /** The base object handle data. */
250 RTVFSOBJINTERNAL Base;
251} RTVFSDIRINTERNAL;
252
253
254/**
255 * The VFS symbolic link handle data.
256 *
257 * @extends RTVFSOBJINTERNAL
258 */
259typedef struct RTVFSSYMLINKINTERNAL
260{
261 /** The VFS magic (RTVFSSYMLINK_MAGIC). */
262 uint32_t uMagic;
263 /** Reserved for flags or something. */
264 uint32_t fReserved;
265 /** The vtable. */
266 PCRTVFSSYMLINKOPS pOps;
267 /** The base object handle data. */
268 RTVFSOBJINTERNAL Base;
269} RTVFSSYMLINKINTERNAL;
270
271
272/**
273 * The VFS I/O stream handle data.
274 *
275 * This is often part of a type specific handle, like a file or pipe.
276 *
277 * @extends RTVFSOBJINTERNAL
278 */
279typedef struct RTVFSIOSTREAMINTERNAL
280{
281 /** The VFS magic (RTVFSIOSTREAM_MAGIC). */
282 uint32_t uMagic;
283 /** File open flags, at a minimum the access mask. */
284 uint32_t fFlags;
285 /** The vtable. */
286 PCRTVFSIOSTREAMOPS pOps;
287 /** The base object handle data. */
288 RTVFSOBJINTERNAL Base;
289} RTVFSIOSTREAMINTERNAL;
290
291
292/**
293 * The VFS file handle data.
294 *
295 * @extends RTVFSIOSTREAMINTERNAL
296 */
297typedef struct RTVFSFILEINTERNAL
298{
299 /** The VFS magic (RTVFSFILE_MAGIC). */
300 uint32_t uMagic;
301 /** Reserved for flags or something. */
302 uint32_t fReserved;
303 /** The vtable. */
304 PCRTVFSFILEOPS pOps;
305 /** The stream handle data. */
306 RTVFSIOSTREAMINTERNAL Stream;
307} RTVFSFILEINTERNAL;
308
309#if 0 /* later */
310
311/**
312 * The VFS pipe handle data.
313 *
314 * @extends RTVFSIOSTREAMINTERNAL
315 */
316typedef struct RTVFSPIPEINTERNAL
317{
318 /** The VFS magic (RTVFSPIPE_MAGIC). */
319 uint32_t uMagic;
320 /** Reserved for flags or something. */
321 uint32_t fReserved;
322 /** The vtable. */
323 PCRTVFSPIPEOPS pOps;
324 /** The stream handle data. */
325 RTVFSIOSTREAMINTERNAL Stream;
326} RTVFSPIPEINTERNAL;
327
328
329/**
330 * The VFS socket handle data.
331 *
332 * @extends RTVFSIOSTREAMINTERNAL
333 */
334typedef struct RTVFSSOCKETINTERNAL
335{
336 /** The VFS magic (RTVFSSOCKET_MAGIC). */
337 uint32_t uMagic;
338 /** Reserved for flags or something. */
339 uint32_t fReserved;
340 /** The vtable. */
341 PCRTVFSSOCKETOPS pOps;
342 /** The stream handle data. */
343 RTVFSIOSTREAMINTERNAL Stream;
344} RTVFSSOCKETINTERNAL;
345
346#endif /* later */
347
348
349/*********************************************************************************************************************************
350* Internal Functions *
351*********************************************************************************************************************************/
352DECLINLINE(uint32_t) rtVfsObjRelease(RTVFSOBJINTERNAL *pThis);
353
354
355
356/*
357 *
358 * V F S L o c k A b s t r a c t i o n
359 * V F S L o c k A b s t r a c t i o n
360 * V F S L o c k A b s t r a c t i o n
361 *
362 *
363 */
364
365
366RTDECL(uint32_t) RTVfsLockRetain(RTVFSLOCK hLock)
367{
368 RTVFSLOCKINTERNAL *pThis = hLock;
369 AssertPtrReturn(pThis, UINT32_MAX);
370 AssertReturn(pThis->enmType > RTVFSLOCKTYPE_INVALID && pThis->enmType < RTVFSLOCKTYPE_END, UINT32_MAX);
371
372 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
373 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p %d\n", cRefs, pThis, pThis->enmType));
374 return cRefs;
375}
376
377
378RTDECL(uint32_t) RTVfsLockRetainDebug(RTVFSLOCK hLock, RT_SRC_POS_DECL)
379{
380 RTVFSLOCKINTERNAL *pThis = hLock;
381 AssertPtrReturn(pThis, UINT32_MAX);
382 AssertReturn(pThis->enmType > RTVFSLOCKTYPE_INVALID && pThis->enmType < RTVFSLOCKTYPE_END, UINT32_MAX);
383
384 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
385 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p %d\n", cRefs, pThis, pThis->enmType));
386 LogFlow(("RTVfsLockRetainDebug(%p) -> %d; caller: %s %s(%u)\n", hLock, cRefs, pszFunction, pszFile, iLine));
387 RT_SRC_POS_NOREF();
388 return cRefs;
389}
390
391
392/**
393 * Destroys a VFS lock handle.
394 *
395 * @param pThis The lock to destroy.
396 */
397static void rtVfsLockDestroy(RTVFSLOCKINTERNAL *pThis)
398{
399 switch (pThis->enmType)
400 {
401 case RTVFSLOCKTYPE_RW:
402 RTSemRWDestroy(pThis->u.hSemRW);
403 pThis->u.hSemRW = NIL_RTSEMRW;
404 break;
405
406 case RTVFSLOCKTYPE_FASTMUTEX:
407 RTSemFastMutexDestroy(pThis->u.hFastMtx);
408 pThis->u.hFastMtx = NIL_RTSEMFASTMUTEX;
409 break;
410
411 case RTVFSLOCKTYPE_MUTEX:
412 RTSemMutexDestroy(pThis->u.hMtx);
413 pThis->u.hFastMtx = NIL_RTSEMMUTEX;
414 break;
415
416 default:
417 AssertMsgFailedReturnVoid(("%p %d\n", pThis, pThis->enmType));
418 }
419
420 pThis->enmType = RTVFSLOCKTYPE_INVALID;
421 RTMemFree(pThis);
422}
423
424
425RTDECL(uint32_t) RTVfsLockRelease(RTVFSLOCK hLock)
426{
427 RTVFSLOCKINTERNAL *pThis = hLock;
428 if (pThis == NIL_RTVFSLOCK)
429 return 0;
430 AssertPtrReturn(pThis, UINT32_MAX);
431 AssertReturn(pThis->enmType > RTVFSLOCKTYPE_INVALID && pThis->enmType < RTVFSLOCKTYPE_END, UINT32_MAX);
432
433 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
434 AssertMsg(cRefs < _1M, ("%#x %p %d\n", cRefs, pThis, pThis->enmType));
435 if (cRefs == 0)
436 rtVfsLockDestroy(pThis);
437 return cRefs;
438}
439
440
441/**
442 * Creates a read/write lock.
443 *
444 * @returns IPRT status code
445 * @param phLock Where to return the lock handle.
446 */
447static int rtVfsLockCreateRW(PRTVFSLOCK phLock)
448{
449 RTVFSLOCKINTERNAL *pThis = (RTVFSLOCKINTERNAL *)RTMemAlloc(sizeof(*pThis));
450 if (!pThis)
451 return VERR_NO_MEMORY;
452
453 pThis->cRefs = 1;
454 pThis->enmType = RTVFSLOCKTYPE_RW;
455
456 int rc = RTSemRWCreate(&pThis->u.hSemRW);
457 if (RT_FAILURE(rc))
458 {
459 RTMemFree(pThis);
460 return rc;
461 }
462
463 *phLock = pThis;
464 return VINF_SUCCESS;
465}
466
467
468/**
469 * Creates a fast mutex lock.
470 *
471 * @returns IPRT status code
472 * @param phLock Where to return the lock handle.
473 */
474static int rtVfsLockCreateFastMutex(PRTVFSLOCK phLock)
475{
476 RTVFSLOCKINTERNAL *pThis = (RTVFSLOCKINTERNAL *)RTMemAlloc(sizeof(*pThis));
477 if (!pThis)
478 return VERR_NO_MEMORY;
479
480 pThis->cRefs = 1;
481 pThis->enmType = RTVFSLOCKTYPE_FASTMUTEX;
482
483 int rc = RTSemFastMutexCreate(&pThis->u.hFastMtx);
484 if (RT_FAILURE(rc))
485 {
486 RTMemFree(pThis);
487 return rc;
488 }
489
490 *phLock = pThis;
491 return VINF_SUCCESS;
492
493}
494
495
496/**
497 * Creates a mutex lock.
498 *
499 * @returns IPRT status code
500 * @param phLock Where to return the lock handle.
501 */
502static int rtVfsLockCreateMutex(PRTVFSLOCK phLock)
503{
504 RTVFSLOCKINTERNAL *pThis = (RTVFSLOCKINTERNAL *)RTMemAlloc(sizeof(*pThis));
505 if (!pThis)
506 return VERR_NO_MEMORY;
507
508 pThis->cRefs = 1;
509 pThis->enmType = RTVFSLOCKTYPE_MUTEX;
510
511 int rc = RTSemMutexCreate(&pThis->u.hMtx);
512 if (RT_FAILURE(rc))
513 {
514 RTMemFree(pThis);
515 return rc;
516 }
517
518 *phLock = pThis;
519 return VINF_SUCCESS;
520}
521
522
523/**
524 * Acquires the lock for reading.
525 *
526 * @param hLock Non-nil lock handle.
527 * @internal
528 */
529RTDECL(void) RTVfsLockAcquireReadSlow(RTVFSLOCK hLock)
530{
531 RTVFSLOCKINTERNAL *pThis = hLock;
532 int rc;
533
534 AssertPtr(pThis);
535 switch (pThis->enmType)
536 {
537 case RTVFSLOCKTYPE_RW:
538 rc = RTSemRWRequestRead(pThis->u.hSemRW, RT_INDEFINITE_WAIT);
539 AssertRC(rc);
540 break;
541
542 case RTVFSLOCKTYPE_FASTMUTEX:
543 rc = RTSemFastMutexRequest(pThis->u.hFastMtx);
544 AssertRC(rc);
545 break;
546
547 case RTVFSLOCKTYPE_MUTEX:
548 rc = RTSemMutexRequest(pThis->u.hMtx, RT_INDEFINITE_WAIT);
549 AssertRC(rc);
550 break;
551 default:
552 AssertFailed();
553 }
554}
555
556
557/**
558 * Release a lock held for reading.
559 *
560 * @param hLock Non-nil lock handle.
561 * @internal
562 */
563RTDECL(void) RTVfsLockReleaseReadSlow(RTVFSLOCK hLock)
564{
565 RTVFSLOCKINTERNAL *pThis = hLock;
566 int rc;
567
568 AssertPtr(pThis);
569 switch (pThis->enmType)
570 {
571 case RTVFSLOCKTYPE_RW:
572 rc = RTSemRWReleaseRead(pThis->u.hSemRW);
573 AssertRC(rc);
574 break;
575
576 case RTVFSLOCKTYPE_FASTMUTEX:
577 rc = RTSemFastMutexRelease(pThis->u.hFastMtx);
578 AssertRC(rc);
579 break;
580
581 case RTVFSLOCKTYPE_MUTEX:
582 rc = RTSemMutexRelease(pThis->u.hMtx);
583 AssertRC(rc);
584 break;
585 default:
586 AssertFailed();
587 }
588}
589
590
591/**
592 * Acquires the lock for writing.
593 *
594 * @param hLock Non-nil lock handle.
595 * @internal
596 */
597RTDECL(void) RTVfsLockAcquireWriteSlow(RTVFSLOCK hLock)
598{
599 RTVFSLOCKINTERNAL *pThis = hLock;
600 int rc;
601
602 AssertPtr(pThis);
603 switch (pThis->enmType)
604 {
605 case RTVFSLOCKTYPE_RW:
606 rc = RTSemRWRequestWrite(pThis->u.hSemRW, RT_INDEFINITE_WAIT);
607 AssertRC(rc);
608 break;
609
610 case RTVFSLOCKTYPE_FASTMUTEX:
611 rc = RTSemFastMutexRequest(pThis->u.hFastMtx);
612 AssertRC(rc);
613 break;
614
615 case RTVFSLOCKTYPE_MUTEX:
616 rc = RTSemMutexRequest(pThis->u.hMtx, RT_INDEFINITE_WAIT);
617 AssertRC(rc);
618 break;
619 default:
620 AssertFailed();
621 }
622}
623
624
625/**
626 * Release a lock held for writing.
627 *
628 * @param hLock Non-nil lock handle.
629 * @internal
630 */
631RTDECL(void) RTVfsLockReleaseWriteSlow(RTVFSLOCK hLock)
632{
633 RTVFSLOCKINTERNAL *pThis = hLock;
634 int rc;
635
636 AssertPtr(pThis);
637 switch (pThis->enmType)
638 {
639 case RTVFSLOCKTYPE_RW:
640 rc = RTSemRWReleaseWrite(pThis->u.hSemRW);
641 AssertRC(rc);
642 break;
643
644 case RTVFSLOCKTYPE_FASTMUTEX:
645 rc = RTSemFastMutexRelease(pThis->u.hFastMtx);
646 AssertRC(rc);
647 break;
648
649 case RTVFSLOCKTYPE_MUTEX:
650 rc = RTSemMutexRelease(pThis->u.hMtx);
651 AssertRC(rc);
652 break;
653 default:
654 AssertFailed();
655 }
656}
657
658
659
660/*
661 *
662 * B A S E O B J E C T
663 * B A S E O B J E C T
664 * B A S E O B J E C T
665 *
666 */
667
668/**
669 * Internal object retainer that asserts sanity in strict builds.
670 *
671 * @param pThis The base object handle data.
672 * @param pszCaller Where we're called from.
673 */
674DECLINLINE(void) rtVfsObjRetainVoid(RTVFSOBJINTERNAL *pThis, const char *pszCaller)
675{
676 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
677LogFlow(("rtVfsObjRetainVoid(%p/%p) -> %d; caller=%s\n", pThis, pThis->pvThis, cRefs, pszCaller)); RT_NOREF(pszCaller);
678 AssertMsg(cRefs > 1 && cRefs < _1M,
679 ("%#x %p ops=%p %s (%d); caller=%s\n", cRefs, pThis, pThis->pOps, pThis->pOps->pszName, pThis->pOps->enmType, pszCaller));
680 NOREF(cRefs);
681}
682
683
684/**
685 * Initializes the base object part of a new object.
686 *
687 * @returns IPRT status code.
688 * @param pThis Pointer to the base object part.
689 * @param pObjOps The base object vtable.
690 * @param hVfs The VFS handle to associate with.
691 * @param fNoVfsRef If set, do not retain an additional reference to
692 * @a hVfs. Permanent root dir hack.
693 * @param hLock The lock handle, pseudo handle or nil.
694 * @param pvThis Pointer to the private data.
695 */
696static int rtVfsObjInitNewObject(RTVFSOBJINTERNAL *pThis, PCRTVFSOBJOPS pObjOps, RTVFS hVfs, bool fNoVfsRef,
697 RTVFSLOCK hLock, void *pvThis)
698{
699 /*
700 * Deal with the lock first as that's the most complicated matter.
701 */
702 if (hLock != NIL_RTVFSLOCK)
703 {
704 int rc;
705 if (hLock == RTVFSLOCK_CREATE_RW)
706 {
707 rc = rtVfsLockCreateRW(&hLock);
708 AssertRCReturn(rc, rc);
709 }
710 else if (hLock == RTVFSLOCK_CREATE_FASTMUTEX)
711 {
712 rc = rtVfsLockCreateFastMutex(&hLock);
713 AssertRCReturn(rc, rc);
714 }
715 else if (hLock == RTVFSLOCK_CREATE_MUTEX)
716 {
717 rc = rtVfsLockCreateMutex(&hLock);
718 AssertRCReturn(rc, rc);
719 }
720 else
721 {
722 /*
723 * The caller specified a lock, we consume the this reference.
724 */
725 AssertPtrReturn(hLock, VERR_INVALID_HANDLE);
726 AssertReturn(hLock->enmType > RTVFSLOCKTYPE_INVALID && hLock->enmType < RTVFSLOCKTYPE_END, VERR_INVALID_HANDLE);
727 AssertReturn(hLock->cRefs > 0, VERR_INVALID_HANDLE);
728 }
729 }
730 else if (hVfs != NIL_RTVFS)
731 {
732 /*
733 * Retain a reference to the VFS lock, if there is one.
734 */
735 hLock = hVfs->Base.hLock;
736 if (hLock != NIL_RTVFSLOCK)
737 {
738 uint32_t cRefs = RTVfsLockRetain(hLock);
739 if (RT_UNLIKELY(cRefs == UINT32_MAX))
740 return VERR_INVALID_HANDLE;
741 }
742 }
743
744
745 /*
746 * Do the actual initializing.
747 */
748 pThis->uMagic = RTVFSOBJ_MAGIC;
749 pThis->fNoVfsRef = fNoVfsRef;
750 pThis->pvThis = pvThis;
751 pThis->pOps = pObjOps;
752 pThis->cRefs = 1;
753 pThis->hVfs = hVfs;
754 pThis->hLock = hLock;
755 if (hVfs != NIL_RTVFS && !fNoVfsRef)
756 rtVfsObjRetainVoid(&hVfs->Base, "rtVfsObjInitNewObject");
757
758 return VINF_SUCCESS;
759}
760
761
762RTDECL(int) RTVfsNewBaseObj(PCRTVFSOBJOPS pObjOps, size_t cbInstance, RTVFS hVfs, RTVFSLOCK hLock,
763 PRTVFSOBJ phVfsObj, void **ppvInstance)
764{
765 /*
766 * Validate the input, be extra strict in strict builds.
767 */
768 AssertPtr(pObjOps);
769 AssertReturn(pObjOps->uVersion == RTVFSOBJOPS_VERSION, VERR_VERSION_MISMATCH);
770 AssertReturn(pObjOps->uEndMarker == RTVFSOBJOPS_VERSION, VERR_VERSION_MISMATCH);
771 RTVFSOBJ_ASSERT_OPS(pObjOps, RTVFSOBJTYPE_BASE);
772 Assert(cbInstance > 0);
773 AssertPtr(ppvInstance);
774 AssertPtr(phVfsObj);
775 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
776
777 /*
778 * Allocate the handle + instance data.
779 */
780 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSOBJINTERNAL), RTVFS_INST_ALIGNMENT)
781 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
782 RTVFSOBJINTERNAL *pThis = (RTVFSOBJINTERNAL *)RTMemAllocZ(cbThis);
783 if (!pThis)
784 return VERR_NO_MEMORY;
785
786 int rc = rtVfsObjInitNewObject(pThis, pObjOps, hVfs, false /*fNoVfsRef*/, hLock,
787 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
788 if (RT_FAILURE(rc))
789 {
790 RTMemFree(pThis);
791 return rc;
792 }
793
794 *phVfsObj = pThis;
795 *ppvInstance = pThis->pvThis;
796 return VINF_SUCCESS;
797}
798
799
800/**
801 * Internal object retainer that asserts sanity in strict builds.
802 *
803 * @returns The new reference count.
804 * @param pThis The base object handle data.
805 */
806DECLINLINE(uint32_t) rtVfsObjRetain(RTVFSOBJINTERNAL *pThis)
807{
808 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
809LogFlow(("rtVfsObjRetain(%p/%p) -> %d\n", pThis, pThis->pvThis, cRefs));
810 AssertMsg(cRefs > 1 && cRefs < _1M,
811 ("%#x %p ops=%p %s (%d)\n", cRefs, pThis, pThis->pOps, pThis->pOps->pszName, pThis->pOps->enmType));
812 return cRefs;
813}
814
815/**
816 * Internal object retainer that asserts sanity in strict builds.
817 *
818 * @returns The new reference count.
819 * @param pThis The base object handle data.
820 */
821DECLINLINE(uint32_t) rtVfsObjRetainDebug(RTVFSOBJINTERNAL *pThis, const char *pszApi, RT_SRC_POS_DECL)
822{
823 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
824 AssertMsg(cRefs > 1 && cRefs < _1M,
825 ("%#x %p ops=%p %s (%d)\n", cRefs, pThis, pThis->pOps, pThis->pOps->pszName, pThis->pOps->enmType));
826 LogFlow(("%s(%p/%p) -> %2d; caller: %s %s(%d) \n", pszApi, pThis, pThis->pvThis, cRefs, pszFunction, pszFile, iLine));
827 RT_SRC_POS_NOREF(); RT_NOREF(pszApi);
828 return cRefs;
829}
830
831
832#ifdef DEBUG
833# undef RTVfsObjRetain
834#endif
835RTDECL(uint32_t) RTVfsObjRetain(RTVFSOBJ hVfsObj)
836{
837 RTVFSOBJINTERNAL *pThis = hVfsObj;
838 AssertPtrReturn(pThis, UINT32_MAX);
839 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, UINT32_MAX);
840
841 return rtVfsObjRetain(pThis);
842}
843#ifdef DEBUG
844# define RTVfsObjRetain(hVfsObj) RTVfsObjRetainDebug(hVfsObj, RT_SRC_POS)
845#endif
846
847
848RTDECL(uint32_t) RTVfsObjRetainDebug(RTVFSOBJ hVfsObj, RT_SRC_POS_DECL)
849{
850 RTVFSOBJINTERNAL *pThis = hVfsObj;
851 AssertPtrReturn(pThis, UINT32_MAX);
852 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, UINT32_MAX);
853
854 return rtVfsObjRetainDebug(pThis, "RTVfsObjRetainDebug", RT_SRC_POS_ARGS);
855}
856
857
858/**
859 * Does the actual object destruction for rtVfsObjRelease().
860 *
861 * @param pThis The object to destroy.
862 */
863static void rtVfsObjDestroy(RTVFSOBJINTERNAL *pThis)
864{
865 RTVFSOBJTYPE const enmType = pThis->pOps->enmType;
866
867 /*
868 * Invalidate the object.
869 */
870 RTVfsLockAcquireWrite(pThis->hLock); /* paranoia */
871 void *pvToFree = NULL;
872 switch (enmType)
873 {
874 case RTVFSOBJTYPE_BASE:
875 pvToFree = pThis;
876 break;
877
878 case RTVFSOBJTYPE_VFS:
879 pvToFree = RT_FROM_MEMBER(pThis, RTVFSINTERNAL, Base);
880 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSINTERNAL, Base)->uMagic, RTVFS_MAGIC_DEAD);
881 break;
882
883 case RTVFSOBJTYPE_FS_STREAM:
884 pvToFree = RT_FROM_MEMBER(pThis, RTVFSFSSTREAMINTERNAL, Base);
885 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSFSSTREAMINTERNAL, Base)->uMagic, RTVFSFSSTREAM_MAGIC_DEAD);
886 break;
887
888 case RTVFSOBJTYPE_IO_STREAM:
889 pvToFree = RT_FROM_MEMBER(pThis, RTVFSIOSTREAMINTERNAL, Base);
890 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSIOSTREAMINTERNAL, Base)->uMagic, RTVFSIOSTREAM_MAGIC_DEAD);
891 break;
892
893 case RTVFSOBJTYPE_DIR:
894 pvToFree = RT_FROM_MEMBER(pThis, RTVFSDIRINTERNAL, Base);
895 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSDIRINTERNAL, Base)->uMagic, RTVFSDIR_MAGIC_DEAD);
896 break;
897
898 case RTVFSOBJTYPE_FILE:
899 pvToFree = RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream.Base);
900 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream.Base)->uMagic, RTVFSFILE_MAGIC_DEAD);
901 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSIOSTREAMINTERNAL, Base)->uMagic, RTVFSIOSTREAM_MAGIC_DEAD);
902 break;
903
904 case RTVFSOBJTYPE_SYMLINK:
905 pvToFree = RT_FROM_MEMBER(pThis, RTVFSSYMLINKINTERNAL, Base);
906 ASMAtomicWriteU32(&RT_FROM_MEMBER(pThis, RTVFSSYMLINKINTERNAL, Base)->uMagic, RTVFSSYMLINK_MAGIC_DEAD);
907 break;
908
909 case RTVFSOBJTYPE_INVALID:
910 case RTVFSOBJTYPE_END:
911 case RTVFSOBJTYPE_32BIT_HACK:
912 AssertMsgFailed(("enmType=%d ops=%p %s\n", enmType, pThis->pOps, pThis->pOps->pszName));
913 break;
914 /* no default as we want gcc warnings. */
915 }
916 pThis->uMagic = RTVFSOBJ_MAGIC_DEAD;
917 RTVfsLockReleaseWrite(pThis->hLock);
918
919 /*
920 * Close the object and free the handle.
921 */
922 int rc = pThis->pOps->pfnClose(pThis->pvThis);
923 AssertRC(rc);
924 if (pThis->hVfs != NIL_RTVFS)
925 {
926 if (!pThis->fNoVfsRef)
927 rtVfsObjRelease(&pThis->hVfs->Base);
928 pThis->hVfs = NIL_RTVFS;
929 }
930 RTMemFree(pvToFree);
931}
932
933
934/**
935 * Internal object releaser that asserts sanity in strict builds.
936 *
937 * @returns The new reference count.
938 * @param pcRefs The reference counter.
939 */
940DECLINLINE(uint32_t) rtVfsObjRelease(RTVFSOBJINTERNAL *pThis)
941{
942 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
943 AssertMsg(cRefs < _1M, ("%#x %p ops=%p %s (%d)\n", cRefs, pThis, pThis->pOps, pThis->pOps->pszName, pThis->pOps->enmType));
944 LogFlow(("rtVfsObjRelease(%p/%p) -> %d\n", pThis, pThis->pvThis, cRefs));
945 if (cRefs == 0)
946 rtVfsObjDestroy(pThis);
947 return cRefs;
948}
949
950
951RTDECL(uint32_t) RTVfsObjRelease(RTVFSOBJ hVfsObj)
952{
953 RTVFSOBJINTERNAL *pThis = hVfsObj;
954 if (pThis == NIL_RTVFSOBJ)
955 return 0;
956 AssertPtrReturn(pThis, UINT32_MAX);
957 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, UINT32_MAX);
958 return rtVfsObjRelease(pThis);
959}
960
961
962RTDECL(RTVFS) RTVfsObjToVfs(RTVFSOBJ hVfsObj)
963{
964 RTVFSOBJINTERNAL *pThis = hVfsObj;
965 if (pThis != NIL_RTVFSOBJ)
966 {
967 AssertPtrReturn(pThis, NIL_RTVFS);
968 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFS);
969
970 if (pThis->pOps->enmType == RTVFSOBJTYPE_VFS)
971 {
972 rtVfsObjRetainVoid(pThis, "RTVfsObjToVfs");
973 LogFlow(("RTVfsObjToVfs(%p) -> %p\n", pThis, RT_FROM_MEMBER(pThis, RTVFSINTERNAL, Base)));
974 return RT_FROM_MEMBER(pThis, RTVFSINTERNAL, Base);
975 }
976 }
977 return NIL_RTVFS;
978}
979
980
981RTDECL(RTVFSFSSTREAM) RTVfsObjToFsStream(RTVFSOBJ hVfsObj)
982{
983 RTVFSOBJINTERNAL *pThis = hVfsObj;
984 if (pThis != NIL_RTVFSOBJ)
985 {
986 AssertPtrReturn(pThis, NIL_RTVFSFSSTREAM);
987 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSFSSTREAM);
988
989 if (pThis->pOps->enmType == RTVFSOBJTYPE_FS_STREAM)
990 {
991 rtVfsObjRetainVoid(pThis, "RTVfsObjToFsStream");
992 return RT_FROM_MEMBER(pThis, RTVFSFSSTREAMINTERNAL, Base);
993 }
994 }
995 return NIL_RTVFSFSSTREAM;
996}
997
998
999RTDECL(RTVFSDIR) RTVfsObjToDir(RTVFSOBJ hVfsObj)
1000{
1001 RTVFSOBJINTERNAL *pThis = hVfsObj;
1002 if (pThis != NIL_RTVFSOBJ)
1003 {
1004 AssertPtrReturn(pThis, NIL_RTVFSDIR);
1005 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSDIR);
1006
1007 if (pThis->pOps->enmType == RTVFSOBJTYPE_DIR)
1008 {
1009 rtVfsObjRetainVoid(pThis, "RTVfsObjToDir");
1010 return RT_FROM_MEMBER(pThis, RTVFSDIRINTERNAL, Base);
1011 }
1012 }
1013 return NIL_RTVFSDIR;
1014}
1015
1016
1017RTDECL(RTVFSIOSTREAM) RTVfsObjToIoStream(RTVFSOBJ hVfsObj)
1018{
1019 RTVFSOBJINTERNAL *pThis = hVfsObj;
1020 if (pThis != NIL_RTVFSOBJ)
1021 {
1022 AssertPtrReturn(pThis, NIL_RTVFSIOSTREAM);
1023 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSIOSTREAM);
1024
1025 if ( pThis->pOps->enmType == RTVFSOBJTYPE_IO_STREAM
1026 || pThis->pOps->enmType == RTVFSOBJTYPE_FILE)
1027 {
1028 rtVfsObjRetainVoid(pThis, "RTVfsObjToIoStream");
1029 return RT_FROM_MEMBER(pThis, RTVFSIOSTREAMINTERNAL, Base);
1030 }
1031 }
1032 return NIL_RTVFSIOSTREAM;
1033}
1034
1035
1036RTDECL(RTVFSFILE) RTVfsObjToFile(RTVFSOBJ hVfsObj)
1037{
1038 RTVFSOBJINTERNAL *pThis = hVfsObj;
1039 if (pThis != NIL_RTVFSOBJ)
1040 {
1041 AssertPtrReturn(pThis, NIL_RTVFSFILE);
1042 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSFILE);
1043
1044 if (pThis->pOps->enmType == RTVFSOBJTYPE_FILE)
1045 {
1046 rtVfsObjRetainVoid(pThis, "RTVfsObjToFile");
1047 return RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream.Base);
1048 }
1049 }
1050 return NIL_RTVFSFILE;
1051}
1052
1053
1054RTDECL(RTVFSSYMLINK) RTVfsObjToSymlink(RTVFSOBJ hVfsObj)
1055{
1056 RTVFSOBJINTERNAL *pThis = hVfsObj;
1057 if (pThis != NIL_RTVFSOBJ)
1058 {
1059 AssertPtrReturn(pThis, NIL_RTVFSSYMLINK);
1060 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSSYMLINK);
1061
1062 if (pThis->pOps->enmType == RTVFSOBJTYPE_SYMLINK)
1063 {
1064 rtVfsObjRetainVoid(pThis, "RTVfsObjToSymlink");
1065 return RT_FROM_MEMBER(pThis, RTVFSSYMLINKINTERNAL, Base);
1066 }
1067 }
1068 return NIL_RTVFSSYMLINK;
1069}
1070
1071
1072RTDECL(RTVFSOBJ) RTVfsObjFromVfs(RTVFS hVfs)
1073{
1074 if (hVfs != NIL_RTVFS)
1075 {
1076 RTVFSOBJINTERNAL *pThis = &hVfs->Base;
1077 AssertPtrReturn(pThis, NIL_RTVFSOBJ);
1078 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSOBJ);
1079
1080 rtVfsObjRetainVoid(pThis, "RTVfsObjFromVfs");
1081 LogFlow(("RTVfsObjFromVfs(%p) -> %p\n", hVfs, pThis));
1082 return pThis;
1083 }
1084 return NIL_RTVFSOBJ;
1085}
1086
1087
1088RTDECL(RTVFSOBJ) RTVfsObjFromFsStream(RTVFSFSSTREAM hVfsFss)
1089{
1090 if (hVfsFss != NIL_RTVFSFSSTREAM)
1091 {
1092 RTVFSOBJINTERNAL *pThis = &hVfsFss->Base;
1093 AssertPtrReturn(pThis, NIL_RTVFSOBJ);
1094 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSOBJ);
1095
1096 rtVfsObjRetainVoid(pThis, "RTVfsObjFromFsStream");
1097 return pThis;
1098 }
1099 return NIL_RTVFSOBJ;
1100}
1101
1102
1103RTDECL(RTVFSOBJ) RTVfsObjFromDir(RTVFSDIR hVfsDir)
1104{
1105 if (hVfsDir != NIL_RTVFSDIR)
1106 {
1107 RTVFSOBJINTERNAL *pThis = &hVfsDir->Base;
1108 AssertPtrReturn(pThis, NIL_RTVFSOBJ);
1109 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSOBJ);
1110
1111 rtVfsObjRetainVoid(pThis, "RTVfsObjFromDir");
1112 return pThis;
1113 }
1114 return NIL_RTVFSOBJ;
1115}
1116
1117
1118RTDECL(RTVFSOBJ) RTVfsObjFromIoStream(RTVFSIOSTREAM hVfsIos)
1119{
1120 if (hVfsIos != NIL_RTVFSIOSTREAM)
1121 {
1122 RTVFSOBJINTERNAL *pThis = &hVfsIos->Base;
1123 AssertPtrReturn(pThis, NIL_RTVFSOBJ);
1124 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSOBJ);
1125
1126 rtVfsObjRetainVoid(pThis, "RTVfsObjFromIoStream");
1127 return pThis;
1128 }
1129 return NIL_RTVFSOBJ;
1130}
1131
1132
1133RTDECL(RTVFSOBJ) RTVfsObjFromFile(RTVFSFILE hVfsFile)
1134{
1135 if (hVfsFile != NIL_RTVFSFILE)
1136 {
1137 RTVFSOBJINTERNAL *pThis = &hVfsFile->Stream.Base;
1138 AssertPtrReturn(pThis, NIL_RTVFSOBJ);
1139 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSOBJ);
1140
1141 rtVfsObjRetainVoid(pThis, "RTVfsObjFromFile");
1142 return pThis;
1143 }
1144 return NIL_RTVFSOBJ;
1145}
1146
1147
1148RTDECL(RTVFSOBJ) RTVfsObjFromSymlink(RTVFSSYMLINK hVfsSym)
1149{
1150 if (hVfsSym != NIL_RTVFSSYMLINK)
1151 {
1152 RTVFSOBJINTERNAL *pThis = &hVfsSym->Base;
1153 AssertPtrReturn(pThis, NIL_RTVFSOBJ);
1154 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, NIL_RTVFSOBJ);
1155
1156 rtVfsObjRetainVoid(pThis, "RTVfsObjFromSymlink");
1157 return pThis;
1158 }
1159 return NIL_RTVFSOBJ;
1160}
1161
1162
1163
1164RTDECL(int) RTVfsObjQueryInfo(RTVFSOBJ hVfsObj, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1165{
1166 RTVFSOBJINTERNAL *pThis = hVfsObj;
1167 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1168 AssertReturn(pThis->uMagic == RTVFSOBJ_MAGIC, VERR_INVALID_HANDLE);
1169
1170 RTVfsLockAcquireRead(pThis->hLock);
1171 int rc = pThis->pOps->pfnQueryInfo(pThis->pvThis, pObjInfo, enmAddAttr);
1172 RTVfsLockReleaseRead(pThis->hLock);
1173 return rc;
1174}
1175
1176
1177
1178/*
1179 *
1180 * U T I L U T I L U T I L
1181 * U T I L U T I L U T I L
1182 * U T I L U T I L U T I L
1183 *
1184 */
1185
1186
1187
1188/**
1189 * Removes dots from the path.
1190 *
1191 * @returns The new @a pszDst value.
1192 * @param pPath The path parsing buffer.
1193 * @param pszDst The current szPath position. This will be
1194 * updated and returned.
1195 * @param fTheEnd Indicates whether we're at the end of the path
1196 * or not.
1197 * @param piRestartComp The component to restart parsing at.
1198 */
1199static char *rtVfsParsePathHandleDots(PRTVFSPARSEDPATH pPath, char *pszDst, bool fTheEnd, uint16_t *piRestartComp)
1200{
1201 if (pszDst[-1] != '.')
1202 return pszDst;
1203
1204 if (pszDst[-2] == '/')
1205 {
1206 pPath->cComponents--;
1207 pszDst = &pPath->szPath[pPath->aoffComponents[pPath->cComponents]];
1208 }
1209 else if (pszDst[-2] == '.' && pszDst[-3] == '/')
1210 {
1211 pPath->cComponents -= pPath->cComponents > 1 ? 2 : 1;
1212 pszDst = &pPath->szPath[pPath->aoffComponents[pPath->cComponents]];
1213 if (piRestartComp && *piRestartComp + 1 >= pPath->cComponents)
1214 *piRestartComp = pPath->cComponents > 0 ? pPath->cComponents - 1 : 0;
1215 }
1216 else
1217 return pszDst;
1218
1219 /*
1220 * Drop the trailing slash if we're at the end of the source path.
1221 */
1222 if (fTheEnd && pPath->cComponents == 0)
1223 pszDst--;
1224 return pszDst;
1225}
1226
1227
1228RTDECL(int) RTVfsParsePathAppend(PRTVFSPARSEDPATH pPath, const char *pszPath, uint16_t *piRestartComp)
1229{
1230 AssertReturn(*pszPath != '/', VERR_INTERNAL_ERROR_4);
1231
1232 /* In case *piRestartComp was set higher than the number of components
1233 before making the call to this function. */
1234 if (piRestartComp && *piRestartComp + 1 >= pPath->cComponents)
1235 *piRestartComp = pPath->cComponents > 0 ? pPath->cComponents - 1 : 0;
1236
1237 /*
1238 * Append a slash to the destination path if necessary.
1239 */
1240 char *pszDst = &pPath->szPath[pPath->cch];
1241 if (pPath->cComponents > 0)
1242 {
1243 *pszDst++ = '/';
1244 if (pszDst - &pPath->szPath[0] >= RTVFSPARSEDPATH_MAX)
1245 return VERR_FILENAME_TOO_LONG;
1246 }
1247 Assert(pszDst[-1] == '/');
1248
1249 /*
1250 * Parse and append the relative path.
1251 */
1252 const char *pszSrc = pszPath;
1253 pPath->fDirSlash = false;
1254 while (pszSrc[0])
1255 {
1256 /* Skip unncessary slashes. */
1257 while (pszSrc[0] == '/')
1258 pszSrc++;
1259
1260 /* Copy until we encounter the next slash. */
1261 pPath->aoffComponents[pPath->cComponents++] = pszDst - &pPath->szPath[0];
1262 while (pszSrc[0])
1263 {
1264 if (pszSrc[0] == '/')
1265 {
1266 pszSrc++;
1267 if (pszSrc[0])
1268 *pszDst++ = '/';
1269 else
1270 pPath->fDirSlash = true;
1271 pszDst = rtVfsParsePathHandleDots(pPath, pszDst, pszSrc[0] == '\0', piRestartComp);
1272 break;
1273 }
1274
1275 *pszDst++ = *pszSrc++;
1276 if (pszDst - &pPath->szPath[0] >= RTVFSPARSEDPATH_MAX)
1277 return VERR_FILENAME_TOO_LONG;
1278 }
1279 }
1280 pszDst = rtVfsParsePathHandleDots(pPath, pszDst, true /*fTheEnd*/, piRestartComp);
1281
1282 /* Terminate the string and enter its length. */
1283 pszDst[0] = '\0';
1284 pszDst[1] = '\0'; /* for aoffComponents */
1285 pPath->cch = (uint16_t)(pszDst - &pPath->szPath[0]);
1286 pPath->aoffComponents[pPath->cComponents] = pPath->cch + 1;
1287
1288 return VINF_SUCCESS;
1289}
1290
1291
1292/** @todo Replace RTVfsParsePath with RTPathParse and friends? */
1293RTDECL(int) RTVfsParsePath(PRTVFSPARSEDPATH pPath, const char *pszPath, const char *pszCwd)
1294{
1295 if (*pszPath != '/')
1296 {
1297 /*
1298 * Relative, recurse and parse pszCwd first.
1299 */
1300 int rc = RTVfsParsePath(pPath, pszCwd, NULL /*crash if pszCwd is not absolute*/);
1301 if (RT_FAILURE(rc))
1302 return rc;
1303 }
1304 else
1305 {
1306 /*
1307 * Make pszPath relative, i.e. set up pPath for the root and skip
1308 * leading slashes in pszPath before appending it.
1309 */
1310 pPath->cch = 1;
1311 pPath->cComponents = 0;
1312 pPath->fDirSlash = false;
1313 pPath->aoffComponents[0] = 1;
1314 pPath->aoffComponents[1] = 2;
1315 pPath->szPath[0] = '/';
1316 pPath->szPath[1] = '\0';
1317 pPath->szPath[2] = '\0';
1318 while (pszPath[0] == '/')
1319 pszPath++;
1320 if (!pszPath[0])
1321 return VINF_SUCCESS;
1322 }
1323 return RTVfsParsePathAppend(pPath, pszPath, NULL);
1324}
1325
1326
1327
1328RTDECL(int) RTVfsParsePathA(const char *pszPath, const char *pszCwd, PRTVFSPARSEDPATH *ppPath)
1329{
1330 /*
1331 * Allocate the output buffer and hand the problem to rtVfsParsePath.
1332 */
1333 int rc;
1334 PRTVFSPARSEDPATH pPath = (PRTVFSPARSEDPATH)RTMemTmpAlloc(sizeof(RTVFSPARSEDPATH));
1335 if (pPath)
1336 {
1337 rc = RTVfsParsePath(pPath, pszPath, pszCwd);
1338 if (RT_FAILURE(rc))
1339 {
1340 RTMemTmpFree(pPath);
1341 pPath = NULL;
1342 }
1343 }
1344 else
1345 rc = VERR_NO_TMP_MEMORY;
1346 *ppPath = pPath; /* always set it */
1347 return rc;
1348}
1349
1350
1351RTDECL(void) RTVfsParsePathFree(PRTVFSPARSEDPATH pPath)
1352{
1353 if (pPath)
1354 {
1355 pPath->cch = UINT16_MAX;
1356 pPath->cComponents = UINT16_MAX;
1357 pPath->aoffComponents[0] = UINT16_MAX;
1358 pPath->aoffComponents[1] = UINT16_MAX;
1359 RTMemTmpFree(pPath);
1360 }
1361}
1362
1363
1364/**
1365 * Handles a symbolic link, adding it to
1366 *
1367 * @returns IPRT status code.
1368 * @param pPath The parsed path to update.
1369 * @param piComponent The component iterator to update.
1370 * @param hSymlink The symbolic link to process.
1371 */
1372static int rtVfsTraverseHandleSymlink(PRTVFSPARSEDPATH pPath, uint16_t *piComponent, RTVFSSYMLINK hSymlink)
1373{
1374 /*
1375 * Read the link.
1376 */
1377 char szPath[RTPATH_MAX];
1378 int rc = RTVfsSymlinkRead(hSymlink, szPath, sizeof(szPath) - 1);
1379 if (RT_SUCCESS(rc))
1380 {
1381 szPath[sizeof(szPath) - 1] = '\0';
1382 if (szPath[0] == '/')
1383 {
1384 /*
1385 * Absolute symlink.
1386 */
1387 rc = RTVfsParsePath(pPath, szPath, NULL);
1388 if (RT_SUCCESS(rc))
1389 {
1390 *piComponent = 0;
1391 return VINF_SUCCESS;
1392 }
1393 }
1394 else
1395 {
1396 /*
1397 * Relative symlink, must replace the current component with the
1398 * link value. We do that by using the remainder of the symlink
1399 * buffer as temporary storage.
1400 */
1401 uint16_t iComponent = *piComponent;
1402 if (iComponent + 1 < pPath->cComponents)
1403 rc = RTPathAppend(szPath, sizeof(szPath), &pPath->szPath[pPath->aoffComponents[iComponent + 1]]);
1404 if (RT_SUCCESS(rc))
1405 {
1406 pPath->cch = pPath->aoffComponents[iComponent] - (iComponent > 0);
1407 pPath->aoffComponents[iComponent + 1] = pPath->cch + 1;
1408 pPath->szPath[pPath->cch] = '\0';
1409 pPath->szPath[pPath->cch + 1] = '\0';
1410
1411 rc = RTVfsParsePathAppend(pPath, szPath, &iComponent);
1412 if (RT_SUCCESS(rc))
1413 {
1414 *piComponent = iComponent;
1415 return VINF_SUCCESS;
1416 }
1417 }
1418 }
1419 }
1420 return rc == VERR_BUFFER_OVERFLOW ? VERR_FILENAME_TOO_LONG : rc;
1421}
1422
1423
1424/**
1425 * Internal worker for various open functions as well as RTVfsTraverseToParent.
1426 *
1427 * @returns IPRT status code.
1428 * @param pThis The VFS.
1429 * @param pPath The parsed path. This may be changed as symbolic
1430 * links are processed during the path traversal.
1431 * @param fFlags RTPATH_F_XXX.
1432 * @param ppVfsParentDir Where to return the parent directory handle
1433 * (referenced).
1434 */
1435static int rtVfsDirTraverseToParent(RTVFSDIRINTERNAL *pThis, PRTVFSPARSEDPATH pPath, uint32_t fFlags,
1436 RTVFSDIRINTERNAL **ppVfsParentDir)
1437{
1438 /*
1439 * Assert sanity.
1440 */
1441 AssertPtr(pThis);
1442 Assert(pThis->uMagic == RTVFSDIR_MAGIC);
1443 Assert(pThis->Base.cRefs > 0);
1444 AssertPtr(pPath);
1445 AssertPtr(ppVfsParentDir);
1446 *ppVfsParentDir = NULL;
1447 AssertReturn(pPath->cComponents > 0, VERR_INTERNAL_ERROR_3);
1448 Assert(RTPATH_F_IS_VALID(fFlags, 0));
1449
1450 /*
1451 * Start with the pThis directory.
1452 */
1453 if (RTVfsDirRetain(pThis) == UINT32_MAX)
1454 return VERR_INVALID_HANDLE;
1455 RTVFSDIRINTERNAL *pCurDir = pThis;
1456
1457 /*
1458 * The traversal loop.
1459 */
1460 int rc = VINF_SUCCESS;
1461 unsigned cLinks = 0;
1462 uint16_t iComponent = 0;
1463 for (;;)
1464 {
1465 /*
1466 * Are we done yet?
1467 */
1468 bool fFinal = iComponent + 1 >= pPath->cComponents;
1469 if (fFinal && (fFlags & RTPATH_F_ON_LINK))
1470 {
1471 *ppVfsParentDir = pCurDir;
1472 return VINF_SUCCESS;
1473 }
1474
1475 /*
1476 * Try open the next entry.
1477 */
1478 const char *pszEntry = &pPath->szPath[pPath->aoffComponents[iComponent]];
1479 char *pszEntryEnd = &pPath->szPath[pPath->aoffComponents[iComponent + 1] - 1];
1480 *pszEntryEnd = '\0';
1481 RTVFSDIR hDir = NIL_RTVFSDIR;
1482 RTVFSSYMLINK hSymlink = NIL_RTVFSSYMLINK;
1483 RTVFS hVfsMnt = NIL_RTVFS;
1484 if (fFinal)
1485 {
1486 RTVfsLockAcquireRead(pCurDir->Base.hLock);
1487 rc = pCurDir->pOps->pfnTraversalOpen(pCurDir->Base.pvThis, pszEntry, NULL, &hSymlink, NULL);
1488 RTVfsLockReleaseRead(pCurDir->Base.hLock);
1489 *pszEntryEnd = '\0';
1490 if ( rc == VERR_PATH_NOT_FOUND
1491 || rc == VERR_NOT_A_DIRECTORY
1492 || rc == VERR_NOT_SYMLINK)
1493 rc = VINF_SUCCESS;
1494 if (RT_FAILURE(rc))
1495 break;
1496
1497 if (hSymlink == NIL_RTVFSSYMLINK)
1498 {
1499 *ppVfsParentDir = pCurDir;
1500 return VINF_SUCCESS;
1501 }
1502 }
1503 else
1504 {
1505 RTVfsLockAcquireRead(pCurDir->Base.hLock);
1506 rc = pCurDir->pOps->pfnTraversalOpen(pCurDir->Base.pvThis, pszEntry, &hDir, &hSymlink, &hVfsMnt);
1507 RTVfsLockReleaseRead(pCurDir->Base.hLock);
1508 *pszEntryEnd = '/';
1509 if (RT_FAILURE(rc))
1510 break;
1511
1512 if ( hDir == NIL_RTVFSDIR
1513 && hSymlink == NIL_RTVFSSYMLINK
1514 && hVfsMnt == NIL_RTVFS)
1515 {
1516 rc = VERR_NOT_A_DIRECTORY;
1517 break;
1518 }
1519 }
1520 Assert( (hDir != NIL_RTVFSDIR && hSymlink == NIL_RTVFSSYMLINK && hVfsMnt == NIL_RTVFS)
1521 || (hDir == NIL_RTVFSDIR && hSymlink != NIL_RTVFSSYMLINK && hVfsMnt == NIL_RTVFS)
1522 || (hDir == NIL_RTVFSDIR && hSymlink == NIL_RTVFSSYMLINK && hVfsMnt != NIL_RTVFS));
1523
1524 if (hDir != NIL_RTVFSDIR)
1525 {
1526 /*
1527 * Directory - advance down the path.
1528 */
1529 AssertPtr(hDir);
1530 Assert(hDir->uMagic == RTVFSDIR_MAGIC);
1531 RTVfsDirRelease(pCurDir);
1532 pCurDir = hDir;
1533 iComponent++;
1534 }
1535 else if (hSymlink != NIL_RTVFSSYMLINK)
1536 {
1537 /*
1538 * Symbolic link - deal with it and retry the current component.
1539 */
1540 AssertPtr(hSymlink);
1541 Assert(hSymlink->uMagic == RTVFSSYMLINK_MAGIC);
1542 if (fFlags & RTPATH_F_NO_SYMLINKS)
1543 {
1544 rc = VERR_SYMLINK_NOT_ALLOWED;
1545 break;
1546 }
1547 cLinks++;
1548 if (cLinks >= RTVFS_MAX_LINKS)
1549 {
1550 rc = VERR_TOO_MANY_SYMLINKS;
1551 break;
1552 }
1553 uint16_t iRestartComp = iComponent;
1554 rc = rtVfsTraverseHandleSymlink(pPath, &iRestartComp, hSymlink);
1555 if (RT_FAILURE(rc))
1556 break;
1557 if (iRestartComp != iComponent)
1558 {
1559 /* Must restart from the root. */
1560 RTVfsDirRelease(pCurDir);
1561 if (RTVfsDirRetain(pThis) == UINT32_MAX)
1562 {
1563 rc = VERR_INVALID_HANDLE;
1564 pCurDir = NULL;
1565 break;
1566 }
1567 pCurDir = pThis;
1568 iComponent = 0;
1569 }
1570 }
1571 else
1572 {
1573 /*
1574 * Mount point - deal with it and retry the current component.
1575 */
1576 RTVfsDirRelease(pCurDir);
1577 RTVfsLockAcquireRead(hVfsMnt->Base.hLock);
1578 rc = hVfsMnt->pOps->pfnOpenRoot(hVfsMnt->Base.pvThis, &pCurDir);
1579 RTVfsLockReleaseRead(hVfsMnt->Base.hLock);
1580 if (RT_FAILURE(rc))
1581 {
1582 pCurDir = NULL;
1583 break;
1584 }
1585 iComponent = 0;
1586 /** @todo union mounts. */
1587 }
1588 }
1589
1590 if (pCurDir)
1591 RTVfsDirRelease(pCurDir);
1592
1593 return rc;
1594}
1595
1596
1597/**
1598 * Internal worker for various open functions as well as RTVfsTraverseToParent.
1599 *
1600 * @returns IPRT status code.
1601 * @param pThis The VFS.
1602 * @param pPath The parsed path. This may be changed as symbolic
1603 * links are processed during the path traversal.
1604 * @param fFlags RTPATH_F_XXX.
1605 * @param ppVfsParentDir Where to return the parent directory handle
1606 * (referenced).
1607 */
1608static int rtVfsTraverseToParent(RTVFSINTERNAL *pThis, PRTVFSPARSEDPATH pPath, uint32_t fFlags, RTVFSDIRINTERNAL **ppVfsParentDir)
1609{
1610 /*
1611 * Assert sanity.
1612 */
1613 AssertPtr(pThis);
1614 Assert(pThis->uMagic == RTVFS_MAGIC);
1615 Assert(pThis->Base.cRefs > 0);
1616 AssertPtr(pPath);
1617 AssertPtr(ppVfsParentDir);
1618 *ppVfsParentDir = NULL;
1619 AssertReturn(pPath->cComponents > 0, VERR_INTERNAL_ERROR_3);
1620 Assert(RTPATH_F_IS_VALID(fFlags, 0));
1621
1622 /*
1623 * Open the root directory and join paths with the directory traversal.
1624 */
1625 /** @todo Union mounts, traversal optimization methods, races, ++ */
1626 RTVFSDIRINTERNAL *pRootDir;
1627 RTVfsLockAcquireRead(pThis->Base.hLock);
1628 int rc = pThis->pOps->pfnOpenRoot(pThis->Base.pvThis, &pRootDir);
1629 RTVfsLockReleaseRead(pThis->Base.hLock);
1630 if (RT_SUCCESS(rc))
1631 {
1632 rc = rtVfsDirTraverseToParent(pRootDir, pPath, fFlags, ppVfsParentDir);
1633 RTVfsDirRelease(pRootDir);
1634 }
1635 return rc;
1636}
1637
1638
1639RTDECL(int) RTVfsUtilDummyPollOne(uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr, uint32_t *pfRetEvents)
1640{
1641 NOREF(fEvents);
1642 int rc;
1643 if (fIntr)
1644 rc = RTThreadSleep(cMillies);
1645 else
1646 {
1647 uint64_t uMsStart = RTTimeMilliTS();
1648 do
1649 rc = RTThreadSleep(cMillies);
1650 while ( rc == VERR_INTERRUPTED
1651 && !fIntr
1652 && RTTimeMilliTS() - uMsStart < cMillies);
1653 if (rc == VERR_INTERRUPTED)
1654 rc = VERR_TIMEOUT;
1655 }
1656
1657 *pfRetEvents = 0;
1658 return rc;
1659}
1660
1661
1662RTDECL(int) RTVfsUtilPumpIoStreams(RTVFSIOSTREAM hVfsIosSrc, RTVFSIOSTREAM hVfsIosDst, size_t cbBufHint)
1663{
1664 /*
1665 * Allocate a temporary buffer.
1666 */
1667 size_t cbBuf = cbBufHint;
1668 if (!cbBuf)
1669 cbBuf = _64K;
1670 else if (cbBuf < _4K)
1671 cbBuf = _4K;
1672 else if (cbBuf > _1M)
1673 cbBuf = _1M;
1674
1675 void *pvBuf = RTMemTmpAlloc(cbBuf);
1676 if (!pvBuf)
1677 {
1678 cbBuf = _4K;
1679 pvBuf = RTMemTmpAlloc(cbBuf);
1680 if (!pvBuf)
1681 return VERR_NO_TMP_MEMORY;
1682 }
1683
1684 /*
1685 * Pump loop.
1686 */
1687 int rc;
1688 for (;;)
1689 {
1690 size_t cbRead;
1691 rc = RTVfsIoStrmRead(hVfsIosSrc, pvBuf, cbBuf, true /*fBlocking*/, &cbRead);
1692 if (RT_FAILURE(rc))
1693 break;
1694 if (rc == VINF_EOF && cbRead == 0)
1695 break;
1696
1697 rc = RTVfsIoStrmWrite(hVfsIosDst, pvBuf, cbRead, true /*fBlocking*/, NULL /*cbWritten*/);
1698 if (RT_FAILURE(rc))
1699 break;
1700 }
1701
1702 RTMemTmpFree(pvBuf);
1703
1704 /*
1705 * Flush the destination stream on success to make sure we've caught
1706 * errors caused by buffering delays.
1707 */
1708 if (RT_SUCCESS(rc))
1709 rc = RTVfsIoStrmFlush(hVfsIosDst);
1710
1711 return rc;
1712}
1713
1714
1715
1716
1717
1718/*
1719 * F I L E S Y S T E M R O O T
1720 * F I L E S Y S T E M R O O T
1721 * F I L E S Y S T E M R O O T
1722 */
1723
1724
1725RTDECL(int) RTVfsNew(PCRTVFSOPS pVfsOps, size_t cbInstance, RTVFS hVfs, RTVFSLOCK hLock,
1726 PRTVFS phVfs, void **ppvInstance)
1727{
1728 /*
1729 * Validate the input, be extra strict in strict builds.
1730 */
1731 AssertPtr(pVfsOps);
1732 AssertReturn(pVfsOps->uVersion == RTVFSOPS_VERSION, VERR_VERSION_MISMATCH);
1733 AssertReturn(pVfsOps->uEndMarker == RTVFSOPS_VERSION, VERR_VERSION_MISMATCH);
1734 RTVFSOBJ_ASSERT_OPS(&pVfsOps->Obj, RTVFSOBJTYPE_VFS);
1735 Assert(cbInstance > 0);
1736 AssertPtr(ppvInstance);
1737 AssertPtr(phVfs);
1738
1739 /*
1740 * Allocate the handle + instance data.
1741 */
1742 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSINTERNAL), RTVFS_INST_ALIGNMENT)
1743 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
1744 RTVFSINTERNAL *pThis = (RTVFSINTERNAL *)RTMemAllocZ(cbThis);
1745 if (!pThis)
1746 return VERR_NO_MEMORY;
1747
1748 int rc = rtVfsObjInitNewObject(&pThis->Base, &pVfsOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
1749 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
1750 if (RT_FAILURE(rc))
1751 {
1752 RTMemFree(pThis);
1753 return rc;
1754 }
1755
1756 pThis->uMagic = RTVFS_MAGIC;
1757 pThis->pOps = pVfsOps;
1758
1759 *phVfs = pThis;
1760 *ppvInstance = pThis->Base.pvThis;
1761
1762 LogFlow(("RTVfsNew -> VINF_SUCCESS; hVfs=%p pvThis=%p\n", pThis, pThis->Base.pvThis));
1763 return VINF_SUCCESS;
1764}
1765
1766#ifdef DEBUG
1767# undef RTVfsRetain
1768#endif
1769RTDECL(uint32_t) RTVfsRetain(RTVFS hVfs)
1770{
1771 RTVFSINTERNAL *pThis = hVfs;
1772 AssertPtrReturn(pThis, UINT32_MAX);
1773 AssertReturn(pThis->uMagic == RTVFS_MAGIC, UINT32_MAX);
1774 uint32_t cRefs = rtVfsObjRetain(&pThis->Base);
1775 LogFlow(("RTVfsRetain(%p/%p) -> %d\n", pThis, pThis->Base.pvThis, cRefs));
1776 return cRefs;
1777}
1778#ifdef DEBUG
1779# define RTVfsRetain(hVfs) RTVfsRetainDebug(hVfs, RT_SRC_POS)
1780#endif
1781
1782
1783RTDECL(uint32_t) RTVfsRetainDebug(RTVFS hVfs, RT_SRC_POS_DECL)
1784{
1785 RTVFSINTERNAL *pThis = hVfs;
1786 AssertPtrReturn(pThis, UINT32_MAX);
1787 AssertReturn(pThis->uMagic == RTVFS_MAGIC, UINT32_MAX);
1788 RT_SRC_POS_NOREF();
1789 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsRetainDebug", RT_SRC_POS_ARGS);
1790}
1791
1792
1793RTDECL(uint32_t) RTVfsRelease(RTVFS hVfs)
1794{
1795 RTVFSINTERNAL *pThis = hVfs;
1796 if (pThis == NIL_RTVFS)
1797 return 0;
1798 AssertPtrReturn(pThis, UINT32_MAX);
1799 AssertReturn(pThis->uMagic == RTVFS_MAGIC, UINT32_MAX);
1800#ifdef LOG_ENABLED
1801 void *pvThis = pThis->Base.pvThis;
1802#endif
1803 uint32_t cRefs = rtVfsObjRelease(&pThis->Base);
1804 Log(("RTVfsRelease(%p/%p) -> %d\n", pThis, pvThis, cRefs));
1805 return cRefs;
1806}
1807
1808
1809RTDECL(int) RTVfsOpenRoot(RTVFS hVfs, PRTVFSDIR phDir)
1810{
1811 RTVFSINTERNAL *pThis = hVfs;
1812 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1813 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
1814 AssertPtrReturn(phDir, VERR_INVALID_POINTER);
1815 *phDir = NIL_RTVFSDIR;
1816
1817 if (!pThis->pOps->pfnIsRangeInUse)
1818 return VERR_NOT_SUPPORTED;
1819 RTVfsLockAcquireRead(pThis->Base.hLock);
1820 int rc = pThis->pOps->pfnOpenRoot(pThis->Base.pvThis, phDir);
1821 RTVfsLockReleaseRead(pThis->Base.hLock);
1822
1823 return rc;
1824}
1825
1826
1827RTDECL(int) RTVfsQueryPathInfo(RTVFS hVfs, const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
1828{
1829 RTVFSINTERNAL *pThis = hVfs;
1830 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1831 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
1832 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
1833 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
1834 AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
1835 AssertReturn(enmAddAttr >= RTFSOBJATTRADD_NOTHING && enmAddAttr <= RTFSOBJATTRADD_LAST, VERR_INVALID_PARAMETER);
1836 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
1837
1838 /*
1839 * Parse the path, assume current directory is root since we've got no
1840 * caller context here. Then traverse to the parent directory.
1841 */
1842 PRTVFSPARSEDPATH pPath;
1843 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
1844 if (RT_SUCCESS(rc))
1845 {
1846 RTVFSDIRINTERNAL *pVfsParentDir;
1847 if (pPath->cComponents > 0)
1848 {
1849 rc = rtVfsTraverseToParent(pThis, pPath, fFlags, &pVfsParentDir);
1850 if (RT_SUCCESS(rc))
1851 {
1852 /*
1853 * Call the query method on the parent directory.
1854 */
1855 /** @todo race condition here :/ */
1856 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
1857 RTVfsLockAcquireRead(pVfsParentDir->Base.hLock);
1858 rc = pVfsParentDir->pOps->pfnQueryEntryInfo(pVfsParentDir->Base.pvThis, pszEntryName, pObjInfo, enmAddAttr);
1859 RTVfsLockReleaseRead(pVfsParentDir->Base.hLock);
1860
1861 RTVfsDirRelease(pVfsParentDir);
1862 }
1863 }
1864 /*
1865 * The path boils down to '.', open the root dir and query its info.
1866 */
1867 else
1868 {
1869 RTVfsLockAcquireRead(pThis->Base.hLock);
1870 RTVFSDIR hRootDir = NIL_RTVFSDIR;
1871 rc = pThis->pOps->pfnOpenRoot(pThis->Base.pvThis, &hRootDir);
1872 RTVfsLockReleaseRead(pThis->Base.hLock);
1873 if (RT_SUCCESS(rc))
1874 {
1875 RTVfsLockAcquireRead(hRootDir->Base.hLock);
1876 rc = hRootDir->Base.pOps->pfnQueryInfo(hRootDir->Base.pvThis, pObjInfo, enmAddAttr);
1877 RTVfsLockReleaseRead(hRootDir->Base.hLock);
1878 RTVfsDirRelease(hRootDir);
1879 }
1880 }
1881
1882 RTVfsParsePathFree(pPath);
1883 }
1884 return rc;
1885}
1886
1887
1888
1889RTDECL(int) RTVfsIsRangeInUse(RTVFS hVfs, uint64_t off, size_t cb, bool *pfUsed)
1890{
1891 RTVFSINTERNAL *pThis = hVfs;
1892 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1893 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
1894
1895 if (!pThis->pOps->pfnIsRangeInUse)
1896 return VERR_NOT_SUPPORTED;
1897 RTVfsLockAcquireRead(pThis->Base.hLock);
1898 int rc = pThis->pOps->pfnIsRangeInUse(pThis->Base.pvThis, off, cb, pfUsed);
1899 RTVfsLockReleaseRead(pThis->Base.hLock);
1900
1901 return rc;
1902}
1903
1904
1905
1906
1907/*
1908 *
1909 * F I L E S Y S T E M S T R E A M
1910 * F I L E S Y S T E M S T R E A M
1911 * F I L E S Y S T E M S T R E A M
1912 *
1913 */
1914
1915
1916RTDECL(int) RTVfsNewFsStream(PCRTVFSFSSTREAMOPS pFsStreamOps, size_t cbInstance, RTVFS hVfs, RTVFSLOCK hLock, bool fReadOnly,
1917 PRTVFSFSSTREAM phVfsFss, void **ppvInstance)
1918{
1919 /*
1920 * Validate the input, be extra strict in strict builds.
1921 */
1922 AssertPtr(pFsStreamOps);
1923 AssertReturn(pFsStreamOps->uVersion == RTVFSFSSTREAMOPS_VERSION, VERR_VERSION_MISMATCH);
1924 AssertReturn(pFsStreamOps->uEndMarker == RTVFSFSSTREAMOPS_VERSION, VERR_VERSION_MISMATCH);
1925 Assert(!pFsStreamOps->fReserved);
1926 RTVFSOBJ_ASSERT_OPS(&pFsStreamOps->Obj, RTVFSOBJTYPE_FS_STREAM);
1927 if (fReadOnly)
1928 AssertPtr(pFsStreamOps->pfnNext);
1929 else
1930 {
1931 AssertPtr(pFsStreamOps->pfnAdd);
1932 AssertPtr(pFsStreamOps->pfnEnd);
1933 }
1934 Assert(cbInstance > 0);
1935 AssertPtr(ppvInstance);
1936 AssertPtr(phVfsFss);
1937 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
1938
1939 /*
1940 * Allocate the handle + instance data.
1941 */
1942 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSFSSTREAMINTERNAL), RTVFS_INST_ALIGNMENT)
1943 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
1944 RTVFSFSSTREAMINTERNAL *pThis = (RTVFSFSSTREAMINTERNAL *)RTMemAllocZ(cbThis);
1945 if (!pThis)
1946 return VERR_NO_MEMORY;
1947
1948 int rc = rtVfsObjInitNewObject(&pThis->Base, &pFsStreamOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
1949 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
1950
1951 if (RT_FAILURE(rc))
1952 {
1953 RTMemFree(pThis);
1954 return rc;
1955 }
1956
1957 pThis->uMagic = RTVFSFSSTREAM_MAGIC;
1958 pThis->fFlags = fReadOnly
1959 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
1960 : RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL;
1961 pThis->pOps = pFsStreamOps;
1962
1963 *phVfsFss = pThis;
1964 *ppvInstance = pThis->Base.pvThis;
1965 return VINF_SUCCESS;
1966}
1967
1968
1969#ifdef DEBUG
1970# undef RTVfsFsStrmRetain
1971#endif
1972RTDECL(uint32_t) RTVfsFsStrmRetain(RTVFSFSSTREAM hVfsFss)
1973{
1974 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1975 AssertPtrReturn(pThis, UINT32_MAX);
1976 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, UINT32_MAX);
1977 return rtVfsObjRetain(&pThis->Base);
1978}
1979#ifdef DEBUG
1980# define RTVfsFsStrmRetain(hVfsFss) RTVfsFsStrmRetainDebug(hVfsFss, RT_SRC_POS)
1981#endif
1982
1983
1984RTDECL(uint32_t) RTVfsFsStrmRetainDebug(RTVFSFSSTREAM hVfsFss, RT_SRC_POS_DECL)
1985{
1986 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1987 AssertPtrReturn(pThis, UINT32_MAX);
1988 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, UINT32_MAX);
1989 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsFsStrmRetain", RT_SRC_POS_ARGS);
1990}
1991
1992
1993RTDECL(uint32_t) RTVfsFsStrmRelease(RTVFSFSSTREAM hVfsFss)
1994{
1995 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1996 if (pThis == NIL_RTVFSFSSTREAM)
1997 return 0;
1998 AssertPtrReturn(pThis, UINT32_MAX);
1999 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, UINT32_MAX);
2000 return rtVfsObjRelease(&pThis->Base);
2001}
2002
2003
2004RTDECL(int) RTVfsFsStrmQueryInfo(RTVFSFSSTREAM hVfsFss, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2005{
2006 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2007 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2008 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2009 return RTVfsObjQueryInfo(&pThis->Base, pObjInfo, enmAddAttr);
2010}
2011
2012
2013RTDECL(int) RTVfsFsStrmNext(RTVFSFSSTREAM hVfsFss, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
2014{
2015 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2016 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2017 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2018 AssertPtrNullReturn(ppszName, VERR_INVALID_POINTER);
2019 if (ppszName)
2020 *ppszName = NULL;
2021 AssertPtrNullReturn(penmType, VERR_INVALID_POINTER);
2022 if (penmType)
2023 *penmType = RTVFSOBJTYPE_INVALID;
2024 AssertPtrNullReturn(penmType, VERR_INVALID_POINTER);
2025 if (phVfsObj)
2026 *phVfsObj = NIL_RTVFSOBJ;
2027
2028 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_INVALID_FUNCTION);
2029
2030 return pThis->pOps->pfnNext(pThis->Base.pvThis, ppszName, penmType, phVfsObj);
2031}
2032
2033
2034RTDECL(int) RTVfsFsStrmAdd(RTVFSFSSTREAM hVfsFss, const char *pszPath, RTVFSOBJ hVfsObj, uint32_t fFlags)
2035{
2036 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2037 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2038 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2039 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2040 AssertReturn(*pszPath != '\0', VERR_INVALID_NAME);
2041 AssertPtrReturn(hVfsObj, VERR_INVALID_HANDLE);
2042 AssertReturn(hVfsObj->uMagic == RTVFSOBJ_MAGIC, VERR_INVALID_HANDLE);
2043 AssertReturn(!(fFlags & ~RTVFSFSSTRM_ADD_F_VALID_MASK), VERR_INVALID_FLAGS);
2044 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_INVALID_FUNCTION);
2045
2046 return pThis->pOps->pfnAdd(pThis->Base.pvThis, pszPath, hVfsObj, fFlags);
2047}
2048
2049
2050RTDECL(int) RTVfsFsStrmPushFile(RTVFSFSSTREAM hVfsFss, const char *pszPath, uint64_t cbFile,
2051 PCRTFSOBJINFO paObjInfo, uint32_t cObjInfo, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos)
2052{
2053 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2054 AssertPtrReturn(phVfsIos, VERR_INVALID_POINTER);
2055 *phVfsIos = NIL_RTVFSIOSTREAM;
2056
2057 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2058 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2059
2060 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2061 AssertReturn(*pszPath != '\0', VERR_INVALID_NAME);
2062
2063 AssertReturn(!(fFlags & ~RTVFSFSSTRM_PUSH_F_VALID_MASK), VERR_INVALID_FLAGS);
2064 AssertReturn(RT_BOOL(cbFile == UINT64_MAX) == RT_BOOL(fFlags & RTVFSFSSTRM_PUSH_F_STREAM), VERR_INVALID_FLAGS);
2065
2066 if (cObjInfo)
2067 {
2068 AssertPtrReturn(paObjInfo, VERR_INVALID_POINTER);
2069 AssertReturn(paObjInfo[0].Attr.enmAdditional == RTFSOBJATTRADD_UNIX, VERR_INVALID_PARAMETER);
2070 }
2071
2072 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_INVALID_FUNCTION);
2073 if (pThis->pOps->pfnPushFile)
2074 return pThis->pOps->pfnPushFile(pThis->Base.pvThis, pszPath, cbFile, paObjInfo, cObjInfo, fFlags, phVfsIos);
2075 return VERR_NOT_SUPPORTED;
2076}
2077
2078
2079RTDECL(int) RTVfsFsStrmEnd(RTVFSFSSTREAM hVfsFss)
2080{
2081 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2082 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2083 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2084
2085 return pThis->pOps->pfnEnd(pThis->Base.pvThis);
2086}
2087
2088
2089RTDECL(void *) RTVfsFsStreamToPrivate(RTVFSFSSTREAM hVfsFss, PCRTVFSFSSTREAMOPS pFsStreamOps)
2090{
2091 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2092 AssertPtrReturn(pThis, NULL);
2093 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, NULL);
2094 if (pThis->pOps != pFsStreamOps)
2095 return NULL;
2096 return pThis->Base.pvThis;
2097}
2098
2099
2100/*
2101 *
2102 * D I R D I R D I R
2103 * D I R D I R D I R
2104 * D I R D I R D I R
2105 *
2106 */
2107
2108
2109RTDECL(int) RTVfsNewDir(PCRTVFSDIROPS pDirOps, size_t cbInstance, uint32_t fFlags, RTVFS hVfs, RTVFSLOCK hLock,
2110 PRTVFSDIR phVfsDir, void **ppvInstance)
2111{
2112 /*
2113 * Validate the input, be extra strict in strict builds.
2114 */
2115 AssertPtr(pDirOps);
2116 AssertReturn(pDirOps->uVersion == RTVFSDIROPS_VERSION, VERR_VERSION_MISMATCH);
2117 AssertReturn(pDirOps->uEndMarker == RTVFSDIROPS_VERSION, VERR_VERSION_MISMATCH);
2118 Assert(!pDirOps->fReserved);
2119 RTVFSDIR_ASSERT_OPS(pDirOps, RTVFSOBJTYPE_DIR);
2120 Assert(cbInstance > 0);
2121 AssertReturn(!(fFlags & ~RTVFSDIR_F_NO_VFS_REF), VERR_INVALID_FLAGS);
2122 AssertPtr(ppvInstance);
2123 AssertPtr(phVfsDir);
2124 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
2125
2126 /*
2127 * Allocate the handle + instance data.
2128 */
2129 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSDIRINTERNAL), RTVFS_INST_ALIGNMENT)
2130 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
2131 RTVFSDIRINTERNAL *pThis = (RTVFSDIRINTERNAL *)RTMemAllocZ(cbThis);
2132 if (!pThis)
2133 return VERR_NO_MEMORY;
2134
2135 int rc = rtVfsObjInitNewObject(&pThis->Base, &pDirOps->Obj, hVfs, RT_BOOL(fFlags & RTVFSDIR_F_NO_VFS_REF), hLock,
2136 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
2137 if (RT_FAILURE(rc))
2138 {
2139 RTMemFree(pThis);
2140 return rc;
2141 }
2142
2143 pThis->uMagic = RTVFSDIR_MAGIC;
2144 pThis->fReserved = 0;
2145 pThis->pOps = pDirOps;
2146
2147 *phVfsDir = pThis;
2148 *ppvInstance = pThis->Base.pvThis;
2149 return VINF_SUCCESS;
2150}
2151
2152
2153#ifdef DEBUG
2154# undef RTVfsDirRetain
2155#endif
2156RTDECL(uint32_t) RTVfsDirRetain(RTVFSDIR hVfsDir)
2157{
2158 RTVFSDIRINTERNAL *pThis = hVfsDir;
2159 AssertPtrReturn(pThis, UINT32_MAX);
2160 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, UINT32_MAX);
2161 uint32_t cRefs = rtVfsObjRetain(&pThis->Base);
2162 LogFlow(("RTVfsDirRetain(%p/%p) -> %#x\n", pThis, pThis->Base.pvThis, cRefs));
2163 return cRefs;
2164}
2165#ifdef DEBUG
2166# define RTVfsDirRetain(hVfsDir) RTVfsDirRetainDebug(hVfsDir, RT_SRC_POS)
2167#endif
2168
2169
2170RTDECL(uint32_t) RTVfsDirRetainDebug(RTVFSDIR hVfsDir, RT_SRC_POS_DECL)
2171{
2172 RTVFSDIRINTERNAL *pThis = hVfsDir;
2173 AssertPtrReturn(pThis, UINT32_MAX);
2174 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, UINT32_MAX);
2175 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsDirRetain", RT_SRC_POS_ARGS);
2176}
2177
2178
2179RTDECL(uint32_t) RTVfsDirRelease(RTVFSDIR hVfsDir)
2180{
2181 RTVFSDIRINTERNAL *pThis = hVfsDir;
2182 if (pThis == NIL_RTVFSDIR)
2183 return 0;
2184 AssertPtrReturn(pThis, UINT32_MAX);
2185 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, UINT32_MAX);
2186#ifdef LOG_ENABLED
2187 void *pvThis = pThis->Base.pvThis;
2188#endif
2189 uint32_t cRefs = rtVfsObjRelease(&pThis->Base);
2190 LogFlow(("RTVfsDirRelease(%p/%p) -> %#x\n", pThis, pvThis, cRefs));
2191 return cRefs;
2192}
2193
2194
2195RTDECL(int) RTVfsDirOpen(RTVFS hVfs, const char *pszPath, uint32_t fFlags, PRTVFSDIR phVfsDir)
2196{
2197 /*
2198 * Validate input.
2199 */
2200 RTVFSINTERNAL *pThis = hVfs;
2201 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2202 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
2203 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2204 AssertPtrReturn(phVfsDir, VERR_INVALID_POINTER);
2205 AssertReturn(!fFlags, VERR_INVALID_FLAGS);
2206
2207 /*
2208 * Parse the path, assume current directory is root since we've got no
2209 * caller context here.
2210 */
2211 PRTVFSPARSEDPATH pPath;
2212 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
2213 if (RT_SUCCESS(rc))
2214 {
2215 if (pPath->cComponents > 0)
2216 {
2217 /*
2218 * Tranverse the path, resolving the parent node and any symlinks
2219 * in the final element, and ask the directory to open the subdir.
2220 */
2221 RTVFSDIRINTERNAL *pVfsParentDir;
2222 rc = rtVfsTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
2223 if (RT_SUCCESS(rc))
2224 {
2225 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2226
2227 /** @todo there is a symlink creation race here. */
2228 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
2229 rc = pVfsParentDir->pOps->pfnOpenDir(pVfsParentDir->Base.pvThis, pszEntryName, fFlags, phVfsDir);
2230 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
2231
2232 RTVfsDirRelease(pVfsParentDir);
2233
2234 if (RT_SUCCESS(rc))
2235 {
2236 AssertPtr(*phVfsDir);
2237 Assert((*phVfsDir)->uMagic == RTVFSDIR_MAGIC);
2238 }
2239 }
2240 }
2241 /*
2242 * If the path boils down to '.' return the root directory.
2243 */
2244 else
2245 {
2246 RTVfsLockAcquireRead(pThis->Base.hLock);
2247 rc = pThis->pOps->pfnOpenRoot(pThis->Base.pvThis, phVfsDir);
2248 RTVfsLockReleaseRead(pThis->Base.hLock);
2249 }
2250 RTVfsParsePathFree(pPath);
2251 }
2252 return rc;
2253}
2254
2255
2256RTDECL(int) RTVfsDirOpenDir(RTVFSDIR hVfsDir, const char *pszPath, uint32_t fFlags, PRTVFSDIR phVfsDir)
2257{
2258 /*
2259 * Validate input.
2260 */
2261 RTVFSDIRINTERNAL *pThis = hVfsDir;
2262 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2263 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2264 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2265 AssertPtrReturn(phVfsDir, VERR_INVALID_POINTER);
2266 AssertReturn(!fFlags, VERR_INVALID_FLAGS);
2267
2268 /*
2269 * Parse the path, it's always relative to the given directory.
2270 */
2271 PRTVFSPARSEDPATH pPath;
2272 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
2273 if (RT_SUCCESS(rc))
2274 {
2275 if (pPath->cComponents > 0)
2276 {
2277 /*
2278 * Tranverse the path, resolving the parent node and any symlinks
2279 * in the final element, and ask the directory to open the subdir.
2280 */
2281 RTVFSDIRINTERNAL *pVfsParentDir;
2282 rc = rtVfsDirTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
2283 if (RT_SUCCESS(rc))
2284 {
2285 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2286
2287 /** @todo there is a symlink creation race here. */
2288 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
2289 rc = pVfsParentDir->pOps->pfnOpenDir(pVfsParentDir->Base.pvThis, pszEntryName, fFlags, phVfsDir);
2290 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
2291
2292 RTVfsDirRelease(pVfsParentDir);
2293
2294 if (RT_SUCCESS(rc))
2295 {
2296 AssertPtr(*phVfsDir);
2297 Assert((*phVfsDir)->uMagic == RTVFSDIR_MAGIC);
2298 }
2299 }
2300 }
2301 /*
2302 * The path boils down to '.', call pfnOpenDir on pThis with '.' as input.
2303 * The caller may wish for a new directory instance to enumerate the entries
2304 * in parallel or some such thing.
2305 */
2306 else
2307 {
2308 RTVfsLockAcquireWrite(pThis->Base.hLock);
2309 rc = pThis->pOps->pfnOpenDir(pThis->Base.pvThis, ".", fFlags, phVfsDir);
2310 RTVfsLockReleaseWrite(pThis->Base.hLock);
2311 }
2312 RTVfsParsePathFree(pPath);
2313 }
2314 return rc;
2315}
2316
2317
2318RTDECL(int) RTVfsDirOpenFile(RTVFSDIR hVfsDir, const char *pszPath, uint64_t fOpen, PRTVFSFILE phVfsFile)
2319{
2320 /*
2321 * Validate input.
2322 */
2323 RTVFSDIRINTERNAL *pThis = hVfsDir;
2324 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2325 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2326 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2327 AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
2328
2329 int rc = rtFileRecalcAndValidateFlags(&fOpen);
2330 if (RT_FAILURE(rc))
2331 return rc;
2332
2333 /*
2334 * Parse the path, assume current directory is root since we've got no
2335 * caller context here.
2336 */
2337 PRTVFSPARSEDPATH pPath;
2338 rc = RTVfsParsePathA(pszPath, "/", &pPath);
2339 if (RT_SUCCESS(rc))
2340 {
2341 if ( !pPath->fDirSlash
2342 && pPath->cComponents > 0)
2343 {
2344 /*
2345 * Tranverse the path, resolving the parent node and any symlinks
2346 * in the final element, and ask the directory to open the file.
2347 */
2348 RTVFSDIRINTERNAL *pVfsParentDir;
2349 rc = rtVfsDirTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
2350 if (RT_SUCCESS(rc))
2351 {
2352 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2353
2354 /** @todo there is a symlink creation race here. */
2355 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
2356 rc = pVfsParentDir->pOps->pfnOpenFile(pVfsParentDir->Base.pvThis, pszEntryName, fOpen, phVfsFile);
2357 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
2358
2359 RTVfsDirRelease(pVfsParentDir);
2360
2361 if (RT_SUCCESS(rc))
2362 {
2363 AssertPtr(*phVfsFile);
2364 Assert((*phVfsFile)->uMagic == RTVFSFILE_MAGIC);
2365 }
2366 }
2367 }
2368 else
2369 rc = VERR_NOT_A_FILE;
2370 RTVfsParsePathFree(pPath);
2371 }
2372 return rc;
2373}
2374
2375
2376RTDECL(int) RTVfsDirOpenFileAsIoStream(RTVFSDIR hVfsDir, const char *pszPath, uint64_t fOpen, PRTVFSIOSTREAM phVfsIos)
2377{
2378 RTVFSFILE hVfsFile;
2379 int rc = RTVfsDirOpenFile(hVfsDir, pszPath, fOpen, &hVfsFile);
2380 if (RT_SUCCESS(rc))
2381 {
2382 *phVfsIos = RTVfsFileToIoStream(hVfsFile);
2383 AssertStmt(*phVfsIos != NIL_RTVFSIOSTREAM, rc = VERR_INTERNAL_ERROR_2);
2384 RTVfsFileRelease(hVfsFile);
2385 }
2386 return rc;
2387}
2388
2389
2390
2391RTDECL(int) RTVfsDirQueryPathInfo(RTVFSDIR hVfsDir, const char *pszPath, PRTFSOBJINFO pObjInfo,
2392 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
2393{
2394 /*
2395 * Validate input.
2396 */
2397 RTVFSDIRINTERNAL *pThis = hVfsDir;
2398 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2399 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2400 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2401 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
2402 AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
2403 AssertReturn(enmAddAttr >= RTFSOBJATTRADD_NOTHING && enmAddAttr <= RTFSOBJATTRADD_LAST, VERR_INVALID_PARAMETER);
2404 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
2405
2406 /*
2407 * Parse the path, assume current directory is root since we've got no
2408 * caller context here. Then traverse to the parent directory.
2409 */
2410 PRTVFSPARSEDPATH pPath;
2411 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
2412 if (RT_SUCCESS(rc))
2413 {
2414 if (pPath->cComponents > 0)
2415 {
2416 RTVFSDIRINTERNAL *pVfsParentDir;
2417 rc = rtVfsDirTraverseToParent(pThis, pPath, fFlags, &pVfsParentDir);
2418 if (RT_SUCCESS(rc))
2419 {
2420 /*
2421 * Call the query method on the parent directory.
2422 */
2423 /** @todo symlink race condition here :/ */
2424 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2425 RTVfsLockAcquireRead(pVfsParentDir->Base.hLock);
2426 rc = pVfsParentDir->pOps->pfnQueryEntryInfo(pVfsParentDir->Base.pvThis, pszEntryName, pObjInfo, enmAddAttr);
2427 RTVfsLockReleaseRead(pVfsParentDir->Base.hLock);
2428
2429 RTVfsDirRelease(pVfsParentDir);
2430 }
2431 else
2432 rc = VERR_INVALID_PARAMETER;
2433 }
2434 /*
2435 * The path boils down to '.' so just query the directory.
2436 */
2437 else
2438 {
2439 RTVfsLockAcquireRead(pThis->Base.hLock);
2440 rc = pThis->Base.pOps->pfnQueryInfo(pThis->Base.pvThis, pObjInfo, enmAddAttr);
2441 RTVfsLockReleaseRead(pThis->Base.hLock);
2442 }
2443 RTVfsParsePathFree(pPath);
2444 }
2445 return rc;
2446}
2447
2448
2449RTDECL(int) RTVfsDirReadEx(RTVFSDIR hVfsDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAddAttr)
2450{
2451 /*
2452 * Validate input.
2453 */
2454 RTVFSDIRINTERNAL *pThis = hVfsDir;
2455 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2456 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2457 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
2458 AssertReturn(enmAddAttr >= RTFSOBJATTRADD_NOTHING && enmAddAttr <= RTFSOBJATTRADD_LAST, VERR_INVALID_PARAMETER);
2459
2460 size_t cbDirEntry = sizeof(*pDirEntry);
2461 if (!pcbDirEntry)
2462 pcbDirEntry = &cbDirEntry;
2463 else
2464 {
2465 cbDirEntry = *pcbDirEntry;
2466 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
2467 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
2468 VERR_INVALID_PARAMETER);
2469 }
2470
2471 /*
2472 * Call the directory method.
2473 */
2474 RTVfsLockAcquireRead(pThis->Base.hLock);
2475 int rc = pThis->pOps->pfnReadDir(pThis->Base.pvThis, pDirEntry, pcbDirEntry, enmAddAttr);
2476 RTVfsLockReleaseRead(pThis->Base.hLock);
2477 return rc;
2478}
2479
2480
2481/*
2482 *
2483 * S Y M B O L I C L I N K
2484 * S Y M B O L I C L I N K
2485 * S Y M B O L I C L I N K
2486 *
2487 */
2488
2489RTDECL(int) RTVfsNewSymlink(PCRTVFSSYMLINKOPS pSymlinkOps, size_t cbInstance, RTVFS hVfs, RTVFSLOCK hLock,
2490 PRTVFSSYMLINK phVfsSym, void **ppvInstance)
2491{
2492 /*
2493 * Validate the input, be extra strict in strict builds.
2494 */
2495 AssertPtr(pSymlinkOps);
2496 AssertReturn(pSymlinkOps->uVersion == RTVFSSYMLINKOPS_VERSION, VERR_VERSION_MISMATCH);
2497 AssertReturn(pSymlinkOps->uEndMarker == RTVFSSYMLINKOPS_VERSION, VERR_VERSION_MISMATCH);
2498 Assert(!pSymlinkOps->fReserved);
2499 RTVFSSYMLINK_ASSERT_OPS(pSymlinkOps, RTVFSOBJTYPE_SYMLINK);
2500 Assert(cbInstance > 0);
2501 AssertPtr(ppvInstance);
2502 AssertPtr(phVfsSym);
2503 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
2504
2505 /*
2506 * Allocate the handle + instance data.
2507 */
2508 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSSYMLINKINTERNAL), RTVFS_INST_ALIGNMENT)
2509 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
2510 RTVFSSYMLINKINTERNAL *pThis = (RTVFSSYMLINKINTERNAL *)RTMemAllocZ(cbThis);
2511 if (!pThis)
2512 return VERR_NO_MEMORY;
2513
2514 int rc = rtVfsObjInitNewObject(&pThis->Base, &pSymlinkOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
2515 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
2516 if (RT_FAILURE(rc))
2517 {
2518 RTMemFree(pThis);
2519 return rc;
2520 }
2521
2522 pThis->uMagic = RTVFSSYMLINK_MAGIC;
2523 pThis->pOps = pSymlinkOps;
2524
2525 *phVfsSym = pThis;
2526 *ppvInstance = pThis->Base.pvThis;
2527 return VINF_SUCCESS;
2528}
2529
2530
2531RTDECL(uint32_t) RTVfsSymlinkRetain(RTVFSSYMLINK hVfsSym)
2532{
2533 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2534 AssertPtrReturn(pThis, UINT32_MAX);
2535 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, UINT32_MAX);
2536 return rtVfsObjRetain(&pThis->Base);
2537}
2538
2539
2540RTDECL(uint32_t) RTVfsSymlinkRetainDebug(RTVFSSYMLINK hVfsSym, RT_SRC_POS_DECL)
2541{
2542 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2543 AssertPtrReturn(pThis, UINT32_MAX);
2544 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, UINT32_MAX);
2545 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsSymlinkRetainDebug", RT_SRC_POS_ARGS);
2546}
2547
2548
2549RTDECL(uint32_t) RTVfsSymlinkRelease(RTVFSSYMLINK hVfsSym)
2550{
2551 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2552 if (pThis == NIL_RTVFSSYMLINK)
2553 return 0;
2554 AssertPtrReturn(pThis, UINT32_MAX);
2555 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, UINT32_MAX);
2556 return rtVfsObjRelease(&pThis->Base);
2557}
2558
2559
2560RTDECL(int) RTVfsSymlinkQueryInfo(RTVFSSYMLINK hVfsSym, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2561{
2562 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2563 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2564 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2565 return RTVfsObjQueryInfo(&pThis->Base, pObjInfo, enmAddAttr);
2566}
2567
2568
2569RTDECL(int) RTVfsSymlinkSetMode(RTVFSSYMLINK hVfsSym, RTFMODE fMode, RTFMODE fMask)
2570{
2571 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2572 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2573 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2574
2575 fMode = rtFsModeNormalize(fMode, NULL, 0);
2576 if (!rtFsModeIsValid(fMode))
2577 return VERR_INVALID_PARAMETER;
2578
2579 RTVfsLockAcquireWrite(pThis->Base.hLock);
2580 int rc = pThis->pOps->ObjSet.pfnSetMode(pThis->Base.pvThis, fMode, fMask);
2581 RTVfsLockReleaseWrite(pThis->Base.hLock);
2582 return rc;
2583}
2584
2585
2586RTDECL(int) RTVfsSymlinkSetTimes(RTVFSSYMLINK hVfsSym, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
2587 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
2588{
2589 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2590 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2591 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2592
2593 AssertPtrNullReturn(pAccessTime, VERR_INVALID_POINTER);
2594 AssertPtrNullReturn(pModificationTime, VERR_INVALID_POINTER);
2595 AssertPtrNullReturn(pChangeTime, VERR_INVALID_POINTER);
2596 AssertPtrNullReturn(pBirthTime, VERR_INVALID_POINTER);
2597
2598 RTVfsLockAcquireWrite(pThis->Base.hLock);
2599 int rc = pThis->pOps->ObjSet.pfnSetTimes(pThis->Base.pvThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
2600 RTVfsLockReleaseWrite(pThis->Base.hLock);
2601 return rc;
2602}
2603
2604
2605RTDECL(int) RTVfsSymlinkSetOwner(RTVFSSYMLINK hVfsSym, RTUID uid, RTGID gid)
2606{
2607 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2608 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2609 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2610
2611 RTVfsLockAcquireWrite(pThis->Base.hLock);
2612 int rc = pThis->pOps->ObjSet.pfnSetOwner(pThis->Base.pvThis, uid, gid);
2613 RTVfsLockReleaseWrite(pThis->Base.hLock);
2614 return rc;
2615}
2616
2617
2618RTDECL(int) RTVfsSymlinkRead(RTVFSSYMLINK hVfsSym, char *pszTarget, size_t cbTarget)
2619{
2620 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2621 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2622 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2623
2624 RTVfsLockAcquireWrite(pThis->Base.hLock);
2625 int rc = pThis->pOps->pfnRead(pThis->Base.pvThis, pszTarget, cbTarget);
2626 RTVfsLockReleaseWrite(pThis->Base.hLock);
2627
2628 return rc;
2629}
2630
2631
2632
2633/*
2634 *
2635 * I / O S T R E A M I / O S T R E A M I / O S T R E A M
2636 * I / O S T R E A M I / O S T R E A M I / O S T R E A M
2637 * I / O S T R E A M I / O S T R E A M I / O S T R E A M
2638 *
2639 */
2640
2641RTDECL(int) RTVfsNewIoStream(PCRTVFSIOSTREAMOPS pIoStreamOps, size_t cbInstance, uint32_t fOpen, RTVFS hVfs, RTVFSLOCK hLock,
2642 PRTVFSIOSTREAM phVfsIos, void **ppvInstance)
2643{
2644 /*
2645 * Validate the input, be extra strict in strict builds.
2646 */
2647 AssertPtr(pIoStreamOps);
2648 AssertReturn(pIoStreamOps->uVersion == RTVFSIOSTREAMOPS_VERSION, VERR_VERSION_MISMATCH);
2649 AssertReturn(pIoStreamOps->uEndMarker == RTVFSIOSTREAMOPS_VERSION, VERR_VERSION_MISMATCH);
2650 Assert(!(pIoStreamOps->fFeatures & ~RTVFSIOSTREAMOPS_FEAT_VALID_MASK));
2651 RTVFSIOSTREAM_ASSERT_OPS(pIoStreamOps, RTVFSOBJTYPE_IO_STREAM);
2652 Assert(cbInstance > 0);
2653 Assert(fOpen & RTFILE_O_ACCESS_MASK);
2654 AssertPtr(ppvInstance);
2655 AssertPtr(phVfsIos);
2656 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
2657
2658 /*
2659 * Allocate the handle + instance data.
2660 */
2661 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSIOSTREAMINTERNAL), RTVFS_INST_ALIGNMENT)
2662 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
2663 RTVFSIOSTREAMINTERNAL *pThis = (RTVFSIOSTREAMINTERNAL *)RTMemAllocZ(cbThis);
2664 if (!pThis)
2665 return VERR_NO_MEMORY;
2666
2667 int rc = rtVfsObjInitNewObject(&pThis->Base, &pIoStreamOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
2668 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
2669 if (RT_FAILURE(rc))
2670 {
2671 RTMemFree(pThis);
2672 return rc;
2673 }
2674
2675 pThis->uMagic = RTVFSIOSTREAM_MAGIC;
2676 pThis->fFlags = fOpen;
2677 pThis->pOps = pIoStreamOps;
2678
2679 *phVfsIos = pThis;
2680 *ppvInstance = pThis->Base.pvThis;
2681 return VINF_SUCCESS;
2682}
2683
2684
2685RTDECL(void *) RTVfsIoStreamToPrivate(RTVFSIOSTREAM hVfsIos, PCRTVFSIOSTREAMOPS pIoStreamOps)
2686{
2687 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2688 AssertPtrReturn(pThis, NULL);
2689 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, NULL);
2690 if (pThis->pOps != pIoStreamOps)
2691 return NULL;
2692 return pThis->Base.pvThis;
2693}
2694
2695
2696#ifdef DEBUG
2697# undef RTVfsIoStrmRetain
2698#endif
2699RTDECL(uint32_t) RTVfsIoStrmRetain(RTVFSIOSTREAM hVfsIos)
2700{
2701 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2702 AssertPtrReturn(pThis, UINT32_MAX);
2703 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
2704 return rtVfsObjRetain(&pThis->Base);
2705}
2706#ifdef DEBUG
2707# define RTVfsIoStrmRetain(hVfsIos) RTVfsIoStrmRetainDebug(hVfsIos, RT_SRC_POS)
2708#endif
2709
2710
2711RTDECL(uint32_t) RTVfsIoStrmRetainDebug(RTVFSIOSTREAM hVfsIos, RT_SRC_POS_DECL)
2712{
2713 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2714 AssertPtrReturn(pThis, UINT32_MAX);
2715 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
2716 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsIoStrmRetainDebug", RT_SRC_POS_ARGS);
2717}
2718
2719
2720RTDECL(uint32_t) RTVfsIoStrmRelease(RTVFSIOSTREAM hVfsIos)
2721{
2722 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2723 if (pThis == NIL_RTVFSIOSTREAM)
2724 return 0;
2725 AssertPtrReturn(pThis, UINT32_MAX);
2726 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
2727 return rtVfsObjRelease(&pThis->Base);
2728}
2729
2730
2731RTDECL(RTVFSFILE) RTVfsIoStrmToFile(RTVFSIOSTREAM hVfsIos)
2732{
2733 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2734 AssertPtrReturn(pThis, NIL_RTVFSFILE);
2735 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, NIL_RTVFSFILE);
2736
2737 if (pThis->pOps->Obj.enmType == RTVFSOBJTYPE_FILE)
2738 {
2739 rtVfsObjRetainVoid(&pThis->Base, "RTVfsIoStrmToFile");
2740 return RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream);
2741 }
2742
2743 /* this is no crime, so don't assert. */
2744 return NIL_RTVFSFILE;
2745}
2746
2747
2748RTDECL(int) RTVfsIoStrmQueryInfo(RTVFSIOSTREAM hVfsIos, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2749{
2750 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2751 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2752 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2753 return RTVfsObjQueryInfo(&pThis->Base, pObjInfo, enmAddAttr);
2754}
2755
2756
2757RTDECL(int) RTVfsIoStrmRead(RTVFSIOSTREAM hVfsIos, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead)
2758{
2759 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
2760 if (pcbRead)
2761 *pcbRead = 0;
2762 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2763 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2764 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2765 AssertReturn(fBlocking || pcbRead, VERR_INVALID_PARAMETER);
2766 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_ACCESS_DENIED);
2767
2768 RTSGSEG Seg = { pvBuf, cbToRead };
2769 RTSGBUF SgBuf;
2770 RTSgBufInit(&SgBuf, &Seg, 1);
2771
2772 RTVfsLockAcquireWrite(pThis->Base.hLock);
2773 int rc = pThis->pOps->pfnRead(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbRead);
2774 RTVfsLockReleaseWrite(pThis->Base.hLock);
2775 return rc;
2776}
2777
2778
2779RTDECL(int) RTVfsIoStrmReadAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, void *pvBuf, size_t cbToRead,
2780 bool fBlocking, size_t *pcbRead)
2781{
2782 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
2783 if (pcbRead)
2784 *pcbRead = 0;
2785 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2786 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2787 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2788 AssertReturn(fBlocking || pcbRead, VERR_INVALID_PARAMETER);
2789 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_ACCESS_DENIED);
2790
2791 RTSGSEG Seg = { pvBuf, cbToRead };
2792 RTSGBUF SgBuf;
2793 RTSgBufInit(&SgBuf, &Seg, 1);
2794
2795 RTVfsLockAcquireWrite(pThis->Base.hLock);
2796 int rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbRead);
2797 RTVfsLockReleaseWrite(pThis->Base.hLock);
2798 return rc;
2799}
2800
2801
2802RTDECL(int) RTVfsIoStrmWrite(RTVFSIOSTREAM hVfsIos, const void *pvBuf, size_t cbToWrite, bool fBlocking, size_t *pcbWritten)
2803{
2804 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
2805 if (pcbWritten)
2806 *pcbWritten = 0;
2807 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2808 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2809 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2810 AssertReturn(fBlocking || pcbWritten, VERR_INVALID_PARAMETER);
2811 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
2812
2813 RTSGSEG Seg = { (void *)pvBuf, cbToWrite };
2814 RTSGBUF SgBuf;
2815 RTSgBufInit(&SgBuf, &Seg, 1);
2816
2817 RTVfsLockAcquireWrite(pThis->Base.hLock);
2818 int rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbWritten);
2819 RTVfsLockReleaseWrite(pThis->Base.hLock);
2820 return rc;
2821}
2822
2823
2824RTDECL(int) RTVfsIoStrmWriteAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, const void *pvBuf, size_t cbToWrite,
2825 bool fBlocking, size_t *pcbWritten)
2826{
2827 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
2828 if (pcbWritten)
2829 *pcbWritten = 0;
2830 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2831 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2832 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2833 AssertReturn(fBlocking || pcbWritten, VERR_INVALID_PARAMETER);
2834 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
2835
2836 RTSGSEG Seg = { (void *)pvBuf, cbToWrite };
2837 RTSGBUF SgBuf;
2838 RTSgBufInit(&SgBuf, &Seg, 1);
2839
2840 RTVfsLockAcquireWrite(pThis->Base.hLock);
2841 int rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbWritten);
2842 RTVfsLockReleaseWrite(pThis->Base.hLock);
2843 return rc;
2844}
2845
2846
2847RTDECL(int) RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
2848{
2849 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
2850 if (pcbRead)
2851 *pcbRead = 0;
2852 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2853 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2854 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2855 AssertPtr(pSgBuf);
2856 AssertReturn(fBlocking || pcbRead, VERR_INVALID_PARAMETER);
2857 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_ACCESS_DENIED);
2858
2859 RTVfsLockAcquireWrite(pThis->Base.hLock);
2860 int rc;
2861 if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
2862 rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbRead);
2863 else
2864 {
2865 size_t cbRead = 0;
2866 rc = VINF_SUCCESS;
2867
2868 for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
2869 {
2870 RTSGBUF SgBuf;
2871 RTSgBufInit(&SgBuf, &pSgBuf->paSegs[iSeg], 1);
2872
2873 size_t cbReadSeg = pcbRead ? 0 : pSgBuf->paSegs[iSeg].cbSeg;
2874 rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbRead ? &cbReadSeg : NULL);
2875 if (RT_FAILURE(rc))
2876 break;
2877 cbRead += cbReadSeg;
2878 if ((pcbRead && cbReadSeg != SgBuf.paSegs[0].cbSeg) || rc != VINF_SUCCESS)
2879 break;
2880 if (off != -1)
2881 off += cbReadSeg;
2882 }
2883
2884 if (pcbRead)
2885 *pcbRead = cbRead;
2886 }
2887 RTVfsLockReleaseWrite(pThis->Base.hLock);
2888 return rc;
2889}
2890
2891
2892RTDECL(int) RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
2893{
2894 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
2895 if (pcbWritten)
2896 *pcbWritten = 0;
2897 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2898 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2899 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2900 AssertPtr(pSgBuf);
2901 AssertReturn(fBlocking || pcbWritten, VERR_INVALID_PARAMETER);
2902 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
2903
2904 RTVfsLockAcquireWrite(pThis->Base.hLock);
2905 int rc;
2906 if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
2907 rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbWritten);
2908 else
2909 {
2910 size_t cbWritten = 0;
2911 rc = VINF_SUCCESS;
2912
2913 for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
2914 {
2915 RTSGBUF SgBuf;
2916 RTSgBufInit(&SgBuf, &pSgBuf->paSegs[iSeg], 1);
2917
2918 size_t cbWrittenSeg = 0;
2919 rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbWritten ? &cbWrittenSeg : NULL);
2920 if (RT_FAILURE(rc))
2921 break;
2922 if (pcbWritten)
2923 {
2924 cbWritten += cbWrittenSeg;
2925 if (cbWrittenSeg != SgBuf.paSegs[0].cbSeg)
2926 break;
2927 if (off != -1)
2928 off += cbWrittenSeg;
2929 }
2930 else if (off != -1)
2931 off += pSgBuf->paSegs[iSeg].cbSeg;
2932 }
2933
2934 if (pcbWritten)
2935 *pcbWritten = cbWritten;
2936 }
2937 RTVfsLockReleaseWrite(pThis->Base.hLock);
2938 return rc;
2939}
2940
2941
2942RTDECL(int) RTVfsIoStrmFlush(RTVFSIOSTREAM hVfsIos)
2943{
2944 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2945 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2946 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2947
2948 RTVfsLockAcquireWrite(pThis->Base.hLock);
2949 int rc = pThis->pOps->pfnFlush(pThis->Base.pvThis);
2950 RTVfsLockReleaseWrite(pThis->Base.hLock);
2951 return rc;
2952}
2953
2954
2955RTDECL(int) RTVfsIoStrmPoll(RTVFSIOSTREAM hVfsIos, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
2956 uint32_t *pfRetEvents)
2957{
2958 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2959 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2960 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2961
2962 RTVfsLockAcquireWrite(pThis->Base.hLock);
2963 int rc = pThis->pOps->pfnPollOne(pThis->Base.pvThis, fEvents, cMillies, fIntr, pfRetEvents);
2964 RTVfsLockReleaseWrite(pThis->Base.hLock);
2965 return rc;
2966}
2967
2968
2969RTDECL(RTFOFF) RTVfsIoStrmTell(RTVFSIOSTREAM hVfsIos)
2970{
2971 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2972 AssertPtrReturn(pThis, -1);
2973 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
2974
2975 RTFOFF off;
2976 RTVfsLockAcquireRead(pThis->Base.hLock);
2977 int rc = pThis->pOps->pfnTell(pThis->Base.pvThis, &off);
2978 RTVfsLockReleaseRead(pThis->Base.hLock);
2979 if (RT_FAILURE(rc))
2980 off = rc;
2981 return off;
2982}
2983
2984
2985RTDECL(int) RTVfsIoStrmSkip(RTVFSIOSTREAM hVfsIos, RTFOFF cb)
2986{
2987 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2988 AssertPtrReturn(pThis, -1);
2989 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
2990 AssertReturn(cb >= 0, VERR_INVALID_PARAMETER);
2991
2992 int rc;
2993 if (pThis->pOps->pfnSkip)
2994 {
2995 RTVfsLockAcquireWrite(pThis->Base.hLock);
2996 rc = pThis->pOps->pfnSkip(pThis->Base.pvThis, cb);
2997 RTVfsLockReleaseWrite(pThis->Base.hLock);
2998 }
2999 else if (pThis->pOps->Obj.enmType == RTVFSOBJTYPE_FILE)
3000 {
3001 RTVFSFILEINTERNAL *pThisFile = RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream);
3002 RTFOFF offIgnored;
3003
3004 RTVfsLockAcquireWrite(pThis->Base.hLock);
3005 rc = pThisFile->pOps->pfnSeek(pThis->Base.pvThis, cb, RTFILE_SEEK_CURRENT, &offIgnored);
3006 RTVfsLockReleaseWrite(pThis->Base.hLock);
3007 }
3008 else
3009 {
3010 void *pvBuf = RTMemTmpAlloc(_64K);
3011 if (pvBuf)
3012 {
3013 rc = VINF_SUCCESS;
3014 while (cb > 0)
3015 {
3016 size_t cbToRead = (size_t)RT_MIN(cb, _64K);
3017 RTVfsLockAcquireWrite(pThis->Base.hLock);
3018 rc = RTVfsIoStrmRead(hVfsIos, pvBuf, cbToRead, true /*fBlocking*/, NULL);
3019 RTVfsLockReleaseWrite(pThis->Base.hLock);
3020 if (RT_FAILURE(rc))
3021 break;
3022 cb -= cbToRead;
3023 }
3024
3025 RTMemTmpFree(pvBuf);
3026 }
3027 else
3028 rc = VERR_NO_TMP_MEMORY;
3029 }
3030 return rc;
3031}
3032
3033
3034RTDECL(int) RTVfsIoStrmZeroFill(RTVFSIOSTREAM hVfsIos, RTFOFF cb)
3035{
3036 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
3037 AssertPtrReturn(pThis, -1);
3038 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
3039
3040 int rc;
3041 if (pThis->pOps->pfnSkip)
3042 {
3043 RTVfsLockAcquireWrite(pThis->Base.hLock);
3044 rc = pThis->pOps->pfnZeroFill(pThis->Base.pvThis, cb);
3045 RTVfsLockReleaseWrite(pThis->Base.hLock);
3046 }
3047 else
3048 {
3049 void *pvBuf = RTMemTmpAllocZ(_64K);
3050 if (pvBuf)
3051 {
3052 rc = VINF_SUCCESS;
3053 while (cb > 0)
3054 {
3055 size_t cbToWrite = (size_t)RT_MIN(cb, _64K);
3056 RTVfsLockAcquireWrite(pThis->Base.hLock);
3057 rc = RTVfsIoStrmWrite(hVfsIos, pvBuf, cbToWrite, true /*fBlocking*/, NULL);
3058 RTVfsLockReleaseWrite(pThis->Base.hLock);
3059 if (RT_FAILURE(rc))
3060 break;
3061 cb -= cbToWrite;
3062 }
3063
3064 RTMemTmpFree(pvBuf);
3065 }
3066 else
3067 rc = VERR_NO_TMP_MEMORY;
3068 }
3069 return rc;
3070}
3071
3072
3073RTDECL(bool) RTVfsIoStrmIsAtEnd(RTVFSIOSTREAM hVfsIos)
3074{
3075 /*
3076 * There is where the zero read behavior comes in handy.
3077 */
3078 char bDummy;
3079 size_t cbRead;
3080 int rc = RTVfsIoStrmRead(hVfsIos, &bDummy, 0 /*cbToRead*/, false /*fBlocking*/, &cbRead);
3081 return rc == VINF_EOF;
3082}
3083
3084
3085
3086RTDECL(uint64_t) RTVfsIoStrmGetOpenFlags(RTVFSIOSTREAM hVfsIos)
3087{
3088 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
3089 AssertPtrReturn(pThis, 0);
3090 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, 0);
3091 return pThis->fFlags;
3092}
3093
3094
3095
3096/*
3097 *
3098 * F I L E F I L E F I L E
3099 * F I L E F I L E F I L E
3100 * F I L E F I L E F I L E
3101 *
3102 */
3103
3104RTDECL(int) RTVfsNewFile(PCRTVFSFILEOPS pFileOps, size_t cbInstance, uint32_t fOpen, RTVFS hVfs, RTVFSLOCK hLock,
3105 PRTVFSFILE phVfsFile, void **ppvInstance)
3106{
3107 /*
3108 * Validate the input, be extra strict in strict builds.
3109 */
3110 AssertPtr(pFileOps);
3111 AssertReturn(pFileOps->uVersion == RTVFSFILEOPS_VERSION, VERR_VERSION_MISMATCH);
3112 AssertReturn(pFileOps->uEndMarker == RTVFSFILEOPS_VERSION, VERR_VERSION_MISMATCH);
3113 Assert(!pFileOps->fReserved);
3114 RTVFSIOSTREAM_ASSERT_OPS(&pFileOps->Stream, RTVFSOBJTYPE_FILE);
3115 Assert(cbInstance > 0);
3116 Assert(fOpen & RTFILE_O_ACCESS_MASK);
3117 AssertPtr(ppvInstance);
3118 AssertPtr(phVfsFile);
3119 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
3120
3121 /*
3122 * Allocate the handle + instance data.
3123 */
3124 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSFILEINTERNAL), RTVFS_INST_ALIGNMENT)
3125 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
3126 RTVFSFILEINTERNAL *pThis = (RTVFSFILEINTERNAL *)RTMemAllocZ(cbThis);
3127 if (!pThis)
3128 return VERR_NO_MEMORY;
3129
3130 int rc = rtVfsObjInitNewObject(&pThis->Stream.Base, &pFileOps->Stream.Obj, hVfs, false /*fNoVfsRef*/, hLock,
3131 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
3132 if (RT_FAILURE(rc))
3133 {
3134 RTMemFree(pThis);
3135 return rc;
3136 }
3137
3138 pThis->uMagic = RTVFSFILE_MAGIC;
3139 pThis->fReserved = 0;
3140 pThis->pOps = pFileOps;
3141 pThis->Stream.uMagic = RTVFSIOSTREAM_MAGIC;
3142 pThis->Stream.fFlags = fOpen;
3143 pThis->Stream.pOps = &pFileOps->Stream;
3144
3145 *phVfsFile = pThis;
3146 *ppvInstance = pThis->Stream.Base.pvThis;
3147 return VINF_SUCCESS;
3148}
3149
3150
3151RTDECL(int) RTVfsFileOpen(RTVFS hVfs, const char *pszFilename, uint64_t fOpen, PRTVFSFILE phVfsFile)
3152{
3153 /*
3154 * Validate input.
3155 */
3156 RTVFSINTERNAL *pThis = hVfs;
3157 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3158 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
3159 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
3160 AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
3161
3162 int rc = rtFileRecalcAndValidateFlags(&fOpen);
3163 if (RT_FAILURE(rc))
3164 return rc;
3165
3166 /*
3167 * Parse the path, assume current directory is root since we've got no
3168 * caller context here.
3169 */
3170 PRTVFSPARSEDPATH pPath;
3171 rc = RTVfsParsePathA(pszFilename, "/", &pPath);
3172 if (RT_SUCCESS(rc))
3173 {
3174 if ( !pPath->fDirSlash
3175 && pPath->cComponents > 0)
3176 {
3177 /*
3178 * Tranverse the path, resolving the parent node and any symlinks
3179 * in the final element, and ask the directory to open the file.
3180 */
3181 RTVFSDIRINTERNAL *pVfsParentDir;
3182 rc = rtVfsTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
3183 if (RT_SUCCESS(rc))
3184 {
3185 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
3186
3187 /** @todo there is a symlink creation race here. */
3188 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
3189 rc = pVfsParentDir->pOps->pfnOpenFile(pVfsParentDir->Base.pvThis, pszEntryName, fOpen, phVfsFile);
3190 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
3191
3192 RTVfsDirRelease(pVfsParentDir);
3193
3194 if (RT_SUCCESS(rc))
3195 {
3196 AssertPtr(*phVfsFile);
3197 Assert((*phVfsFile)->uMagic == RTVFSFILE_MAGIC);
3198 }
3199 }
3200 }
3201 else
3202 rc = VERR_NOT_A_FILE;
3203 RTVfsParsePathFree(pPath);
3204 }
3205 return rc;
3206}
3207
3208
3209#ifdef DEBUG
3210# undef RTVfsFileRetain
3211#endif
3212RTDECL(uint32_t) RTVfsFileRetain(RTVFSFILE hVfsFile)
3213{
3214 RTVFSFILEINTERNAL *pThis = hVfsFile;
3215 AssertPtrReturn(pThis, UINT32_MAX);
3216 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, UINT32_MAX);
3217 return rtVfsObjRetain(&pThis->Stream.Base);
3218}
3219#ifdef DEBUG
3220# define RTVfsFileRetain(hVfsFile) RTVfsFileRetainDebug(hVfsFile, RT_SRC_POS)
3221#endif
3222
3223
3224RTDECL(uint32_t) RTVfsFileRetainDebug(RTVFSFILE hVfsFile, RT_SRC_POS_DECL)
3225{
3226 RTVFSFILEINTERNAL *pThis = hVfsFile;
3227 AssertPtrReturn(pThis, UINT32_MAX);
3228 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, UINT32_MAX);
3229 return rtVfsObjRetainDebug(&pThis->Stream.Base, "RTVFsFileRetainDebug", RT_SRC_POS_ARGS);
3230}
3231
3232
3233RTDECL(uint32_t) RTVfsFileRelease(RTVFSFILE hVfsFile)
3234{
3235 RTVFSFILEINTERNAL *pThis = hVfsFile;
3236 if (pThis == NIL_RTVFSFILE)
3237 return 0;
3238 AssertPtrReturn(pThis, UINT32_MAX);
3239 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, UINT32_MAX);
3240 return rtVfsObjRelease(&pThis->Stream.Base);
3241}
3242
3243
3244RTDECL(RTVFSIOSTREAM) RTVfsFileToIoStream(RTVFSFILE hVfsFile)
3245{
3246 RTVFSFILEINTERNAL *pThis = hVfsFile;
3247 AssertPtrReturn(pThis, NIL_RTVFSIOSTREAM);
3248 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, NIL_RTVFSIOSTREAM);
3249
3250 rtVfsObjRetainVoid(&pThis->Stream.Base, "RTVfsFileToIoStream");
3251 return &pThis->Stream;
3252}
3253
3254
3255RTDECL(int) RTVfsFileQueryInfo(RTVFSFILE hVfsFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
3256{
3257 RTVFSFILEINTERNAL *pThis = hVfsFile;
3258 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3259 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3260 return RTVfsObjQueryInfo(&pThis->Stream.Base, pObjInfo, enmAddAttr);
3261}
3262
3263
3264RTDECL(int) RTVfsFileRead(RTVFSFILE hVfsFile, void *pvBuf, size_t cbToRead, size_t *pcbRead)
3265{
3266 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
3267 if (pcbRead)
3268 *pcbRead = 0;
3269 RTVFSFILEINTERNAL *pThis = hVfsFile;
3270 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3271 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3272 return RTVfsIoStrmRead(&pThis->Stream, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
3273}
3274
3275
3276RTDECL(int) RTVfsFileWrite(RTVFSFILE hVfsFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
3277{
3278 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
3279 if (pcbWritten)
3280 *pcbWritten = 0;
3281 RTVFSFILEINTERNAL *pThis = hVfsFile;
3282 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3283 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3284 return RTVfsIoStrmWrite(&pThis->Stream, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
3285}
3286
3287
3288RTDECL(int) RTVfsFileWriteAt(RTVFSFILE hVfsFile, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
3289{
3290 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
3291 if (pcbWritten)
3292 *pcbWritten = 0;
3293 RTVFSFILEINTERNAL *pThis = hVfsFile;
3294 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3295 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3296
3297 int rc = RTVfsFileSeek(hVfsFile, off, RTFILE_SEEK_BEGIN, NULL);
3298 if (RT_SUCCESS(rc))
3299 rc = RTVfsIoStrmWriteAt(&pThis->Stream, off, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
3300
3301 return rc;
3302}
3303
3304
3305RTDECL(int) RTVfsFileReadAt(RTVFSFILE hVfsFile, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
3306{
3307 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
3308 if (pcbRead)
3309 *pcbRead = 0;
3310 RTVFSFILEINTERNAL *pThis = hVfsFile;
3311 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3312 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3313
3314 int rc = RTVfsFileSeek(hVfsFile, off, RTFILE_SEEK_BEGIN, NULL);
3315 if (RT_SUCCESS(rc))
3316 rc = RTVfsIoStrmReadAt(&pThis->Stream, off, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
3317
3318 return rc;
3319}
3320
3321
3322RTDECL(int) RTVfsFileSgRead(RTVFSFILE hVfsFile, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
3323{
3324 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
3325 if (pcbRead)
3326 *pcbRead = 0;
3327 RTVFSFILEINTERNAL *pThis = hVfsFile;
3328 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3329 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3330
3331 return RTVfsIoStrmSgRead(&pThis->Stream, off, pSgBuf, fBlocking, pcbRead);
3332}
3333
3334
3335RTDECL(int) RTVfsFileSgWrite(RTVFSFILE hVfsFile, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
3336{
3337 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
3338 if (pcbWritten)
3339 *pcbWritten = 0;
3340 RTVFSFILEINTERNAL *pThis = hVfsFile;
3341 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3342 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3343
3344 return RTVfsIoStrmSgWrite(&pThis->Stream, off, pSgBuf, fBlocking, pcbWritten);
3345}
3346
3347
3348RTDECL(int) RTVfsFileFlush(RTVFSFILE hVfsFile)
3349{
3350 RTVFSFILEINTERNAL *pThis = hVfsFile;
3351 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3352 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3353 return RTVfsIoStrmFlush(&pThis->Stream);
3354}
3355
3356
3357RTDECL(RTFOFF) RTVfsFilePoll(RTVFSFILE hVfsFile, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
3358 uint32_t *pfRetEvents)
3359{
3360 RTVFSFILEINTERNAL *pThis = hVfsFile;
3361 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3362 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3363 return RTVfsIoStrmPoll(&pThis->Stream, fEvents, cMillies, fIntr, pfRetEvents);
3364}
3365
3366
3367RTDECL(RTFOFF) RTVfsFileTell(RTVFSFILE hVfsFile)
3368{
3369 RTVFSFILEINTERNAL *pThis = hVfsFile;
3370 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3371 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3372 return RTVfsIoStrmTell(&pThis->Stream);
3373}
3374
3375
3376RTDECL(int) RTVfsFileSeek(RTVFSFILE hVfsFile, RTFOFF offSeek, uint32_t uMethod, uint64_t *poffActual)
3377{
3378 RTVFSFILEINTERNAL *pThis = hVfsFile;
3379 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3380 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3381
3382 AssertReturn( uMethod == RTFILE_SEEK_BEGIN
3383 || uMethod == RTFILE_SEEK_CURRENT
3384 || uMethod == RTFILE_SEEK_END, VERR_INVALID_PARAMETER);
3385 AssertPtrNullReturn(poffActual, VERR_INVALID_POINTER);
3386
3387 RTFOFF offActual = 0;
3388 RTVfsLockAcquireWrite(pThis->Stream.Base.hLock);
3389 int rc = pThis->pOps->pfnSeek(pThis->Stream.Base.pvThis, offSeek, uMethod, &offActual);
3390 RTVfsLockReleaseWrite(pThis->Stream.Base.hLock);
3391 if (RT_SUCCESS(rc) && poffActual)
3392 {
3393 Assert(offActual >= 0);
3394 *poffActual = offActual;
3395 }
3396
3397 return rc;
3398}
3399
3400
3401RTDECL(int) RTVfsFileGetSize(RTVFSFILE hVfsFile, uint64_t *pcbSize)
3402{
3403 RTVFSFILEINTERNAL *pThis = hVfsFile;
3404 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3405 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3406 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
3407
3408 RTVfsLockAcquireWrite(pThis->Stream.Base.hLock);
3409 int rc = pThis->pOps->pfnQuerySize(pThis->Stream.Base.pvThis, pcbSize);
3410 RTVfsLockReleaseWrite(pThis->Stream.Base.hLock);
3411
3412 return rc;
3413}
3414
3415
3416RTDECL(uint64_t) RTVfsFileGetOpenFlags(RTVFSFILE hVfsFile)
3417{
3418 RTVFSFILEINTERNAL *pThis = hVfsFile;
3419 AssertPtrReturn(pThis, 0);
3420 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, 0);
3421 return pThis->Stream.fFlags;
3422}
3423
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