VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/vfs/vfsmount.cpp

Last change on this file was 103012, checked in by vboxsync, 4 months ago

iprt/asm.h,*: Split out the ASMMem* and related stuff into a separate header, asm-mem.h, so that we can get the RT_ASM_PAGE_SIZE stuff out of the way. [build fixes]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1/* $Id: vfsmount.cpp 103012 2024-01-24 00:38:46Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, Mounting.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_VFS
42#include <iprt/vfs.h>
43
44#define RT_ASM_INCLUDE_PAGE_SIZE
45#include <iprt/asm-mem.h>
46#include <iprt/asm.h> /* ASMByteSwapU32/U16 on sparc */
47#include <iprt/assert.h>
48#include <iprt/err.h>
49#include <iprt/file.h>
50#include <iprt/fsvfs.h>
51#include <iprt/mem.h>
52#include <iprt/log.h>
53#include <iprt/string.h>
54#include <iprt/vfslowlevel.h>
55
56#include <iprt/formats/fat.h>
57#include <iprt/formats/iso9660.h>
58#include <iprt/formats/udf.h>
59#include <iprt/formats/ext.h>
60
61
62/*********************************************************************************************************************************
63* Structures and Typedefs *
64*********************************************************************************************************************************/
65/** Buffer structure for the detection routines. */
66typedef union RTVFSMOUNTBUF
67{
68 uint8_t ab[2048];
69 uint32_t au32[2048/4];
70 FATBOOTSECTOR Bootsector;
71 ISO9660VOLDESCHDR IsoHdr;
72} RTVFSMOUNTBUF;
73AssertCompileSize(RTVFSMOUNTBUF, 2048);
74typedef RTVFSMOUNTBUF *PRTVFSMOUNTBUF;
75
76
77
78/**
79 * Checks if the given 2K sector at offset 32KB looks like ISO-9660 or UDF.
80 *
81 * @returns true if likely ISO or UDF, otherwise false.
82 * @param pVolDescHdr Whatever is at offset 32KB. 2KB buffer.
83 */
84static bool rtVfsMountIsIsoFs(PCISO9660VOLDESCHDR pVolDescHdr)
85{
86 if ( memcmp(pVolDescHdr->achStdId, RT_STR_TUPLE(ISO9660VOLDESC_STD_ID)) == 0
87 && pVolDescHdr->bDescType <= ISO9660VOLDESC_TYPE_PARTITION
88 && pVolDescHdr->bDescVersion != 0
89 && pVolDescHdr->bDescVersion <= 3 /* don't be too picky, just increase the likelyhood */ )
90 return true;
91
92 if ( memcmp(pVolDescHdr->achStdId, RT_STR_TUPLE(UDF_EXT_VOL_DESC_STD_ID_BEGIN)) == 0
93 && pVolDescHdr->bDescType == UDF_EXT_VOL_DESC_TYPE
94 && pVolDescHdr->bDescVersion == UDF_EXT_VOL_DESC_VERSION)
95 return true;
96
97 return false;
98}
99
100
101/**
102 * Check if the given bootsector is a NTFS boot sector.
103 *
104 * @returns true if NTFS, false if not.
105 * @param pBootSector The boot sector to inspect.
106 */
107static bool rtVfsMountIsNtfs(PCFATBOOTSECTOR pBootSector)
108{
109 if (memcmp(pBootSector->achOemName, RT_STR_TUPLE("NTFS ")) != 0)
110 return false;
111
112 uint16_t cbSector = RT_LE2H_U16(pBootSector->Bpb.Bpb331.cbSector);
113 if ( cbSector < 0x100
114 || cbSector >= 0x1000
115 || (cbSector & 0xff) != 0)
116 {
117 Log2(("rtVfsMountIsNtfs: cbSector=%#x: out of range\n", cbSector));
118 return false;
119 }
120
121 if ( !RT_IS_POWER_OF_TWO(pBootSector->Bpb.Bpb331.cSectorsPerCluster)
122 || pBootSector->Bpb.Bpb331.cSectorsPerCluster == 0
123 || pBootSector->Bpb.Bpb331.cSectorsPerCluster > 128)
124 {
125 Log2(("rtVfsMountIsNtfs: cSectorsPerCluster=%#x: out of range\n", pBootSector->Bpb.Bpb331.cSectorsPerCluster));
126 return false;
127 }
128
129 if ((uint32_t)pBootSector->Bpb.Bpb331.cSectorsPerCluster * cbSector > _64K)
130 {
131 Log2(("rtVfsMountIsNtfs: cSectorsPerCluster=%#x * cbSector=%#x => %#x: out of range\n",
132 pBootSector->Bpb.Bpb331.cSectorsPerCluster, cbSector,
133 (uint32_t)pBootSector->Bpb.Bpb331.cSectorsPerCluster * cbSector));
134 return false;
135 }
136
137 if ( pBootSector->Bpb.Bpb331.cReservedSectors != 0
138 || pBootSector->Bpb.Bpb331.cMaxRootDirEntries != 0
139 || pBootSector->Bpb.Bpb331.cTotalSectors16 != 0
140 || pBootSector->Bpb.Bpb331.cTotalSectors32 != 0
141 || pBootSector->Bpb.Bpb331.cSectorsPerFat != 0
142 || pBootSector->Bpb.Bpb331.cFats != 0)
143 {
144 Log2(("rtVfsMountIsNtfs: cReservedSectors=%#x cMaxRootDirEntries=%#x cTotalSectors=%#x cTotalSectors32=%#x cSectorsPerFat=%#x cFats=%#x: should all be zero, but one or more aren't\n",
145 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cReservedSectors),
146 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cMaxRootDirEntries),
147 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cTotalSectors16),
148 RT_LE2H_U32(pBootSector->Bpb.Bpb331.cTotalSectors32),
149 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cSectorsPerFat),
150 pBootSector->Bpb.Bpb331.cFats));
151 return false;
152 }
153
154 /** @todo NTFS specific checks: MFT cluster number, cluster per index block. */
155
156 return true;
157}
158
159
160/**
161 * Check if the given bootsector is a HPFS boot sector.
162 *
163 * @returns true if NTFS, false if not.
164 * @param pBootSector The boot sector to inspect.
165 * @param hVfsFileIn The volume file.
166 * @param pBuf2 A 2nd buffer.
167 */
168static bool rtVfsMountIsHpfs(PCFATBOOTSECTOR pBootSector, RTVFSFILE hVfsFileIn, PRTVFSMOUNTBUF pBuf2)
169{
170 if (memcmp(pBootSector->Bpb.Ebpb.achType, RT_STR_TUPLE("HPFS ")) != 0)
171 return false;
172
173 /* Superblock is at sector 16, spare superblock at 17. */
174 int rc = RTVfsFileReadAt(hVfsFileIn, 16 * 512, pBuf2, 512 * 2, NULL);
175 if (RT_FAILURE(rc))
176 {
177 Log2(("rtVfsMountIsHpfs: Error reading superblock: %Rrc\n", rc));
178 return false;
179 }
180
181 if ( RT_LE2H_U32(pBuf2->au32[0]) != UINT32_C(0xf995e849)
182 || RT_LE2H_U32(pBuf2->au32[1]) != UINT32_C(0xfa53e9c5)
183 || RT_LE2H_U32(pBuf2->au32[512/4 + 0]) != UINT32_C(0xf9911849)
184 || RT_LE2H_U32(pBuf2->au32[512/4 + 1]) != UINT32_C(0xfa5229c5))
185 {
186 Log2(("rtVfsMountIsHpfs: Superblock or spare superblock signature mismatch: %#x %#x %#x %#x\n",
187 RT_LE2H_U32(pBuf2->au32[0]), RT_LE2H_U32(pBuf2->au32[1]),
188 RT_LE2H_U32(pBuf2->au32[512/4 + 0]), RT_LE2H_U32(pBuf2->au32[512/4 + 1]) ));
189 return false;
190 }
191
192 return true;
193}
194
195
196/**
197 * Check if the given bootsector is a FAT boot sector.
198 *
199 * @returns true if NTFS, false if not.
200 * @param pBootSector The boot sector to inspect.
201 * @param pbRaw Pointer to the raw boot sector buffer.
202 * @param cbRaw Number of bytes read starting with the boot
203 * sector (which @a pbRaw points to).
204 * @param hVfsFileIn The volume file.
205 * @param pBuf2 A 2nd buffer.
206 */
207static bool rtVfsMountIsFat(PCFATBOOTSECTOR pBootSector, uint8_t const *pbRaw, size_t cbRaw,
208 RTVFSFILE hVfsFileIn, PRTVFSMOUNTBUF pBuf2)
209{
210 Assert(cbRaw >= 1024);
211
212 /*
213 * Check the DOS signature first. The PC-DOS 1.0 boot floppy does not have
214 * a signature and we ASSUME this is the case for all floppies formated by it.
215 */
216 if (pBootSector->uSignature != FATBOOTSECTOR_SIGNATURE)
217 {
218 if (pBootSector->uSignature != 0)
219 return false;
220
221 /*
222 * PC-DOS 1.0 does a 2fh byte short jump w/o any NOP following it.
223 * Instead the following are three words and a 9 byte build date
224 * string. The remaining space is zero filled.
225 *
226 * Note! No idea how this would look like for 8" floppies, only got 5"1/4'.
227 *
228 * ASSUME all non-BPB disks are using this format.
229 */
230 if ( pBootSector->abJmp[0] != 0xeb /* jmp rel8 */
231 || pBootSector->abJmp[1] < 0x2f
232 || pBootSector->abJmp[1] >= 0x80
233 || pBootSector->abJmp[2] == 0x90 /* nop */)
234 {
235 Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - invalid jmp: %.3Rhxs\n", pBootSector->abJmp));
236 return false;
237 }
238
239 /* Check the FAT ID so we can tell if this is double or single sided, as well as being a valid FAT12 start. */
240 if ( (pbRaw[512] != 0xfe && pbRaw[0] != 0xff)
241 || pbRaw[512 + 1] != 0xff
242 || pbRaw[512 + 2] != 0xff)
243 {
244 Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - unexpected start of FAT: %.3Rhxs\n", &pbRaw[512]));
245 return false;
246 }
247
248 uint32_t const offJump = 2 + pBootSector->abJmp[1];
249 uint32_t const offFirstZero = 2 /*jmp */ + 3 * 2 /* words */ + 9 /* date string */;
250 Assert(offFirstZero >= RT_UOFFSETOF(FATBOOTSECTOR, Bpb));
251 uint32_t const cbZeroPad = RT_MIN(offJump - offFirstZero,
252 sizeof(pBootSector->Bpb.Bpb20) - (offFirstZero - RT_UOFFSETOF(FATBOOTSECTOR, Bpb)));
253
254 if (!ASMMemIsAllU8((uint8_t const *)pBootSector + offFirstZero, cbZeroPad, 0))
255 {
256 Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - expected zero padding %#x LB %#x: %.*Rhxs\n",
257 offFirstZero, cbZeroPad, cbZeroPad, (uint8_t const *)pBootSector + offFirstZero));
258 return false;
259 }
260 }
261 else
262 {
263 /*
264 * DOS 2.0 or later.
265 *
266 * Start by checking if we've got a known jump instruction first, because
267 * that will give us a max (E)BPB size hint.
268 */
269 uint8_t offJmp = UINT8_MAX;
270 if ( pBootSector->abJmp[0] == 0xeb
271 && pBootSector->abJmp[1] <= 0x7f)
272 offJmp = pBootSector->abJmp[1] + 2;
273 else if ( pBootSector->abJmp[0] == 0x90
274 && pBootSector->abJmp[1] == 0xeb
275 && pBootSector->abJmp[2] <= 0x7f)
276 offJmp = pBootSector->abJmp[2] + 3;
277 else if ( pBootSector->abJmp[0] == 0xe9
278 && pBootSector->abJmp[2] <= 0x7f)
279 offJmp = RT_MIN(127, RT_MAKE_U16(pBootSector->abJmp[1], pBootSector->abJmp[2]));
280 uint8_t const cbMaxBpb = offJmp - RT_UOFFSETOF(FATBOOTSECTOR, Bpb);
281 if (cbMaxBpb < sizeof(FATBPB20))
282 {
283 Log2(("rtVfsMountIsFat: DOS signature, but jmp too short for any BPB: %#x (max %#x BPB)\n", offJmp, cbMaxBpb));
284 return false;
285 }
286
287 if ( pBootSector->Bpb.Bpb20.cFats == 0
288 || pBootSector->Bpb.Bpb20.cFats > 4)
289 {
290 if (pBootSector->Bpb.Bpb20.cFats == 0)
291 Log2(("rtVfsMountIsFat: DOS signature, number of FATs is zero, so not FAT file system\n"));
292 else
293 Log2(("rtVfsMountIsFat: DOS signature, too many FATs: %#x\n", pBootSector->Bpb.Bpb20.cFats));
294 return false;
295 }
296
297 if (!FATBPB_MEDIA_IS_VALID(pBootSector->Bpb.Bpb20.bMedia))
298 {
299 Log2(("rtVfsMountIsFat: DOS signature, invalid media byte: %#x\n", pBootSector->Bpb.Bpb20.bMedia));
300 return false;
301 }
302
303 uint16_t cbSector = RT_LE2H_U16(pBootSector->Bpb.Bpb20.cbSector);
304 if ( cbSector != 512
305 && cbSector != 4096
306 && cbSector != 1024
307 && cbSector != 128)
308 {
309 Log2(("rtVfsMountIsFat: DOS signature, unsupported sector size: %#x\n", cbSector));
310 return false;
311 }
312
313 if ( !RT_IS_POWER_OF_TWO(pBootSector->Bpb.Bpb20.cSectorsPerCluster)
314 || !pBootSector->Bpb.Bpb20.cSectorsPerCluster)
315 {
316 Log2(("rtVfsMountIsFat: DOS signature, cluster size not non-zero power of two: %#x",
317 pBootSector->Bpb.Bpb20.cSectorsPerCluster));
318 return false;
319 }
320
321 uint16_t const cReservedSectors = RT_LE2H_U16(pBootSector->Bpb.Bpb20.cReservedSectors);
322 if ( cReservedSectors == 0
323 || cReservedSectors >= _32K)
324 {
325 Log2(("rtVfsMountIsFat: DOS signature, bogus reserved sector count: %#x\n", cReservedSectors));
326 return false;
327 }
328
329 /*
330 * Match the media byte with the first FAT byte and check that the next
331 * 4 bits are set. (To match further bytes in the FAT we'd need to
332 * determin the FAT type, which is too much hazzle to do here.)
333 */
334 uint8_t const *pbFat;
335 if ((size_t)cReservedSectors * cbSector < cbRaw)
336 pbFat = &pbRaw[cReservedSectors * cbSector];
337 else
338 {
339 int rc = RTVfsFileReadAt(hVfsFileIn, cReservedSectors * cbSector, pBuf2, 512, NULL);
340 if (RT_FAILURE(rc))
341 {
342 Log2(("rtVfsMountIsFat: error reading first FAT sector at %#x: %Rrc\n", cReservedSectors * cbSector, rc));
343 return false;
344 }
345 pbFat = pBuf2->ab;
346 }
347 if (*pbFat != pBootSector->Bpb.Bpb20.bMedia)
348 {
349 Log2(("rtVfsMountIsFat: Media byte and FAT ID mismatch: %#x vs %#x (%.8Rhxs)\n",
350 pbFat[0], pBootSector->Bpb.Bpb20.bMedia, pbFat));
351 return false;
352 }
353 if ((pbFat[1] & 0xf) != 0xf)
354 {
355 Log2(("rtVfsMountIsFat: Media byte and FAT ID mismatch: %#x vs %#x (%.8Rhxs)\n",
356 pbFat[0], pBootSector->Bpb.Bpb20.bMedia, pbFat));
357 return false;
358 }
359 }
360
361 return true;
362}
363
364
365/**
366 * Check if the given bootsector is an ext2/3/4 super block.
367 *
368 * @returns true if NTFS, false if not.
369 * @param pSuperBlock The ext2 superblock.
370 */
371static bool rtVfsMountIsExt(PCEXTSUPERBLOCK pSuperBlock)
372{
373 if (RT_LE2H_U16(pSuperBlock->u16Signature) != EXT_SB_SIGNATURE)
374 return false;
375
376 uint32_t cShift = RT_LE2H_U32(pSuperBlock->cLogBlockSize);
377 if (cShift > 54)
378 {
379 Log2(("rtVfsMountIsExt: cLogBlockSize=%#x: out of range\n", cShift));
380 return false;
381 }
382
383 cShift = RT_LE2H_U32(pSuperBlock->cLogClusterSize);
384 if (cShift > 54)
385 {
386 Log2(("rtVfsMountIsExt: cLogClusterSize=%#x: out of range\n", cShift));
387 return false;
388 }
389
390 /* Some more checks here would be nice actually since a 16-bit word and a
391 couple of field limits doesn't feel all that conclusive. */
392
393 return true;
394}
395
396
397/**
398 * Does the file system detection and mounting.
399 *
400 * Since we only support a handful of file systems at the moment and the
401 * interface isn't yet extensible in any way, we combine the file system
402 * recognition code for all. This reduces the number of reads we need to do and
403 * avoids unnecessary processing.
404 *
405 * @returns IPRT status code.
406 * @param hVfsFileIn The volume file.
407 * @param fFlags RTVFSMTN_F_XXX.
408 * @param pBuf Pointer to the primary buffer
409 * @param pBuf2 Pointer to the secondary buffer.
410 * @param phVfs Where to return the .
411 * @param pErrInfo Where to return additional error information.
412 * Optional.
413 */
414static int rtVfsMountInner(RTVFSFILE hVfsFileIn, uint32_t fFlags, RTVFSMOUNTBUF *pBuf,
415 RTVFSMOUNTBUF *pBuf2, PRTVFS phVfs, PRTERRINFO pErrInfo)
416{
417 AssertCompile(sizeof(*pBuf) >= ISO9660_SECTOR_SIZE);
418
419 /* Start by checking for ISO-9660 and UDFS since these may have confusing
420 data at the start of the volume. */
421 int rc = RTVfsFileReadAt(hVfsFileIn, _32K, pBuf, ISO9660_SECTOR_SIZE, NULL);
422 if (RT_SUCCESS(rc))
423 {
424 if (rtVfsMountIsIsoFs(&pBuf->IsoHdr))
425 {
426 Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
427 return RTFsIso9660VolOpen(hVfsFileIn, 0 /*fFlags*/, phVfs, pErrInfo);
428 }
429 }
430
431 /* Now read the boot sector and whatever the next 1536 bytes may contain.
432 With ext2 superblock at 1024, we can recognize quite a bit thru this read. */
433 rc = RTVfsFileReadAt(hVfsFileIn, 0, pBuf, sizeof(*pBuf), NULL);
434 if (RT_FAILURE(rc))
435 return RTErrInfoSet(pErrInfo, rc, "Error reading boot sector");
436
437 if (rtVfsMountIsNtfs(&pBuf->Bootsector))
438 return RTFsNtfsVolOpen(hVfsFileIn, fFlags, 0 /*fNtfsFlags*/, phVfs, pErrInfo);
439
440 if (rtVfsMountIsHpfs(&pBuf->Bootsector, hVfsFileIn, pBuf2))
441 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "HPFS not yet supported");
442
443 if (rtVfsMountIsFat(&pBuf->Bootsector, pBuf->ab, sizeof(*pBuf), hVfsFileIn, pBuf2))
444 {
445 Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
446 return RTFsFatVolOpen(hVfsFileIn, RT_BOOL(fFlags & RTVFSMNT_F_READ_ONLY), 0 /*offBootSector*/, phVfs, pErrInfo);
447 }
448
449 AssertCompile(sizeof(*pBuf) >= 1024 + sizeof(EXTSUPERBLOCK));
450 if (rtVfsMountIsExt((PCEXTSUPERBLOCK)&pBuf->ab[1024]))
451 {
452 Log(("RTVfsMount: Detected EXT2/3/4.\n"));
453 return RTFsExtVolOpen(hVfsFileIn, fFlags, 0 /*fExt2Flags*/, phVfs, pErrInfo);
454 }
455
456 return VERR_VFS_UNSUPPORTED_FORMAT;
457}
458
459
460RTDECL(int) RTVfsMountVol(RTVFSFILE hVfsFileIn, uint32_t fFlags, PRTVFS phVfs, PRTERRINFO pErrInfo)
461{
462 AssertReturn(!(fFlags & ~RTVFSMNT_F_VALID_MASK), VERR_INVALID_FLAGS);
463 AssertPtrReturn(hVfsFileIn, VERR_INVALID_HANDLE);
464 AssertPtrReturn(phVfs, VERR_INVALID_HANDLE);
465
466 *phVfs = NIL_RTVFS;
467
468 RTVFSMOUNTBUF *pBufs = (RTVFSMOUNTBUF *)RTMemTmpAlloc(sizeof(*pBufs) * 2);
469 AssertReturn(pBufs, VERR_NO_TMP_MEMORY);
470
471 int rc = rtVfsMountInner(hVfsFileIn, fFlags, pBufs, pBufs + 1, phVfs, pErrInfo);
472
473 RTMemTmpFree(pBufs);
474
475 return rc;
476}
477
478
479/**
480 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
481 */
482static DECLCALLBACK(int) rtVfsChainMountVol_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
483 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
484{
485 RT_NOREF(pProviderReg);
486
487 /*
488 * Basic checks.
489 */
490 if (pElement->enmTypeIn != RTVFSOBJTYPE_FILE)
491 return pElement->enmTypeIn == RTVFSOBJTYPE_INVALID ? VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT : VERR_VFS_CHAIN_TAKES_FILE;
492 if ( pElement->enmType != RTVFSOBJTYPE_VFS
493 && pElement->enmType != RTVFSOBJTYPE_DIR)
494 return VERR_VFS_CHAIN_ONLY_DIR_OR_VFS;
495 if (pElement->cArgs > 1)
496 return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
497
498 /*
499 * Parse the flag if present, save in pElement->uProvider.
500 */
501 bool fReadOnly = (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ;
502 if (pElement->cArgs > 0)
503 {
504 const char *psz = pElement->paArgs[0].psz;
505 if (*psz)
506 {
507 if (!strcmp(psz, "ro"))
508 fReadOnly = true;
509 else if (!strcmp(psz, "rw"))
510 fReadOnly = false;
511 else
512 {
513 *poffError = pElement->paArgs[0].offSpec;
514 return RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected 'ro' or 'rw' as argument");
515 }
516 }
517 }
518
519 pElement->uProvider = fReadOnly ? RTVFSMNT_F_READ_ONLY : 0;
520 return VINF_SUCCESS;
521}
522
523
524/**
525 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
526 */
527static DECLCALLBACK(int) rtVfsChainMountVol_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
528 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
529 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
530{
531 RT_NOREF(pProviderReg, pSpec, poffError);
532
533 int rc;
534 RTVFSFILE hVfsFileIn = RTVfsObjToFile(hPrevVfsObj);
535 if (hVfsFileIn != NIL_RTVFSFILE)
536 {
537 RTVFS hVfs;
538 rc = RTVfsMountVol(hVfsFileIn, (uint32_t)pElement->uProvider, &hVfs, pErrInfo);
539 RTVfsFileRelease(hVfsFileIn);
540 if (RT_SUCCESS(rc))
541 {
542 *phVfsObj = RTVfsObjFromVfs(hVfs);
543 RTVfsRelease(hVfs);
544 if (*phVfsObj != NIL_RTVFSOBJ)
545 return VINF_SUCCESS;
546 rc = VERR_VFS_CHAIN_CAST_FAILED;
547 }
548 }
549 else
550 rc = VERR_VFS_CHAIN_CAST_FAILED;
551 return rc;
552}
553
554
555/**
556 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
557 */
558static DECLCALLBACK(bool) rtVfsChainMountVol_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
559 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
560 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
561{
562 RT_NOREF(pProviderReg, pSpec, pReuseSpec);
563 if ( pElement->paArgs[0].uProvider == pReuseElement->paArgs[0].uProvider
564 || !pReuseElement->paArgs[0].uProvider)
565 return true;
566 return false;
567}
568
569
570/** VFS chain element 'file'. */
571static RTVFSCHAINELEMENTREG g_rtVfsChainMountVolReg =
572{
573 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
574 /* fReserved = */ 0,
575 /* pszName = */ "mount",
576 /* ListEntry = */ { NULL, NULL },
577 /* pszHelp = */ "Open a file system, requires a file object on the left side.\n"
578 "First argument is an optional 'ro' (read-only) or 'rw' (read-write) flag.\n",
579 /* pfnValidate = */ rtVfsChainMountVol_Validate,
580 /* pfnInstantiate = */ rtVfsChainMountVol_Instantiate,
581 /* pfnCanReuseElement = */ rtVfsChainMountVol_CanReuseElement,
582 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
583};
584
585RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainMountVolReg, rtVfsChainMountVolReg);
586
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use