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