VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/fs.cpp@ 32048

Last change on this file since 32048 was 30365, checked in by vboxsync, 15 years ago

RTFsQueryType: Use an enum. Added RTFsTypeName() for translating a enum value into a string. Added more file system types.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.2 KB
Line 
1/* $Id: fs.cpp 30365 2010-06-22 12:08:20Z vboxsync $ */
2/** @file
3 * IPRT - File System.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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#ifndef RT_OS_WINDOWS
32# define RTTIME_INCL_TIMESPEC
33# include <sys/time.h>
34# include <sys/param.h>
35# ifndef DEV_BSIZE
36# include <sys/stat.h>
37# define DEV_BSIZE S_BLKSIZE /** @todo bird: add DEV_BSIZE to sys/param.h on OS/2. */
38# endif
39#endif
40
41#include <iprt/fs.h>
42#include "internal/iprt.h"
43
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/ctype.h>
47#include <iprt/path.h>
48#include <iprt/string.h>
49#include <iprt/time.h>
50#include "internal/fs.h"
51
52
53/**
54 * Converts dos-style attributes to Unix attributes.
55 *
56 * @returns
57 * @param fMode The mode mask containing dos-style attibutes only.
58 * @param pszName The filename which this applies to (exe check).
59 * @param cbName The length of that filename. (optional, set 0)
60 */
61RTFMODE rtFsModeFromDos(RTFMODE fMode, const char *pszName, size_t cbName)
62{
63 fMode &= ~((1 << RTFS_DOS_SHIFT) - 1);
64
65 /* everything is readable. */
66 fMode |= RTFS_UNIX_IRUSR | RTFS_UNIX_IRGRP | RTFS_UNIX_IROTH;
67 if (fMode & RTFS_DOS_DIRECTORY)
68 /* directories are executable. */
69 fMode |= RTFS_TYPE_DIRECTORY | RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
70 else
71 {
72 fMode |= RTFS_TYPE_FILE;
73 if (!cbName && pszName)
74 cbName = strlen(pszName);
75 if (cbName >= 4 && pszName[cbName - 4] == '.')
76 {
77 /* check for executable extension. */
78 const char *pszExt = &pszName[cbName - 3];
79 char szExt[4];
80 szExt[0] = RT_C_TO_LOWER(pszExt[0]);
81 szExt[1] = RT_C_TO_LOWER(pszExt[1]);
82 szExt[2] = RT_C_TO_LOWER(pszExt[2]);
83 szExt[3] = '\0';
84 if ( !memcmp(szExt, "exe", 4)
85 || !memcmp(szExt, "bat", 4)
86 || !memcmp(szExt, "com", 4)
87 || !memcmp(szExt, "cmd", 4)
88 || !memcmp(szExt, "btm", 4)
89 )
90 fMode |= RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
91 }
92 }
93 /* writable? */
94 if (!(fMode & RTFS_DOS_READONLY))
95 fMode |= RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH;
96 return fMode;
97}
98
99
100/**
101 * Converts Unix attributes to Dos-style attributes.
102 *
103 * @returns File mode mask.
104 * @param fMode The mode mask containing dos-style attibutes only.
105 * @param pszName The filename which this applies to (hidden check).
106 * @param cbName The length of that filename. (optional, set 0)
107 */
108RTFMODE rtFsModeFromUnix(RTFMODE fMode, const char *pszName, size_t cbName)
109{
110 fMode &= RTFS_UNIX_MASK;
111
112 if (!(fMode & (RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH)))
113 fMode |= RTFS_DOS_READONLY;
114 if (RTFS_IS_DIRECTORY(fMode))
115 fMode |= RTFS_DOS_DIRECTORY;
116 if (!(fMode & RTFS_DOS_MASK))
117 fMode |= RTFS_DOS_NT_NORMAL;
118 if (!(fMode & RTFS_DOS_HIDDEN) && pszName)
119 {
120 pszName = RTPathFilename(pszName);
121 if (pszName && *pszName == '.')
122 fMode |= RTFS_DOS_HIDDEN;
123 }
124 return fMode;
125}
126
127
128/**
129 * Normalizes the give mode mask.
130 *
131 * It will create the missing unix or dos mask from the other (one
132 * of them is required by all APIs), and guess the file type if that's
133 * missing.
134 *
135 * @returns Normalized file mode.
136 * @param fMode The mode mask that may contain a partial/incomplete mask.
137 * @param pszName The filename which this applies to (exe check).
138 * @param cbName The length of that filename. (optional, set 0)
139 */
140RTFMODE rtFsModeNormalize(RTFMODE fMode, const char *pszName, size_t cbName)
141{
142 if (!(fMode & RTFS_UNIX_MASK))
143 fMode = rtFsModeFromDos(fMode, pszName, cbName);
144 else if (!(fMode & RTFS_DOS_MASK))
145 fMode = rtFsModeFromUnix(fMode, pszName, cbName);
146 else if (!(fMode & RTFS_TYPE_MASK))
147 fMode |= fMode & RTFS_DOS_DIRECTORY ? RTFS_TYPE_DIRECTORY : RTFS_TYPE_FILE;
148 else if (RTFS_IS_DIRECTORY(fMode))
149 fMode |= RTFS_DOS_DIRECTORY;
150 return fMode;
151}
152
153
154/**
155 * Checks if the file mode is valid or not.
156 *
157 * @return true if valid.
158 * @return false if invalid, done bitching.
159 * @param fMode The file mode.
160 */
161bool rtFsModeIsValid(RTFMODE fMode)
162{
163 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
164 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
165 ("%RTfmode\n", fMode), false);
166 AssertMsgReturn(RTFS_TYPE_MASK & fMode,
167 ("%RTfmode\n", fMode), false);
168 /** @todo more checks! */
169 return true;
170}
171
172
173/**
174 * Checks if the file mode is valid as a permission mask or not.
175 *
176 * @return true if valid.
177 * @return false if invalid, done bitching.
178 * @param fMode The file mode.
179 */
180bool rtFsModeIsValidPermissions(RTFMODE fMode)
181{
182 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
183 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
184 ("%RTfmode\n", fMode), false);
185 /** @todo more checks! */
186 return true;
187}
188
189
190#ifndef RT_OS_WINDOWS
191/**
192 * Internal worker function which setups RTFSOBJINFO based on a UNIX stat struct.
193 *
194 * @param pObjInfo The file system object info structure to setup.
195 * @param pStat The stat structure to use.
196 * @param pszName The filename which this applies to (exe/hidden check).
197 * @param cbName The length of that filename. (optional, set 0)
198 */
199void rtFsConvertStatToObjInfo(PRTFSOBJINFO pObjInfo, const struct stat *pStat, const char *pszName, unsigned cbName)
200{
201 pObjInfo->cbObject = pStat->st_size;
202 pObjInfo->cbAllocated = pStat->st_blocks * DEV_BSIZE;
203
204#ifdef HAVE_STAT_NSEC
205 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->AccessTime, pStat->st_atime), pStat->st_atimensec);
206 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->ModificationTime, pStat->st_mtime), pStat->st_mtimensec);
207 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->ChangeTime, pStat->st_ctime), pStat->st_ctimensec);
208#ifdef HAVE_STAT_BIRTHTIME
209 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->BirthTime, pStat->st_birthtime), pStat->st_birthtimensec);
210#endif
211
212#elif defined(HAVE_STAT_TIMESPEC_BRIEF)
213 RTTimeSpecSetTimespec(&pObjInfo->AccessTime, &pStat->st_atim);
214 RTTimeSpecSetTimespec(&pObjInfo->ModificationTime, &pStat->st_mtim);
215 RTTimeSpecSetTimespec(&pObjInfo->ChangeTime, &pStat->st_ctim);
216# ifdef HAVE_STAT_BIRTHTIME
217 RTTimeSpecSetTimespec(&pObjInfo->BirthTime, &pStat->st_birthtim);
218# endif
219
220#elif defined(HAVE_STAT_TIMESPEC)
221 RTTimeSpecSetTimespec(&pObjInfo->AccessTime, pStat->st_atimespec);
222 RTTimeSpecSetTimespec(&pObjInfo->ModificationTime, pStat->st_mtimespec);
223 RTTimeSpecSetTimespec(&pObjInfo->ChangeTime, pStat->st_ctimespec);
224# ifdef HAVE_STAT_BIRTHTIME
225 RTTimeSpecSetTimespec(&pObjInfo->BirthTime, pStat->st_birthtimespec);
226# endif
227
228#else /* just the normal stuff */
229 RTTimeSpecSetSeconds(&pObjInfo->AccessTime, pStat->st_atime);
230 RTTimeSpecSetSeconds(&pObjInfo->ModificationTime, pStat->st_mtime);
231 RTTimeSpecSetSeconds(&pObjInfo->ChangeTime, pStat->st_ctime);
232# ifdef HAVE_STAT_BIRTHTIME
233 RTTimeSpecSetSeconds(&pObjInfo->BirthTime, pStat->st_birthtime);
234# endif
235#endif
236#ifndef HAVE_STAT_BIRTHTIME
237 pObjInfo->BirthTime = pObjInfo->ChangeTime;
238#endif
239
240
241 /* the file mode */
242 RTFMODE fMode = pStat->st_mode & RTFS_UNIX_MASK;
243 Assert(RTFS_UNIX_ISUID == S_ISUID);
244 Assert(RTFS_UNIX_ISGID == S_ISGID);
245#ifdef S_ISTXT
246 Assert(RTFS_UNIX_ISTXT == S_ISTXT);
247#elif defined(S_ISVTX)
248 Assert(RTFS_UNIX_ISTXT == S_ISVTX);
249#else
250#error "S_ISVTX / S_ISTXT isn't defined"
251#endif
252 Assert(RTFS_UNIX_IRWXU == S_IRWXU);
253 Assert(RTFS_UNIX_IRUSR == S_IRUSR);
254 Assert(RTFS_UNIX_IWUSR == S_IWUSR);
255 Assert(RTFS_UNIX_IXUSR == S_IXUSR);
256 Assert(RTFS_UNIX_IRWXG == S_IRWXG);
257 Assert(RTFS_UNIX_IRGRP == S_IRGRP);
258 Assert(RTFS_UNIX_IWGRP == S_IWGRP);
259 Assert(RTFS_UNIX_IXGRP == S_IXGRP);
260 Assert(RTFS_UNIX_IRWXO == S_IRWXO);
261 Assert(RTFS_UNIX_IROTH == S_IROTH);
262 Assert(RTFS_UNIX_IWOTH == S_IWOTH);
263 Assert(RTFS_UNIX_IXOTH == S_IXOTH);
264 Assert(RTFS_TYPE_FIFO == S_IFIFO);
265 Assert(RTFS_TYPE_DEV_CHAR == S_IFCHR);
266 Assert(RTFS_TYPE_DIRECTORY == S_IFDIR);
267 Assert(RTFS_TYPE_DEV_BLOCK == S_IFBLK);
268 Assert(RTFS_TYPE_FILE == S_IFREG);
269 Assert(RTFS_TYPE_SYMLINK == S_IFLNK);
270 Assert(RTFS_TYPE_SOCKET == S_IFSOCK);
271#ifdef S_IFWHT
272 Assert(RTFS_TYPE_WHITEOUT == S_IFWHT);
273#endif
274 Assert(RTFS_TYPE_MASK == S_IFMT);
275
276 pObjInfo->Attr.fMode = rtFsModeFromUnix(fMode, pszName, cbName);
277
278 /* additional unix attribs */
279 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
280 pObjInfo->Attr.u.Unix.uid = pStat->st_uid;
281 pObjInfo->Attr.u.Unix.gid = pStat->st_gid;
282 pObjInfo->Attr.u.Unix.cHardlinks = pStat->st_nlink;
283 pObjInfo->Attr.u.Unix.INodeIdDevice = pStat->st_dev;
284 pObjInfo->Attr.u.Unix.INodeId = pStat->st_ino;
285#ifdef HAVE_STAT_FLAGS
286 pObjInfo->Attr.u.Unix.fFlags = pStat->st_flags;
287#else
288 pObjInfo->Attr.u.Unix.fFlags = 0;
289#endif
290#ifdef HAVE_STAT_GEN
291 pObjInfo->Attr.u.Unix.GenerationId = pStat->st_gen;
292#else
293 pObjInfo->Attr.u.Unix.GenerationId = 0;
294#endif
295 pObjInfo->Attr.u.Unix.Device = pStat->st_rdev;
296}
297#endif /* !RT_OS_WINDOWS */
298
299
300RTDECL(const char *) RTFsTypeName(RTFSTYPE enmType)
301{
302 switch (enmType)
303 {
304 case RTFSTYPE_UNKNOWN: return "unknown";
305 case RTFSTYPE_UDF: return "udf";
306 case RTFSTYPE_ISO9660: return "iso9660";
307 case RTFSTYPE_FUSE: return "fuse";
308 case RTFSTYPE_VBOXSHF: return "vboxshf";
309
310 case RTFSTYPE_EXT: return "ext";
311 case RTFSTYPE_EXT2: return "ext2";
312 case RTFSTYPE_EXT3: return "ext3";
313 case RTFSTYPE_EXT4: return "ext4";
314 case RTFSTYPE_XFS: return "xfs";
315 case RTFSTYPE_CIFS: return "cifs";
316 case RTFSTYPE_SMBFS: return "smbfs";
317 case RTFSTYPE_TMPFS: return "tmpfs";
318 case RTFSTYPE_SYSFS: return "sysfs";
319 case RTFSTYPE_PROC: return "proc";
320
321 case RTFSTYPE_NTFS: return "ntfs";
322 case RTFSTYPE_FAT: return "fat";
323
324 case RTFSTYPE_ZFS: return "zfs";
325 case RTFSTYPE_UFS: return "ufs";
326 case RTFSTYPE_NFS: return "nfs";
327
328 case RTFSTYPE_HFS: return "hfs";
329 case RTFSTYPE_AUTOFS: return "autofs";
330 case RTFSTYPE_DEVFS: return "devfs";
331
332 case RTFSTYPE_HPFS: return "hpfs";
333 case RTFSTYPE_JFS: return "jfs";
334
335 case RTFSTYPE_END: return "end";
336 case RTFSTYPE_32BIT_HACK: break;
337 }
338
339 /* Don't put this in as 'default:', we wish GCC to warn about missing cases. */
340 static char s_asz[4][64];
341 static uint32_t volatile s_i = 0;
342 uint32_t i = ASMAtomicIncU32(&s_i) % RT_ELEMENTS(s_asz);
343 RTStrPrintf(s_asz[i], sizeof(s_asz[i]), "type=%d", enmType);
344 return s_asz[i];
345}
346
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