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