VirtualBox

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

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

iso9660: Rough ReadDir implementation. Also a build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 103.1 KB
Line 
1/* $Id: vfsbase.cpp 66767 2017-05-04 00:46:42Z 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.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
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,
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 AssertPtr(pFsStreamOps->pfnNext);
1928 Assert(cbInstance > 0);
1929 AssertPtr(ppvInstance);
1930 AssertPtr(phVfsFss);
1931 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
1932
1933 /*
1934 * Allocate the handle + instance data.
1935 */
1936 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSFSSTREAMINTERNAL), RTVFS_INST_ALIGNMENT)
1937 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
1938 RTVFSFSSTREAMINTERNAL *pThis = (RTVFSFSSTREAMINTERNAL *)RTMemAllocZ(cbThis);
1939 if (!pThis)
1940 return VERR_NO_MEMORY;
1941
1942 int rc = rtVfsObjInitNewObject(&pThis->Base, &pFsStreamOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
1943 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
1944
1945 if (RT_FAILURE(rc))
1946 {
1947 RTMemFree(pThis);
1948 return rc;
1949 }
1950
1951 pThis->uMagic = RTVFSFSSTREAM_MAGIC;
1952 pThis->fFlags = RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE;
1953 pThis->pOps = pFsStreamOps;
1954
1955 *phVfsFss = pThis;
1956 *ppvInstance = pThis->Base.pvThis;
1957 return VINF_SUCCESS;
1958}
1959
1960
1961#ifdef DEBUG
1962# undef RTVfsFsStrmRetain
1963#endif
1964RTDECL(uint32_t) RTVfsFsStrmRetain(RTVFSFSSTREAM hVfsFss)
1965{
1966 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1967 AssertPtrReturn(pThis, UINT32_MAX);
1968 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, UINT32_MAX);
1969 return rtVfsObjRetain(&pThis->Base);
1970}
1971#ifdef DEBUG
1972# define RTVfsFsStrmRetain(hVfsFss) RTVfsFsStrmRetainDebug(hVfsFss, RT_SRC_POS)
1973#endif
1974
1975
1976RTDECL(uint32_t) RTVfsFsStrmRetainDebug(RTVFSFSSTREAM hVfsFss, RT_SRC_POS_DECL)
1977{
1978 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1979 AssertPtrReturn(pThis, UINT32_MAX);
1980 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, UINT32_MAX);
1981 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsFsStrmRetain", RT_SRC_POS_ARGS);
1982}
1983
1984
1985RTDECL(uint32_t) RTVfsFsStrmRelease(RTVFSFSSTREAM hVfsFss)
1986{
1987 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1988 if (pThis == NIL_RTVFSFSSTREAM)
1989 return 0;
1990 AssertPtrReturn(pThis, UINT32_MAX);
1991 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, UINT32_MAX);
1992 return rtVfsObjRelease(&pThis->Base);
1993}
1994
1995
1996RTDECL(int) RTVfsFsStrmQueryInfo(RTVFSFSSTREAM hVfsFss, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1997{
1998 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
1999 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2000 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2001 return RTVfsObjQueryInfo(&pThis->Base, pObjInfo, enmAddAttr);
2002}
2003
2004
2005RTDECL(int) RTVfsFsStrmNext(RTVFSFSSTREAM hVfsFss, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
2006{
2007 RTVFSFSSTREAMINTERNAL *pThis = hVfsFss;
2008 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2009 AssertReturn(pThis->uMagic == RTVFSFSSTREAM_MAGIC, VERR_INVALID_HANDLE);
2010 AssertPtrNullReturn(ppszName, VERR_INVALID_POINTER);
2011 if (ppszName)
2012 *ppszName = NULL;
2013 AssertPtrNullReturn(penmType, VERR_INVALID_POINTER);
2014 if (penmType)
2015 *penmType = RTVFSOBJTYPE_INVALID;
2016 AssertPtrNullReturn(penmType, VERR_INVALID_POINTER);
2017 if (phVfsObj)
2018 *phVfsObj = NIL_RTVFSOBJ;
2019
2020 return pThis->pOps->pfnNext(pThis->Base.pvThis, ppszName, penmType, phVfsObj);
2021}
2022
2023
2024
2025
2026/*
2027 *
2028 * D I R D I R D I R
2029 * D I R D I R D I R
2030 * D I R D I R D I R
2031 *
2032 */
2033
2034
2035RTDECL(int) RTVfsNewDir(PCRTVFSDIROPS pDirOps, size_t cbInstance, uint32_t fFlags, RTVFS hVfs, RTVFSLOCK hLock,
2036 PRTVFSDIR phVfsDir, void **ppvInstance)
2037{
2038 /*
2039 * Validate the input, be extra strict in strict builds.
2040 */
2041 AssertPtr(pDirOps);
2042 AssertReturn(pDirOps->uVersion == RTVFSDIROPS_VERSION, VERR_VERSION_MISMATCH);
2043 AssertReturn(pDirOps->uEndMarker == RTVFSDIROPS_VERSION, VERR_VERSION_MISMATCH);
2044 Assert(!pDirOps->fReserved);
2045 RTVFSDIR_ASSERT_OPS(pDirOps, RTVFSOBJTYPE_DIR);
2046 Assert(cbInstance > 0);
2047 AssertReturn(!(fFlags & ~RTVFSDIR_F_NO_VFS_REF), VERR_INVALID_FLAGS);
2048 AssertPtr(ppvInstance);
2049 AssertPtr(phVfsDir);
2050 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
2051
2052 /*
2053 * Allocate the handle + instance data.
2054 */
2055 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSDIRINTERNAL), RTVFS_INST_ALIGNMENT)
2056 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
2057 RTVFSDIRINTERNAL *pThis = (RTVFSDIRINTERNAL *)RTMemAllocZ(cbThis);
2058 if (!pThis)
2059 return VERR_NO_MEMORY;
2060
2061 int rc = rtVfsObjInitNewObject(&pThis->Base, &pDirOps->Obj, hVfs, RT_BOOL(fFlags & RTVFSDIR_F_NO_VFS_REF), hLock,
2062 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
2063 if (RT_FAILURE(rc))
2064 {
2065 RTMemFree(pThis);
2066 return rc;
2067 }
2068
2069 pThis->uMagic = RTVFSDIR_MAGIC;
2070 pThis->fReserved = 0;
2071 pThis->pOps = pDirOps;
2072
2073 *phVfsDir = pThis;
2074 *ppvInstance = pThis->Base.pvThis;
2075 return VINF_SUCCESS;
2076}
2077
2078
2079#ifdef DEBUG
2080# undef RTVfsDirRetain
2081#endif
2082RTDECL(uint32_t) RTVfsDirRetain(RTVFSDIR hVfsDir)
2083{
2084 RTVFSDIRINTERNAL *pThis = hVfsDir;
2085 AssertPtrReturn(pThis, UINT32_MAX);
2086 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, UINT32_MAX);
2087 uint32_t cRefs = rtVfsObjRetain(&pThis->Base);
2088 LogFlow(("RTVfsDirRetain(%p/%p) -> %#x\n", pThis, pThis->Base.pvThis, cRefs));
2089 return cRefs;
2090}
2091#ifdef DEBUG
2092# define RTVfsDirRetain(hVfsDir) RTVfsDirRetainDebug(hVfsDir, RT_SRC_POS)
2093#endif
2094
2095
2096RTDECL(uint32_t) RTVfsDirRetainDebug(RTVFSDIR hVfsDir, RT_SRC_POS_DECL)
2097{
2098 RTVFSDIRINTERNAL *pThis = hVfsDir;
2099 AssertPtrReturn(pThis, UINT32_MAX);
2100 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, UINT32_MAX);
2101 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsDirRetain", RT_SRC_POS_ARGS);
2102}
2103
2104
2105RTDECL(uint32_t) RTVfsDirRelease(RTVFSDIR hVfsDir)
2106{
2107 RTVFSDIRINTERNAL *pThis = hVfsDir;
2108 if (pThis == NIL_RTVFSDIR)
2109 return 0;
2110 AssertPtrReturn(pThis, UINT32_MAX);
2111 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, UINT32_MAX);
2112#ifdef LOG_ENABLED
2113 void *pvThis = pThis->Base.pvThis;
2114#endif
2115 uint32_t cRefs = rtVfsObjRelease(&pThis->Base);
2116 LogFlow(("RTVfsDirRelease(%p/%p) -> %#x\n", pThis, pvThis, cRefs));
2117 return cRefs;
2118}
2119
2120
2121RTDECL(int) RTVfsDirOpen(RTVFS hVfs, const char *pszPath, uint32_t fFlags, PRTVFSDIR phVfsDir)
2122{
2123 /*
2124 * Validate input.
2125 */
2126 RTVFSINTERNAL *pThis = hVfs;
2127 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2128 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
2129 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2130 AssertPtrReturn(phVfsDir, VERR_INVALID_POINTER);
2131 AssertReturn(!fFlags, VERR_INVALID_FLAGS);
2132
2133 /*
2134 * Parse the path, assume current directory is root since we've got no
2135 * caller context here.
2136 */
2137 PRTVFSPARSEDPATH pPath;
2138 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
2139 if (RT_SUCCESS(rc))
2140 {
2141 if (pPath->cComponents > 0)
2142 {
2143 /*
2144 * Tranverse the path, resolving the parent node and any symlinks
2145 * in the final element, and ask the directory to open the subdir.
2146 */
2147 RTVFSDIRINTERNAL *pVfsParentDir;
2148 rc = rtVfsTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
2149 if (RT_SUCCESS(rc))
2150 {
2151 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2152
2153 /** @todo there is a symlink creation race here. */
2154 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
2155 rc = pVfsParentDir->pOps->pfnOpenDir(pVfsParentDir->Base.pvThis, pszEntryName, fFlags, phVfsDir);
2156 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
2157
2158 RTVfsDirRelease(pVfsParentDir);
2159
2160 if (RT_SUCCESS(rc))
2161 {
2162 AssertPtr(*phVfsDir);
2163 Assert((*phVfsDir)->uMagic == RTVFSDIR_MAGIC);
2164 }
2165 }
2166 }
2167 /*
2168 * If the path boils down to '.' return the root directory.
2169 */
2170 else
2171 {
2172 RTVfsLockAcquireRead(pThis->Base.hLock);
2173 rc = pThis->pOps->pfnOpenRoot(pThis->Base.pvThis, phVfsDir);
2174 RTVfsLockReleaseRead(pThis->Base.hLock);
2175 }
2176 RTVfsParsePathFree(pPath);
2177 }
2178 return rc;
2179}
2180
2181
2182RTDECL(int) RTVfsDirOpenDir(RTVFSDIR hVfsDir, const char *pszPath, uint32_t fFlags, PRTVFSDIR phVfsDir)
2183{
2184 /*
2185 * Validate input.
2186 */
2187 RTVFSDIRINTERNAL *pThis = hVfsDir;
2188 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2189 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2190 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2191 AssertPtrReturn(phVfsDir, VERR_INVALID_POINTER);
2192 AssertReturn(!fFlags, VERR_INVALID_FLAGS);
2193
2194 /*
2195 * Parse the path, it's always relative to the given directory.
2196 */
2197 PRTVFSPARSEDPATH pPath;
2198 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
2199 if (RT_SUCCESS(rc))
2200 {
2201 if (pPath->cComponents > 0)
2202 {
2203 /*
2204 * Tranverse the path, resolving the parent node and any symlinks
2205 * in the final element, and ask the directory to open the subdir.
2206 */
2207 RTVFSDIRINTERNAL *pVfsParentDir;
2208 rc = rtVfsDirTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
2209 if (RT_SUCCESS(rc))
2210 {
2211 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2212
2213 /** @todo there is a symlink creation race here. */
2214 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
2215 rc = pVfsParentDir->pOps->pfnOpenDir(pVfsParentDir->Base.pvThis, pszEntryName, fFlags, phVfsDir);
2216 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
2217
2218 RTVfsDirRelease(pVfsParentDir);
2219
2220 if (RT_SUCCESS(rc))
2221 {
2222 AssertPtr(*phVfsDir);
2223 Assert((*phVfsDir)->uMagic == RTVFSDIR_MAGIC);
2224 }
2225 }
2226 }
2227 /*
2228 * The path boils down to '.', call pfnOpenDir on pThis with '.' as input.
2229 * The caller may wish for a new directory instance to enumerate the entries
2230 * in parallel or some such thing.
2231 */
2232 else
2233 {
2234 RTVfsLockAcquireWrite(pThis->Base.hLock);
2235 rc = pThis->pOps->pfnOpenDir(pThis->Base.pvThis, ".", fFlags, phVfsDir);
2236 RTVfsLockReleaseWrite(pThis->Base.hLock);
2237 }
2238 RTVfsParsePathFree(pPath);
2239 }
2240 return rc;
2241}
2242
2243
2244RTDECL(int) RTVfsDirOpenFile(RTVFSDIR hVfsDir, const char *pszPath, uint64_t fOpen, PRTVFSFILE phVfsFile)
2245{
2246 /*
2247 * Validate input.
2248 */
2249 RTVFSDIRINTERNAL *pThis = hVfsDir;
2250 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2251 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2252 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2253 AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
2254
2255 int rc = rtFileRecalcAndValidateFlags(&fOpen);
2256 if (RT_FAILURE(rc))
2257 return rc;
2258
2259 /*
2260 * Parse the path, assume current directory is root since we've got no
2261 * caller context here.
2262 */
2263 PRTVFSPARSEDPATH pPath;
2264 rc = RTVfsParsePathA(pszPath, "/", &pPath);
2265 if (RT_SUCCESS(rc))
2266 {
2267 if ( !pPath->fDirSlash
2268 && pPath->cComponents > 0)
2269 {
2270 /*
2271 * Tranverse the path, resolving the parent node and any symlinks
2272 * in the final element, and ask the directory to open the file.
2273 */
2274 RTVFSDIRINTERNAL *pVfsParentDir;
2275 rc = rtVfsDirTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
2276 if (RT_SUCCESS(rc))
2277 {
2278 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2279
2280 /** @todo there is a symlink creation race here. */
2281 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
2282 rc = pVfsParentDir->pOps->pfnOpenFile(pVfsParentDir->Base.pvThis, pszEntryName, fOpen, phVfsFile);
2283 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
2284
2285 RTVfsDirRelease(pVfsParentDir);
2286
2287 if (RT_SUCCESS(rc))
2288 {
2289 AssertPtr(*phVfsFile);
2290 Assert((*phVfsFile)->uMagic == RTVFSFILE_MAGIC);
2291 }
2292 }
2293 }
2294 else
2295 rc = VERR_NOT_A_FILE;
2296 RTVfsParsePathFree(pPath);
2297 }
2298 return rc;
2299}
2300
2301
2302RTDECL(int) RTVfsDirQueryPathInfo(RTVFSDIR hVfsDir, const char *pszPath, PRTFSOBJINFO pObjInfo,
2303 RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
2304{
2305 /*
2306 * Validate input.
2307 */
2308 RTVFSDIRINTERNAL *pThis = hVfsDir;
2309 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2310 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2311 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
2312 AssertReturn(*pszPath, VERR_INVALID_PARAMETER);
2313 AssertPtrReturn(pObjInfo, VERR_INVALID_POINTER);
2314 AssertReturn(enmAddAttr >= RTFSOBJATTRADD_NOTHING && enmAddAttr <= RTFSOBJATTRADD_LAST, VERR_INVALID_PARAMETER);
2315 AssertMsgReturn(RTPATH_F_IS_VALID(fFlags, 0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
2316
2317 /*
2318 * Parse the path, assume current directory is root since we've got no
2319 * caller context here. Then traverse to the parent directory.
2320 */
2321 PRTVFSPARSEDPATH pPath;
2322 int rc = RTVfsParsePathA(pszPath, "/", &pPath);
2323 if (RT_SUCCESS(rc))
2324 {
2325 if (pPath->cComponents > 0)
2326 {
2327 RTVFSDIRINTERNAL *pVfsParentDir;
2328 rc = rtVfsDirTraverseToParent(pThis, pPath, fFlags, &pVfsParentDir);
2329 if (RT_SUCCESS(rc))
2330 {
2331 /*
2332 * Call the query method on the parent directory.
2333 */
2334 /** @todo symlink race condition here :/ */
2335 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
2336 RTVfsLockAcquireRead(pVfsParentDir->Base.hLock);
2337 rc = pVfsParentDir->pOps->pfnQueryEntryInfo(pVfsParentDir->Base.pvThis, pszEntryName, pObjInfo, enmAddAttr);
2338 RTVfsLockReleaseRead(pVfsParentDir->Base.hLock);
2339
2340 RTVfsDirRelease(pVfsParentDir);
2341 }
2342 else
2343 rc = VERR_INVALID_PARAMETER;
2344 }
2345 /*
2346 * The path boils down to '.' so just query the directory.
2347 */
2348 else
2349 {
2350 RTVfsLockAcquireRead(pThis->Base.hLock);
2351 rc = pThis->Base.pOps->pfnQueryInfo(pThis->Base.pvThis, pObjInfo, enmAddAttr);
2352 RTVfsLockReleaseRead(pThis->Base.hLock);
2353 }
2354 RTVfsParsePathFree(pPath);
2355 }
2356 return rc;
2357}
2358
2359
2360RTDECL(int) RTVfsDirReadEx(RTVFSDIR hVfsDir, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAddAttr)
2361{
2362 /*
2363 * Validate input.
2364 */
2365 RTVFSDIRINTERNAL *pThis = hVfsDir;
2366 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2367 AssertReturn(pThis->uMagic == RTVFSDIR_MAGIC, VERR_INVALID_HANDLE);
2368 AssertPtrReturn(pDirEntry, VERR_INVALID_POINTER);
2369 AssertReturn(enmAddAttr >= RTFSOBJATTRADD_NOTHING && enmAddAttr <= RTFSOBJATTRADD_LAST, VERR_INVALID_PARAMETER);
2370
2371 size_t cbDirEntry = sizeof(*pDirEntry);
2372 if (!pcbDirEntry)
2373 pcbDirEntry = &cbDirEntry;
2374 else
2375 {
2376 cbDirEntry = *pcbDirEntry;
2377 AssertMsgReturn(cbDirEntry >= RT_UOFFSETOF(RTDIRENTRYEX, szName[2]),
2378 ("Invalid *pcbDirEntry=%d (min %d)\n", *pcbDirEntry, RT_OFFSETOF(RTDIRENTRYEX, szName[2])),
2379 VERR_INVALID_PARAMETER);
2380 }
2381
2382 /*
2383 * Call the directory method.
2384 */
2385 RTVfsLockAcquireRead(pThis->Base.hLock);
2386 int rc = pThis->pOps->pfnReadDir(pThis->Base.pvThis, pDirEntry, pcbDirEntry, enmAddAttr);
2387 RTVfsLockReleaseRead(pThis->Base.hLock);
2388 return rc;
2389}
2390
2391
2392/*
2393 *
2394 * S Y M B O L I C L I N K
2395 * S Y M B O L I C L I N K
2396 * S Y M B O L I C L I N K
2397 *
2398 */
2399
2400RTDECL(int) RTVfsNewSymlink(PCRTVFSSYMLINKOPS pSymlinkOps, size_t cbInstance, RTVFS hVfs, RTVFSLOCK hLock,
2401 PRTVFSSYMLINK phVfsSym, void **ppvInstance)
2402{
2403 /*
2404 * Validate the input, be extra strict in strict builds.
2405 */
2406 AssertPtr(pSymlinkOps);
2407 AssertReturn(pSymlinkOps->uVersion == RTVFSSYMLINKOPS_VERSION, VERR_VERSION_MISMATCH);
2408 AssertReturn(pSymlinkOps->uEndMarker == RTVFSSYMLINKOPS_VERSION, VERR_VERSION_MISMATCH);
2409 Assert(!pSymlinkOps->fReserved);
2410 RTVFSSYMLINK_ASSERT_OPS(pSymlinkOps, RTVFSOBJTYPE_SYMLINK);
2411 Assert(cbInstance > 0);
2412 AssertPtr(ppvInstance);
2413 AssertPtr(phVfsSym);
2414 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
2415
2416 /*
2417 * Allocate the handle + instance data.
2418 */
2419 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSSYMLINKINTERNAL), RTVFS_INST_ALIGNMENT)
2420 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
2421 RTVFSSYMLINKINTERNAL *pThis = (RTVFSSYMLINKINTERNAL *)RTMemAllocZ(cbThis);
2422 if (!pThis)
2423 return VERR_NO_MEMORY;
2424
2425 int rc = rtVfsObjInitNewObject(&pThis->Base, &pSymlinkOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
2426 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
2427 if (RT_FAILURE(rc))
2428 {
2429 RTMemFree(pThis);
2430 return rc;
2431 }
2432
2433 pThis->uMagic = RTVFSSYMLINK_MAGIC;
2434 pThis->pOps = pSymlinkOps;
2435
2436 *phVfsSym = pThis;
2437 *ppvInstance = pThis->Base.pvThis;
2438 return VINF_SUCCESS;
2439}
2440
2441
2442RTDECL(uint32_t) RTVfsSymlinkRetain(RTVFSSYMLINK hVfsSym)
2443{
2444 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2445 AssertPtrReturn(pThis, UINT32_MAX);
2446 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, UINT32_MAX);
2447 return rtVfsObjRetain(&pThis->Base);
2448}
2449
2450
2451RTDECL(uint32_t) RTVfsSymlinkRetainDebug(RTVFSSYMLINK hVfsSym, RT_SRC_POS_DECL)
2452{
2453 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2454 AssertPtrReturn(pThis, UINT32_MAX);
2455 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, UINT32_MAX);
2456 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsSymlinkRetainDebug", RT_SRC_POS_ARGS);
2457}
2458
2459
2460RTDECL(uint32_t) RTVfsSymlinkRelease(RTVFSSYMLINK hVfsSym)
2461{
2462 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2463 if (pThis == NIL_RTVFSSYMLINK)
2464 return 0;
2465 AssertPtrReturn(pThis, UINT32_MAX);
2466 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, UINT32_MAX);
2467 return rtVfsObjRelease(&pThis->Base);
2468}
2469
2470
2471RTDECL(int) RTVfsSymlinkQueryInfo(RTVFSSYMLINK hVfsSym, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2472{
2473 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2474 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2475 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2476 return RTVfsObjQueryInfo(&pThis->Base, pObjInfo, enmAddAttr);
2477}
2478
2479
2480RTDECL(int) RTVfsSymlinkSetMode(RTVFSSYMLINK hVfsSym, RTFMODE fMode, RTFMODE fMask)
2481{
2482 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2483 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2484 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2485
2486 fMode = rtFsModeNormalize(fMode, NULL, 0);
2487 if (!rtFsModeIsValid(fMode))
2488 return VERR_INVALID_PARAMETER;
2489
2490 RTVfsLockAcquireWrite(pThis->Base.hLock);
2491 int rc = pThis->pOps->ObjSet.pfnSetMode(pThis->Base.pvThis, fMode, fMask);
2492 RTVfsLockReleaseWrite(pThis->Base.hLock);
2493 return rc;
2494}
2495
2496
2497RTDECL(int) RTVfsSymlinkSetTimes(RTVFSSYMLINK hVfsSym, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
2498 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
2499{
2500 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2501 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2502 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2503
2504 AssertPtrNullReturn(pAccessTime, VERR_INVALID_POINTER);
2505 AssertPtrNullReturn(pModificationTime, VERR_INVALID_POINTER);
2506 AssertPtrNullReturn(pChangeTime, VERR_INVALID_POINTER);
2507 AssertPtrNullReturn(pBirthTime, VERR_INVALID_POINTER);
2508
2509 RTVfsLockAcquireWrite(pThis->Base.hLock);
2510 int rc = pThis->pOps->ObjSet.pfnSetTimes(pThis->Base.pvThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
2511 RTVfsLockReleaseWrite(pThis->Base.hLock);
2512 return rc;
2513}
2514
2515
2516RTDECL(int) RTVfsSymlinkSetOwner(RTVFSSYMLINK hVfsSym, RTUID uid, RTGID gid)
2517{
2518 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2519 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2520 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2521
2522 RTVfsLockAcquireWrite(pThis->Base.hLock);
2523 int rc = pThis->pOps->ObjSet.pfnSetOwner(pThis->Base.pvThis, uid, gid);
2524 RTVfsLockReleaseWrite(pThis->Base.hLock);
2525 return rc;
2526}
2527
2528
2529RTDECL(int) RTVfsSymlinkRead(RTVFSSYMLINK hVfsSym, char *pszTarget, size_t cbTarget)
2530{
2531 RTVFSSYMLINKINTERNAL *pThis = hVfsSym;
2532 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2533 AssertReturn(pThis->uMagic == RTVFSSYMLINK_MAGIC, VERR_INVALID_HANDLE);
2534
2535 RTVfsLockAcquireWrite(pThis->Base.hLock);
2536 int rc = pThis->pOps->pfnRead(pThis->Base.pvThis, pszTarget, cbTarget);
2537 RTVfsLockReleaseWrite(pThis->Base.hLock);
2538
2539 return rc;
2540}
2541
2542
2543
2544/*
2545 *
2546 * I / O S T R E A M I / O S T R E A M I / O S T R E A M
2547 * I / O S T R E A M I / O S T R E A M I / O S T R E A M
2548 * I / O S T R E A M I / O S T R E A M I / O S T R E A M
2549 *
2550 */
2551
2552RTDECL(int) RTVfsNewIoStream(PCRTVFSIOSTREAMOPS pIoStreamOps, size_t cbInstance, uint32_t fOpen, RTVFS hVfs, RTVFSLOCK hLock,
2553 PRTVFSIOSTREAM phVfsIos, void **ppvInstance)
2554{
2555 /*
2556 * Validate the input, be extra strict in strict builds.
2557 */
2558 AssertPtr(pIoStreamOps);
2559 AssertReturn(pIoStreamOps->uVersion == RTVFSIOSTREAMOPS_VERSION, VERR_VERSION_MISMATCH);
2560 AssertReturn(pIoStreamOps->uEndMarker == RTVFSIOSTREAMOPS_VERSION, VERR_VERSION_MISMATCH);
2561 Assert(!(pIoStreamOps->fFeatures & ~RTVFSIOSTREAMOPS_FEAT_VALID_MASK));
2562 RTVFSIOSTREAM_ASSERT_OPS(pIoStreamOps, RTVFSOBJTYPE_IO_STREAM);
2563 Assert(cbInstance > 0);
2564 Assert(fOpen & RTFILE_O_ACCESS_MASK);
2565 AssertPtr(ppvInstance);
2566 AssertPtr(phVfsIos);
2567 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
2568
2569 /*
2570 * Allocate the handle + instance data.
2571 */
2572 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSIOSTREAMINTERNAL), RTVFS_INST_ALIGNMENT)
2573 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
2574 RTVFSIOSTREAMINTERNAL *pThis = (RTVFSIOSTREAMINTERNAL *)RTMemAllocZ(cbThis);
2575 if (!pThis)
2576 return VERR_NO_MEMORY;
2577
2578 int rc = rtVfsObjInitNewObject(&pThis->Base, &pIoStreamOps->Obj, hVfs, false /*fNoVfsRef*/, hLock,
2579 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
2580 if (RT_FAILURE(rc))
2581 {
2582 RTMemFree(pThis);
2583 return rc;
2584 }
2585
2586 pThis->uMagic = RTVFSIOSTREAM_MAGIC;
2587 pThis->fFlags = fOpen;
2588 pThis->pOps = pIoStreamOps;
2589
2590 *phVfsIos = pThis;
2591 *ppvInstance = pThis->Base.pvThis;
2592 return VINF_SUCCESS;
2593}
2594
2595
2596RTDECL(void *) RTVfsIoStreamToPrivate(RTVFSIOSTREAM hVfsIos, PCRTVFSIOSTREAMOPS pIoStreamOps)
2597{
2598 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2599 AssertPtrReturn(pThis, NULL);
2600 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, NULL);
2601 if (pThis->pOps != pIoStreamOps)
2602 return NULL;
2603 return pThis->Base.pvThis;
2604}
2605
2606
2607#ifdef DEBUG
2608# undef RTVfsIoStrmRetain
2609#endif
2610RTDECL(uint32_t) RTVfsIoStrmRetain(RTVFSIOSTREAM hVfsIos)
2611{
2612 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2613 AssertPtrReturn(pThis, UINT32_MAX);
2614 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
2615 return rtVfsObjRetain(&pThis->Base);
2616}
2617#ifdef DEBUG
2618# define RTVfsIoStrmRetain(hVfsIos) RTVfsIoStrmRetainDebug(hVfsIos, RT_SRC_POS)
2619#endif
2620
2621
2622RTDECL(uint32_t) RTVfsIoStrmRetainDebug(RTVFSIOSTREAM hVfsIos, RT_SRC_POS_DECL)
2623{
2624 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2625 AssertPtrReturn(pThis, UINT32_MAX);
2626 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
2627 return rtVfsObjRetainDebug(&pThis->Base, "RTVfsIoStrmRetainDebug", RT_SRC_POS_ARGS);
2628}
2629
2630
2631RTDECL(uint32_t) RTVfsIoStrmRelease(RTVFSIOSTREAM hVfsIos)
2632{
2633 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2634 if (pThis == NIL_RTVFSIOSTREAM)
2635 return 0;
2636 AssertPtrReturn(pThis, UINT32_MAX);
2637 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
2638 return rtVfsObjRelease(&pThis->Base);
2639}
2640
2641
2642RTDECL(RTVFSFILE) RTVfsIoStrmToFile(RTVFSIOSTREAM hVfsIos)
2643{
2644 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2645 AssertPtrReturn(pThis, NIL_RTVFSFILE);
2646 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, NIL_RTVFSFILE);
2647
2648 if (pThis->pOps->Obj.enmType == RTVFSOBJTYPE_FILE)
2649 {
2650 rtVfsObjRetainVoid(&pThis->Base, "RTVfsIoStrmToFile");
2651 return RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream);
2652 }
2653
2654 /* this is no crime, so don't assert. */
2655 return NIL_RTVFSFILE;
2656}
2657
2658
2659RTDECL(int) RTVfsIoStrmQueryInfo(RTVFSIOSTREAM hVfsIos, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2660{
2661 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2662 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2663 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2664 return RTVfsObjQueryInfo(&pThis->Base, pObjInfo, enmAddAttr);
2665}
2666
2667
2668RTDECL(int) RTVfsIoStrmRead(RTVFSIOSTREAM hVfsIos, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead)
2669{
2670 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
2671 if (pcbRead)
2672 *pcbRead = 0;
2673 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2674 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2675 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2676 AssertReturn(fBlocking || pcbRead, VERR_INVALID_PARAMETER);
2677 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_ACCESS_DENIED);
2678
2679 RTSGSEG Seg = { pvBuf, cbToRead };
2680 RTSGBUF SgBuf;
2681 RTSgBufInit(&SgBuf, &Seg, 1);
2682
2683 RTVfsLockAcquireWrite(pThis->Base.hLock);
2684 int rc = pThis->pOps->pfnRead(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbRead);
2685 RTVfsLockReleaseWrite(pThis->Base.hLock);
2686 return rc;
2687}
2688
2689
2690RTDECL(int) RTVfsIoStrmReadAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, void *pvBuf, size_t cbToRead,
2691 bool fBlocking, size_t *pcbRead)
2692{
2693 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
2694 if (pcbRead)
2695 *pcbRead = 0;
2696 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2697 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2698 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2699 AssertReturn(fBlocking || pcbRead, VERR_INVALID_PARAMETER);
2700 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_ACCESS_DENIED);
2701
2702 RTSGSEG Seg = { pvBuf, cbToRead };
2703 RTSGBUF SgBuf;
2704 RTSgBufInit(&SgBuf, &Seg, 1);
2705
2706 RTVfsLockAcquireWrite(pThis->Base.hLock);
2707 int rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbRead);
2708 RTVfsLockReleaseWrite(pThis->Base.hLock);
2709 return rc;
2710}
2711
2712
2713RTDECL(int) RTVfsIoStrmWrite(RTVFSIOSTREAM hVfsIos, const void *pvBuf, size_t cbToWrite, bool fBlocking, size_t *pcbWritten)
2714{
2715 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
2716 if (pcbWritten)
2717 *pcbWritten = 0;
2718 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2719 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2720 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2721 AssertReturn(fBlocking || pcbWritten, VERR_INVALID_PARAMETER);
2722 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
2723
2724 RTSGSEG Seg = { (void *)pvBuf, cbToWrite };
2725 RTSGBUF SgBuf;
2726 RTSgBufInit(&SgBuf, &Seg, 1);
2727
2728 RTVfsLockAcquireWrite(pThis->Base.hLock);
2729 int rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbWritten);
2730 RTVfsLockReleaseWrite(pThis->Base.hLock);
2731 return rc;
2732}
2733
2734
2735RTDECL(int) RTVfsIoStrmWriteAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, const void *pvBuf, size_t cbToWrite,
2736 bool fBlocking, size_t *pcbWritten)
2737{
2738 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
2739 if (pcbWritten)
2740 *pcbWritten = 0;
2741 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2742 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2743 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2744 AssertReturn(fBlocking || pcbWritten, VERR_INVALID_PARAMETER);
2745 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
2746
2747 RTSGSEG Seg = { (void *)pvBuf, cbToWrite };
2748 RTSGBUF SgBuf;
2749 RTSgBufInit(&SgBuf, &Seg, 1);
2750
2751 RTVfsLockAcquireWrite(pThis->Base.hLock);
2752 int rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbWritten);
2753 RTVfsLockReleaseWrite(pThis->Base.hLock);
2754 return rc;
2755}
2756
2757
2758RTDECL(int) RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
2759{
2760 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
2761 if (pcbRead)
2762 *pcbRead = 0;
2763 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2764 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2765 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2766 AssertPtr(pSgBuf);
2767 AssertReturn(fBlocking || pcbRead, VERR_INVALID_PARAMETER);
2768 AssertReturn(pThis->fFlags & RTFILE_O_READ, VERR_ACCESS_DENIED);
2769
2770 RTVfsLockAcquireWrite(pThis->Base.hLock);
2771 int rc;
2772 if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
2773 rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbRead);
2774 else
2775 {
2776 size_t cbRead = 0;
2777 rc = VINF_SUCCESS;
2778
2779 for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
2780 {
2781 RTSGBUF SgBuf;
2782 RTSgBufInit(&SgBuf, &pSgBuf->paSegs[iSeg], 1);
2783
2784 size_t cbReadSeg = pcbRead ? 0 : pSgBuf->paSegs[iSeg].cbSeg;
2785 rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbRead ? &cbReadSeg : NULL);
2786 if (RT_FAILURE(rc))
2787 break;
2788 cbRead += cbReadSeg;
2789 if ((pcbRead && cbReadSeg != SgBuf.paSegs[0].cbSeg) || rc != VINF_SUCCESS)
2790 break;
2791 if (off != -1)
2792 off += cbReadSeg;
2793 }
2794
2795 if (pcbRead)
2796 *pcbRead = cbRead;
2797 }
2798 RTVfsLockReleaseWrite(pThis->Base.hLock);
2799 return rc;
2800}
2801
2802
2803RTDECL(int) RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
2804{
2805 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
2806 if (pcbWritten)
2807 *pcbWritten = 0;
2808 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2809 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2810 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2811 AssertPtr(pSgBuf);
2812 AssertReturn(fBlocking || pcbWritten, VERR_INVALID_PARAMETER);
2813 AssertReturn(pThis->fFlags & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
2814
2815 RTVfsLockAcquireWrite(pThis->Base.hLock);
2816 int rc;
2817 if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
2818 rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbWritten);
2819 else
2820 {
2821 size_t cbWritten = 0;
2822 rc = VINF_SUCCESS;
2823
2824 for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
2825 {
2826 RTSGBUF SgBuf;
2827 RTSgBufInit(&SgBuf, &pSgBuf->paSegs[iSeg], 1);
2828
2829 size_t cbWrittenSeg = 0;
2830 rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbWritten ? &cbWrittenSeg : NULL);
2831 if (RT_FAILURE(rc))
2832 break;
2833 if (pcbWritten)
2834 {
2835 cbWritten += cbWrittenSeg;
2836 if (cbWrittenSeg != SgBuf.paSegs[0].cbSeg)
2837 break;
2838 if (off != -1)
2839 off += cbWrittenSeg;
2840 }
2841 else if (off != -1)
2842 off += pSgBuf->paSegs[iSeg].cbSeg;
2843 }
2844
2845 if (pcbWritten)
2846 *pcbWritten = cbWritten;
2847 }
2848 RTVfsLockReleaseWrite(pThis->Base.hLock);
2849 return rc;
2850}
2851
2852
2853RTDECL(int) RTVfsIoStrmFlush(RTVFSIOSTREAM hVfsIos)
2854{
2855 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2856 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2857 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2858
2859 RTVfsLockAcquireWrite(pThis->Base.hLock);
2860 int rc = pThis->pOps->pfnFlush(pThis->Base.pvThis);
2861 RTVfsLockReleaseWrite(pThis->Base.hLock);
2862 return rc;
2863}
2864
2865
2866RTDECL(int) RTVfsIoStrmPoll(RTVFSIOSTREAM hVfsIos, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
2867 uint32_t *pfRetEvents)
2868{
2869 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2870 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2871 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
2872
2873 RTVfsLockAcquireWrite(pThis->Base.hLock);
2874 int rc = pThis->pOps->pfnPollOne(pThis->Base.pvThis, fEvents, cMillies, fIntr, pfRetEvents);
2875 RTVfsLockReleaseWrite(pThis->Base.hLock);
2876 return rc;
2877}
2878
2879
2880RTDECL(RTFOFF) RTVfsIoStrmTell(RTVFSIOSTREAM hVfsIos)
2881{
2882 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2883 AssertPtrReturn(pThis, -1);
2884 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
2885
2886 RTFOFF off;
2887 RTVfsLockAcquireRead(pThis->Base.hLock);
2888 int rc = pThis->pOps->pfnTell(pThis->Base.pvThis, &off);
2889 RTVfsLockReleaseRead(pThis->Base.hLock);
2890 if (RT_FAILURE(rc))
2891 off = rc;
2892 return off;
2893}
2894
2895
2896RTDECL(int) RTVfsIoStrmSkip(RTVFSIOSTREAM hVfsIos, RTFOFF cb)
2897{
2898 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2899 AssertPtrReturn(pThis, -1);
2900 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
2901 AssertReturn(cb >= 0, VERR_INVALID_PARAMETER);
2902
2903 int rc;
2904 if (pThis->pOps->pfnSkip)
2905 {
2906 RTVfsLockAcquireWrite(pThis->Base.hLock);
2907 rc = pThis->pOps->pfnSkip(pThis->Base.pvThis, cb);
2908 RTVfsLockReleaseWrite(pThis->Base.hLock);
2909 }
2910 else if (pThis->pOps->Obj.enmType == RTVFSOBJTYPE_FILE)
2911 {
2912 RTVFSFILEINTERNAL *pThisFile = RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream);
2913 RTFOFF offIgnored;
2914
2915 RTVfsLockAcquireWrite(pThis->Base.hLock);
2916 rc = pThisFile->pOps->pfnSeek(pThis->Base.pvThis, cb, RTFILE_SEEK_CURRENT, &offIgnored);
2917 RTVfsLockReleaseWrite(pThis->Base.hLock);
2918 }
2919 else
2920 {
2921 void *pvBuf = RTMemTmpAlloc(_64K);
2922 if (pvBuf)
2923 {
2924 rc = VINF_SUCCESS;
2925 while (cb > 0)
2926 {
2927 size_t cbToRead = (size_t)RT_MIN(cb, _64K);
2928 RTVfsLockAcquireWrite(pThis->Base.hLock);
2929 rc = RTVfsIoStrmRead(hVfsIos, pvBuf, cbToRead, true /*fBlocking*/, NULL);
2930 RTVfsLockReleaseWrite(pThis->Base.hLock);
2931 if (RT_FAILURE(rc))
2932 break;
2933 cb -= cbToRead;
2934 }
2935
2936 RTMemTmpFree(pvBuf);
2937 }
2938 else
2939 rc = VERR_NO_TMP_MEMORY;
2940 }
2941 return rc;
2942}
2943
2944
2945RTDECL(int) RTVfsIoStrmZeroFill(RTVFSIOSTREAM hVfsIos, RTFOFF cb)
2946{
2947 RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
2948 AssertPtrReturn(pThis, -1);
2949 AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
2950
2951 int rc;
2952 if (pThis->pOps->pfnSkip)
2953 {
2954 RTVfsLockAcquireWrite(pThis->Base.hLock);
2955 rc = pThis->pOps->pfnZeroFill(pThis->Base.pvThis, cb);
2956 RTVfsLockReleaseWrite(pThis->Base.hLock);
2957 }
2958 else
2959 {
2960 void *pvBuf = RTMemTmpAllocZ(_64K);
2961 if (pvBuf)
2962 {
2963 rc = VINF_SUCCESS;
2964 while (cb > 0)
2965 {
2966 size_t cbToWrite = (size_t)RT_MIN(cb, _64K);
2967 RTVfsLockAcquireWrite(pThis->Base.hLock);
2968 rc = RTVfsIoStrmWrite(hVfsIos, pvBuf, cbToWrite, true /*fBlocking*/, NULL);
2969 RTVfsLockReleaseWrite(pThis->Base.hLock);
2970 if (RT_FAILURE(rc))
2971 break;
2972 cb -= cbToWrite;
2973 }
2974
2975 RTMemTmpFree(pvBuf);
2976 }
2977 else
2978 rc = VERR_NO_TMP_MEMORY;
2979 }
2980 return rc;
2981}
2982
2983
2984RTDECL(bool) RTVfsIoStrmIsAtEnd(RTVFSIOSTREAM hVfsIos)
2985{
2986 /*
2987 * There is where the zero read behavior comes in handy.
2988 */
2989 char bDummy;
2990 size_t cbRead;
2991 int rc = RTVfsIoStrmRead(hVfsIos, &bDummy, 0 /*cbToRead*/, false /*fBlocking*/, &cbRead);
2992 return rc == VINF_EOF;
2993}
2994
2995
2996
2997
2998
2999
3000/*
3001 *
3002 * F I L E F I L E F I L E
3003 * F I L E F I L E F I L E
3004 * F I L E F I L E F I L E
3005 *
3006 */
3007
3008RTDECL(int) RTVfsNewFile(PCRTVFSFILEOPS pFileOps, size_t cbInstance, uint32_t fOpen, RTVFS hVfs, RTVFSLOCK hLock,
3009 PRTVFSFILE phVfsFile, void **ppvInstance)
3010{
3011 /*
3012 * Validate the input, be extra strict in strict builds.
3013 */
3014 AssertPtr(pFileOps);
3015 AssertReturn(pFileOps->uVersion == RTVFSFILEOPS_VERSION, VERR_VERSION_MISMATCH);
3016 AssertReturn(pFileOps->uEndMarker == RTVFSFILEOPS_VERSION, VERR_VERSION_MISMATCH);
3017 Assert(!pFileOps->fReserved);
3018 RTVFSIOSTREAM_ASSERT_OPS(&pFileOps->Stream, RTVFSOBJTYPE_FILE);
3019 Assert(cbInstance > 0);
3020 Assert(fOpen & RTFILE_O_ACCESS_MASK);
3021 AssertPtr(ppvInstance);
3022 AssertPtr(phVfsFile);
3023 RTVFS_ASSERT_VALID_HANDLE_OR_NIL_RETURN(hVfs, VERR_INVALID_HANDLE);
3024
3025 /*
3026 * Allocate the handle + instance data.
3027 */
3028 size_t const cbThis = RT_ALIGN_Z(sizeof(RTVFSFILEINTERNAL), RTVFS_INST_ALIGNMENT)
3029 + RT_ALIGN_Z(cbInstance, RTVFS_INST_ALIGNMENT);
3030 RTVFSFILEINTERNAL *pThis = (RTVFSFILEINTERNAL *)RTMemAllocZ(cbThis);
3031 if (!pThis)
3032 return VERR_NO_MEMORY;
3033
3034 int rc = rtVfsObjInitNewObject(&pThis->Stream.Base, &pFileOps->Stream.Obj, hVfs, false /*fNoVfsRef*/, hLock,
3035 (char *)pThis + RT_ALIGN_Z(sizeof(*pThis), RTVFS_INST_ALIGNMENT));
3036 if (RT_FAILURE(rc))
3037 {
3038 RTMemFree(pThis);
3039 return rc;
3040 }
3041
3042 pThis->uMagic = RTVFSFILE_MAGIC;
3043 pThis->fReserved = 0;
3044 pThis->pOps = pFileOps;
3045 pThis->Stream.uMagic = RTVFSIOSTREAM_MAGIC;
3046 pThis->Stream.fFlags = fOpen;
3047 pThis->Stream.pOps = &pFileOps->Stream;
3048
3049 *phVfsFile = pThis;
3050 *ppvInstance = pThis->Stream.Base.pvThis;
3051 return VINF_SUCCESS;
3052}
3053
3054
3055RTDECL(int) RTVfsFileOpen(RTVFS hVfs, const char *pszFilename, uint64_t fOpen, PRTVFSFILE phVfsFile)
3056{
3057 /*
3058 * Validate input.
3059 */
3060 RTVFSINTERNAL *pThis = hVfs;
3061 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3062 AssertReturn(pThis->uMagic == RTVFS_MAGIC, VERR_INVALID_HANDLE);
3063 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
3064 AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
3065
3066 int rc = rtFileRecalcAndValidateFlags(&fOpen);
3067 if (RT_FAILURE(rc))
3068 return rc;
3069
3070 /*
3071 * Parse the path, assume current directory is root since we've got no
3072 * caller context here.
3073 */
3074 PRTVFSPARSEDPATH pPath;
3075 rc = RTVfsParsePathA(pszFilename, "/", &pPath);
3076 if (RT_SUCCESS(rc))
3077 {
3078 if ( !pPath->fDirSlash
3079 && pPath->cComponents > 0)
3080 {
3081 /*
3082 * Tranverse the path, resolving the parent node and any symlinks
3083 * in the final element, and ask the directory to open the file.
3084 */
3085 RTVFSDIRINTERNAL *pVfsParentDir;
3086 rc = rtVfsTraverseToParent(pThis, pPath, RTPATH_F_FOLLOW_LINK, &pVfsParentDir);
3087 if (RT_SUCCESS(rc))
3088 {
3089 const char *pszEntryName = &pPath->szPath[pPath->aoffComponents[pPath->cComponents - 1]];
3090
3091 /** @todo there is a symlink creation race here. */
3092 RTVfsLockAcquireWrite(pVfsParentDir->Base.hLock);
3093 rc = pVfsParentDir->pOps->pfnOpenFile(pVfsParentDir->Base.pvThis, pszEntryName, fOpen, phVfsFile);
3094 RTVfsLockReleaseWrite(pVfsParentDir->Base.hLock);
3095
3096 RTVfsDirRelease(pVfsParentDir);
3097
3098 if (RT_SUCCESS(rc))
3099 {
3100 AssertPtr(*phVfsFile);
3101 Assert((*phVfsFile)->uMagic == RTVFSFILE_MAGIC);
3102 }
3103 }
3104 }
3105 else
3106 rc = VERR_NOT_A_FILE;
3107 RTVfsParsePathFree(pPath);
3108 }
3109 return rc;
3110}
3111
3112
3113#ifdef DEBUG
3114# undef RTVfsFileRetain
3115#endif
3116RTDECL(uint32_t) RTVfsFileRetain(RTVFSFILE hVfsFile)
3117{
3118 RTVFSFILEINTERNAL *pThis = hVfsFile;
3119 AssertPtrReturn(pThis, UINT32_MAX);
3120 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, UINT32_MAX);
3121 return rtVfsObjRetain(&pThis->Stream.Base);
3122}
3123#ifdef DEBUG
3124# define RTVfsFileRetain(hVfsFile) RTVfsFileRetainDebug(hVfsFile, RT_SRC_POS)
3125#endif
3126
3127
3128RTDECL(uint32_t) RTVfsFileRetainDebug(RTVFSFILE hVfsFile, RT_SRC_POS_DECL)
3129{
3130 RTVFSFILEINTERNAL *pThis = hVfsFile;
3131 AssertPtrReturn(pThis, UINT32_MAX);
3132 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, UINT32_MAX);
3133 return rtVfsObjRetainDebug(&pThis->Stream.Base, "RTVFsFileRetainDebug", RT_SRC_POS_ARGS);
3134}
3135
3136
3137RTDECL(uint32_t) RTVfsFileRelease(RTVFSFILE hVfsFile)
3138{
3139 RTVFSFILEINTERNAL *pThis = hVfsFile;
3140 if (pThis == NIL_RTVFSFILE)
3141 return 0;
3142 AssertPtrReturn(pThis, UINT32_MAX);
3143 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, UINT32_MAX);
3144 return rtVfsObjRelease(&pThis->Stream.Base);
3145}
3146
3147
3148RTDECL(RTVFSIOSTREAM) RTVfsFileToIoStream(RTVFSFILE hVfsFile)
3149{
3150 RTVFSFILEINTERNAL *pThis = hVfsFile;
3151 AssertPtrReturn(pThis, NIL_RTVFSIOSTREAM);
3152 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, NIL_RTVFSIOSTREAM);
3153
3154 rtVfsObjRetainVoid(&pThis->Stream.Base, "RTVfsFileToIoStream");
3155 return &pThis->Stream;
3156}
3157
3158
3159RTDECL(int) RTVfsFileQueryInfo(RTVFSFILE hVfsFile, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
3160{
3161 RTVFSFILEINTERNAL *pThis = hVfsFile;
3162 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3163 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3164 return RTVfsObjQueryInfo(&pThis->Stream.Base, pObjInfo, enmAddAttr);
3165}
3166
3167
3168RTDECL(int) RTVfsFileRead(RTVFSFILE hVfsFile, void *pvBuf, size_t cbToRead, size_t *pcbRead)
3169{
3170 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
3171 if (pcbRead)
3172 *pcbRead = 0;
3173 RTVFSFILEINTERNAL *pThis = hVfsFile;
3174 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3175 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3176 return RTVfsIoStrmRead(&pThis->Stream, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
3177}
3178
3179
3180RTDECL(int) RTVfsFileWrite(RTVFSFILE hVfsFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
3181{
3182 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
3183 if (pcbWritten)
3184 *pcbWritten = 0;
3185 RTVFSFILEINTERNAL *pThis = hVfsFile;
3186 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3187 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3188 return RTVfsIoStrmWrite(&pThis->Stream, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
3189}
3190
3191
3192RTDECL(int) RTVfsFileWriteAt(RTVFSFILE hVfsFile, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
3193{
3194 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
3195 if (pcbWritten)
3196 *pcbWritten = 0;
3197 RTVFSFILEINTERNAL *pThis = hVfsFile;
3198 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3199 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3200
3201 int rc = RTVfsFileSeek(hVfsFile, off, RTFILE_SEEK_BEGIN, NULL);
3202 if (RT_SUCCESS(rc))
3203 rc = RTVfsIoStrmWriteAt(&pThis->Stream, off, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
3204
3205 return rc;
3206}
3207
3208
3209RTDECL(int) RTVfsFileReadAt(RTVFSFILE hVfsFile, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
3210{
3211 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
3212 if (pcbRead)
3213 *pcbRead = 0;
3214 RTVFSFILEINTERNAL *pThis = hVfsFile;
3215 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3216 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3217
3218 int rc = RTVfsFileSeek(hVfsFile, off, RTFILE_SEEK_BEGIN, NULL);
3219 if (RT_SUCCESS(rc))
3220 rc = RTVfsIoStrmReadAt(&pThis->Stream, off, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
3221
3222 return rc;
3223}
3224
3225
3226RTDECL(int) RTVfsFileSgRead(RTVFSFILE hVfsFile, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
3227{
3228 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
3229 if (pcbRead)
3230 *pcbRead = 0;
3231 RTVFSFILEINTERNAL *pThis = hVfsFile;
3232 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3233 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3234
3235 return RTVfsIoStrmSgRead(&pThis->Stream, off, pSgBuf, fBlocking, pcbRead);
3236}
3237
3238
3239RTDECL(int) RTVfsFileSgWrite(RTVFSFILE hVfsFile, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
3240{
3241 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
3242 if (pcbWritten)
3243 *pcbWritten = 0;
3244 RTVFSFILEINTERNAL *pThis = hVfsFile;
3245 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3246 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3247
3248 return RTVfsIoStrmSgWrite(&pThis->Stream, off, pSgBuf, fBlocking, pcbWritten);
3249}
3250
3251
3252RTDECL(int) RTVfsFileFlush(RTVFSFILE hVfsFile)
3253{
3254 RTVFSFILEINTERNAL *pThis = hVfsFile;
3255 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3256 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3257 return RTVfsIoStrmFlush(&pThis->Stream);
3258}
3259
3260
3261RTDECL(RTFOFF) RTVfsFilePoll(RTVFSFILE hVfsFile, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
3262 uint32_t *pfRetEvents)
3263{
3264 RTVFSFILEINTERNAL *pThis = hVfsFile;
3265 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3266 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3267 return RTVfsIoStrmPoll(&pThis->Stream, fEvents, cMillies, fIntr, pfRetEvents);
3268}
3269
3270
3271RTDECL(RTFOFF) RTVfsFileTell(RTVFSFILE hVfsFile)
3272{
3273 RTVFSFILEINTERNAL *pThis = hVfsFile;
3274 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3275 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3276 return RTVfsIoStrmTell(&pThis->Stream);
3277}
3278
3279
3280RTDECL(int) RTVfsFileSeek(RTVFSFILE hVfsFile, RTFOFF offSeek, uint32_t uMethod, uint64_t *poffActual)
3281{
3282 RTVFSFILEINTERNAL *pThis = hVfsFile;
3283 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3284 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3285
3286 AssertReturn( uMethod == RTFILE_SEEK_BEGIN
3287 || uMethod == RTFILE_SEEK_CURRENT
3288 || uMethod == RTFILE_SEEK_END, VERR_INVALID_PARAMETER);
3289 AssertPtrNullReturn(poffActual, VERR_INVALID_POINTER);
3290
3291 RTFOFF offActual = 0;
3292 RTVfsLockAcquireWrite(pThis->Stream.Base.hLock);
3293 int rc = pThis->pOps->pfnSeek(pThis->Stream.Base.pvThis, offSeek, uMethod, &offActual);
3294 RTVfsLockReleaseWrite(pThis->Stream.Base.hLock);
3295 if (RT_SUCCESS(rc) && poffActual)
3296 {
3297 Assert(offActual >= 0);
3298 *poffActual = offActual;
3299 }
3300
3301 return rc;
3302}
3303
3304
3305RTDECL(int) RTVfsFileGetSize(RTVFSFILE hVfsFile, uint64_t *pcbSize)
3306{
3307 RTVFSFILEINTERNAL *pThis = hVfsFile;
3308 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
3309 AssertReturn(pThis->uMagic == RTVFSFILE_MAGIC, VERR_INVALID_HANDLE);
3310 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
3311
3312 RTVfsLockAcquireWrite(pThis->Stream.Base.hLock);
3313 int rc = pThis->pOps->pfnQuerySize(pThis->Stream.Base.pvThis, pcbSize);
3314 RTVfsLockReleaseWrite(pThis->Stream.Base.hLock);
3315
3316 return rc;
3317}
Note: See TracBrowser for help on using the repository browser.

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