VirtualBox

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

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

use DEV_BSIZE instead of hardcoded 512 bytes in r53300

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