VirtualBox

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

Last change on this file since 57358 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.0 KB
Line 
1/* $Id: fs-posix.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - File System, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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/err.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 pProperties->fCaseSensitive = true;
156 pProperties->fCompressed = false;
157 pProperties->fFileCompression = false;
158 pProperties->fReadOnly = !!(StatVFS.f_flag & ST_RDONLY);
159 pProperties->fRemote = false;
160 pProperties->fSupportsUnicode = true;
161 }
162 else
163 rc = RTErrConvertFromErrno(errno);
164 rtPathFreeNative(pszNativeFsPath, pszFsPath);
165 }
166
167 LogFlow(("RTFsQueryProperties(%p:{%s}, %p:{.cbMaxComponent=%u, .fReadOnly=%RTbool}): returns %Rrc\n",
168 pszFsPath, pszFsPath, pProperties, pProperties->cbMaxComponent, pProperties->fReadOnly, rc));
169 return rc;
170}
171
172
173RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
174{
175 *penmType = RTFSTYPE_UNKNOWN;
176
177 /*
178 * Validate input.
179 */
180 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
181 AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
182
183 /*
184 * Convert the path and query the stats.
185 * We're simply return the device id.
186 */
187 char const *pszNativeFsPath;
188 int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
189 if (RT_SUCCESS(rc))
190 {
191 struct stat Stat;
192 if (!stat(pszNativeFsPath, &Stat))
193 {
194#if defined(RT_OS_LINUX)
195 FILE *mounted = setmntent("/proc/mounts", "r");
196 if (!mounted)
197 mounted = setmntent("/etc/mtab", "r");
198 if (mounted)
199 {
200 char szBuf[1024];
201 struct stat mntStat;
202 struct mntent mntEnt;
203 while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
204 {
205 if (!stat(mntEnt.mnt_dir, &mntStat))
206 {
207 if (mntStat.st_dev == Stat.st_dev)
208 {
209 if (!strcmp("ext4", mntEnt.mnt_type))
210 *penmType = RTFSTYPE_EXT4;
211 else if (!strcmp("ext3", mntEnt.mnt_type))
212 *penmType = RTFSTYPE_EXT3;
213 else if (!strcmp("ext2", mntEnt.mnt_type))
214 *penmType = RTFSTYPE_EXT2;
215 else if (!strcmp("jfs", mntEnt.mnt_type))
216 *penmType = RTFSTYPE_JFS;
217 else if (!strcmp("xfs", mntEnt.mnt_type))
218 *penmType = RTFSTYPE_XFS;
219 else if (!strcmp("btrfs", mntEnt.mnt_type))
220 *penmType = RTFSTYPE_BTRFS;
221 else if ( !strcmp("vfat", mntEnt.mnt_type)
222 || !strcmp("msdos", mntEnt.mnt_type))
223 *penmType = RTFSTYPE_FAT;
224 else if (!strcmp("ntfs", mntEnt.mnt_type))
225 *penmType = RTFSTYPE_NTFS;
226 else if (!strcmp("hpfs", mntEnt.mnt_type))
227 *penmType = RTFSTYPE_HPFS;
228 else if (!strcmp("ufs", mntEnt.mnt_type))
229 *penmType = RTFSTYPE_UFS;
230 else if (!strcmp("tmpfs", mntEnt.mnt_type))
231 *penmType = RTFSTYPE_TMPFS;
232 else if (!strcmp("hfsplus", mntEnt.mnt_type))
233 *penmType = RTFSTYPE_HFS;
234 else if (!strcmp("udf", mntEnt.mnt_type))
235 *penmType = RTFSTYPE_UDF;
236 else if (!strcmp("iso9660", mntEnt.mnt_type))
237 *penmType = RTFSTYPE_ISO9660;
238 else if (!strcmp("smbfs", mntEnt.mnt_type))
239 *penmType = RTFSTYPE_SMBFS;
240 else if (!strcmp("cifs", mntEnt.mnt_type))
241 *penmType = RTFSTYPE_CIFS;
242 else if (!strcmp("nfs", mntEnt.mnt_type))
243 *penmType = RTFSTYPE_NFS;
244 else if (!strcmp("nfs4", mntEnt.mnt_type))
245 *penmType = RTFSTYPE_NFS;
246 else if (!strcmp("ocfs2", mntEnt.mnt_type))
247 *penmType = RTFSTYPE_OCFS2;
248 else if (!strcmp("sysfs", mntEnt.mnt_type))
249 *penmType = RTFSTYPE_SYSFS;
250 else if (!strcmp("proc", mntEnt.mnt_type))
251 *penmType = RTFSTYPE_PROC;
252 else if ( !strcmp("fuse", mntEnt.mnt_type)
253 || !strncmp("fuse.", mntEnt.mnt_type, 5)
254 || !strcmp("fuseblk", mntEnt.mnt_type))
255 *penmType = RTFSTYPE_FUSE;
256 else
257 {
258 /* sometimes there are more than one entry for the same partition */
259 continue;
260 }
261 break;
262 }
263 }
264 }
265 endmntent(mounted);
266 }
267
268#elif defined(RT_OS_SOLARIS)
269 if (!strcmp("zfs", Stat.st_fstype))
270 *penmType = RTFSTYPE_ZFS;
271 else if (!strcmp("ufs", Stat.st_fstype))
272 *penmType = RTFSTYPE_UFS;
273 else if (!strcmp("nfs", Stat.st_fstype))
274 *penmType = RTFSTYPE_NFS;
275
276#elif defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
277 struct statfs statfsBuf;
278 if (!statfs(pszNativeFsPath, &statfsBuf))
279 {
280 if (!strcmp("hfs", statfsBuf.f_fstypename))
281 *penmType = RTFSTYPE_HFS;
282 else if ( !strcmp("fat", statfsBuf.f_fstypename)
283 || !strcmp("msdos", statfsBuf.f_fstypename))
284 *penmType = RTFSTYPE_FAT;
285 else if (!strcmp("ntfs", statfsBuf.f_fstypename))
286 *penmType = RTFSTYPE_NTFS;
287 else if (!strcmp("autofs", statfsBuf.f_fstypename))
288 *penmType = RTFSTYPE_AUTOFS;
289 else if (!strcmp("devfs", statfsBuf.f_fstypename))
290 *penmType = RTFSTYPE_DEVFS;
291 else if (!strcmp("nfs", statfsBuf.f_fstypename))
292 *penmType = RTFSTYPE_NFS;
293 else if (!strcmp("ufs", statfsBuf.f_fstypename))
294 *penmType = RTFSTYPE_UFS;
295 else if (!strcmp("zfs", statfsBuf.f_fstypename))
296 *penmType = RTFSTYPE_ZFS;
297 }
298 else
299 rc = RTErrConvertFromErrno(errno);
300#endif
301 }
302 else
303 rc = RTErrConvertFromErrno(errno);
304 rtPathFreeNative(pszNativeFsPath, pszFsPath);
305 }
306
307 return rc;
308}
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