1 | /* $Id: fs-nt.cpp 47533 2013-08-03 22:31:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File System, Native NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 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 "internal-r3-nt.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 |
|
---|
46 | RTR3DECL(int) RTFsQuerySizes(const char *pszFsPath, RTFOFF *pcbTotal, RTFOFF *pcbFree,
|
---|
47 | uint32_t *pcbBlock, uint32_t *pcbSector)
|
---|
48 | {
|
---|
49 | AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * Open the file/dir/whatever.
|
---|
53 | */
|
---|
54 | HANDLE hFile;
|
---|
55 | int rc = rtNtPathOpen(pszFsPath,
|
---|
56 | GENERIC_READ,
|
---|
57 | FILE_ATTRIBUTE_NORMAL,
|
---|
58 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
59 | FILE_OPEN,
|
---|
60 | FILE_OPEN_FOR_BACKUP_INTENT,
|
---|
61 | OBJ_CASE_INSENSITIVE,
|
---|
62 | &hFile,
|
---|
63 | NULL);
|
---|
64 | if (RT_SUCCESS(rc))
|
---|
65 | {
|
---|
66 | /*
|
---|
67 | * Get the volume information.
|
---|
68 | */
|
---|
69 | FILE_FS_SIZE_INFORMATION FsSizeInfo;
|
---|
70 | IO_STATUS_BLOCK Ios = MY_IO_STATUS_BLOCK_INITIALIZER;
|
---|
71 | NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &FsSizeInfo, sizeof(FsSizeInfo), FileFsSizeInformation);
|
---|
72 | if (NT_SUCCESS(rcNt))
|
---|
73 | {
|
---|
74 | /*
|
---|
75 | * Calculate the return values.
|
---|
76 | */
|
---|
77 | if (pcbTotal)
|
---|
78 | {
|
---|
79 | *pcbTotal = FsSizeInfo.TotalAllocationUnits.QuadPart
|
---|
80 | * FsSizeInfo.SectorsPerAllocationUnit
|
---|
81 | * FsSizeInfo.BytesPerSector;
|
---|
82 | if ( *pcbTotal / FsSizeInfo.SectorsPerAllocationUnit / FsSizeInfo.BytesPerSector
|
---|
83 | != FsSizeInfo.TotalAllocationUnits.QuadPart)
|
---|
84 | *pcbTotal = UINT64_MAX;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (pcbFree)
|
---|
88 | {
|
---|
89 | *pcbFree = FsSizeInfo.AvailableAllocationUnits.QuadPart
|
---|
90 | * FsSizeInfo.SectorsPerAllocationUnit
|
---|
91 | * FsSizeInfo.BytesPerSector;
|
---|
92 | if ( *pcbFree / FsSizeInfo.SectorsPerAllocationUnit / FsSizeInfo.BytesPerSector
|
---|
93 | != FsSizeInfo.AvailableAllocationUnits.QuadPart)
|
---|
94 | *pcbFree = UINT64_MAX;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (pcbBlock)
|
---|
98 | {
|
---|
99 | *pcbBlock = FsSizeInfo.SectorsPerAllocationUnit * FsSizeInfo.BytesPerSector;
|
---|
100 | if (*pcbBlock / FsSizeInfo.BytesPerSector != FsSizeInfo.SectorsPerAllocationUnit)
|
---|
101 | rc = VERR_OUT_OF_RANGE;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (pcbSector)
|
---|
105 | *pcbSector = FsSizeInfo.BytesPerSector;
|
---|
106 | }
|
---|
107 | else
|
---|
108 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
109 |
|
---|
110 | rtNtPathClose(hFile);
|
---|
111 | }
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | RTR3DECL(int) RTFsQuerySerial(const char *pszFsPath, uint32_t *pu32Serial)
|
---|
117 | {
|
---|
118 | /*
|
---|
119 | * Validate & get valid root path.
|
---|
120 | */
|
---|
121 | AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
|
---|
122 | AssertPtrReturn(pu32Serial, VERR_INVALID_POINTER);
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Open the file/dir/whatever.
|
---|
126 | */
|
---|
127 | HANDLE hFile;
|
---|
128 | int rc = rtNtPathOpen(pszFsPath,
|
---|
129 | GENERIC_READ,
|
---|
130 | FILE_ATTRIBUTE_NORMAL,
|
---|
131 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
132 | FILE_OPEN,
|
---|
133 | FILE_OPEN_FOR_BACKUP_INTENT,
|
---|
134 | OBJ_CASE_INSENSITIVE,
|
---|
135 | &hFile,
|
---|
136 | NULL);
|
---|
137 | if (RT_SUCCESS(rc))
|
---|
138 | {
|
---|
139 | /*
|
---|
140 | * Get the volume information.
|
---|
141 | */
|
---|
142 | union
|
---|
143 | {
|
---|
144 | FILE_FS_VOLUME_INFORMATION FsVolInfo;
|
---|
145 | uint8_t abBuf[sizeof(FILE_FS_VOLUME_INFORMATION) + 4096];
|
---|
146 | } u;
|
---|
147 | IO_STATUS_BLOCK Ios = MY_IO_STATUS_BLOCK_INITIALIZER;
|
---|
148 | NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &u, sizeof(u), FileFsVolumeInformation);
|
---|
149 | if (NT_SUCCESS(rcNt))
|
---|
150 | *pu32Serial = u.FsVolInfo.VolumeSerialNumber;
|
---|
151 | else
|
---|
152 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
153 |
|
---|
154 | rtNtPathClose(hFile);
|
---|
155 | }
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties)
|
---|
161 | {
|
---|
162 | /*
|
---|
163 | * Validate & get valid root path.
|
---|
164 | */
|
---|
165 | AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
|
---|
166 | AssertPtrReturn(pProperties, VERR_INVALID_POINTER);
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Open the file/dir/whatever.
|
---|
170 | */
|
---|
171 | HANDLE hFile;
|
---|
172 | int rc = rtNtPathOpen(pszFsPath,
|
---|
173 | GENERIC_READ,
|
---|
174 | FILE_ATTRIBUTE_NORMAL,
|
---|
175 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
176 | FILE_OPEN,
|
---|
177 | FILE_OPEN_FOR_BACKUP_INTENT,
|
---|
178 | OBJ_CASE_INSENSITIVE,
|
---|
179 | &hFile,
|
---|
180 | NULL);
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | {
|
---|
183 | /*
|
---|
184 | * Get the volume information.
|
---|
185 | */
|
---|
186 | union
|
---|
187 | {
|
---|
188 | FILE_FS_ATTRIBUTE_INFORMATION FsAttrInfo;
|
---|
189 | uint8_t abBuf[sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + 4096];
|
---|
190 | } u;
|
---|
191 | IO_STATUS_BLOCK Ios = MY_IO_STATUS_BLOCK_INITIALIZER;
|
---|
192 | NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &u, sizeof(u), FileFsAttributeInformation);
|
---|
193 | if (NT_SUCCESS(rcNt))
|
---|
194 | {
|
---|
195 | FILE_FS_DEVICE_INFORMATION FsDevInfo;
|
---|
196 | rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &FsDevInfo, sizeof(FsDevInfo), FileFsDeviceInformation);
|
---|
197 | if (NT_SUCCESS(rcNt))
|
---|
198 | {
|
---|
199 | /*
|
---|
200 | * Fill in the return structure.
|
---|
201 | */
|
---|
202 | memset(pProperties, 0, sizeof(*pProperties));
|
---|
203 | pProperties->cbMaxComponent = u.FsAttrInfo.MaximumComponentNameLength;
|
---|
204 | pProperties->fFileCompression = !!(u.FsAttrInfo.FileSystemAttributes & FILE_FILE_COMPRESSION);
|
---|
205 | pProperties->fCompressed = !!(u.FsAttrInfo.FileSystemAttributes & FILE_VOLUME_IS_COMPRESSED);
|
---|
206 | pProperties->fReadOnly = !!(u.FsAttrInfo.FileSystemAttributes & FILE_READ_ONLY_VOLUME);
|
---|
207 | pProperties->fSupportsUnicode = !!(u.FsAttrInfo.FileSystemAttributes & FILE_UNICODE_ON_DISK);
|
---|
208 | pProperties->fCaseSensitive = false; /* win32 is case preserving only */
|
---|
209 | /** @todo r=bird: What about FILE_CASE_SENSITIVE_SEARCH ? Is this set for NTFS
|
---|
210 | * as well perchance? If so, better mention it instead of just setting
|
---|
211 | * fCaseSensitive to false. */
|
---|
212 |
|
---|
213 | /* figure the remote stuff */
|
---|
214 | pProperties->fRemote = RT_BOOL(FsDevInfo.Characteristics & FILE_REMOTE_DEVICE);
|
---|
215 | }
|
---|
216 | else
|
---|
217 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
218 | }
|
---|
219 | else
|
---|
220 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
221 |
|
---|
222 | rtNtPathClose(hFile);
|
---|
223 | }
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Internal helper for comparing a WCHAR string with a char string.
|
---|
230 | *
|
---|
231 | * @returns @c true if equal, @c false if not.
|
---|
232 | * @param pwsz1 The first string.
|
---|
233 | * @param cb1 The length of the first string, in bytes.
|
---|
234 | * @param psz2 The second string.
|
---|
235 | * @param cch2 The length of the second string.
|
---|
236 | */
|
---|
237 | static bool rtFsCompWideStrAndAscii(WCHAR const *pwsz1, size_t cch1, const char *psz2, size_t cch2)
|
---|
238 | {
|
---|
239 | if (cch1 != cch2 * 2)
|
---|
240 | return false;
|
---|
241 | while (cch2-- > 0)
|
---|
242 | {
|
---|
243 | unsigned ch1 = *pwsz1++;
|
---|
244 | unsigned ch2 = (unsigned char)*psz2++;
|
---|
245 | if (ch1 != ch2)
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 | return true;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
|
---|
253 | {
|
---|
254 | /*
|
---|
255 | * Validate input.
|
---|
256 | */
|
---|
257 | *penmType = RTFSTYPE_UNKNOWN;
|
---|
258 | AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
|
---|
259 | AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * Open the file/dir/whatever.
|
---|
263 | */
|
---|
264 | HANDLE hFile;
|
---|
265 | int rc = rtNtPathOpen(pszFsPath,
|
---|
266 | GENERIC_READ,
|
---|
267 | FILE_ATTRIBUTE_NORMAL,
|
---|
268 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
---|
269 | FILE_OPEN,
|
---|
270 | FILE_OPEN_FOR_BACKUP_INTENT,
|
---|
271 | OBJ_CASE_INSENSITIVE,
|
---|
272 | &hFile,
|
---|
273 | NULL);
|
---|
274 | if (RT_SUCCESS(rc))
|
---|
275 | {
|
---|
276 | /*
|
---|
277 | * Get the file system name.
|
---|
278 | */
|
---|
279 | union
|
---|
280 | {
|
---|
281 | FILE_FS_ATTRIBUTE_INFORMATION FsAttrInfo;
|
---|
282 | uint8_t abBuf[sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + 4096];
|
---|
283 | } u;
|
---|
284 | IO_STATUS_BLOCK Ios = MY_IO_STATUS_BLOCK_INITIALIZER;
|
---|
285 | NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &u, sizeof(u), FileFsAttributeInformation);
|
---|
286 | if (NT_SUCCESS(rcNt))
|
---|
287 | {
|
---|
288 | #define IS_FS(a_szName) \
|
---|
289 | rtFsCompWideStrAndAscii(u.FsAttrInfo.FileSystemName, u.FsAttrInfo.FileSystemNameLength, RT_STR_TUPLE(a_szName))
|
---|
290 | if (IS_FS("NTFS"))
|
---|
291 | *penmType = RTFSTYPE_NTFS;
|
---|
292 | else if (IS_FS("FAT"))
|
---|
293 | *penmType = RTFSTYPE_FAT;
|
---|
294 | else if (IS_FS("FAT32"))
|
---|
295 | *penmType = RTFSTYPE_FAT;
|
---|
296 | else if (IS_FS("VBoxSharedFolderFS"))
|
---|
297 | *penmType = RTFSTYPE_VBOXSHF;
|
---|
298 | #undef IS_FS
|
---|
299 | }
|
---|
300 | else
|
---|
301 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
302 |
|
---|
303 | rtNtPathClose(hFile);
|
---|
304 | }
|
---|
305 | return rc;
|
---|
306 | }
|
---|
307 |
|
---|