VirtualBox

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

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

IPRT,UINetworkReply.cpp: Added RTPathGlob, a set of RTCrStoreCertAddWantedDir/File/Store, a RTCrStoreCertAddWantedFromFishingExpedition, RTCrStoreCertCheckWanted, RTCrStoreCertCount, RTFsIsCaseSensitive and RTFileOpenTemp. Reworked some RTHttp bits and UINetworkReply stuff - this needs testing.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.3 KB
Line 
1/* $Id: fs-posix.cpp 57613 2015-09-04 02:19:44Z 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#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#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
180 return false;
181#else
182 return true;
183#endif
184}
185
186
187RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
188{
189 *penmType = RTFSTYPE_UNKNOWN;
190
191 /*
192 * Validate input.
193 */
194 AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
195 AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
196
197 /*
198 * Convert the path and query the stats.
199 * We're simply return the device id.
200 */
201 char const *pszNativeFsPath;
202 int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
203 if (RT_SUCCESS(rc))
204 {
205 struct stat Stat;
206 if (!stat(pszNativeFsPath, &Stat))
207 {
208#if defined(RT_OS_LINUX)
209 FILE *mounted = setmntent("/proc/mounts", "r");
210 if (!mounted)
211 mounted = setmntent("/etc/mtab", "r");
212 if (mounted)
213 {
214 char szBuf[1024];
215 struct stat mntStat;
216 struct mntent mntEnt;
217 while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
218 {
219 if (!stat(mntEnt.mnt_dir, &mntStat))
220 {
221 if (mntStat.st_dev == Stat.st_dev)
222 {
223 if (!strcmp("ext4", mntEnt.mnt_type))
224 *penmType = RTFSTYPE_EXT4;
225 else if (!strcmp("ext3", mntEnt.mnt_type))
226 *penmType = RTFSTYPE_EXT3;
227 else if (!strcmp("ext2", mntEnt.mnt_type))
228 *penmType = RTFSTYPE_EXT2;
229 else if (!strcmp("jfs", mntEnt.mnt_type))
230 *penmType = RTFSTYPE_JFS;
231 else if (!strcmp("xfs", mntEnt.mnt_type))
232 *penmType = RTFSTYPE_XFS;
233 else if (!strcmp("btrfs", mntEnt.mnt_type))
234 *penmType = RTFSTYPE_BTRFS;
235 else if ( !strcmp("vfat", mntEnt.mnt_type)
236 || !strcmp("msdos", mntEnt.mnt_type))
237 *penmType = RTFSTYPE_FAT;
238 else if (!strcmp("ntfs", mntEnt.mnt_type))
239 *penmType = RTFSTYPE_NTFS;
240 else if (!strcmp("hpfs", mntEnt.mnt_type))
241 *penmType = RTFSTYPE_HPFS;
242 else if (!strcmp("ufs", mntEnt.mnt_type))
243 *penmType = RTFSTYPE_UFS;
244 else if (!strcmp("tmpfs", mntEnt.mnt_type))
245 *penmType = RTFSTYPE_TMPFS;
246 else if (!strcmp("hfsplus", mntEnt.mnt_type))
247 *penmType = RTFSTYPE_HFS;
248 else if (!strcmp("udf", mntEnt.mnt_type))
249 *penmType = RTFSTYPE_UDF;
250 else if (!strcmp("iso9660", mntEnt.mnt_type))
251 *penmType = RTFSTYPE_ISO9660;
252 else if (!strcmp("smbfs", mntEnt.mnt_type))
253 *penmType = RTFSTYPE_SMBFS;
254 else if (!strcmp("cifs", mntEnt.mnt_type))
255 *penmType = RTFSTYPE_CIFS;
256 else if (!strcmp("nfs", mntEnt.mnt_type))
257 *penmType = RTFSTYPE_NFS;
258 else if (!strcmp("nfs4", mntEnt.mnt_type))
259 *penmType = RTFSTYPE_NFS;
260 else if (!strcmp("ocfs2", mntEnt.mnt_type))
261 *penmType = RTFSTYPE_OCFS2;
262 else if (!strcmp("sysfs", mntEnt.mnt_type))
263 *penmType = RTFSTYPE_SYSFS;
264 else if (!strcmp("proc", mntEnt.mnt_type))
265 *penmType = RTFSTYPE_PROC;
266 else if ( !strcmp("fuse", mntEnt.mnt_type)
267 || !strncmp("fuse.", mntEnt.mnt_type, 5)
268 || !strcmp("fuseblk", mntEnt.mnt_type))
269 *penmType = RTFSTYPE_FUSE;
270 else
271 {
272 /* sometimes there are more than one entry for the same partition */
273 continue;
274 }
275 break;
276 }
277 }
278 }
279 endmntent(mounted);
280 }
281
282#elif defined(RT_OS_SOLARIS)
283 if (!strcmp("zfs", Stat.st_fstype))
284 *penmType = RTFSTYPE_ZFS;
285 else if (!strcmp("ufs", Stat.st_fstype))
286 *penmType = RTFSTYPE_UFS;
287 else if (!strcmp("nfs", Stat.st_fstype))
288 *penmType = RTFSTYPE_NFS;
289
290#elif defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
291 struct statfs statfsBuf;
292 if (!statfs(pszNativeFsPath, &statfsBuf))
293 {
294 if (!strcmp("hfs", statfsBuf.f_fstypename))
295 *penmType = RTFSTYPE_HFS;
296 else if ( !strcmp("fat", statfsBuf.f_fstypename)
297 || !strcmp("msdos", statfsBuf.f_fstypename))
298 *penmType = RTFSTYPE_FAT;
299 else if (!strcmp("ntfs", statfsBuf.f_fstypename))
300 *penmType = RTFSTYPE_NTFS;
301 else if (!strcmp("autofs", statfsBuf.f_fstypename))
302 *penmType = RTFSTYPE_AUTOFS;
303 else if (!strcmp("devfs", statfsBuf.f_fstypename))
304 *penmType = RTFSTYPE_DEVFS;
305 else if (!strcmp("nfs", statfsBuf.f_fstypename))
306 *penmType = RTFSTYPE_NFS;
307 else if (!strcmp("ufs", statfsBuf.f_fstypename))
308 *penmType = RTFSTYPE_UFS;
309 else if (!strcmp("zfs", statfsBuf.f_fstypename))
310 *penmType = RTFSTYPE_ZFS;
311 }
312 else
313 rc = RTErrConvertFromErrno(errno);
314#endif
315 }
316 else
317 rc = RTErrConvertFromErrno(errno);
318 rtPathFreeNative(pszNativeFsPath, pszFsPath);
319 }
320
321 return rc;
322}
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