VirtualBox

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

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

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.

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

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