VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/fs-posix.cpp@ 90330

Last change on this file since 90330 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.5 KB
Line 
1/* $Id: fs-posix.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - File System, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 <sys/statvfs.h>
33#include <errno.h>
34#include <stdio.h>
35#ifdef RT_OS_LINUX
36# include <mntent.h>
37#endif
38#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
39# include <sys/mount.h>
40#endif
41
42#include <iprt/fs.h>
43#include "internal/iprt.h"
44
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include <iprt/log.h>
48#include <iprt/string.h>
49#include "internal/fs.h"
50#include "internal/path.h"
51
52
53
54RTR3DECL(int) RTFsQuerySizes(const char *pszFsPath, RTFOFF *pcbTotal, RTFOFF *pcbFree,
55 uint32_t *pcbBlock, uint32_t *pcbSector)
56{
57 /*
58 * Validate input.
59 */
60 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
61
62 /*
63 * Convert the path and query the information.
64 */
65 char const *pszNativeFsPath;
66 int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
67 if (RT_SUCCESS(rc))
68 {
69 /** @todo I'm not quite sure if statvfs was properly specified by SuS, I have to check my own
70 * implementation and FreeBSD before this can eventually be promoted to posix. */
71 struct statvfs StatVFS;
72 RT_ZERO(StatVFS);
73 if (!statvfs(pszNativeFsPath, &StatVFS))
74 {
75 /*
76 * Calc the returned values.
77 */
78 if (pcbTotal)
79 *pcbTotal = (RTFOFF)StatVFS.f_blocks * StatVFS.f_frsize;
80 if (pcbFree)
81 *pcbFree = (RTFOFF)StatVFS.f_bavail * StatVFS.f_frsize;
82 if (pcbBlock)
83 *pcbBlock = StatVFS.f_frsize;
84 /* no idea how to get the sector... */
85 if (pcbSector)
86 *pcbSector = 512;
87 }
88 else
89 rc = RTErrConvertFromErrno(errno);
90 rtPathFreeNative(pszNativeFsPath, pszFsPath);
91 }
92
93 LogFlow(("RTFsQuerySizes(%p:{%s}, %p:{%RTfoff}, %p:{%RTfoff}, %p:{%RX32}, %p:{%RX32}): returns %Rrc\n",
94 pszFsPath, pszFsPath, pcbTotal, pcbTotal ? *pcbTotal : 0, pcbFree, pcbFree ? *pcbFree : 0,
95 pcbBlock, pcbBlock ? *pcbBlock : 0, pcbSector, pcbSector ? *pcbSector : 0, rc));
96 return rc;
97}
98
99
100RTR3DECL(int) RTFsQuerySerial(const char *pszFsPath, uint32_t *pu32Serial)
101{
102 /*
103 * Validate input.
104 */
105 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
106 AssertMsgReturn(VALID_PTR(pu32Serial), ("%p", pu32Serial), VERR_INVALID_PARAMETER);
107
108 /*
109 * Convert the path and query the stats.
110 * We're simply return the device id.
111 */
112 char const *pszNativeFsPath;
113 int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
114 if (RT_SUCCESS(rc))
115 {
116 struct stat Stat;
117 if (!stat(pszNativeFsPath, &Stat))
118 {
119 if (pu32Serial)
120 *pu32Serial = (uint32_t)Stat.st_dev;
121 }
122 else
123 rc = RTErrConvertFromErrno(errno);
124 rtPathFreeNative(pszNativeFsPath, pszFsPath);
125 }
126 LogFlow(("RTFsQuerySerial(%p:{%s}, %p:{%RX32}: returns %Rrc\n",
127 pszFsPath, pszFsPath, pu32Serial, pu32Serial ? *pu32Serial : 0, rc));
128 return rc;
129}
130
131
132RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties)
133{
134 /*
135 * Validate.
136 */
137 AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
138 AssertMsgReturn(VALID_PTR(pProperties), ("%p", pProperties), VERR_INVALID_PARAMETER);
139
140 /*
141 * Convert the path and query the information.
142 */
143 char const *pszNativeFsPath;
144 int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
145 if (RT_SUCCESS(rc))
146 {
147 struct statvfs StatVFS;
148 RT_ZERO(StatVFS);
149 if (!statvfs(pszNativeFsPath, &StatVFS))
150 {
151 /*
152 * Calc/fake the returned values.
153 */
154 pProperties->cbMaxComponent = StatVFS.f_namemax;
155#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
156 pProperties->fCaseSensitive = false;
157#else
158 pProperties->fCaseSensitive = true;
159#endif
160 pProperties->fCompressed = false;
161 pProperties->fFileCompression = false;
162 pProperties->fReadOnly = !!(StatVFS.f_flag & ST_RDONLY);
163 pProperties->fRemote = false;
164 pProperties->fSupportsUnicode = true;
165 }
166 else
167 rc = RTErrConvertFromErrno(errno);
168 rtPathFreeNative(pszNativeFsPath, pszFsPath);
169 }
170
171 LogFlow(("RTFsQueryProperties(%p:{%s}, %p:{.cbMaxComponent=%u, .fReadOnly=%RTbool}): returns %Rrc\n",
172 pszFsPath, pszFsPath, pProperties, pProperties->cbMaxComponent, pProperties->fReadOnly, rc));
173 return rc;
174}
175
176
177RTR3DECL(bool) RTFsIsCaseSensitive(const char *pszFsPath)
178{
179 RT_NOREF_PV(pszFsPath);
180#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
181 return false;
182#else
183 return true;
184#endif
185}
186
187
188RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
189{
190 *penmType = RTFSTYPE_UNKNOWN;
191
192 /*
193 * Validate input.
194 */
195 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
196 AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
197
198 /*
199 * Convert the path and query the stats.
200 * We're simply return the device id.
201 */
202 char const *pszNativeFsPath;
203 int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
204 if (RT_SUCCESS(rc))
205 {
206 struct stat Stat;
207 if (!stat(pszNativeFsPath, &Stat))
208 {
209#if defined(RT_OS_LINUX)
210 FILE *mounted = setmntent("/proc/mounts", "r");
211 if (!mounted)
212 mounted = setmntent("/etc/mtab", "r");
213 if (mounted)
214 {
215 char szBuf[1024];
216 struct stat mntStat;
217 struct mntent mntEnt;
218 while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
219 {
220 if (!stat(mntEnt.mnt_dir, &mntStat))
221 {
222 if (mntStat.st_dev == Stat.st_dev)
223 {
224 if (!strcmp("ext4", mntEnt.mnt_type))
225 *penmType = RTFSTYPE_EXT4;
226 else if (!strcmp("ext3", mntEnt.mnt_type))
227 *penmType = RTFSTYPE_EXT3;
228 else if (!strcmp("ext2", mntEnt.mnt_type))
229 *penmType = RTFSTYPE_EXT2;
230 else if (!strcmp("jfs", mntEnt.mnt_type))
231 *penmType = RTFSTYPE_JFS;
232 else if (!strcmp("xfs", mntEnt.mnt_type))
233 *penmType = RTFSTYPE_XFS;
234 else if (!strcmp("btrfs", mntEnt.mnt_type))
235 *penmType = RTFSTYPE_BTRFS;
236 else if ( !strcmp("vfat", mntEnt.mnt_type)
237 || !strcmp("msdos", mntEnt.mnt_type))
238 *penmType = RTFSTYPE_FAT;
239 else if (!strcmp("ntfs", mntEnt.mnt_type))
240 *penmType = RTFSTYPE_NTFS;
241 else if (!strcmp("hpfs", mntEnt.mnt_type))
242 *penmType = RTFSTYPE_HPFS;
243 else if (!strcmp("ufs", mntEnt.mnt_type))
244 *penmType = RTFSTYPE_UFS;
245 else if (!strcmp("tmpfs", mntEnt.mnt_type))
246 *penmType = RTFSTYPE_TMPFS;
247 else if (!strcmp("hfsplus", mntEnt.mnt_type))
248 *penmType = RTFSTYPE_HFS;
249 else if (!strcmp("udf", mntEnt.mnt_type))
250 *penmType = RTFSTYPE_UDF;
251 else if (!strcmp("iso9660", mntEnt.mnt_type))
252 *penmType = RTFSTYPE_ISO9660;
253 else if (!strcmp("smbfs", mntEnt.mnt_type))
254 *penmType = RTFSTYPE_SMBFS;
255 else if (!strcmp("cifs", mntEnt.mnt_type))
256 *penmType = RTFSTYPE_CIFS;
257 else if (!strcmp("nfs", mntEnt.mnt_type))
258 *penmType = RTFSTYPE_NFS;
259 else if (!strcmp("nfs4", mntEnt.mnt_type))
260 *penmType = RTFSTYPE_NFS;
261 else if (!strcmp("ocfs2", mntEnt.mnt_type))
262 *penmType = RTFSTYPE_OCFS2;
263 else if (!strcmp("sysfs", mntEnt.mnt_type))
264 *penmType = RTFSTYPE_SYSFS;
265 else if (!strcmp("proc", mntEnt.mnt_type))
266 *penmType = RTFSTYPE_PROC;
267 else if ( !strcmp("fuse", mntEnt.mnt_type)
268 || !strncmp("fuse.", mntEnt.mnt_type, 5)
269 || !strcmp("fuseblk", mntEnt.mnt_type))
270 *penmType = RTFSTYPE_FUSE;
271 else
272 {
273 /* sometimes there are more than one entry for the same partition */
274 continue;
275 }
276 break;
277 }
278 }
279 }
280 endmntent(mounted);
281 }
282
283#elif defined(RT_OS_SOLARIS)
284 if (!strcmp("zfs", Stat.st_fstype))
285 *penmType = RTFSTYPE_ZFS;
286 else if (!strcmp("ufs", Stat.st_fstype))
287 *penmType = RTFSTYPE_UFS;
288 else if (!strcmp("nfs", Stat.st_fstype))
289 *penmType = RTFSTYPE_NFS;
290
291#elif defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
292 struct statfs statfsBuf;
293 if (!statfs(pszNativeFsPath, &statfsBuf))
294 {
295 if (!strcmp("hfs", statfsBuf.f_fstypename))
296 *penmType = RTFSTYPE_HFS;
297 else if (!strcmp("apfs", statfsBuf.f_fstypename)) /** @todo verify apfs signature. */
298 *penmType = RTFSTYPE_APFS;
299 else if ( !strcmp("fat", statfsBuf.f_fstypename)
300 || !strcmp("msdos", statfsBuf.f_fstypename))
301 *penmType = RTFSTYPE_FAT;
302 else if (!strcmp("ntfs", statfsBuf.f_fstypename))
303 *penmType = RTFSTYPE_NTFS;
304 else if (!strcmp("autofs", statfsBuf.f_fstypename))
305 *penmType = RTFSTYPE_AUTOFS;
306 else if (!strcmp("devfs", statfsBuf.f_fstypename))
307 *penmType = RTFSTYPE_DEVFS;
308 else if (!strcmp("nfs", statfsBuf.f_fstypename))
309 *penmType = RTFSTYPE_NFS;
310 else if (!strcmp("ufs", statfsBuf.f_fstypename))
311 *penmType = RTFSTYPE_UFS;
312 else if (!strcmp("zfs", statfsBuf.f_fstypename))
313 *penmType = RTFSTYPE_ZFS;
314 }
315 else
316 rc = RTErrConvertFromErrno(errno);
317#endif
318 }
319 else
320 rc = RTErrConvertFromErrno(errno);
321 rtPathFreeNative(pszNativeFsPath, pszFsPath);
322 }
323
324 return rc;
325}
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