1 | /* $Id: fs-posix.cpp 30254 2010-06-16 14:37:24Z 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.alldomusa.eu.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 |
|
---|
39 | #include <iprt/fs.h>
|
---|
40 | #include "internal/iprt.h"
|
---|
41 |
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/log.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include "internal/fs.h"
|
---|
47 | #include "internal/path.h"
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | RTR3DECL(int) RTFsQuerySizes(const char *pszFsPath, RTFOFF *pcbTotal, RTFOFF *pcbFree,
|
---|
52 | uint32_t *pcbBlock, uint32_t *pcbSector)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Validate input.
|
---|
56 | */
|
---|
57 | AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Convert the path and query the information.
|
---|
61 | */
|
---|
62 | char const *pszNativeFsPath;
|
---|
63 | int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
|
---|
64 | if (RT_SUCCESS(rc))
|
---|
65 | {
|
---|
66 | /** @todo I'm not quite sure if statvfs was properly specified by SuS, I have to check my own
|
---|
67 | * implementation and FreeBSD before this can eventually be promoted to posix. */
|
---|
68 | struct statvfs StatVFS;
|
---|
69 | RT_ZERO(StatVFS);
|
---|
70 | if (!statvfs(pszNativeFsPath, &StatVFS))
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Calc the returned values.
|
---|
74 | */
|
---|
75 | if (pcbTotal)
|
---|
76 | *pcbTotal = (RTFOFF)StatVFS.f_blocks * StatVFS.f_frsize;
|
---|
77 | if (pcbFree)
|
---|
78 | *pcbFree = (RTFOFF)StatVFS.f_bavail * StatVFS.f_frsize;
|
---|
79 | if (pcbBlock)
|
---|
80 | *pcbBlock = StatVFS.f_frsize;
|
---|
81 | /* no idea how to get the sector... */
|
---|
82 | if (pcbSector)
|
---|
83 | *pcbSector = 512;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | rc = RTErrConvertFromErrno(errno);
|
---|
87 | rtPathFreeNative(pszNativeFsPath, pszFsPath);
|
---|
88 | }
|
---|
89 |
|
---|
90 | LogFlow(("RTFsQuerySizes(%p:{%s}, %p:{%RTfoff}, %p:{%RTfoff}, %p:{%RX32}, %p:{%RX32}): returns %Rrc\n",
|
---|
91 | pszFsPath, pszFsPath, pcbTotal, pcbTotal ? *pcbTotal : 0, pcbFree, pcbFree ? *pcbFree : 0,
|
---|
92 | pcbBlock, pcbBlock ? *pcbBlock : 0, pcbSector, pcbSector ? *pcbSector : 0, rc));
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | RTR3DECL(int) RTFsQuerySerial(const char *pszFsPath, uint32_t *pu32Serial)
|
---|
98 | {
|
---|
99 | /*
|
---|
100 | * Validate input.
|
---|
101 | */
|
---|
102 | AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
|
---|
103 | AssertMsgReturn(VALID_PTR(pu32Serial), ("%p", pu32Serial), VERR_INVALID_PARAMETER);
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Conver the path and query the stats.
|
---|
107 | * We're simply return the device id.
|
---|
108 | */
|
---|
109 | char const *pszNativeFsPath;
|
---|
110 | int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | struct stat Stat;
|
---|
114 | if (!stat(pszNativeFsPath, &Stat))
|
---|
115 | {
|
---|
116 | if (pu32Serial)
|
---|
117 | *pu32Serial = (uint32_t)Stat.st_dev;
|
---|
118 | }
|
---|
119 | else
|
---|
120 | rc = RTErrConvertFromErrno(errno);
|
---|
121 | rtPathFreeNative(pszNativeFsPath, pszFsPath);
|
---|
122 | }
|
---|
123 | LogFlow(("RTFsQuerySerial(%p:{%s}, %p:{%RX32}: returns %Rrc\n",
|
---|
124 | pszFsPath, pszFsPath, pu32Serial, pu32Serial ? *pu32Serial : 0, rc));
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | RTR3DECL(int) RTFsQueryProperties(const char *pszFsPath, PRTFSPROPERTIES pProperties)
|
---|
130 | {
|
---|
131 | /*
|
---|
132 | * Validate.
|
---|
133 | */
|
---|
134 | AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
|
---|
135 | AssertMsgReturn(VALID_PTR(pProperties), ("%p", pProperties), VERR_INVALID_PARAMETER);
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Convert the path and query the information.
|
---|
139 | */
|
---|
140 | char const *pszNativeFsPath;
|
---|
141 | int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
|
---|
142 | if (RT_SUCCESS(rc))
|
---|
143 | {
|
---|
144 | struct statvfs StatVFS;
|
---|
145 | RT_ZERO(StatVFS);
|
---|
146 | if (!statvfs(pszNativeFsPath, &StatVFS))
|
---|
147 | {
|
---|
148 | /*
|
---|
149 | * Calc/fake the returned values.
|
---|
150 | */
|
---|
151 | pProperties->cbMaxComponent = StatVFS.f_namemax;
|
---|
152 | pProperties->fCaseSensitive = true;
|
---|
153 | pProperties->fCompressed = false;
|
---|
154 | pProperties->fFileCompression = false;
|
---|
155 | pProperties->fReadOnly = !!(StatVFS.f_flag & ST_RDONLY);
|
---|
156 | pProperties->fRemote = false;
|
---|
157 | pProperties->fSupportsUnicode = true;
|
---|
158 | }
|
---|
159 | else
|
---|
160 | rc = RTErrConvertFromErrno(errno);
|
---|
161 | rtPathFreeNative(pszNativeFsPath, pszFsPath);
|
---|
162 | }
|
---|
163 |
|
---|
164 | LogFlow(("RTFsQueryProperties(%p:{%s}, %p:{.cbMaxComponent=%u, .fCaseSensitive=%RTbool}): returns %Rrc\n",
|
---|
165 | pszFsPath, pszFsPath, pProperties, pProperties->cbMaxComponent, pProperties->fReadOnly));
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | RTR3DECL(int) RTFsQueryType(const char *pszFsPath, uint32_t *pu32Type)
|
---|
171 | {
|
---|
172 | /*
|
---|
173 | * Validate input.
|
---|
174 | */
|
---|
175 | AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Convert the path and query the stats.
|
---|
179 | * We're simply return the device id.
|
---|
180 | */
|
---|
181 | char const *pszNativeFsPath;
|
---|
182 | int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | {
|
---|
185 | *pu32Type = RTFS_FS_TYPE_UNKNOWN;
|
---|
186 |
|
---|
187 | struct stat Stat;
|
---|
188 | if (!stat(pszNativeFsPath, &Stat))
|
---|
189 | {
|
---|
190 | #if defined(RT_OS_LINUX)
|
---|
191 | FILE *mounted = setmntent("/proc/mounts", "r");
|
---|
192 | if (!mounted)
|
---|
193 | mounted = setmntent("/etc/mtab", "r");
|
---|
194 | if (mounted)
|
---|
195 | {
|
---|
196 | char szBuf[1024];
|
---|
197 | struct stat mntStat;
|
---|
198 | struct mntent mntEnt;
|
---|
199 | while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
|
---|
200 | {
|
---|
201 | if (!stat(mntEnt.mnt_dir, &mntStat))
|
---|
202 | {
|
---|
203 | if (mntStat.st_dev == Stat.st_dev)
|
---|
204 | {
|
---|
205 | if (!strcmp("ext4", mntEnt.mnt_type))
|
---|
206 | *pu32Type = RTFS_FS_TYPE_EXT4;
|
---|
207 | else if (!strcmp("ext3", mntEnt.mnt_type))
|
---|
208 | *pu32Type = RTFS_FS_TYPE_EXT3;
|
---|
209 | else if (!strcmp("ext2", mntEnt.mnt_type))
|
---|
210 | *pu32Type = RTFS_FS_TYPE_EXT2;
|
---|
211 | else if ( !strcmp("vfat", mntEnt.mnt_type)
|
---|
212 | || !strcmp("msdos", mntEnt.mnt_type))
|
---|
213 | *pu32Type = RTFS_FS_TYPE_FAT;
|
---|
214 | else if (!strcmp("tmpfs", mntEnt.mnt_type))
|
---|
215 | *pu32Type = RTFS_FS_TYPE_TMPFS;
|
---|
216 | else
|
---|
217 | {
|
---|
218 | /* sometimes there are more than one entry for the same partition */
|
---|
219 | continue;
|
---|
220 | }
|
---|
221 | break;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | endmntent(mounted);
|
---|
226 | }
|
---|
227 | #elif defined(RT_OS_SOLARIS)
|
---|
228 | if (!strcmp("zfs", Stat.st_fstype))
|
---|
229 | *pu32Type = RTFS_FS_TYPE_ZFS;
|
---|
230 | #endif
|
---|
231 | }
|
---|
232 | else
|
---|
233 | rc = RTErrConvertFromErrno(errno);
|
---|
234 | rtPathFreeNative(pszNativeFsPath, pszFsPath);
|
---|
235 | }
|
---|
236 |
|
---|
237 | return rc;
|
---|
238 | }
|
---|