VirtualBox

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

Last change on this file since 24584 was 24287, checked in by vboxsync, 15 years ago

Made the common parts of the OS/2 additions build again.

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