VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/nt/fs-nt.cpp

Last change on this file was 107099, checked in by vboxsync, 6 months ago

IPRT: Adding RTArchValToString and RTSystemGetNativeArch for detecting win.amd64 binaries running in an emulator in win.arm64 and suchlike. jiraref:VBP-1466

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.6 KB
Line 
1/* $Id: fs-nt.cpp 107099 2024-11-22 01:44:29Z vboxsync $ */
2/** @file
3 * IPRT - File System, Native NT.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.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_FS
42#include "internal-r3-nt.h"
43
44#include <iprt/fs.h>
45#include <iprt/file.h>
46#include <iprt/path.h>
47#include <iprt/string.h>
48#include <iprt/param.h>
49#include <iprt/errcore.h>
50#include <iprt/log.h>
51#include <iprt/assert.h>
52#include "internal/fs.h"
53
54
55
56
57RTR3DECL(int) RTFsQuerySizes(const char *pszFsPath, RTFOFF *pcbTotal, RTFOFF *pcbFree,
58 uint32_t *pcbBlock, uint32_t *pcbSector)
59{
60 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
61
62 /*
63 * Open the file/dir/whatever.
64 */
65 HANDLE hFile;
66 int rc = RTNtPathOpen(pszFsPath,
67 GENERIC_READ,
68 FILE_ATTRIBUTE_NORMAL,
69 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
70 FILE_OPEN,
71 FILE_OPEN_FOR_BACKUP_INTENT,
72 OBJ_CASE_INSENSITIVE,
73 &hFile,
74 NULL);
75 if (RT_SUCCESS(rc))
76 {
77 RTFILE hIprtFile = NIL_RTFILE;
78 rc = RTFileFromNative(&hIprtFile, (RTHCINTPTR)hFile);
79 AssertRC(rc);
80 if (RT_SUCCESS(rc))
81 rc = RTFileQueryFsSizes(hIprtFile, pcbTotal, pcbFree, pcbBlock, pcbSector);
82
83 RTNtPathClose(hFile);
84 }
85 return rc;
86}
87
88
89RTR3DECL(int) RTFsQuerySerial(const char *pszFsPath, uint32_t *pu32Serial)
90{
91 /*
92 * Validate & get valid root path.
93 */
94 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
95 AssertPtrReturn(pu32Serial, VERR_INVALID_POINTER);
96
97 /*
98 * Open the file/dir/whatever.
99 */
100 HANDLE hFile;
101 int rc = RTNtPathOpen(pszFsPath,
102 GENERIC_READ,
103 FILE_ATTRIBUTE_NORMAL,
104 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
105 FILE_OPEN,
106 FILE_OPEN_FOR_BACKUP_INTENT,
107 OBJ_CASE_INSENSITIVE,
108 &hFile,
109 NULL);
110 if (RT_SUCCESS(rc))
111 {
112 /*
113 * Get the volume information.
114 */
115 union
116 {
117 FILE_FS_VOLUME_INFORMATION FsVolInfo;
118 uint8_t abBuf[sizeof(FILE_FS_VOLUME_INFORMATION) + 4096];
119 } u;
120 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
121 NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &u, sizeof(u), FileFsVolumeInformation);
122 if (NT_SUCCESS(rcNt))
123 *pu32Serial = u.FsVolInfo.VolumeSerialNumber;
124 else
125 rc = RTErrConvertFromNtStatus(rcNt);
126
127 RTNtPathClose(hFile);
128 }
129 return rc;
130}
131
132
133RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties)
134{
135 /*
136 * Validate & get valid root path.
137 */
138 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
139 AssertPtrReturn(pProperties, VERR_INVALID_POINTER);
140
141 /*
142 * Open the file/dir/whatever.
143 */
144 HANDLE hFile;
145 int rc = RTNtPathOpen(pszFsPath,
146 GENERIC_READ,
147 FILE_ATTRIBUTE_NORMAL,
148 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
149 FILE_OPEN,
150 FILE_OPEN_FOR_BACKUP_INTENT,
151 OBJ_CASE_INSENSITIVE,
152 &hFile,
153 NULL);
154 if (RT_SUCCESS(rc))
155 {
156 /*
157 * Get the volume information.
158 */
159 union
160 {
161 FILE_FS_ATTRIBUTE_INFORMATION FsAttrInfo;
162 uint8_t abBuf[sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + 4096];
163 } u;
164 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
165 NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &u, sizeof(u), FileFsAttributeInformation);
166 if (NT_SUCCESS(rcNt))
167 {
168 FILE_FS_DEVICE_INFORMATION FsDevInfo;
169 rcNt = NtQueryVolumeInformationFile(hFile, &Ios, &FsDevInfo, sizeof(FsDevInfo), FileFsDeviceInformation);
170 if (NT_SUCCESS(rcNt))
171 {
172 /*
173 * Fill in the return structure.
174 */
175 memset(pProperties, 0, sizeof(*pProperties));
176 pProperties->cbMaxComponent = u.FsAttrInfo.MaximumComponentNameLength;
177 pProperties->fFileCompression = !!(u.FsAttrInfo.FileSystemAttributes & FILE_FILE_COMPRESSION);
178 pProperties->fCompressed = !!(u.FsAttrInfo.FileSystemAttributes & FILE_VOLUME_IS_COMPRESSED);
179 pProperties->fReadOnly = !!(u.FsAttrInfo.FileSystemAttributes & FILE_READ_ONLY_VOLUME);
180 pProperties->fSupportsUnicode = !!(u.FsAttrInfo.FileSystemAttributes & FILE_UNICODE_ON_DISK);
181 pProperties->fCaseSensitive = false; /* win32 is case preserving only */
182 /** @todo r=bird: What about FILE_CASE_SENSITIVE_SEARCH ? Is this set for NTFS
183 * as well perchance? If so, better mention it instead of just setting
184 * fCaseSensitive to false. */
185
186 /* figure the remote stuff */
187 pProperties->fRemote = (FsDevInfo.Characteristics & FILE_REMOTE_DEVICE)
188 || FsDevInfo.DeviceType == FILE_DEVICE_NETWORK
189 || FsDevInfo.DeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM;
190 }
191 else
192 rc = RTErrConvertFromNtStatus(rcNt);
193 }
194 else
195 rc = RTErrConvertFromNtStatus(rcNt);
196
197 RTNtPathClose(hFile);
198 }
199 return rc;
200}
201
202
203RTR3DECL(bool) RTFsIsCaseSensitive(const char *pszFsPath)
204{
205 RT_NOREF_PV(pszFsPath);
206 return false;
207}
208
209
210int rtNtQueryFsType(HANDLE hHandle, PRTFSTYPE penmType)
211{
212 /*
213 * Get the file system name.
214 */
215 union
216 {
217 FILE_FS_ATTRIBUTE_INFORMATION FsAttrInfo;
218 uint8_t abBuf[sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + 4096];
219 } u;
220 IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
221 NTSTATUS rcNt = NtQueryVolumeInformationFile(hHandle, &Ios, &u, sizeof(u), FileFsAttributeInformation);
222 if (NT_SUCCESS(rcNt))
223 {
224#define IS_FS(a_szName) rtNtCompWideStrAndAscii(u.FsAttrInfo.FileSystemName, u.FsAttrInfo.FileSystemNameLength, RT_STR_TUPLE(a_szName))
225 if (IS_FS("NTFS"))
226 *penmType = RTFSTYPE_NTFS;
227 else if (IS_FS("FAT"))
228 *penmType = RTFSTYPE_FAT;
229 else if (IS_FS("FAT32"))
230 *penmType = RTFSTYPE_FAT;
231 else if (IS_FS("exFAT"))
232 *penmType = RTFSTYPE_EXFAT;
233 else if (IS_FS("UDF"))
234 *penmType = RTFSTYPE_UDF;
235 else if (IS_FS("CDFS"))
236 *penmType = RTFSTYPE_ISO9660;
237 else if (IS_FS("HPFS"))
238 *penmType = RTFSTYPE_HPFS;
239 else if (IS_FS("ReFS")) /** @todo verify ReFS signature. */
240 *penmType = RTFSTYPE_REFS;
241 else if (IS_FS("VBoxSharedFolderFS"))
242 *penmType = RTFSTYPE_VBOXSHF;
243#undef IS_FS
244 return VINF_SUCCESS;
245 }
246
247 *penmType = RTFSTYPE_UNKNOWN;
248 return RTErrConvertFromNtStatus(rcNt);
249}
250
251
252RTR3DECL(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 rc = rtNtQueryFsType(hFile, penmType);
277 RTNtPathClose(hFile);
278 }
279 return rc;
280}
281
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