VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/fs-win.cpp@ 28863

Last change on this file since 28863 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1/* $Id: fs-win.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - File System, Win32.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 <windows.h>
33
34#include <iprt/fs.h>
35#include <iprt/path.h>
36#include <iprt/string.h>
37#include <iprt/param.h>
38#include <iprt/err.h>
39#include <iprt/log.h>
40#include <iprt/assert.h>
41#include "internal/fs.h"
42
43
44/**
45 * Checks quickly if this is an correct root specification.
46 * Root specs ends with a slash of some kind.
47 *
48 * @returns indicator.
49 * @param pszFsPath Path to check.
50 */
51static bool rtFsIsRoot(const char *pszFsPath)
52{
53 /*
54 * UNC has exactly two slashes..
55 *
56 * Anything else starting with slashe(s) requires
57 * expansion and will have to take the long road.
58 */
59 if (RTPATH_IS_SLASH(pszFsPath[0]))
60 {
61 if ( !RTPATH_IS_SLASH(pszFsPath[1])
62 || RTPATH_IS_SLASH(pszFsPath[2]))
63 return false;
64
65 /* end of machine name */
66 const char *pszSlash = strpbrk(pszFsPath + 2, "\\/");
67 if (!pszSlash)
68 return false;
69
70 /* end of service name. */
71 pszSlash = strpbrk(pszSlash + 1, "\\/");
72 if (!pszSlash)
73 return false;
74
75 return pszSlash[1] == '\0';
76 }
77
78 /*
79 * Ok the other alternative is driver letter.
80 */
81 return pszFsPath[0] >= 'A' && pszFsPath[0] <= 'Z'
82 && pszFsPath[1] == ':'
83 && RTPATH_IS_SLASH(pszFsPath[2])
84 && !pszFsPath[3];
85}
86
87
88#ifndef RT_DONT_CONVERT_FILENAMES
89
90/**
91 * Finds the root of the specified volume.
92 *
93 * @returns iprt status code.
94 * @param pszFsPath Path within the filesystem. Verified as one byte or more.
95 * @param ppwszFsRoot Where to store the returned string. Free with rtFsFreeRoot(),
96 */
97static int rtFsGetRoot(const char *pszFsPath, PRTUTF16 *ppwszFsRoot)
98{
99 /*
100 * Do straight forward stuff first,
101 */
102 if (rtFsIsRoot(pszFsPath))
103 return RTStrToUtf16(pszFsPath, ppwszFsRoot);
104
105 /*
106 * Expand and add slash (if required).
107 */
108 char szFullPath[RTPATH_MAX];
109 int rc = RTPathAbs(pszFsPath, szFullPath, sizeof(szFullPath));
110 if (RT_FAILURE(rc))
111 return rc;
112 size_t cb = strlen(szFullPath);
113 if (!RTPATH_IS_SLASH(szFullPath[cb - 1]))
114 {
115 AssertReturn(cb + 1 < RTPATH_MAX, VERR_FILENAME_TOO_LONG);
116 szFullPath[cb] = '\\';
117 szFullPath[++cb] = '\0';
118 }
119
120 /*
121 * Convert the path.
122 */
123 rc = RTStrToUtf16(szFullPath, ppwszFsRoot);
124 if (RT_FAILURE(rc))
125 return rc == VERR_BUFFER_OVERFLOW ? VERR_FILENAME_TOO_LONG : rc;
126
127 /*
128 * Walk the path until our proper API is happy or there is no more path left.
129 */
130 PRTUTF16 pwszStart = *ppwszFsRoot;
131 if (!GetVolumeInformationW(pwszStart, NULL, 0, NULL, NULL, 0, NULL, 0))
132 {
133 PRTUTF16 pwszEnd = pwszStart + RTUtf16Len(pwszStart);
134 PRTUTF16 pwszMin = pwszStart + 2;
135 do
136 {
137 /* Strip off the last path component. */
138 while (pwszEnd-- > pwszMin)
139 if (RTPATH_IS_SLASH(*pwszEnd))
140 break;
141 AssertReturn(pwszEnd >= pwszMin, VERR_INTERNAL_ERROR); /* leaks, but that's irrelevant for an internal error. */
142 pwszEnd[1] = '\0';
143 } while (!GetVolumeInformationW(pwszStart, NULL, 0, NULL, NULL, 0, NULL, 0));
144 }
145
146 return VINF_SUCCESS;
147}
148
149/**
150 * Frees string returned by rtFsGetRoot().
151 */
152static void rtFsFreeRoot(PRTUTF16 pwszFsRoot)
153{
154 RTUtf16Free(pwszFsRoot);
155}
156
157#else /* RT_DONT_CONVERT_FILENAMES */
158
159/**
160 * Finds the root of the specified volume.
161 *
162 * @returns iprt status code.
163 * @param pszFsPath Path within the filesystem. Verified as one byte or more.
164 * @param ppszFsRoot Where to store the returned string. Free with rtFsFreeRoot(),
165 */
166static int rtFsGetRoot(const char *pszFsPath, char **ppszFsRoot)
167{
168 /*
169 * Do straight forward stuff first,
170 */
171 if (rtFsIsRoot(pszFsPath))
172 return RTStrDupEx(ppszFsRoot, pszFsPath);
173
174 /*
175 * Expand and add slash (if required).
176 */
177 char szFullPath[RTPATH_MAX];
178 int rc = RTPathAbs(pszFsPath, szFullPath, sizeof(szFullPath));
179 if (RT_FAILURE(rc))
180 return rc;
181 size_t cb = strlen(szFullPath);
182 if (!RTPATH_IS_SLASH(szFullPath[cb - 1]))
183 {
184 AssertReturn(cb + 1 < RTPATH_MAX);
185 szFullPath[cb] = '\\';
186 szFullPath[++cb] = '\0';
187 }
188
189 /*
190 * Walk the path until our proper API is happy or there is no more path left.
191 */
192 if (GetVolumeInformation(szFullPath, NULL, 0, NULL, NULL, 0, NULL, 0))
193 {
194 char *pszEnd = szFullPath + cb;
195 char *pszMin = szFullPath + 2;
196 do
197 {
198 /* Strip off the last path component. */
199 while (pszEnd-- > pszMin)
200 if (RTPATH_IS_SLASH(*pszEnd))
201 break;
202 AssertReturn(pszEnd >= pszMin, VERR_INTERNAL_ERROR);
203 pszEnd[1] = '\0';
204 } while (GetVolumeInformationA(pszStart, NULL, 0, NULL, NULL, 0, NULL, 0));
205 }
206
207 return RTStrDupEx(ppszFsRoot, szFullPath);
208}
209
210/**
211 * Frees string returned by rtFsGetRoot().
212 */
213static void rtFsFreeRoot(char *pszFsRoot)
214{
215 RTStrFree(pszFsRoot);
216}
217
218#endif /* RT_DONT_CONVERT_FILENAMES*/
219
220
221RTR3DECL(int) RTFsQuerySizes(const char *pszFsPath, RTFOFF *pcbTotal, RTFOFF *pcbFree,
222 uint32_t *pcbBlock, uint32_t *pcbSector)
223{
224 /*
225 * Validate & get valid root path.
226 */
227 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
228#ifndef RT_DONT_CONVERT_FILENAMES
229 PRTUTF16 pwszFsRoot;
230 int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot);
231#else
232 char pszFsRoot;
233 int rc = rtFsGetRoot(pszFsPath, &pszFsRoot);
234#endif
235 if (RT_FAILURE(rc))
236 return rc;
237
238 /*
239 * Free and total.
240 */
241 if (pcbTotal || pcbFree)
242 {
243 ULARGE_INTEGER cbTotal;
244 ULARGE_INTEGER cbFree;
245#ifndef RT_DONT_CONVERT_FILENAMES
246 if (GetDiskFreeSpaceExW(pwszFsRoot, &cbFree, &cbTotal, NULL))
247#else
248 if (GetDiskFreeSpaceExA(pszFsRoot, &cbFree, &cbTotal, NULL))
249#endif
250 {
251 if (pcbTotal)
252 *pcbTotal = cbTotal.QuadPart;
253 if (pcbFree)
254 *pcbFree = cbFree.QuadPart;
255 }
256 else
257 {
258 DWORD Err = GetLastError();
259 rc = RTErrConvertFromWin32(Err);
260 Log(("RTFsQuerySizes(%s,): GetDiskFreeSpaceEx failed with lasterr %d (%Rrc)\n",
261 pszFsPath, Err, rc));
262 }
263 }
264
265 /*
266 * Block and sector size.
267 */
268 if ( RT_SUCCESS(rc)
269 && (pcbBlock || pcbSector))
270 {
271 DWORD dwDummy1, dwDummy2;
272 DWORD cbSector;
273 DWORD cSectorsPerCluster;
274#ifndef RT_DONT_CONVERT_FILENAMES
275 if (GetDiskFreeSpaceW(pwszFsRoot, &cSectorsPerCluster, &cbSector, &dwDummy1, &dwDummy2))
276#else
277 if (GetDiskFreeSpaceA(pszFsRoot, &cSectorsPerCluster, &cbSector, &dwDummy1, &dwDummy2))
278#endif
279 {
280 if (pcbBlock)
281 *pcbBlock = cbSector * cSectorsPerCluster;
282 if (pcbSector)
283 *pcbSector = cbSector;
284 }
285 else
286 {
287 DWORD Err = GetLastError();
288 rc = RTErrConvertFromWin32(Err);
289 Log(("RTFsQuerySizes(%s,): GetDiskFreeSpace failed with lasterr %d (%Rrc)\n",
290 pszFsPath, Err, rc));
291 }
292 }
293
294#ifndef RT_DONT_CONVERT_FILENAMES
295 rtFsFreeRoot(pwszFsRoot);
296#else
297 rtFsFreeRoot(pszFsRoot);
298#endif
299 return rc;
300}
301
302
303/**
304 * Query the serial number of a filesystem.
305 *
306 * @returns iprt status code.
307 * @param pszFsPath Path within the mounted filesystem.
308 * @param pu32Serial Where to store the serial number.
309 */
310RTR3DECL(int) RTFsQuerySerial(const char *pszFsPath, uint32_t *pu32Serial)
311{
312 /*
313 * Validate & get valid root path.
314 */
315 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
316 AssertMsgReturn(VALID_PTR(pu32Serial), ("%p", pu32Serial), VERR_INVALID_PARAMETER);
317#ifndef RT_DONT_CONVERT_FILENAMES
318 PRTUTF16 pwszFsRoot;
319 int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot);
320#else
321 char pszFsRoot;
322 int rc = rtFsGetRoot(pszFsPath, &pszFsRoot);
323#endif
324 if (RT_FAILURE(rc))
325 return rc;
326
327 /*
328 * Do work.
329 */
330 DWORD dwMaxName;
331 DWORD dwFlags;
332 DWORD dwSerial;
333#ifndef RT_DONT_CONVERT_FILENAMES
334 if (GetVolumeInformationW(pwszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0))
335#else
336 if (GetVolumeInformationA(pszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0))
337#endif
338 *pu32Serial = dwSerial;
339 else
340 {
341 DWORD Err = GetLastError();
342 rc = RTErrConvertFromWin32(Err);
343 Log(("RTFsQuerySizes(%s,): GetDiskFreeSpaceEx failed with lasterr %d (%Rrc)\n",
344 pszFsPath, Err, rc));
345 }
346
347#ifndef RT_DONT_CONVERT_FILENAMES
348 RTUtf16Free(pwszFsRoot);
349#else
350 RTStrFree(pszFsRoot);
351#endif
352 return rc;
353}
354
355
356/**
357 * Query the properties of a mounted filesystem.
358 *
359 * @returns iprt status code.
360 * @param pszFsPath Path within the mounted filesystem.
361 * @param pProperties Where to store the properties.
362 */
363RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties)
364{
365 /*
366 * Validate & get valid root path.
367 */
368 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
369 AssertMsgReturn(VALID_PTR(pProperties), ("%p", pProperties), VERR_INVALID_PARAMETER);
370#ifndef RT_DONT_CONVERT_FILENAMES
371 PRTUTF16 pwszFsRoot;
372 int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot);
373#else
374 char pszFsRoot;
375 int rc = rtFsGetRoot(pszFsPath, &pszFsRoot);
376#endif
377 if (RT_FAILURE(rc))
378 return rc;
379
380 /*
381 * Do work.
382 */
383 DWORD dwMaxName;
384 DWORD dwFlags;
385 DWORD dwSerial;
386#ifndef RT_DONT_CONVERT_FILENAMES
387 if (GetVolumeInformationW(pwszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0))
388#else
389 if (GetVolumeInformationA(pszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0))
390#endif
391 {
392 memset(pProperties, 0, sizeof(*pProperties));
393 pProperties->cbMaxComponent = dwMaxName;
394 pProperties->fFileCompression = !!(dwFlags & FILE_FILE_COMPRESSION);
395 pProperties->fCompressed = !!(dwFlags & FILE_VOLUME_IS_COMPRESSED);
396 pProperties->fReadOnly = !!(dwFlags & FILE_READ_ONLY_VOLUME);
397 pProperties->fSupportsUnicode = !!(dwFlags & FILE_UNICODE_ON_DISK);
398 pProperties->fCaseSensitive = false; /* win32 is case preserving only */
399 pProperties->fRemote = false; /* no idea yet */
400 }
401 else
402 {
403 DWORD Err = GetLastError();
404 rc = RTErrConvertFromWin32(Err);
405 Log(("RTFsQuerySizes(%s,): GetVolumeInformation failed with lasterr %d (%Rrc)\n",
406 pszFsPath, Err, rc));
407 }
408
409#ifndef RT_DONT_CONVERT_FILENAMES
410 RTUtf16Free(pwszFsRoot);
411#else
412 RTStrFree(pszFsRoot);
413#endif
414 return rc;
415}
416
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