VirtualBox

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

Last change on this file since 980 was 1, checked in by vboxsync, 55 years ago

import

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