1 | /* $Id: fs-posix.cpp 30324 2010-06-21 11:50:02Z 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.215389.xyz. 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, uint32_t *pu32Type)
|
---|
174 | {
|
---|
175 | /*
|
---|
176 | * Validate input.
|
---|
177 | */
|
---|
178 | AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Convert the path and query the stats.
|
---|
182 | * We're simply return the device id.
|
---|
183 | */
|
---|
184 | char const *pszNativeFsPath;
|
---|
185 | int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | *pu32Type = RTFS_FS_TYPE_UNKNOWN;
|
---|
189 |
|
---|
190 | struct stat Stat;
|
---|
191 | if (!stat(pszNativeFsPath, &Stat))
|
---|
192 | {
|
---|
193 | #if defined(RT_OS_LINUX)
|
---|
194 | FILE *mounted = setmntent("/proc/mounts", "r");
|
---|
195 | if (!mounted)
|
---|
196 | mounted = setmntent("/etc/mtab", "r");
|
---|
197 | if (mounted)
|
---|
198 | {
|
---|
199 | char szBuf[1024];
|
---|
200 | struct stat mntStat;
|
---|
201 | struct mntent mntEnt;
|
---|
202 | while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
|
---|
203 | {
|
---|
204 | if (!stat(mntEnt.mnt_dir, &mntStat))
|
---|
205 | {
|
---|
206 | if (mntStat.st_dev == Stat.st_dev)
|
---|
207 | {
|
---|
208 | if (!strcmp("ext4", mntEnt.mnt_type))
|
---|
209 | *pu32Type = RTFS_FS_TYPE_EXT4;
|
---|
210 | else if (!strcmp("ext3", mntEnt.mnt_type))
|
---|
211 | *pu32Type = RTFS_FS_TYPE_EXT3;
|
---|
212 | else if (!strcmp("ext2", mntEnt.mnt_type))
|
---|
213 | *pu32Type = RTFS_FS_TYPE_EXT2;
|
---|
214 | else if (!strcmp("xfs", mntEnt.mnt_type))
|
---|
215 | *pu32Type = RTFS_FS_TYPE_XFS;
|
---|
216 | else if ( !strcmp("vfat", mntEnt.mnt_type)
|
---|
217 | || !strcmp("msdos", mntEnt.mnt_type))
|
---|
218 | *pu32Type = RTFS_FS_TYPE_FAT;
|
---|
219 | else if (!strcmp("tmpfs", mntEnt.mnt_type))
|
---|
220 | *pu32Type = RTFS_FS_TYPE_TMPFS;
|
---|
221 | else if (!strcmp("hfsplus", mntEnt.mnt_type))
|
---|
222 | *pu32Type = RTFS_FS_TYPE_HFS;
|
---|
223 | else
|
---|
224 | {
|
---|
225 | /* sometimes there are more than one entry for the same partition */
|
---|
226 | continue;
|
---|
227 | }
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | endmntent(mounted);
|
---|
233 | }
|
---|
234 | #elif defined(RT_OS_SOLARIS)
|
---|
235 | if (!strcmp("zfs", Stat.st_fstype))
|
---|
236 | *pu32Type = RTFS_FS_TYPE_ZFS;
|
---|
237 | #elif defined(RT_OS_DARWIN)
|
---|
238 | struct statfs statfsBuf;
|
---|
239 | if (!statfs(pszNativeFsPath, &statfsBuf))
|
---|
240 | {
|
---|
241 | if (!strcmp("hfs", statfsBuf.f_fstypename))
|
---|
242 | *pu32Type = RTFS_FS_TYPE_HFS;
|
---|
243 | else if ( !strcmp("fat", statfsBuf.f_fstypename)
|
---|
244 | || !strcmp("msdos", statfsBuf.f_fstypename))
|
---|
245 | *pu32Type = RTFS_FS_TYPE_FAT;
|
---|
246 | else if (!strcmp("ntfs", statfsBuf.f_fstypename))
|
---|
247 | *pu32Type = RTFS_FS_TYPE_NTFS;
|
---|
248 | else if (!strcmp("autofs", statfsBuf.f_fstypename))
|
---|
249 | *pu32Type = RTFS_FS_TYPE_AUTOFS;
|
---|
250 | else if (!strcmp("devfs", statfsBuf.f_fstypename))
|
---|
251 | *pu32Type = RTFS_FS_TYPE_DEVFS;
|
---|
252 | else if (!strcmp("nfs", statfsBuf.f_fstypename))
|
---|
253 | *pu32Type = RTFS_FS_TYPE_NFS;
|
---|
254 | }
|
---|
255 | else
|
---|
256 | rc = RTErrConvertFromErrno(errno);
|
---|
257 | #endif
|
---|
258 | }
|
---|
259 | else
|
---|
260 | rc = RTErrConvertFromErrno(errno);
|
---|
261 | rtPathFreeNative(pszNativeFsPath, pszFsPath);
|
---|
262 | }
|
---|
263 |
|
---|
264 | return rc;
|
---|
265 | }
|
---|