summaryrefslogtreecommitdiff
path: root/qmake/tools/qfileinfo.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qfileinfo.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qfileinfo.cpp659
1 files changed, 659 insertions, 0 deletions
diff --git a/qmake/tools/qfileinfo.cpp b/qmake/tools/qfileinfo.cpp
new file mode 100644
index 0000000..3af7932
--- a/dev/null
+++ b/qmake/tools/qfileinfo.cpp
@@ -0,0 +1,659 @@
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 may use this file in accordance with the Qt Commercial License
23** 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
40#include "qfileinfo.h"
41#include "qdatetime.h"
42#include "qdir.h"
43#include "qfiledefs_p.h"
44#if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4)
45#include <limits.h>
46#endif
47
48
49extern bool qt_file_access( const QString& fn, int t );
50
51/*!
52 \class QFileInfo
53 \reentrant
54 \brief The QFileInfo class provides system-independent file information.
55
56 \ingroup io
57
58 QFileInfo provides information about a file's name and position
59 (path) in the file system, its access rights and whether it is a
60 directory or symbolic link, etc. The file's size and last
61 modified/read times are also available.
62
63 A QFileInfo can point to a file with either a relative or an
64 absolute file path. Absolute file paths begin with the directory
65 separator "/" (or with a drive specification on Windows). Relative
66 file names begin with a directory name or a file name and specify
67 a path relative to the current working directory. An example of an
68 absolute path is the string "/tmp/quartz". A relative path might
69 look like "src/fatlib". You can use the function isRelative() to
70 check whether a QFileInfo is using a relative or an absolute file
71 path. You can call the function convertToAbs() to convert a
72 relative QFileInfo's path to an absolute path.
73
74 The file that the QFileInfo works on is set in the constructor or
75 later with setFile(). Use exists() to see if the file exists and
76 size() to get its size.
77
78 To speed up performance, QFileInfo caches information about the
79 file. Because files can be changed by other users or programs, or
80 even by other parts of the same program, there is a function that
81 refreshes the file information: refresh(). If you want to switch
82 off a QFileInfo's caching and force it to access the file system
83 every time you request information from it call setCaching(FALSE).
84
85 The file's type is obtained with isFile(), isDir() and
86 isSymLink(). The readLink() function provides the name of the file
87 the symlink points to.
88
89 Elements of the file's name can be extracted with dirPath() and
90 fileName(). The fileName()'s parts can be extracted with
91 baseName() and extension().
92
93 The file's dates are returned by created(), lastModified() and
94 lastRead(). Information about the file's access permissions is
95 obtained with isReadable(), isWritable() and isExecutable(). The
96 file's ownership is available from owner(), ownerId(), group() and
97 groupId(). You can examine a file's permissions and ownership in a
98 single statement using the permission() function.
99
100 If you need to read and traverse directories, see the QDir class.
101*/
102
103/*!
104 \enum QFileInfo::PermissionSpec
105
106 This enum is used by the permission() function to report the
107 permissions and ownership of a file. The values may be OR-ed
108 together to test multiple permissions and ownership values.
109
110 \value ReadUser The file is readable by the user.
111 \value WriteUser The file is writable by the user.
112 \value ExeUser The file is executable by the user.
113 \value ReadGroup The file is readable by the group.
114 \value WriteGroup The file is writable by the group.
115 \value ExeGroup The file is executable by the group.
116 \value ReadOther The file is readable by anyone.
117 \value WriteOther The file is writable by anyone.
118 \value ExeOther The file is executable by anyone.
119*/
120
121
122/*!
123 Constructs a new empty QFileInfo.
124*/
125
126QFileInfo::QFileInfo()
127{
128 fic = 0;
129 cache = TRUE;
130#if defined(Q_OS_UNIX)
131 symLink = FALSE;
132#endif
133}
134
135/*!
136 Constructs a new QFileInfo that gives information about the given
137 file. The \a file can also include an absolute or relative path.
138
139 \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()
140*/
141
142QFileInfo::QFileInfo( const QString &file )
143{
144 fn = file;
145 slashify( fn );
146 fic = 0;
147 cache = TRUE;
148#if defined(Q_OS_UNIX)
149 symLink = FALSE;
150#endif
151}
152
153/*!
154 Constructs a new QFileInfo that gives information about file \a
155 file.
156
157 If the \a file has a relative path, the QFileInfo will also have a
158 relative path.
159
160 \sa isRelative()
161*/
162
163QFileInfo::QFileInfo( const QFile &file )
164{
165 fn = file.name();
166 slashify( fn );
167 fic = 0;
168 cache = TRUE;
169#if defined(Q_OS_UNIX)
170 symLink = FALSE;
171#endif
172}
173
174/*!
175 Constructs a new QFileInfo that gives information about the file
176 called \a fileName in the directory \a d.
177
178 If \a d has a relative path, the QFileInfo will also have a
179 relative path.
180
181 \sa isRelative()
182*/
183#ifndef QT_NO_DIR
184QFileInfo::QFileInfo( const QDir &d, const QString &fileName )
185{
186 fn = d.filePath( fileName );
187 slashify( fn );
188 fic = 0;
189 cache = TRUE;
190#if defined(Q_OS_UNIX)
191 symLink = FALSE;
192#endif
193}
194#endif
195/*!
196 Constructs a new QFileInfo that is a copy of \a fi.
197*/
198
199QFileInfo::QFileInfo( const QFileInfo &fi )
200{
201 fn = fi.fn;
202 if ( fi.fic ) {
203 fic = new QFileInfoCache;
204 *fic = *fi.fic;
205 } else {
206 fic = 0;
207 }
208 cache = fi.cache;
209#if defined(Q_OS_UNIX)
210 symLink = fi.symLink;
211#endif
212}
213
214/*!
215 Destroys the QFileInfo and frees its resources.
216*/
217
218QFileInfo::~QFileInfo()
219{
220 delete fic;
221}
222
223
224/*!
225 Makes a copy of \a fi and assigns it to this QFileInfo.
226*/
227
228QFileInfo &QFileInfo::operator=( const QFileInfo &fi )
229{
230 fn = fi.fn;
231 if ( !fi.fic ) {
232 delete fic;
233 fic = 0;
234 } else {
235 if ( !fic ) {
236 fic = new QFileInfoCache;
237 Q_CHECK_PTR( fic );
238 }
239 *fic = *fi.fic;
240 }
241 cache = fi.cache;
242#if defined(Q_OS_UNIX)
243 symLink = fi.symLink;
244#endif
245 return *this;
246}
247
248
249/*!
250 Sets the file that the QFileInfo provides information about to \a
251 file.
252
253 The \a file can also include an absolute or relative file path.
254 Absolute paths begin with the directory separator (e.g. "/" under
255 Unix) or a drive specification (under Windows). Relative file
256 names begin with a directory name or a file name and specify a
257 path relative to the current directory.
258
259 Example:
260 \code
261 QString absolute = "/local/bin";
262 QString relative = "local/bin";
263 QFileInfo absFile( absolute );
264 QFileInfo relFile( relative );
265
266 QDir::setCurrent( QDir::rootDirPath() );
267 // absFile and relFile now point to the same file
268
269 QDir::setCurrent( "/tmp" );
270 // absFile now points to "/local/bin",
271 // while relFile points to "/tmp/local/bin"
272 \endcode
273
274 \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()
275*/
276
277void QFileInfo::setFile( const QString &file )
278{
279 fn = file;
280 slashify( fn );
281 delete fic;
282 fic = 0;
283}
284
285/*!
286 \overload
287
288 Sets the file that the QFileInfo provides information about to \a
289 file.
290
291 If \a file includes a relative path, the QFileInfo will also have
292 a relative path.
293
294 \sa isRelative()
295*/
296
297void QFileInfo::setFile( const QFile &file )
298{
299 fn= file.name();
300 slashify( fn );
301 delete fic;
302 fic = 0;
303}
304
305/*!
306 \overload
307
308 Sets the file that the QFileInfo provides information about to \a
309 fileName in directory \a d.
310
311 If \a fileName includes a relative path, the QFileInfo will also
312 have a relative path.
313
314 \sa isRelative()
315*/
316#ifndef QT_NO_DIR
317void QFileInfo::setFile( const QDir &d, const QString &fileName )
318{
319 fn= d.filePath( fileName );
320 slashify( fn );
321 delete fic;
322 fic = 0;
323}
324#endif
325
326/*!
327 Returns TRUE if the file exists; otherwise returns FALSE.
328*/
329
330bool QFileInfo::exists() const
331{
332 return qt_file_access( fn, F_OK );
333}
334
335/*!
336 Refreshes the information about the file, i.e. reads in information
337 from the file system the next time a cached property is fetched.
338
339 \sa setCaching()
340*/
341
342void QFileInfo::refresh() const
343{
344 QFileInfo *that = (QFileInfo*)this; // Mutable function
345 delete that->fic;
346 that->fic = 0;
347}
348
349/*!
350 \fn bool QFileInfo::caching() const
351
352 Returns TRUE if caching is enabled; otherwise returns FALSE.
353
354 \sa setCaching(), refresh()
355*/
356
357/*!
358 If \a enable is TRUE, enables caching of file information. If \a
359 enable is FALSE caching is disabled.
360
361 When caching is enabled, QFileInfo reads the file information from
362 the file system the first time it's needed, but generally not
363 later.
364
365 Caching is enabled by default.
366
367 \sa refresh(), caching()
368*/
369
370void QFileInfo::setCaching( bool enable )
371{
372 if ( cache == enable )
373 return;
374 cache = enable;
375 if ( cache ) {
376 delete fic;
377 fic = 0;
378 }
379}
380
381
382/*!
383 Returns the file name, including the path (which may be absolute
384 or relative).
385
386 \sa isRelative(), absFilePath()
387*/
388
389QString QFileInfo::filePath() const
390{
391 return fn;
392}
393
394/*!
395 Returns the base name of the file.
396
397 If \a complete is FALSE (the default) the base name consists of
398 all characters in the file name up to (but not including) the \e
399 first '.' character.
400
401 If \a complete is TRUE the base name consists of all characters in
402 the file up to (but not including) the \e last '.' character.
403
404 The path is not included in either case.
405
406 Example:
407 \code
408 QFileInfo fi( "/tmp/archive.tar.gz" );
409 QString base = fi.baseName(); // base = "archive"
410 base = fi.baseName( TRUE ); // base = "archive.tar"
411 \endcode
412
413 \sa fileName(), extension()
414*/
415
416QString QFileInfo::baseName( bool complete ) const
417{
418 QString tmp = fileName();
419 int pos = complete ? tmp.findRev( '.' ) : tmp.find( '.' );
420 if ( pos == -1 )
421 return tmp;
422 else
423 return tmp.left( pos );
424}
425
426/*!
427 Returns the file's extension name.
428
429 If \a complete is TRUE (the default), extension() returns the
430 string of all characters in the file name after (but not
431 including) the first '.' character.
432
433 If \a complete is FALSE, extension() returns the string of all
434 characters in the file name after (but not including) the last '.'
435 character.
436
437 Example:
438 \code
439 QFileInfo fi( "/tmp/archive.tar.gz" );
440 QString ext = fi.extension(); // ext = "tar.gz"
441 ext = fi.extension( FALSE ); // ext = "gz"
442 \endcode
443
444 \sa fileName(), baseName()
445*/
446
447QString QFileInfo::extension( bool complete ) const
448{
449 QString s = fileName();
450 int pos = complete ? s.find( '.' ) : s.findRev( '.' );
451 if ( pos < 0 )
452 return QString::fromLatin1( "" );
453 else
454 return s.right( s.length() - pos - 1 );
455}
456
457/*!
458 Returns the file's path as a QDir object.
459
460 If the QFileInfo is relative and \a absPath is FALSE, the QDir
461 will be relative; otherwise it will be absolute.
462
463 \sa dirPath(), filePath(), fileName(), isRelative()
464*/
465#ifndef QT_NO_DIR
466QDir QFileInfo::dir( bool absPath ) const
467{
468 return QDir( dirPath(absPath) );
469}
470#endif
471
472
473/*!
474 Returns TRUE if the file is readable; otherwise returns FALSE.
475
476 \sa isWritable(), isExecutable(), permission()
477*/
478
479bool QFileInfo::isReadable() const
480{
481 return qt_file_access( fn, R_OK ) && permission( ReadUser );
482}
483
484/*!
485 Returns TRUE if the file is writable; otherwise returns FALSE.
486
487 \sa isReadable(), isExecutable(), permission()
488*/
489
490bool QFileInfo::isWritable() const
491{
492 return qt_file_access( fn, W_OK ) && permission( WriteUser );
493}
494
495/*!
496 Returns TRUE if the file is executable; otherwise returns FALSE.
497
498 \sa isReadable(), isWritable(), permission()
499*/
500
501bool QFileInfo::isExecutable() const
502{
503 return qt_file_access( fn, X_OK ) && permission( ExeUser );
504}
505
506#ifndef Q_WS_WIN
507bool QFileInfo::isHidden() const
508{
509 return fileName()[ 0 ] == QChar( '.' );
510}
511#endif
512
513/*!
514 Returns TRUE if the file path name is relative. Returns FALSE if
515 the path is absolute (e.g. under Unix a path is absolute if it
516 begins with a "/").
517*/
518#ifndef QT_NO_DIR
519bool QFileInfo::isRelative() const
520{
521 return QDir::isRelativePath( fn );
522}
523
524/*!
525 Converts the file's path to an absolute path.
526
527 If it is already absolute, nothing is done.
528
529 \sa filePath(), isRelative()
530*/
531
532bool QFileInfo::convertToAbs()
533{
534 if ( isRelative() )
535 fn = absFilePath();
536 return QDir::isRelativePath( fn );
537}
538#endif
539
540/*!
541 Returns the file size in bytes, or 0 if the file does not exist or
542 if the size is 0 or if the size cannot be fetched.
543*/
544#if defined(QT_ABI_QT4)
545QIODevice::Offset QFileInfo::size() const
546#else
547uint QFileInfo::size() const
548#endif
549{
550 if ( !fic || !cache )
551 doStat();
552 if ( fic )
553#if defined(QT_ABI_QT4)
554 return (QIODevice::Offset)fic->st.st_size;
555#elif defined(QT_LARGEFILE_SUPPORT)
556 return (uint)fic->st.st_size > UINT_MAX ? UINT_MAX : (uint)fic->st.st_size;
557#else
558 return (uint)fic->st.st_size;
559#endif
560 else
561 return 0;
562}
563
564/*!
565 Returns the date and time when the file was created.
566
567 On platforms where this information is not available, returns the
568 same as lastModified().
569
570 \sa created() lastModified() lastRead()
571*/
572
573QDateTime QFileInfo::created() const
574{
575 QDateTime dt;
576 if ( !fic || !cache )
577 doStat();
578 if ( fic && fic->st.st_ctime != 0 ) {
579 dt.setTime_t( fic->st.st_ctime );
580 return dt;
581 } else {
582 return lastModified();
583 }
584}
585
586/*!
587 Returns the date and time when the file was last modified.
588
589 \sa created() lastModified() lastRead()
590*/
591
592QDateTime QFileInfo::lastModified() const
593{
594 QDateTime dt;
595 if ( !fic || !cache )
596 doStat();
597 if ( fic )
598 dt.setTime_t( fic->st.st_mtime );
599 return dt;
600}
601
602/*!
603 Returns the date and time when the file was last read (accessed).
604
605 On platforms where this information is not available, returns the
606 same as lastModified().
607
608 \sa created() lastModified() lastRead()
609*/
610
611QDateTime QFileInfo::lastRead() const
612{
613 QDateTime dt;
614 if ( !fic || !cache )
615 doStat();
616 if ( fic && fic->st.st_atime != 0 ) {
617 dt.setTime_t( fic->st.st_atime );
618 return dt;
619 } else {
620 return lastModified();
621 }
622}
623
624#ifndef QT_NO_DIR
625
626/*!
627 Returns the absolute path including the file name.
628
629 The absolute path name consists of the full path and the file
630 name. On Unix this will always begin with the root, '/',
631 directory. On Windows this will always begin 'D:/' where D is a
632 drive letter, except for network shares that are not mapped to a
633 drive letter, in which case the path will begin '//sharename/'.
634
635 This function returns the same as filePath(), unless isRelative()
636 is TRUE.
637
638 This function can be time consuming under Unix (in the order of
639 milliseconds).
640
641 \sa isRelative(), filePath()
642*/
643QString QFileInfo::absFilePath() const
644{
645 QString tmp;
646 if ( QDir::isRelativePath(fn)
647#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)
648 && fn[1] != ':'
649#endif
650 ) {
651 tmp = QDir::currentDirPath();
652 tmp += '/';
653 }
654 tmp += fn;
655 makeAbs( tmp );
656 return QDir::cleanDirPath( tmp );
657}
658
659#endif