1 | /* $Id: fs-posix.cpp 30365 2010-06-22 12:08:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File System, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | #ifdef RT_OS_DARWIN
|
---|
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 |
|
---|
54 | RTR3DECL(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 |
|
---|
100 | RTR3DECL(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 | * Conver 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 |
|
---|
132 | RTR3DECL(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, .fCaseSensitive=%RTbool}): returns %Rrc\n",
|
---|
168 | pszFsPath, pszFsPath, pProperties, pProperties->cbMaxComponent, pProperties->fReadOnly));
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | RTR3DECL(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("vfat", mntEnt.mnt_type)
|
---|
220 | || !strcmp("msdos", mntEnt.mnt_type))
|
---|
221 | *penmType = RTFSTYPE_FAT;
|
---|
222 | else if (!strcmp("ntfs", mntEnt.mnt_type))
|
---|
223 | *penmType = RTFSTYPE_NTFS;
|
---|
224 | else if (!strcmp("hpfs", mntEnt.mnt_type))
|
---|
225 | *penmType = RTFSTYPE_HPFS;
|
---|
226 | else if (!strcmp("ufs", mntEnt.mnt_type))
|
---|
227 | *penmType = RTFSTYPE_UFS;
|
---|
228 | else if (!strcmp("tmpfs", mntEnt.mnt_type))
|
---|
229 | *penmType = RTFSTYPE_TMPFS;
|
---|
230 | else if (!strcmp("hfsplus", mntEnt.mnt_type))
|
---|
231 | *penmType = RTFSTYPE_HFS;
|
---|
232 | else if (!strcmp("udf", mntEnt.mnt_type))
|
---|
233 | *penmType = RTFSTYPE_UDF;
|
---|
234 | else if (!strcmp("iso9660", mntEnt.mnt_type))
|
---|
235 | *penmType = RTFSTYPE_ISO9660;
|
---|
236 | else if (!strcmp("smbfs", mntEnt.mnt_type))
|
---|
237 | *penmType = RTFSTYPE_SMBFS;
|
---|
238 | else if (!strcmp("cifs", mntEnt.mnt_type))
|
---|
239 | *penmType = RTFSTYPE_CIFS;
|
---|
240 | else if (!strcmp("nfs", mntEnt.mnt_type))
|
---|
241 | *penmType = RTFSTYPE_NFS;
|
---|
242 | else if (!strcmp("nfs4", mntEnt.mnt_type))
|
---|
243 | *penmType = RTFSTYPE_NFS;
|
---|
244 | else if (!strcmp("sysfs", mntEnt.mnt_type))
|
---|
245 | *penmType = RTFSTYPE_SYSFS;
|
---|
246 | else if (!strcmp("proc", mntEnt.mnt_type))
|
---|
247 | *penmType = RTFSTYPE_PROC;
|
---|
248 | else if ( !strcmp("fuse", mntEnt.mnt_type)
|
---|
249 | || !strncmp("fuse.", mntEnt.mnt_type, 5))
|
---|
250 | *penmType = RTFSTYPE_FUSE;
|
---|
251 | else
|
---|
252 | {
|
---|
253 | /* sometimes there are more than one entry for the same partition */
|
---|
254 | continue;
|
---|
255 | }
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | endmntent(mounted);
|
---|
261 | }
|
---|
262 |
|
---|
263 | #elif defined(RT_OS_SOLARIS)
|
---|
264 | if (!strcmp("zfs", Stat.st_fstype))
|
---|
265 | *penmType = RTFSTYPE_ZFS;
|
---|
266 | else if (!strcmp("ufs", Stat.st_fstype))
|
---|
267 | *penmType = RTFSTYPE_UFS;
|
---|
268 | else if (!strcmp("nfs", Stat.st_fstype))
|
---|
269 | *penmType = RTFSTYPE_NFS;
|
---|
270 |
|
---|
271 | #elif defined(RT_OS_DARWIN)
|
---|
272 | struct statfs statfsBuf;
|
---|
273 | if (!statfs(pszNativeFsPath, &statfsBuf))
|
---|
274 | {
|
---|
275 | if (!strcmp("hfs", statfsBuf.f_fstypename))
|
---|
276 | *penmType = RTFSTYPE_HFS;
|
---|
277 | else if ( !strcmp("fat", statfsBuf.f_fstypename)
|
---|
278 | || !strcmp("msdos", statfsBuf.f_fstypename))
|
---|
279 | *penmType = RTFSTYPE_FAT;
|
---|
280 | else if (!strcmp("ntfs", statfsBuf.f_fstypename))
|
---|
281 | *penmType = RTFSTYPE_NTFS;
|
---|
282 | else if (!strcmp("autofs", statfsBuf.f_fstypename))
|
---|
283 | *penmType = RTFSTYPE_AUTOFS;
|
---|
284 | else if (!strcmp("devfs", statfsBuf.f_fstypename))
|
---|
285 | *penmType = RTFSTYPE_DEVFS;
|
---|
286 | else if (!strcmp("nfs", statfsBuf.f_fstypename))
|
---|
287 | *penmType = RTFSTYPE_NFS;
|
---|
288 | }
|
---|
289 | else
|
---|
290 | rc = RTErrConvertFromErrno(errno);
|
---|
291 | #endif
|
---|
292 | }
|
---|
293 | else
|
---|
294 | rc = RTErrConvertFromErrno(errno);
|
---|
295 | rtPathFreeNative(pszNativeFsPath, pszFsPath);
|
---|
296 | }
|
---|
297 |
|
---|
298 | return rc;
|
---|
299 | }
|
---|