VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.2 KB
Line 
1/* $Id: fs-win.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - File System, Win32.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/win/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/* from ntdef.h */
44typedef LONG NTSTATUS;
45
46/* from ntddk.h */
47typedef struct _IO_STATUS_BLOCK {
48 union {
49 NTSTATUS Status;
50 PVOID Pointer;
51 };
52 ULONG_PTR Information;
53} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
54
55typedef enum _FSINFOCLASS {
56 FileFsAttributeInformation = 5,
57} FS_INFORMATION_CLASS, *PFS_INFORMATION_CLASS;
58
59/* from ntifs.h */
60
61typedef struct _FILE_FS_ATTRIBUTE_INFORMATION {
62 ULONG FileSystemAttributes;
63 LONG MaximumComponentNameLength;
64 ULONG FileSystemNameLength;
65 WCHAR FileSystemName[1];
66} FILE_FS_ATTRIBUTE_INFORMATION, *PFILE_FS_ATTRIBUTE_INFORMATION;
67
68extern "C" NTSTATUS NTAPI NtQueryVolumeInformationFile(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FS_INFORMATION_CLASS);
69
70/**
71 * Checks quickly if this is an correct root specification.
72 * Root specs ends with a slash of some kind.
73 *
74 * @returns indicator.
75 * @param pszFsPath Path to check.
76 */
77static bool rtFsIsRoot(const char *pszFsPath)
78{
79 /*
80 * UNC has exactly two slashes..
81 *
82 * Anything else starting with slashe(s) requires
83 * expansion and will have to take the long road.
84 */
85 if (RTPATH_IS_SLASH(pszFsPath[0]))
86 {
87 if ( !RTPATH_IS_SLASH(pszFsPath[1])
88 || RTPATH_IS_SLASH(pszFsPath[2]))
89 return false;
90
91 /* end of machine name */
92 const char *pszSlash = strpbrk(pszFsPath + 2, "\\/");
93 if (!pszSlash)
94 return false;
95
96 /* end of service name. */
97 pszSlash = strpbrk(pszSlash + 1, "\\/");
98 if (!pszSlash)
99 return false;
100
101 return pszSlash[1] == '\0';
102 }
103
104 /*
105 * Ok the other alternative is driver letter.
106 */
107 return pszFsPath[0] >= 'A' && pszFsPath[0] <= 'Z'
108 && pszFsPath[1] == ':'
109 && RTPATH_IS_SLASH(pszFsPath[2])
110 && !pszFsPath[3];
111}
112
113
114
115/**
116 * Finds the root of the specified volume.
117 *
118 * @returns iprt status code.
119 * @param pszFsPath Path within the filesystem. Verified as one byte or more.
120 * @param ppwszFsRoot Where to store the returned string. Free with rtFsFreeRoot(),
121 */
122static int rtFsGetRoot(const char *pszFsPath, PRTUTF16 *ppwszFsRoot)
123{
124 /*
125 * Do straight forward stuff first,
126 */
127 if (rtFsIsRoot(pszFsPath))
128 return RTStrToUtf16(pszFsPath, ppwszFsRoot);
129
130 /*
131 * Expand and add slash (if required).
132 */
133 char szFullPath[RTPATH_MAX];
134 int rc = RTPathAbs(pszFsPath, szFullPath, sizeof(szFullPath));
135 if (RT_FAILURE(rc))
136 return rc;
137 size_t cb = strlen(szFullPath);
138 if (!RTPATH_IS_SLASH(szFullPath[cb - 1]))
139 {
140 AssertReturn(cb + 1 < RTPATH_MAX, VERR_FILENAME_TOO_LONG);
141 szFullPath[cb] = '\\';
142 szFullPath[++cb] = '\0';
143 }
144
145 /*
146 * Convert the path.
147 */
148 rc = RTStrToUtf16(szFullPath, ppwszFsRoot);
149 if (RT_FAILURE(rc))
150 return rc == VERR_BUFFER_OVERFLOW ? VERR_FILENAME_TOO_LONG : rc;
151
152 /*
153 * Walk the path until our proper API is happy or there is no more path left.
154 */
155 PRTUTF16 pwszStart = *ppwszFsRoot;
156 if (!GetVolumeInformationW(pwszStart, NULL, 0, NULL, NULL, 0, NULL, 0))
157 {
158 PRTUTF16 pwszEnd = pwszStart + RTUtf16Len(pwszStart);
159 PRTUTF16 pwszMin = pwszStart + 2;
160 do
161 {
162 /* Strip off the last path component. */
163 while (pwszEnd-- > pwszMin)
164 if (RTPATH_IS_SLASH(*pwszEnd))
165 break;
166 AssertReturn(pwszEnd >= pwszMin, VERR_INTERNAL_ERROR); /* leaks, but that's irrelevant for an internal error. */
167 pwszEnd[1] = '\0';
168 } while (!GetVolumeInformationW(pwszStart, NULL, 0, NULL, NULL, 0, NULL, 0));
169 }
170
171 return VINF_SUCCESS;
172}
173
174/**
175 * Frees string returned by rtFsGetRoot().
176 */
177static void rtFsFreeRoot(PRTUTF16 pwszFsRoot)
178{
179 RTUtf16Free(pwszFsRoot);
180}
181
182
183RTR3DECL(int) RTFsQuerySizes(const char *pszFsPath, RTFOFF *pcbTotal, RTFOFF *pcbFree,
184 uint32_t *pcbBlock, uint32_t *pcbSector)
185{
186 /*
187 * Validate & get valid root path.
188 */
189 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
190 PRTUTF16 pwszFsRoot;
191 int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot);
192 if (RT_FAILURE(rc))
193 return rc;
194
195 /*
196 * Free and total.
197 */
198 if (pcbTotal || pcbFree)
199 {
200 ULARGE_INTEGER cbTotal;
201 ULARGE_INTEGER cbFree;
202 if (GetDiskFreeSpaceExW(pwszFsRoot, &cbFree, &cbTotal, NULL))
203 {
204 if (pcbTotal)
205 *pcbTotal = cbTotal.QuadPart;
206 if (pcbFree)
207 *pcbFree = cbFree.QuadPart;
208 }
209 else
210 {
211 DWORD Err = GetLastError();
212 rc = RTErrConvertFromWin32(Err);
213 Log(("RTFsQuerySizes(%s,): GetDiskFreeSpaceEx failed with lasterr %d (%Rrc)\n",
214 pszFsPath, Err, rc));
215 }
216 }
217
218 /*
219 * Block and sector size.
220 */
221 if ( RT_SUCCESS(rc)
222 && (pcbBlock || pcbSector))
223 {
224 DWORD dwDummy1, dwDummy2;
225 DWORD cbSector;
226 DWORD cSectorsPerCluster;
227 if (GetDiskFreeSpaceW(pwszFsRoot, &cSectorsPerCluster, &cbSector, &dwDummy1, &dwDummy2))
228 {
229 if (pcbBlock)
230 *pcbBlock = cbSector * cSectorsPerCluster;
231 if (pcbSector)
232 *pcbSector = cbSector;
233 }
234 else
235 {
236 DWORD Err = GetLastError();
237 rc = RTErrConvertFromWin32(Err);
238 Log(("RTFsQuerySizes(%s,): GetDiskFreeSpace failed with lasterr %d (%Rrc)\n",
239 pszFsPath, Err, rc));
240 }
241 }
242
243 rtFsFreeRoot(pwszFsRoot);
244 return rc;
245}
246
247
248/**
249 * Query the serial number of a filesystem.
250 *
251 * @returns iprt status code.
252 * @param pszFsPath Path within the mounted filesystem.
253 * @param pu32Serial Where to store the serial number.
254 */
255RTR3DECL(int) RTFsQuerySerial(const char *pszFsPath, uint32_t *pu32Serial)
256{
257 /*
258 * Validate & get valid root path.
259 */
260 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
261 AssertMsgReturn(VALID_PTR(pu32Serial), ("%p", pu32Serial), VERR_INVALID_PARAMETER);
262 PRTUTF16 pwszFsRoot;
263 int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot);
264 if (RT_FAILURE(rc))
265 return rc;
266
267 /*
268 * Do work.
269 */
270 DWORD dwMaxName;
271 DWORD dwFlags;
272 DWORD dwSerial;
273 if (GetVolumeInformationW(pwszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0))
274 *pu32Serial = dwSerial;
275 else
276 {
277 DWORD Err = GetLastError();
278 rc = RTErrConvertFromWin32(Err);
279 Log(("RTFsQuerySizes(%s,): GetDiskFreeSpaceEx failed with lasterr %d (%Rrc)\n",
280 pszFsPath, Err, rc));
281 }
282
283 rtFsFreeRoot(pwszFsRoot);
284 return rc;
285}
286
287
288/**
289 * Query the properties of a mounted filesystem.
290 *
291 * @returns iprt status code.
292 * @param pszFsPath Path within the mounted filesystem.
293 * @param pProperties Where to store the properties.
294 */
295RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties)
296{
297 /*
298 * Validate & get valid root path.
299 */
300 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
301 AssertMsgReturn(VALID_PTR(pProperties), ("%p", pProperties), VERR_INVALID_PARAMETER);
302 PRTUTF16 pwszFsRoot;
303 int rc = rtFsGetRoot(pszFsPath, &pwszFsRoot);
304 if (RT_FAILURE(rc))
305 return rc;
306
307 /*
308 * Do work.
309 */
310 DWORD dwMaxName;
311 DWORD dwFlags;
312 DWORD dwSerial;
313 if (GetVolumeInformationW(pwszFsRoot, NULL, 0, &dwSerial, &dwMaxName, &dwFlags, NULL, 0))
314 {
315 memset(pProperties, 0, sizeof(*pProperties));
316 pProperties->cbMaxComponent = dwMaxName;
317 pProperties->fFileCompression = !!(dwFlags & FILE_FILE_COMPRESSION);
318 pProperties->fCompressed = !!(dwFlags & FILE_VOLUME_IS_COMPRESSED);
319 pProperties->fReadOnly = !!(dwFlags & FILE_READ_ONLY_VOLUME);
320 pProperties->fSupportsUnicode = !!(dwFlags & FILE_UNICODE_ON_DISK);
321 pProperties->fCaseSensitive = false; /* win32 is case preserving only */
322 /** @todo r=bird: What about FILE_CASE_SENSITIVE_SEARCH ? Is this set for NTFS
323 * as well perchance? If so, better mention it instead of just setting
324 * fCaseSensitive to false. */
325 pProperties->fRemote = false; /* no idea yet */
326 }
327 else
328 {
329 DWORD Err = GetLastError();
330 rc = RTErrConvertFromWin32(Err);
331 Log(("RTFsQuerySizes(%s,): GetVolumeInformation failed with lasterr %d (%Rrc)\n",
332 pszFsPath, Err, rc));
333 }
334
335 rtFsFreeRoot(pwszFsRoot);
336 return rc;
337}
338
339
340RTR3DECL(bool) RTFsIsCaseSensitive(const char *pszFsPath)
341{
342 return false;
343}
344
345
346/**
347 * Internal helper for comparing a WCHAR string with a char string.
348 *
349 * @returns @c true if equal, @c false if not.
350 * @param pwsz1 The first string.
351 * @param cch1 The length of the first string, in bytes.
352 * @param psz2 The second string.
353 * @param cch2 The length of the second string.
354 */
355static bool rtFsWinAreEqual(WCHAR const *pwsz1, size_t cch1, const char *psz2, size_t cch2)
356{
357 if (cch1 != cch2 * 2)
358 return false;
359 while (cch2-- > 0)
360 {
361 unsigned ch1 = *pwsz1++;
362 unsigned ch2 = (unsigned char)*psz2++;
363 if (ch1 != ch2)
364 return false;
365 }
366 return true;
367}
368
369
370RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
371{
372 *penmType = RTFSTYPE_UNKNOWN;
373
374 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
375 AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
376
377 /*
378 * Convert the path and try open it.
379 */
380 PRTUTF16 pwszFsPath;
381 int rc = RTPathWinFromUtf8(&pwszFsPath, pszFsPath, 0 /*fFlags*/);
382 if (RT_SUCCESS(rc))
383 {
384 HANDLE hFile = CreateFileW(pwszFsPath,
385 GENERIC_READ,
386 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
387 NULL,
388 OPEN_EXISTING,
389 FILE_FLAG_BACKUP_SEMANTICS,
390 NULL);
391 if (hFile != INVALID_HANDLE_VALUE)
392 {
393 /*
394 * Use the NT api directly to get the file system name.
395 */
396 char abBuf[8192];
397 IO_STATUS_BLOCK Ios;
398 NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios,
399 abBuf, sizeof(abBuf),
400 FileFsAttributeInformation);
401 if (rcNt >= 0)
402 {
403 PFILE_FS_ATTRIBUTE_INFORMATION pFsAttrInfo = (PFILE_FS_ATTRIBUTE_INFORMATION)abBuf;
404#define IS_FS(szName) \
405 rtFsWinAreEqual(pFsAttrInfo->FileSystemName, pFsAttrInfo->FileSystemNameLength, szName, sizeof(szName) - 1)
406 if (IS_FS("NTFS"))
407 *penmType = RTFSTYPE_NTFS;
408 else if (IS_FS("FAT"))
409 *penmType = RTFSTYPE_FAT;
410 else if (IS_FS("FAT32"))
411 *penmType = RTFSTYPE_FAT;
412 else if (IS_FS("EXFAT"))
413 *penmType = RTFSTYPE_EXFAT;
414 else if (IS_FS("VBoxSharedFolderFS"))
415 *penmType = RTFSTYPE_VBOXSHF;
416#undef IS_FS
417 }
418 else
419 rc = RTErrConvertFromNtStatus(rcNt);
420 CloseHandle(hFile);
421 }
422 else
423 rc = RTErrConvertFromWin32(GetLastError());
424 RTPathWinFree(pwszFsPath);
425 }
426 return rc;
427}
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