VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/filesystem/filesystem.cpp@ 40988

Last change on this file since 40988 was 40038, checked in by vboxsync, 13 years ago

fix OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1/* $Id: filesystem.cpp 40038 2012-02-08 11:37:52Z vboxsync $ */
2/** @file
3 * IPRT Filesystem API (Filesystem) - generic code.
4 */
5
6/*
7 * Copyright (C) 2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP LOG_GROUP_DEFAULT
32#include <iprt/types.h>
33#include <iprt/assert.h>
34#include <iprt/mem.h>
35#include <iprt/filesystem.h>
36#include <iprt/err.h>
37#include <iprt/asm.h>
38#include <iprt/string.h>
39#include <iprt/list.h>
40#include "internal/filesystem.h"
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45
46/**
47 * Medium descriptor.
48 */
49typedef struct RTFILESYSTEMMEDIUMINT
50{
51 /** Size of the medium in bytes. */
52 uint64_t cbMedium;
53 /** Sector size. */
54 uint64_t cbSector;
55 /** Read callback */
56 PFNRTFILESYSTEMREAD pfnRead;
57 /** Write callback. */
58 PFNRTFILESYSTEMWRITE pfnWrite;
59 /** Opaque user data. */
60 void *pvUser;
61} RTFILESYSTEMMEDIUMINT;
62/** Pointer to a disk descriptor. */
63typedef RTFILESYSTEMMEDIUMINT *PRTFILESYSTEMMEDIUMINT;
64/** Pointer to a const descriptor. */
65typedef const RTFILESYSTEMMEDIUMINT *PCRTFILESYSTEMMEDIUMINT;
66
67/**
68 * The internal filesystem object structure.
69 */
70typedef struct RTFILESYSTEMINT
71{
72 /** The filesytem object magic (RTFILESYSTEM_MAGIC). */
73 uint32_t u32Magic;
74 /** Medium descriptor. */
75 RTFILESYSTEMMEDIUMINT Medium;
76 /** Filesystem format operations */
77 PCRTFILESYSTEMFMTOPS pcFsFmtOps;
78 /** Filesystem format handle. */
79 RTFILESYSTEMFMT hFsFmt;
80 /** Reference counter. */
81 uint32_t volatile cRefs;
82} RTFILESYSTEMINT;
83/** Pointer to an internal volume manager. */
84typedef RTFILESYSTEMINT *PRTFILESYSTEMINT;
85
86/*******************************************************************************
87* Global variables *
88*******************************************************************************/
89extern RTFILESYSTEMFMTOPS g_rtFilesystemFmtExt;
90
91/**
92 * Supported volume formats.
93 */
94static PCRTFILESYSTEMFMTOPS g_aFilesystemFmts[] =
95{
96 &g_rtFilesystemFmtExt
97};
98
99DECLHIDDEN(uint64_t) rtFilesystemMediumGetSize(RTFILESYSTEMMEDIUM hMedium)
100{
101 PRTFILESYSTEMMEDIUMINT pMedInt = hMedium;
102 AssertPtrReturn(pMedInt, 0);
103
104 return pMedInt->cbMedium;
105}
106
107DECLHIDDEN(int) rtFilesystemMediumRead(RTFILESYSTEMMEDIUM hMedium, uint64_t off,
108 void *pvBuf, size_t cbRead)
109{
110 PRTFILESYSTEMMEDIUMINT pMedInt = hMedium;
111 AssertPtrReturn(pMedInt, VERR_INVALID_HANDLE);
112
113 return pMedInt->pfnRead(pMedInt->pvUser, off, pvBuf, cbRead);
114}
115
116DECLHIDDEN(int) rtFilesystemMediumWrite(RTFILESYSTEMMEDIUM hMedium, uint64_t off,
117 const void *pvBuf, size_t cbWrite)
118{
119 PRTFILESYSTEMMEDIUMINT pMedInt = hMedium;
120 AssertPtrReturn(pMedInt, VERR_INVALID_HANDLE);
121
122 return pMedInt->pfnWrite(pMedInt->pvUser, off, pvBuf, cbWrite);
123}
124
125RTDECL(uint32_t) RTFilesystemRetain(RTFILESYSTEM hFs)
126{
127 PRTFILESYSTEMINT pThis = hFs;
128 AssertPtrReturn(pThis, UINT32_MAX);
129 AssertReturn(pThis->u32Magic == RTFILESYSTEM_MAGIC, UINT32_MAX);
130
131 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
132 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
133 return cRefs;
134}
135
136/**
137 * Destroys a volume manager handle.
138 *
139 * @param pThis The filesystem object to destroy.
140 */
141static void rtFilesystemDestroy(PRTFILESYSTEMINT pThis)
142{
143 if (pThis->hFsFmt != NIL_RTFILESYSTEMFMT)
144 {
145 AssertPtr(pThis->pcFsFmtOps);
146
147 /* Let the backend do it's own cleanup first. */
148 pThis->pcFsFmtOps->pfnClose(pThis->hFsFmt);
149 pThis->hFsFmt = NIL_RTFILESYSTEMFMT;
150 }
151
152 pThis->Medium.cbMedium = 0;
153 pThis->Medium.pvUser = NULL;
154 pThis->Medium.pfnRead = NULL;
155 pThis->Medium.pfnWrite = NULL;
156 pThis->u32Magic = RTFILESYSTEM_MAGIC_DEAD;
157 RTMemFree(pThis);
158}
159
160RTDECL(uint32_t) RTFilesystemRelease(RTFILESYSTEM hFs)
161{
162 PRTFILESYSTEMINT pThis = hFs;
163 if (pThis == NIL_RTFILESYSTEM)
164 return 0;
165 AssertPtrReturn(pThis, UINT32_MAX);
166 AssertReturn(pThis->u32Magic == RTFILESYSTEM_MAGIC, UINT32_MAX);
167
168 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
169 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
170 if (cRefs == 0)
171 rtFilesystemDestroy(pThis);
172 return cRefs;
173}
174
175RTDECL(int) RTFilesystemOpen(PRTFILESYSTEM phFs, PFNRTFILESYSTEMREAD pfnRead,
176 PFNRTFILESYSTEMWRITE pfnWrite, uint64_t cbMedium,
177 uint64_t cbSector, void *pvUser, uint32_t fFlags)
178{
179 int rc = VINF_SUCCESS;
180 uint32_t uScoreMax = RTFILESYSTEM_MATCH_SCORE_UNSUPPORTED;
181 PCRTFILESYSTEMFMTOPS pcFsFmtOpsMatch = NULL;
182 PRTFILESYSTEMINT pThis = NULL;
183 AssertPtrReturn(phFs, VERR_INVALID_POINTER);
184 AssertPtrReturn(pfnRead, VERR_INVALID_POINTER);
185
186 pThis = (PRTFILESYSTEMINT)RTMemAllocZ(sizeof(RTFILESYSTEMINT));
187 if (!pThis)
188 return VERR_NO_MEMORY;
189
190 pThis->u32Magic = RTFILESYSTEM_MAGIC;
191 pThis->Medium.cbMedium = cbMedium;
192 pThis->Medium.cbSector = cbSector;
193 pThis->Medium.pfnRead = pfnRead;
194 pThis->Medium.pfnWrite = pfnWrite;
195 pThis->Medium.pvUser = pvUser;
196 pThis->cRefs = 1;
197
198 for (unsigned i = 0; i < RT_ELEMENTS(g_aFilesystemFmts); i++)
199 {
200 uint32_t uScore;
201 PCRTFILESYSTEMFMTOPS pcFsFmtOps = g_aFilesystemFmts[i];
202
203 rc = pcFsFmtOps->pfnProbe(&pThis->Medium, &uScore);
204 if ( RT_SUCCESS(rc)
205 && uScore > uScoreMax)
206 {
207 pcFsFmtOpsMatch = pcFsFmtOps;
208 uScoreMax = uScore;
209 }
210 else if (RT_FAILURE(rc))
211 break;
212 }
213
214 if (RT_SUCCESS(rc))
215 {
216 if (uScoreMax > RTFILESYSTEM_MATCH_SCORE_UNSUPPORTED)
217 {
218 AssertPtr(pcFsFmtOpsMatch);
219
220 /* Open the format. */
221 rc = pcFsFmtOpsMatch->pfnOpen(&pThis->Medium, &pThis->hFsFmt);
222 if (RT_SUCCESS(rc))
223 pThis->pcFsFmtOps = pcFsFmtOpsMatch;
224 }
225 else
226 rc = VERR_NOT_SUPPORTED;
227 }
228
229 if (RT_SUCCESS(rc))
230 *phFs = pThis;
231 else
232 RTMemFree(pThis);
233
234 return rc;
235}
236
237RTDECL(const char *) RTFilesystemGetFormat(RTFILESYSTEM hFs)
238{
239 PRTFILESYSTEMINT pThis = hFs;
240 AssertPtrReturn(pThis, NULL);
241 AssertReturn(pThis->u32Magic == RTFILESYSTEM_MAGIC, NULL);
242
243 return pThis->pcFsFmtOps->pcszFmt;
244}
245
246RTDECL(uint64_t) RTFilesystemGetBlockSize(RTFILESYSTEM hFs)
247{
248 PRTFILESYSTEMINT pThis = hFs;
249 AssertPtrReturn(pThis, 0);
250 AssertReturn(pThis->u32Magic == RTFILESYSTEM_MAGIC, 0);
251
252 return pThis->pcFsFmtOps->pfnGetBlockSize(pThis->hFsFmt);
253}
254
255RTDECL(int) RTFilesystemQueryRangeUse(RTFILESYSTEM hFs, uint64_t offStart, size_t cb,
256 bool *pfUsed)
257{
258 PRTFILESYSTEMINT pThis = hFs;
259 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
260 AssertReturn(pThis->u32Magic == RTFILESYSTEM_MAGIC, VERR_INVALID_HANDLE);
261 AssertPtrReturn(pfUsed, VERR_INVALID_POINTER);
262 AssertReturn(offStart + cb <= pThis->Medium.cbMedium, VERR_OUT_OF_RANGE);
263
264 return pThis->pcFsFmtOps->pfnQueryRangeUse(pThis->hFsFmt, offStart, cb, pfUsed);
265}
Note: See TracBrowser for help on using the repository browser.

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