Diffstat (limited to 'qmake/tools/qfileinfo_unix.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | qmake/tools/qfileinfo_unix.cpp | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/qmake/tools/qfileinfo_unix.cpp b/qmake/tools/qfileinfo_unix.cpp new file mode 100644 index 0000000..f7c3a97 --- a/dev/null +++ b/qmake/tools/qfileinfo_unix.cpp | |||
@@ -0,0 +1,331 @@ | |||
1 | /**************************************************************************** | ||
2 | ** $Id$ | ||
3 | ** | ||
4 | ** Implementation of QFileInfo class | ||
5 | ** | ||
6 | ** Created : 950628 | ||
7 | ** | ||
8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. | ||
9 | ** | ||
10 | ** This file is part of the tools module of the Qt GUI Toolkit. | ||
11 | ** | ||
12 | ** This file may be distributed under the terms of the Q Public License | ||
13 | ** as defined by Trolltech AS of Norway and appearing in the file | ||
14 | ** LICENSE.QPL included in the packaging of this file. | ||
15 | ** | ||
16 | ** This file may be distributed and/or modified under the terms of the | ||
17 | ** GNU General Public License version 2 as published by the Free Software | ||
18 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
19 | ** packaging of this file. | ||
20 | ** | ||
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | ||
22 | ** licenses for Unix/X11 or for Qt/Embedded may use this file in accordance | ||
23 | ** with the Qt Commercial License Agreement provided with the Software. | ||
24 | ** | ||
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
27 | ** | ||
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | ||
29 | ** information about Qt Commercial License Agreements. | ||
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | ||
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
32 | ** | ||
33 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
34 | ** not clear to you. | ||
35 | ** | ||
36 | **********************************************************************/ | ||
37 | |||
38 | #include "qplatformdefs.h" | ||
39 | #include "qfileinfo.h" | ||
40 | #include "qfiledefs_p.h" | ||
41 | #include "qdatetime.h" | ||
42 | #include "qdir.h" | ||
43 | |||
44 | #include <limits.h> | ||
45 | |||
46 | |||
47 | void QFileInfo::slashify( QString& ) | ||
48 | { | ||
49 | return; | ||
50 | } | ||
51 | |||
52 | |||
53 | void QFileInfo::makeAbs( QString & ) | ||
54 | { | ||
55 | return; | ||
56 | } | ||
57 | |||
58 | /*! | ||
59 | Returns TRUE if this object points to a file. Returns FALSE if the | ||
60 | object points to something which isn't a file, e.g. a directory or | ||
61 | a symlink. | ||
62 | |||
63 | \sa isDir(), isSymLink() | ||
64 | */ | ||
65 | bool QFileInfo::isFile() const | ||
66 | { | ||
67 | if ( !fic || !cache ) | ||
68 | doStat(); | ||
69 | return fic ? (fic->st.st_mode & S_IFMT) == S_IFREG : FALSE; | ||
70 | } | ||
71 | |||
72 | /*! | ||
73 | Returns TRUE if this object points to a directory or to a symbolic | ||
74 | link to a directory; otherwise returns FALSE. | ||
75 | |||
76 | \sa isFile(), isSymLink() | ||
77 | */ | ||
78 | bool QFileInfo::isDir() const | ||
79 | { | ||
80 | if ( !fic || !cache ) | ||
81 | doStat(); | ||
82 | return fic ? (fic->st.st_mode & S_IFMT) == S_IFDIR : FALSE; | ||
83 | } | ||
84 | |||
85 | /*! | ||
86 | Returns TRUE if this object points to a symbolic link (or to a | ||
87 | shortcut on Windows); otherwise returns FALSE. | ||
88 | |||
89 | \sa isFile(), isDir(), readLink() | ||
90 | */ | ||
91 | |||
92 | bool QFileInfo::isSymLink() const | ||
93 | { | ||
94 | if ( !fic || !cache ) | ||
95 | doStat(); | ||
96 | return symLink; | ||
97 | } | ||
98 | |||
99 | /*! | ||
100 | Returns the name a symlink (or shortcut on Windows) points to, or | ||
101 | a QString::null if the object isn't a symbolic link. | ||
102 | |||
103 | This name may not represent an existing file; it is only a string. | ||
104 | QFileInfo::exists() returns TRUE if the symlink points to an | ||
105 | existing file. | ||
106 | |||
107 | \sa exists(), isSymLink(), isDir(), isFile() | ||
108 | */ | ||
109 | |||
110 | QString QFileInfo::readLink() const | ||
111 | { | ||
112 | QString r; | ||
113 | |||
114 | #if defined(Q_OS_UNIX) && !defined(Q_OS_OS2EMX) | ||
115 | char s[PATH_MAX+1]; | ||
116 | if ( !isSymLink() ) | ||
117 | return QString(); | ||
118 | int len = readlink( QFile::encodeName(fn).data(), s, PATH_MAX ); | ||
119 | if ( len >= 0 ) { | ||
120 | s[len] = '\0'; | ||
121 | r = QFile::decodeName(s); | ||
122 | } | ||
123 | #endif | ||
124 | |||
125 | return r; | ||
126 | } | ||
127 | |||
128 | static const uint nobodyID = (uint) -2; | ||
129 | |||
130 | /*! | ||
131 | Returns the owner of the file. On Windows, on systems where files | ||
132 | do not have owners, or if an error occurs, QString::null is | ||
133 | returned. | ||
134 | |||
135 | This function can be time consuming under Unix (in the order of | ||
136 | milliseconds). | ||
137 | |||
138 | \sa ownerId(), group(), groupId() | ||
139 | */ | ||
140 | |||
141 | QString QFileInfo::owner() const | ||
142 | { | ||
143 | passwd *pw = getpwuid( ownerId() ); | ||
144 | if ( pw ) | ||
145 | return QFile::decodeName( pw->pw_name ); | ||
146 | return QString::null; | ||
147 | } | ||
148 | |||
149 | /*! | ||
150 | Returns the id of the owner of the file. | ||
151 | |||
152 | On Windows and on systems where files do not have owners this | ||
153 | function returns ((uint) -2). | ||
154 | |||
155 | \sa owner(), group(), groupId() | ||
156 | */ | ||
157 | |||
158 | uint QFileInfo::ownerId() const | ||
159 | { | ||
160 | if ( !fic || !cache ) | ||
161 | doStat(); | ||
162 | if ( fic ) | ||
163 | return fic->st.st_uid; | ||
164 | return nobodyID; | ||
165 | } | ||
166 | |||
167 | /*! | ||
168 | Returns the group of the file. On Windows, on systems where files | ||
169 | do not have groups, or if an error occurs, QString::null is | ||
170 | returned. | ||
171 | |||
172 | This function can be time consuming under Unix (in the order of | ||
173 | milliseconds). | ||
174 | |||
175 | \sa groupId(), owner(), ownerId() | ||
176 | */ | ||
177 | |||
178 | QString QFileInfo::group() const | ||
179 | { | ||
180 | struct group *gr = getgrgid( groupId() ); | ||
181 | if ( gr ) | ||
182 | return QFile::decodeName( gr->gr_name ); | ||
183 | return QString::null; | ||
184 | } | ||
185 | |||
186 | /*! | ||
187 | Returns the id of the group the file belongs to. | ||
188 | |||
189 | On Windows and on systems where files do not have groups this | ||
190 | function always returns (uint) -2. | ||
191 | |||
192 | \sa group(), owner(), ownerId() | ||
193 | */ | ||
194 | |||
195 | uint QFileInfo::groupId() const | ||
196 | { | ||
197 | if ( !fic || !cache ) | ||
198 | doStat(); | ||
199 | if ( fic ) | ||
200 | return fic->st.st_gid; | ||
201 | return nobodyID; | ||
202 | } | ||
203 | |||
204 | |||
205 | /*! | ||
206 | Tests for file permissions. The \a permissionSpec argument can be | ||
207 | several flags of type \c PermissionSpec OR-ed together to check | ||
208 | for permission combinations. | ||
209 | |||
210 | On systems where files do not have permissions this function | ||
211 | always returns TRUE. | ||
212 | |||
213 | Example: | ||
214 | \code | ||
215 | QFileInfo fi( "/tmp/archive.tar.gz" ); | ||
216 | if ( fi.permission( QFileInfo::WriteUser | QFileInfo::ReadGroup ) ) | ||
217 | qWarning( "I can change the file; my group can read the file."); | ||
218 | if ( fi.permission( QFileInfo::WriteGroup | QFileInfo::WriteOther ) ) | ||
219 | qWarning( "The group or others can change the file!" ); | ||
220 | \endcode | ||
221 | |||
222 | \sa isReadable(), isWritable(), isExecutable() | ||
223 | */ | ||
224 | |||
225 | bool QFileInfo::permission( int permissionSpec ) const | ||
226 | { | ||
227 | if ( !fic || !cache ) | ||
228 | doStat(); | ||
229 | if ( fic ) { | ||
230 | uint mask = 0; | ||
231 | if ( permissionSpec & ReadUser ) | ||
232 | mask |= S_IRUSR; | ||
233 | if ( permissionSpec & WriteUser ) | ||
234 | mask |= S_IWUSR; | ||
235 | if ( permissionSpec & ExeUser ) | ||
236 | mask |= S_IXUSR; | ||
237 | if ( permissionSpec & ReadGroup ) | ||
238 | mask |= S_IRGRP; | ||
239 | if ( permissionSpec & WriteGroup ) | ||
240 | mask |= S_IWGRP; | ||
241 | if ( permissionSpec & ExeGroup ) | ||
242 | mask |= S_IXGRP; | ||
243 | if ( permissionSpec & ReadOther ) | ||
244 | mask |= S_IROTH; | ||
245 | if ( permissionSpec & WriteOther ) | ||
246 | mask |= S_IWOTH; | ||
247 | if ( permissionSpec & ExeOther ) | ||
248 | mask |= S_IXOTH; | ||
249 | if ( mask ) { | ||
250 | return (fic->st.st_mode & mask) == mask; | ||
251 | } else { | ||
252 | #if defined(QT_CHECK_NULL) | ||
253 | qWarning( "QFileInfo::permission: permissionSpec is 0" ); | ||
254 | #endif | ||
255 | return TRUE; | ||
256 | } | ||
257 | } else { | ||
258 | return FALSE; | ||
259 | } | ||
260 | } | ||
261 | |||
262 | void QFileInfo::doStat() const | ||
263 | { | ||
264 | QFileInfo *that = ((QFileInfo*)this);// mutable function | ||
265 | if ( !that->fic ) | ||
266 | that->fic = new QFileInfoCache; | ||
267 | that->symLink = FALSE; | ||
268 | struct stat *b = &that->fic->st; | ||
269 | #if defined(Q_OS_UNIX) && defined(S_IFLNK) | ||
270 | if ( ::lstat( QFile::encodeName(fn), b ) == 0 ) { | ||
271 | if ( S_ISLNK( b->st_mode ) ) | ||
272 | that->symLink = TRUE; | ||
273 | else | ||
274 | return; | ||
275 | } | ||
276 | #endif | ||
277 | |||
278 | int r = ::stat( QFile::encodeName(fn), b ); | ||
279 | if ( r != 0 && !that->symLink ) { | ||
280 | delete that->fic; | ||
281 | that->fic = 0; | ||
282 | } | ||
283 | } | ||
284 | |||
285 | /*! | ||
286 | Returns the file's path. | ||
287 | |||
288 | If \a absPath is TRUE an absolute path is returned. | ||
289 | |||
290 | \sa dir(), filePath(), fileName(), isRelative() | ||
291 | */ | ||
292 | #ifndef QT_NO_DIR | ||
293 | QString QFileInfo::dirPath( bool absPath ) const | ||
294 | { | ||
295 | QString s; | ||
296 | if ( absPath ) | ||
297 | s = absFilePath(); | ||
298 | else | ||
299 | s = fn; | ||
300 | int pos = s.findRev( '/' ); | ||
301 | if ( pos == -1 ) { | ||
302 | return QString::fromLatin1( "." ); | ||
303 | } else { | ||
304 | if ( pos == 0 ) | ||
305 | return QString::fromLatin1( "/" ); | ||
306 | return s.left( pos ); | ||
307 | } | ||
308 | } | ||
309 | #endif | ||
310 | |||
311 | /*! | ||
312 | Returns the name of the file, excluding the path. | ||
313 | |||
314 | Example: | ||
315 | \code | ||
316 | QFileInfo fi( "/tmp/archive.tar.gz" ); | ||
317 | QString name = fi.fileName(); // name = "archive.tar.gz" | ||
318 | \endcode | ||
319 | |||
320 | \sa isRelative(), filePath(), baseName(), extension() | ||
321 | */ | ||
322 | |||
323 | QString QFileInfo::fileName() const | ||
324 | { | ||
325 | int p = fn.findRev( '/' ); | ||
326 | if ( p == -1 ) { | ||
327 | return fn; | ||
328 | } else { | ||
329 | return fn.mid( p + 1 ); | ||
330 | } | ||
331 | } | ||