VirtualBox

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

Last change on this file since 33463 was 33437, checked in by vboxsync, 14 years ago

iprt: Made tstRTSymlink work on Windows.

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