VirtualBox

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

Last change on this file since 8170 was 8170, checked in by vboxsync, 16 years ago

Rebranding: replacing more innotek strings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1/* $Id: fs.cpp 8170 2008-04-18 17:52:25Z vboxsync $ */
2/** @file
3 * Incredibly Portable Runtime - File System.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#ifndef RT_OS_WINDOWS
36# define RTTIME_INCL_TIMESPEC
37# include <sys/time.h>
38#endif
39
40#include <iprt/fs.h>
41#include <iprt/assert.h>
42#include <iprt/time.h>
43#include <iprt/string.h>
44#include <iprt/path.h>
45#include <iprt/ctype.h>
46#include "internal/fs.h"
47
48
49/**
50 * Converts dos-style attributes to Unix attributes.
51 *
52 * @returns
53 * @param fMode The mode mask containing dos-style attibutes only.
54 * @param pszName The filename which this applies to (exe check).
55 * @param cbName The length of that filename. (optional, set 0)
56 */
57RTFMODE rtFsModeFromDos(RTFMODE fMode, const char *pszName, unsigned cbName)
58{
59 fMode &= ~((1 << RTFS_DOS_SHIFT) - 1);
60
61 /* everything is readable. */
62 fMode |= RTFS_UNIX_IRUSR | RTFS_UNIX_IRGRP | RTFS_UNIX_IROTH;
63 if (fMode & RTFS_DOS_DIRECTORY)
64 /* directories are executable. */
65 fMode |= RTFS_TYPE_DIRECTORY | RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
66 else
67 {
68 fMode |= RTFS_TYPE_FILE;
69 if (!cbName && pszName)
70 cbName = strlen(pszName);
71 if (cbName >= 4 && pszName[cbName - 4] == '.')
72 {
73 /* check for executable extension. */
74 const char *pszExt = &pszName[cbName - 3];
75 char szExt[4];
76 szExt[0] = tolower(pszExt[0]);
77 szExt[1] = tolower(pszExt[1]);
78 szExt[2] = tolower(pszExt[2]);
79 szExt[3] = '\0';
80 if ( !memcmp(szExt, "exe", 4)
81 || !memcmp(szExt, "bat", 4)
82 || !memcmp(szExt, "com", 4)
83 || !memcmp(szExt, "cmd", 4)
84 || !memcmp(szExt, "btm", 4)
85 )
86 fMode |= RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
87 }
88 }
89 /* writable? */
90 if (!(fMode & RTFS_DOS_READONLY))
91 fMode |= RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH;
92 return fMode;
93}
94
95
96/**
97 * Converts Unix attributes to Dos-style attributes.
98 *
99 * @returns File mode mask.
100 * @param fMode The mode mask containing dos-style attibutes only.
101 * @param pszName The filename which this applies to (hidden check).
102 * @param cbName The length of that filename. (optional, set 0)
103 */
104RTFMODE rtFsModeFromUnix(RTFMODE fMode, const char *pszName, unsigned cbName)
105{
106 fMode &= RTFS_UNIX_MASK;
107
108 if (!(fMode & (RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH)))
109 fMode |= RTFS_DOS_READONLY;
110 if (RTFS_IS_DIRECTORY(fMode))
111 fMode |= RTFS_DOS_DIRECTORY;
112 if (!(fMode & RTFS_DOS_MASK))
113 fMode |= RTFS_DOS_NT_NORMAL;
114 if (!(fMode & RTFS_DOS_HIDDEN) && pszName)
115 {
116 pszName = RTPathFilename(pszName);
117 if (pszName && *pszName == '.')
118 fMode |= RTFS_DOS_HIDDEN;
119 }
120 return fMode;
121}
122
123
124/**
125 * Converts dos-style attributes to Unix attributes.
126 *
127 * @returns Normalized file mode.
128 * @param fMode The mode mask containing dos-style attibutes only.
129 * @param pszName The filename which this applies to (exe check).
130 * @param cbName The length of that filename. (optional, set 0)
131 */
132RTFMODE rtFsModeNormalize(RTFMODE fMode, const char *pszName, unsigned cbName)
133{
134 if (!(fMode & RTFS_UNIX_MASK))
135 rtFsModeFromDos(fMode, pszName, cbName);
136 else if (!(fMode & RTFS_DOS_MASK))
137 rtFsModeFromUnix(fMode, pszName, cbName);
138 else if (!(fMode & RTFS_TYPE_MASK))
139 fMode |= fMode & RTFS_DOS_DIRECTORY ? RTFS_TYPE_DIRECTORY : RTFS_TYPE_FILE;
140 else if (RTFS_IS_DIRECTORY(fMode))
141 fMode |= RTFS_DOS_DIRECTORY;
142 return fMode;
143}
144
145
146/**
147 * Checks if the file mode is valid or not.
148 *
149 * @return true if valid.
150 * @return false if invalid, done bitching.
151 * @param fMode The file mode.
152 */
153bool rtFsModeIsValid(RTFMODE fMode)
154{
155 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
156 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
157 ("%RTfmode\n", fMode), false);
158 AssertMsgReturn(RTFS_TYPE_MASK & fMode,
159 ("%RTfmode\n", fMode), false);
160 /** @todo more checks! */
161 return true;
162}
163
164
165/**
166 * Checks if the file mode is valid as a permission mask or not.
167 *
168 * @return true if valid.
169 * @return false if invalid, done bitching.
170 * @param fMode The file mode.
171 */
172bool rtFsModeIsValidPermissions(RTFMODE fMode)
173{
174 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
175 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
176 ("%RTfmode\n", fMode), false);
177 /** @todo more checks! */
178 return true;
179}
180
181
182#ifndef RT_OS_WINDOWS
183/**
184 * Internal worker function which setups RTFSOBJINFO based on a UNIX stat struct.
185 *
186 * @param pObjInfo The file system object info structure to setup.
187 * @param pStat The stat structure to use.
188 * @param pszName The filename which this applies to (exe/hidden check).
189 * @param cbName The length of that filename. (optional, set 0)
190 */
191void rtFsConvertStatToObjInfo(PRTFSOBJINFO pObjInfo, const struct stat *pStat, const char *pszName, unsigned cbName)
192{
193 pObjInfo->cbObject = pStat->st_size;
194 pObjInfo->cbAllocated = pStat->st_size;
195
196#ifdef HAVE_STAT_NSEC
197 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->AccessTime, pStat->st_atime), pStat->st_atimensec);
198 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->ModificationTime, pStat->st_mtime), pStat->st_mtimensec);
199 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->ChangeTime, pStat->st_ctime), pStat->st_ctimensec);
200#ifdef HAVE_STAT_BIRTHTIME
201 RTTimeSpecAddNano(RTTimeSpecSetSeconds(&pObjInfo->BirthTime, pStat->st_birthtime), pStat->st_birthtimensec);
202#endif
203
204#elif defined(HAVE_STAT_TIMESPEC_BRIEF)
205 RTTimeSpecSetTimespec(&pObjInfo->AccessTime, &pStat->st_atim);
206 RTTimeSpecSetTimespec(&pObjInfo->ModificationTime, &pStat->st_mtim);
207 RTTimeSpecSetTimespec(&pObjInfo->ChangeTime, &pStat->st_ctim);
208# ifdef HAVE_STAT_BIRTHTIME
209 RTTimeSpecSetTimespec(&pObjInfo->BirthTime, &pStat->st_birthtim);
210# endif
211
212#elif defined(HAVE_STAT_TIMESPEC)
213 RTTimeSpecSetTimespec(&pObjInfo->AccessTime, pStat->st_atimespec);
214 RTTimeSpecSetTimespec(&pObjInfo->ModificationTime, pStat->st_mtimespec);
215 RTTimeSpecSetTimespec(&pObjInfo->ChangeTime, pStat->st_ctimespec);
216# ifdef HAVE_STAT_BIRTHTIME
217 RTTimeSpecSetTimespec(&pObjInfo->BirthTime, pStat->st_birthtimespec);
218# endif
219
220#else /* just the normal stuff */
221 RTTimeSpecSetSeconds(&pObjInfo->AccessTime, pStat->st_atime);
222 RTTimeSpecSetSeconds(&pObjInfo->ModificationTime, pStat->st_mtime);
223 RTTimeSpecSetSeconds(&pObjInfo->ChangeTime, pStat->st_ctime);
224# ifdef HAVE_STAT_BIRTHTIME
225 RTTimeSpecSetSeconds(&pObjInfo->BirthTime, pStat->st_birthtime);
226# endif
227#endif
228#ifndef HAVE_STAT_BIRTHTIME
229 pObjInfo->BirthTime = pObjInfo->ChangeTime;
230#endif
231
232
233 /* the file mode */
234 RTFMODE fMode = pStat->st_mode & RTFS_UNIX_MASK;
235 Assert(RTFS_UNIX_ISUID == S_ISUID);
236 Assert(RTFS_UNIX_ISGID == S_ISGID);
237#ifdef S_ISTXT
238 Assert(RTFS_UNIX_ISTXT == S_ISTXT);
239#elif defined(S_ISVTX)
240 Assert(RTFS_UNIX_ISTXT == S_ISVTX);
241#else
242#error "S_ISVTX / S_ISTXT isn't defined"
243#endif
244 Assert(RTFS_UNIX_IRWXU == S_IRWXU);
245 Assert(RTFS_UNIX_IRUSR == S_IRUSR);
246 Assert(RTFS_UNIX_IWUSR == S_IWUSR);
247 Assert(RTFS_UNIX_IXUSR == S_IXUSR);
248 Assert(RTFS_UNIX_IRWXG == S_IRWXG);
249 Assert(RTFS_UNIX_IRGRP == S_IRGRP);
250 Assert(RTFS_UNIX_IWGRP == S_IWGRP);
251 Assert(RTFS_UNIX_IXGRP == S_IXGRP);
252 Assert(RTFS_UNIX_IRWXO == S_IRWXO);
253 Assert(RTFS_UNIX_IROTH == S_IROTH);
254 Assert(RTFS_UNIX_IWOTH == S_IWOTH);
255 Assert(RTFS_UNIX_IXOTH == S_IXOTH);
256 Assert(RTFS_TYPE_FIFO == S_IFIFO);
257 Assert(RTFS_TYPE_DEV_CHAR == S_IFCHR);
258 Assert(RTFS_TYPE_DIRECTORY == S_IFDIR);
259 Assert(RTFS_TYPE_DEV_BLOCK == S_IFBLK);
260 Assert(RTFS_TYPE_FILE == S_IFREG);
261 Assert(RTFS_TYPE_SYMLINK == S_IFLNK);
262 Assert(RTFS_TYPE_SOCKET == S_IFSOCK);
263#ifdef S_IFWHT
264 Assert(RTFS_TYPE_WHITEOUT == S_IFWHT);
265#endif
266 Assert(RTFS_TYPE_MASK == S_IFMT);
267
268 pObjInfo->Attr.fMode = rtFsModeFromUnix(fMode, pszName, cbName);
269
270 /* additional unix attribs */
271 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
272 pObjInfo->Attr.u.Unix.uid = pStat->st_uid;
273 pObjInfo->Attr.u.Unix.gid = pStat->st_gid;
274 pObjInfo->Attr.u.Unix.cHardlinks = pStat->st_nlink;
275 pObjInfo->Attr.u.Unix.INodeIdDevice = pStat->st_dev;
276 pObjInfo->Attr.u.Unix.INodeId = pStat->st_ino;
277#ifdef HAVE_STAT_FLAGS
278 pObjInfo->Attr.u.Unix.fFlags = pStat->st_flags;
279#else
280 pObjInfo->Attr.u.Unix.fFlags = 0;
281#endif
282#ifdef HAVE_STAT_GEN
283 pObjInfo->Attr.u.Unix.GenerationId = pStat->st_gen;
284#else
285 pObjInfo->Attr.u.Unix.GenerationId = 0;
286#endif
287 pObjInfo->Attr.u.Unix.Device = pStat->st_rdev;
288}
289
290#endif /* !RT_OS_WINDOWS */
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