source: trunk/src/corelib/io/qfileinfo.cpp@ 624

Last change on this file since 624 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 37.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43#include "qfileinfo.h"
44#include "qdatetime.h"
45#include "qabstractfileengine.h"
46#include "qfsfileengine_p.h"
47#include "qglobal.h"
48#include "qatomic.h"
49#include "qhash.h"
50#include "qdir.h"
51#include "qfileinfo_p.h"
52
53QT_BEGIN_NAMESPACE
54
55QFileInfoPrivate::QFileInfoPrivate(const QFileInfo *copy)
56{
57 if(copy) {
58 copy->d_func()->data->ref.ref();
59 data = copy->d_func()->data;
60 } else {
61 data = new QFileInfoPrivate::Data;
62 }
63}
64
65QFileInfoPrivate::~QFileInfoPrivate()
66{
67 if (!data->ref.deref())
68 delete data;
69 data = 0;
70}
71
72void QFileInfoPrivate::initFileEngine(const QString &file)
73{
74 detach();
75 delete data->fileEngine;
76 data->fileEngine = 0;
77 data->clear();
78 data->fileEngine = QAbstractFileEngine::create(file);
79 data->fileName = file;
80}
81
82bool QFileInfoPrivate::hasAccess(Access access) const
83{
84 if (!(getFileFlags(QAbstractFileEngine::FileInfoAll) & QAbstractFileEngine::LocalDiskFlag)) {
85 switch (access) {
86 case ReadAccess:
87 return getFileFlags(QAbstractFileEngine::ReadUserPerm);
88 case WriteAccess:
89 return getFileFlags(QAbstractFileEngine::WriteUserPerm);
90 case ExecuteAccess:
91 return getFileFlags(QAbstractFileEngine::ExeUserPerm);
92 default:
93 return false;
94 }
95 }
96
97 int mode = 0;
98 switch (access) {
99 case ReadAccess:
100 mode = R_OK;
101 break;
102 case WriteAccess:
103 mode = W_OK;
104 break;
105 case ExecuteAccess:
106 mode = X_OK;
107 break;
108 };
109#ifdef Q_OS_UNIX
110 return QT_ACCESS(QFile::encodeName(data->fileName).data(), mode) == 0;
111#endif
112#ifdef Q_OS_OS2
113 if ((access == ReadAccess && !getFileFlags(QAbstractFileEngine::ReadUserPerm))
114 || (access == WriteAccess && !getFileFlags(QAbstractFileEngine::WriteUserPerm))) {
115 return false;
116 }
117 if (access == ExecuteAccess)
118 return getFileFlags(QAbstractFileEngine::ExeUserPerm);
119
120 return QT_ACCESS(QFile::encodeName(data->fileName).data(), mode) == 0;
121#endif
122#ifdef Q_OS_WIN
123 if ((access == ReadAccess && !getFileFlags(QAbstractFileEngine::ReadUserPerm))
124 || (access == WriteAccess && !getFileFlags(QAbstractFileEngine::WriteUserPerm))) {
125 return false;
126 }
127 if (access == ExecuteAccess)
128 return getFileFlags(QAbstractFileEngine::ExeUserPerm);
129
130 return ::_waccess((wchar_t*)QFSFileEnginePrivate::longFileName(data->fileName).utf16(), mode) == 0;
131#endif
132 return false;
133}
134
135void QFileInfoPrivate::detach()
136{
137 qAtomicDetach(data);
138}
139
140QString QFileInfoPrivate::getFileName(QAbstractFileEngine::FileName name) const
141{
142 if(data->cache_enabled && !data->fileNames[(int)name].isNull())
143 return data->fileNames[(int)name];
144 QString ret = data->fileEngine->fileName(name);
145 if(data->cache_enabled)
146 data->fileNames[(int)name] = ret;
147 return ret;
148}
149
150uint QFileInfoPrivate::getFileFlags(QAbstractFileEngine::FileFlags request) const
151{
152 // We split the testing into tests for for LinkType, BundleType and the rest.
153 // In order to determine if a file is a symlink or not, we have to lstat().
154 // If we're not interested in that information, we might as well avoid one
155 // extra syscall. Bundle detecton on Mac can be slow, expecially on network
156 // paths, so we separate out that as well.
157
158 QAbstractFileEngine::FileFlags flags;
159 if (!data->getCachedFlag(CachedFileFlags)) {
160 QAbstractFileEngine::FileFlags req = QAbstractFileEngine::FileInfoAll;
161 req &= (~QAbstractFileEngine::LinkType);
162 req &= (~QAbstractFileEngine::BundleType);
163
164 flags = data->fileEngine->fileFlags(req);
165 data->setCachedFlag(CachedFileFlags);
166 data->fileFlags |= uint(flags);
167 } else {
168 flags = QAbstractFileEngine::FileFlags(data->fileFlags & request);
169 }
170
171 if (request & QAbstractFileEngine::LinkType) {
172 if (!data->getCachedFlag(CachedLinkTypeFlag)) {
173 QAbstractFileEngine::FileFlags linkflag;
174 linkflag = data->fileEngine->fileFlags(QAbstractFileEngine::LinkType);
175
176 data->setCachedFlag(CachedLinkTypeFlag);
177 data->fileFlags |= uint(linkflag);
178 flags |= linkflag;
179 }
180 }
181
182 if (request & QAbstractFileEngine::BundleType) {
183 if (!data->getCachedFlag(CachedBundleTypeFlag)) {
184 QAbstractFileEngine::FileFlags bundleflag;
185 bundleflag = data->fileEngine->fileFlags(QAbstractFileEngine::BundleType);
186
187 data->setCachedFlag(CachedBundleTypeFlag);
188 data->fileFlags |= uint(bundleflag);
189 flags |= bundleflag;
190 }
191 }
192
193 // no else branch
194 // if we had it cached, it was caught in the previous else branch
195
196 return flags & request;
197}
198
199QDateTime &QFileInfoPrivate::getFileTime(QAbstractFileEngine::FileTime request) const
200{
201 if (!data->cache_enabled)
202 data->clearFlags();
203 if(request == QAbstractFileEngine::CreationTime) {
204 if(data->getCachedFlag(CachedCTime))
205 return data->fileTimes[request];
206 data->setCachedFlag(CachedCTime);
207 return (data->fileTimes[request] = data->fileEngine->fileTime(request));
208 }
209 if(request == QAbstractFileEngine::ModificationTime) {
210 if(data->getCachedFlag(CachedMTime))
211 return data->fileTimes[request];
212 data->setCachedFlag(CachedMTime);
213 return (data->fileTimes[request] = data->fileEngine->fileTime(request));
214 }
215 if(request == QAbstractFileEngine::AccessTime) {
216 if(data->getCachedFlag(CachedATime))
217 return data->fileTimes[request];
218 data->setCachedFlag(CachedATime);
219 return (data->fileTimes[request] = data->fileEngine->fileTime(request));
220 }
221 return data->fileTimes[0]; //cannot really happen
222}
223
224//************* QFileInfo
225
226/*!
227 \class QFileInfo
228 \reentrant
229 \brief The QFileInfo class provides system-independent file information.
230
231 \ingroup io
232 \ingroup shared
233
234 QFileInfo provides information about a file's name and position
235 (path) in the file system, its access rights and whether it is a
236 directory or symbolic link, etc. The file's size and last
237 modified/read times are also available. QFileInfo can also be
238 used to obtain information about a Qt \l{resource
239 system}{resource}.
240
241 A QFileInfo can point to a file with either a relative or an
242 absolute file path. Absolute file paths begin with the directory
243 separator "/" (or with a drive specification on Windows). Relative
244 file names begin with a directory name or a file name and specify
245 a path relative to the current working directory. An example of an
246 absolute path is the string "/tmp/quartz". A relative path might
247 look like "src/fatlib". You can use the function isRelative() to
248 check whether a QFileInfo is using a relative or an absolute file
249 path. You can call the function makeAbsolute() to convert a
250 relative QFileInfo's path to an absolute path.
251
252 The file that the QFileInfo works on is set in the constructor or
253 later with setFile(). Use exists() to see if the file exists and
254 size() to get its size.
255
256 The file's type is obtained with isFile(), isDir() and
257 isSymLink(). The symLinkTarget() function provides the name of the file
258 the symlink points to.
259
260 On Unix (including Mac OS X), the symlink has the same size() has
261 the file it points to, because Unix handles symlinks
262 transparently; similarly, opening a symlink using QFile
263 effectively opens the link's target. For example:
264
265 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 0
266
267 On Windows, symlinks (shortcuts) are \c .lnk files. The reported
268 size() is that of the symlink (not the link's target), and
269 opening a symlink using QFile opens the \c .lnk file. For
270 example:
271
272 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 1
273
274 Elements of the file's name can be extracted with path() and
275 fileName(). The fileName()'s parts can be extracted with
276 baseName(), suffix() or completeSuffix(). QFileInfo objects to
277 directories created by Qt classes will not have a trailing file
278 separator. If you wish to use trailing separators in your own file
279 info objects, just append one to the file name given to the constructors
280 or setFile().
281
282 The file's dates are returned by created(), lastModified() and
283 lastRead(). Information about the file's access permissions is
284 obtained with isReadable(), isWritable() and isExecutable(). The
285 file's ownership is available from owner(), ownerId(), group() and
286 groupId(). You can examine a file's permissions and ownership in a
287 single statement using the permission() function.
288
289 \section1 Performance Issues
290
291 Some of QFileInfo's functions query the file system, but for
292 performance reasons, some functions only operate on the
293 file name itself. For example: To return the absolute path of
294 a relative file name, absolutePath() has to query the file system.
295 The path() function, however, can work on the file name directly,
296 and so it is faster.
297
298 \note To speed up performance, QFileInfo caches information about
299 the file.
300
301 To speed up performance, QFileInfo caches information about the
302 file. Because files can be changed by other users or programs, or
303 even by other parts of the same program, there is a function that
304 refreshes the file information: refresh(). If you want to switch
305 off a QFileInfo's caching and force it to access the file system
306 every time you request information from it call setCaching(false).
307
308 \sa QDir, QFile
309*/
310
311/*!
312 Constructs an empty QFileInfo object.
313
314 Note that an empty QFileInfo object contain no file reference.
315
316 \sa setFile()
317*/
318
319QFileInfo::QFileInfo() : d_ptr(new QFileInfoPrivate())
320{
321}
322
323/*!
324 Constructs a new QFileInfo that gives information about the given
325 file. The \a file can also include an absolute or relative path.
326
327 \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()
328*/
329
330QFileInfo::QFileInfo(const QString &file) : d_ptr(new QFileInfoPrivate())
331{
332 d_ptr->initFileEngine(file);
333}
334
335/*!
336 Constructs a new QFileInfo that gives information about file \a
337 file.
338
339 If the \a file has a relative path, the QFileInfo will also have a
340 relative path.
341
342 \sa isRelative()
343*/
344
345QFileInfo::QFileInfo(const QFile &file) : d_ptr(new QFileInfoPrivate())
346{
347 d_ptr->initFileEngine(file.fileName());
348}
349
350/*!
351 Constructs a new QFileInfo that gives information about the given
352 \a file in the directory \a dir.
353
354 If \a dir has a relative path, the QFileInfo will also have a
355 relative path.
356
357 If \a file is an absolute path, then the directory specified
358 by \a dir will be disregarded.
359
360 \sa isRelative()
361*/
362
363QFileInfo::QFileInfo(const QDir &dir, const QString &file) : d_ptr(new QFileInfoPrivate())
364{
365 d_ptr->initFileEngine(dir.filePath(file));
366}
367
368/*!
369 Constructs a new QFileInfo that is a copy of the given \a fileinfo.
370*/
371
372QFileInfo::QFileInfo(const QFileInfo &fileinfo) : d_ptr(new QFileInfoPrivate(&fileinfo))
373{
374
375}
376
377/*!
378 Destroys the QFileInfo and frees its resources.
379*/
380
381
382QFileInfo::~QFileInfo()
383{
384}
385
386/*!
387 \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo)
388
389 Returns true if this QFileInfo object refers to a different file
390 than the one specified by \a fileinfo; otherwise returns false.
391
392 \sa operator==()
393*/
394
395/*!
396 \overload
397 \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo) const
398*/
399
400/*!
401 \overload
402*/
403bool QFileInfo::operator==(const QFileInfo &fileinfo) const
404{
405 Q_D(const QFileInfo);
406 // ### Qt 5: understand long and short file names on Windows
407 // ### (GetFullPathName()).
408 if(fileinfo.d_func()->data == d->data)
409 return true;
410 if(!d->data->fileEngine || !fileinfo.d_func()->data->fileEngine)
411 return false;
412 if(d->data->fileEngine->caseSensitive() != fileinfo.d_func()->data->fileEngine->caseSensitive())
413 return false;
414 if(fileinfo.size() == size()) { //if the size isn't the same...
415 QString file1 = canonicalFilePath(),
416 file2 = fileinfo.canonicalFilePath();
417 if(file1.length() == file2.length()) {
418 if(!fileinfo.d_func()->data->fileEngine->caseSensitive()) {
419 for(int i = 0; i < file1.length(); i++) {
420 if(file1.at(i).toLower() != file2.at(i).toLower())
421 return false;
422 }
423 return true;
424 }
425 return (file1 == file2);
426 }
427 }
428 return false;
429}
430
431/*!
432 Returns true if this QFileInfo object refers to a file in the same
433 location as \a fileinfo; otherwise returns false.
434
435 Note that the result of comparing two empty QFileInfo objects,
436 containing no file references, is undefined.
437
438 \warning This will not compare two different symbolic links
439 pointing to the same file.
440
441 \warning Long and short file names that refer to the same file on Windows
442 are treated as if they referred to different files.
443
444 \sa operator!=()
445*/
446bool QFileInfo::operator==(const QFileInfo &fileinfo)
447{
448 return const_cast<const QFileInfo *>(this)->operator==(fileinfo);
449}
450
451/*!
452 Makes a copy of the given \a fileinfo and assigns it to this QFileInfo.
453*/
454
455QFileInfo &QFileInfo::operator=(const QFileInfo &fileinfo)
456{
457 Q_D(QFileInfo);
458 qAtomicAssign(d->data, fileinfo.d_func()->data);
459 return *this;
460}
461
462/*!
463 Sets the file that the QFileInfo provides information about to \a
464 file.
465
466 The \a file can also include an absolute or relative file path.
467 Absolute paths begin with the directory separator (e.g. "/" under
468 Unix) or a drive specification (under Windows). Relative file
469 names begin with a directory name or a file name and specify a
470 path relative to the current directory.
471
472 Example:
473 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 2
474
475 \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()
476*/
477
478void QFileInfo::setFile(const QString &file)
479{
480 *this = QFileInfo(file);
481}
482
483/*!
484 \overload
485
486 Sets the file that the QFileInfo provides information about to \a
487 file.
488
489 If \a file includes a relative path, the QFileInfo will also have
490 a relative path.
491
492 \sa isRelative()
493*/
494
495void QFileInfo::setFile(const QFile &file)
496{
497 *this = QFileInfo(file.fileName());
498}
499
500/*!
501 \overload
502
503 Sets the file that the QFileInfo provides information about to \a
504 file in directory \a dir.
505
506 If \a file includes a relative path, the QFileInfo will also
507 have a relative path.
508
509 \sa isRelative()
510*/
511
512void QFileInfo::setFile(const QDir &dir, const QString &file)
513{
514 *this = QFileInfo(dir.filePath(file));
515}
516
517/*!
518 Returns an absolute path including the file name.
519
520 The absolute path name consists of the full path and the file
521 name. On Unix this will always begin with the root, '/',
522 directory. On Windows this will always begin 'D:/' where D is a
523 drive letter, except for network shares that are not mapped to a
524 drive letter, in which case the path will begin '//sharename/'.
525 QFileInfo will uppercase drive letters. Note that QDir does not do
526 this. The code snippet below shows this.
527
528 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp newstuff
529
530 This function returns the same as filePath(), unless isRelative()
531 is true. In contrast to canonicalFilePath(), symbolic links or
532 redundant "." or ".." elements are not necessarily removed.
533
534 If the QFileInfo is empty it returns QDir::currentPath().
535
536 \sa filePath(), canonicalFilePath(), isRelative()
537*/
538QString QFileInfo::absoluteFilePath() const
539{
540 Q_D(const QFileInfo);
541 if(!d->data->fileEngine)
542 return QLatin1String("");
543 return d->getFileName(QAbstractFileEngine::AbsoluteName);
544}
545
546/*!
547 Returns the canonical path including the file name, i.e. an absolute
548 path without symbolic links or redundant "." or ".." elements.
549
550 If the file does not exist, canonicalFilePath() returns an empty
551 string.
552
553 \sa filePath(), absoluteFilePath(), dir()
554*/
555
556QString QFileInfo::canonicalFilePath() const
557{
558 Q_D(const QFileInfo);
559 if(!d->data->fileEngine)
560 return QLatin1String("");
561 return d->getFileName(QAbstractFileEngine::CanonicalName);
562}
563
564
565/*!
566 Returns a file's path absolute path. This doesn't include the
567 file name.
568
569 On Unix the absolute path will always begin with the root, '/',
570 directory. On Windows this will always begin 'D:/' where D is a
571 drive letter, except for network shares that are not mapped to a
572 drive letter, in which case the path will begin '//sharename/'.
573
574 In contrast to canonicalPath() symbolic links or redundant "." or
575 ".." elements are not necessarily removed.
576
577 \warning If the QFileInfo object was created with an empty QString,
578 the behavior of this function is undefined.
579
580 \sa absoluteFilePath(), path(), canonicalPath(), fileName(), isRelative()
581*/
582
583QString QFileInfo::absolutePath() const
584{
585 Q_D(const QFileInfo);
586 if (d->data->fileName.isEmpty())
587 qWarning("QFileInfo::absolutePath: Constructed with empty filename");
588 if(!d->data->fileEngine)
589 return QLatin1String("");
590 return d->getFileName(QAbstractFileEngine::AbsolutePathName);
591}
592
593/*!
594 Returns the file's path canonical path (excluding the file name),
595 i.e. an absolute path without symbolic links or redundant "." or ".." elements.
596
597 If the file does not exist, canonicalPath() returns an empty string.
598
599 \sa path(), absolutePath()
600*/
601
602QString QFileInfo::canonicalPath() const
603{
604 Q_D(const QFileInfo);
605 if(!d->data->fileEngine)
606 return QLatin1String("");
607 return d->getFileName(QAbstractFileEngine::CanonicalPathName);
608}
609
610
611/*!
612 Returns the file's path. This doesn't include the file name.
613
614 Note that, if this QFileInfo object is given a path ending in a
615 slash, the name of the file is considered empty and this function
616 will return the entire path.
617
618 \sa filePath(), absolutePath(), canonicalPath(), dir(), fileName(), isRelative()
619*/
620
621QString QFileInfo::path() const
622{
623 Q_D(const QFileInfo);
624 if(!d->data->fileEngine)
625 return QLatin1String("");
626 return d->getFileName(QAbstractFileEngine::PathName);
627}
628
629/*!
630 \fn bool QFileInfo::isAbsolute() const
631
632 Returns true if the file path name is absolute, otherwise returns
633 false if the path is relative.
634
635 \sa isRelative()
636*/
637
638/*!
639 Returns true if the file path name is relative, otherwise returns
640 false if the path is absolute (e.g. under Unix a path is absolute
641 if it begins with a "/").
642
643 \sa isAbsolute()
644*/
645
646bool QFileInfo::isRelative() const
647{
648 Q_D(const QFileInfo);
649 if(!d->data->fileEngine)
650 return true;
651 return d->data->fileEngine->isRelativePath();
652}
653
654
655/*!
656 Converts the file's path to an absolute path if it is not already in that form.
657 Returns true to indicate that the path was converted; otherwise returns false
658 to indicate that the path was already absolute.
659
660 \sa filePath(), isRelative()
661*/
662
663bool QFileInfo::makeAbsolute()
664{
665 Q_D(QFileInfo);
666 if(!d->data->fileEngine || !d->data->fileEngine->isRelativePath())
667 return false;
668 QString absFileName = d->getFileName(QAbstractFileEngine::AbsoluteName);
669 d->initFileEngine(absFileName);
670 return true;
671}
672
673/*!
674 Returns true if the file exists; otherwise returns false.
675
676 \note If the file is a symlink that points to a non existing
677 file, false is returned.
678*/
679
680bool QFileInfo::exists() const
681{
682 Q_D(const QFileInfo);
683 if(!d->data->fileEngine)
684 return false;
685 return d->getFileFlags(QAbstractFileEngine::ExistsFlag);
686}
687
688/*!
689 Refreshes the information about the file, i.e. reads in information
690 from the file system the next time a cached property is fetched.
691
692 \note On Windows CE, there might be a delay for the file system driver
693 to detect changes on the file.
694*/
695
696void QFileInfo::refresh()
697{
698 Q_D(QFileInfo);
699 d->reset();
700}
701
702/*!
703 Returns the file name, including the path (which may be absolute
704 or relative).
705
706 \sa absoluteFilePath(), canonicalFilePath(), isRelative()
707*/
708
709QString QFileInfo::filePath() const
710{
711 Q_D(const QFileInfo);
712 if(!d->data->fileEngine)
713 return QLatin1String("");
714 return d->getFileName(QAbstractFileEngine::DefaultName);
715}
716
717/*!
718 Returns the name of the file, excluding the path.
719
720 Example:
721 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 3
722
723 Note that, if this QFileInfo object is given a path ending in a
724 slash, the name of the file is considered empty.
725
726 \sa isRelative(), filePath(), baseName(), extension()
727*/
728
729QString QFileInfo::fileName() const
730{
731 Q_D(const QFileInfo);
732 if(!d->data->fileEngine)
733 return QLatin1String("");
734 return d->getFileName(QAbstractFileEngine::BaseName);
735}
736
737/*!
738 \since 4.3
739 Returns the name of the bundle.
740
741 On Mac OS X this returns the proper localized name for a bundle if the
742 path isBundle(). On all other platforms an empty QString is returned.
743
744 Example:
745 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 4
746
747 \sa isBundle(), filePath(), baseName(), extension()
748*/
749
750QString QFileInfo::bundleName() const
751{
752 Q_D(const QFileInfo);
753 if(!d->data->fileEngine)
754 return QLatin1String("");
755 return d->getFileName(QAbstractFileEngine::BundleName);
756}
757
758/*!
759 Returns the base name of the file without the path.
760
761 The base name consists of all characters in the file up to (but
762 not including) the \e first '.' character.
763
764 Example:
765 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 5
766
767
768 The base name of a file is computed equally on all platforms, independent
769 of file naming conventions (e.g., ".bashrc" on Unix has an empty base
770 name, and the suffix is "bashrc").
771
772 \sa fileName(), suffix(), completeSuffix(), completeBaseName()
773*/
774
775QString QFileInfo::baseName() const
776{
777 Q_D(const QFileInfo);
778 if(!d->data->fileEngine)
779 return QLatin1String("");
780 return d->getFileName(QAbstractFileEngine::BaseName).section(QLatin1Char('.'), 0, 0);
781}
782
783/*!
784 Returns the complete base name of the file without the path.
785
786 The complete base name consists of all characters in the file up
787 to (but not including) the \e last '.' character.
788
789 Example:
790 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 6
791
792 \sa fileName(), suffix(), completeSuffix(), baseName()
793*/
794
795QString QFileInfo::completeBaseName() const
796{
797 Q_D(const QFileInfo);
798 if(!d->data->fileEngine)
799 return QLatin1String("");
800 QString name = d->getFileName(QAbstractFileEngine::BaseName);
801 int index = name.lastIndexOf(QLatin1Char('.'));
802 return (index == -1) ? name : name.left(index);
803}
804
805/*!
806 Returns the complete suffix of the file.
807
808 The complete suffix consists of all characters in the file after
809 (but not including) the first '.'.
810
811 Example:
812 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 7
813
814 \sa fileName(), suffix(), baseName(), completeBaseName()
815*/
816
817QString QFileInfo::completeSuffix() const
818{
819 Q_D(const QFileInfo);
820 if(!d->data->fileEngine)
821 return QLatin1String("");
822 QString fileName = d->getFileName(QAbstractFileEngine::BaseName);
823 int firstDot = fileName.indexOf(QLatin1Char('.'));
824 if (firstDot == -1)
825 return QLatin1String("");
826 return fileName.mid(firstDot + 1);
827}
828
829/*!
830 Returns the suffix of the file.
831
832 The suffix consists of all characters in the file after (but not
833 including) the last '.'.
834
835 Example:
836 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 8
837
838 The suffix of a file is computed equally on all platforms, independent of
839 file naming conventions (e.g., ".bashrc" on Unix has an empty base name,
840 and the suffix is "bashrc").
841
842 \sa fileName(), completeSuffix(), baseName(), completeBaseName()
843*/
844
845QString QFileInfo::suffix() const
846{
847 Q_D(const QFileInfo);
848 if(!d->data->fileEngine)
849 return QLatin1String("");
850 QString fileName = d->getFileName(QAbstractFileEngine::BaseName);
851 int lastDot = fileName.lastIndexOf(QLatin1Char('.'));
852 if (lastDot == -1)
853 return QLatin1String("");
854 return fileName.mid(lastDot + 1);
855}
856
857
858/*!
859 Returns the path of the object's parent directory as a QDir object.
860
861 \bold{Note:} The QDir returned always corresponds to the object's
862 parent directory, even if the QFileInfo represents a directory.
863
864 For each of the follwing, dir() returns a QDir for
865 \c{"~/examples/191697"}.
866
867 \snippet doc/src/snippets/fileinfo/main.cpp 0
868
869 For each of the follwing, dir() returns a QDir for
870 \c{"."}.
871
872 \snippet doc/src/snippets/fileinfo/main.cpp 1
873
874 \sa absolutePath(), filePath(), fileName(), isRelative(), absoluteDir()
875*/
876
877QDir QFileInfo::dir() const
878{
879 // ### Qt5: Maybe rename this to parentDirectory(), considering what it actually do?
880 return QDir(path());
881}
882
883/*!
884 Returns the file's absolute path as a QDir object.
885
886 \sa dir(), filePath(), fileName(), isRelative()
887*/
888
889QDir QFileInfo::absoluteDir() const
890{
891 return QDir(absolutePath());
892}
893
894#ifdef QT3_SUPPORT
895/*!
896 Use absoluteDir() or the dir() overload that takes no parameters
897 instead.
898*/
899QDir QFileInfo::dir(bool absPath) const
900{
901 if(absPath)
902 return absoluteDir();
903 return dir();
904}
905#endif //QT3_SUPPORT
906
907/*!
908 Returns true if the user can read the file; otherwise returns false.
909
910 \sa isWritable(), isExecutable(), permission()
911*/
912
913bool QFileInfo::isReadable() const
914{
915 Q_D(const QFileInfo);
916 if(!d->data->fileEngine)
917 return false;
918 return d->hasAccess(QFileInfoPrivate::ReadAccess);
919}
920
921/*!
922 Returns true if the user can write to the file; otherwise returns false.
923
924 \sa isReadable(), isExecutable(), permission()
925*/
926
927bool QFileInfo::isWritable() const
928{
929 Q_D(const QFileInfo);
930 if(!d->data->fileEngine)
931 return false;
932 return d->hasAccess(QFileInfoPrivate::WriteAccess);
933}
934
935/*!
936 Returns true if the file is executable; otherwise returns false.
937
938 \sa isReadable(), isWritable(), permission()
939*/
940
941bool QFileInfo::isExecutable() const
942{
943 Q_D(const QFileInfo);
944 if(!d->data->fileEngine)
945 return false;
946 return d->hasAccess(QFileInfoPrivate::ExecuteAccess);
947}
948
949/*!
950 Returns true if this is a `hidden' file; otherwise returns false.
951
952 \bold{Note:} This function returns true for the special entries
953 "." and ".." on Unix, even though QDir::entryList threats them as shown.
954*/
955bool QFileInfo::isHidden() const
956{
957 Q_D(const QFileInfo);
958 if(!d->data->fileEngine)
959 return false;
960 return d->getFileFlags(QAbstractFileEngine::HiddenFlag);
961}
962
963/*!
964 Returns true if this object points to a file or to a symbolic
965 link to a file. Returns false if the
966 object points to something which isn't a file, such as a directory.
967
968 \sa isDir(), isSymLink(), isBundle()
969*/
970
971bool QFileInfo::isFile() const
972{
973 Q_D(const QFileInfo);
974 if(!d->data->fileEngine)
975 return false;
976 return d->getFileFlags(QAbstractFileEngine::FileType);
977}
978
979/*!
980 Returns true if this object points to a directory or to a symbolic
981 link to a directory; otherwise returns false.
982
983 \sa isFile(), isSymLink(), isBundle()
984*/
985
986bool QFileInfo::isDir() const
987{
988 Q_D(const QFileInfo);
989 if(!d->data->fileEngine)
990 return false;
991 return d->getFileFlags(QAbstractFileEngine::DirectoryType);
992}
993
994
995/*!
996 \since 4.3
997 Returns true if this object points to a bundle or to a symbolic
998 link to a bundle on Mac OS X; otherwise returns false.
999
1000 \sa isDir(), isSymLink(), isFile()
1001*/
1002
1003bool QFileInfo::isBundle() const
1004{
1005 Q_D(const QFileInfo);
1006 if(!d->data->fileEngine)
1007 return false;
1008 return d->getFileFlags(QAbstractFileEngine::BundleType);
1009}
1010
1011/*!
1012 Returns true if this object points to a symbolic link (or to a
1013 shortcut on Windows); otherwise returns false.
1014
1015 On Unix (including Mac OS X), opening a symlink effectively opens
1016 the \l{symLinkTarget()}{link's target}. On Windows, it opens the \c
1017 .lnk file itself.
1018
1019 Example:
1020
1021 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 9
1022
1023 \note If the symlink points to a non existing file, exists() returns
1024 false.
1025
1026 \sa isFile(), isDir(), symLinkTarget()
1027*/
1028
1029bool QFileInfo::isSymLink() const
1030{
1031 Q_D(const QFileInfo);
1032 if(!d->data->fileEngine)
1033 return false;
1034 return d->getFileFlags(QAbstractFileEngine::LinkType);
1035}
1036
1037/*!
1038 Returns true if the object points to a directory or to a symbolic
1039 link to a directory, and that directory is the root directory; otherwise
1040 returns false.
1041*/
1042
1043bool QFileInfo::isRoot() const
1044{
1045 Q_D(const QFileInfo);
1046 if (!d->data->fileEngine)
1047 return true;
1048 return d->getFileFlags(QAbstractFileEngine::RootFlag);
1049}
1050
1051/*!
1052 \fn QString QFileInfo::symLinkTarget() const
1053 \since 4.2
1054
1055 Returns the absolute path to the file or directory a symlink (or shortcut
1056 on Windows) points to, or a an empty string if the object isn't a symbolic
1057 link.
1058
1059 This name may not represent an existing file; it is only a string.
1060 QFileInfo::exists() returns true if the symlink points to an
1061 existing file.
1062
1063 \sa exists(), isSymLink(), isDir(), isFile()
1064*/
1065
1066/*!
1067 \obsolete
1068
1069 Use symLinkTarget() instead.
1070*/
1071QString QFileInfo::readLink() const
1072{
1073 Q_D(const QFileInfo);
1074 if(!d->data->fileEngine)
1075 return QLatin1String("");
1076 return d->getFileName(QAbstractFileEngine::LinkName);
1077}
1078
1079/*!
1080 Returns the owner of the file. On systems where files
1081 do not have owners, or if an error occurs, an empty string is
1082 returned.
1083
1084 This function can be time consuming under Unix (in the order of
1085 milliseconds).
1086
1087 \sa ownerId(), group(), groupId()
1088*/
1089
1090QString QFileInfo::owner() const
1091{
1092 Q_D(const QFileInfo);
1093 if(!d->data->fileEngine)
1094 return QLatin1String("");
1095 return d->data->fileEngine->owner(QAbstractFileEngine::OwnerUser);
1096}
1097
1098/*!
1099 Returns the id of the owner of the file.
1100
1101 On Windows and on systems where files do not have owners this
1102 function returns ((uint) -2).
1103
1104 \sa owner(), group(), groupId()
1105*/
1106
1107uint QFileInfo::ownerId() const
1108{
1109 Q_D(const QFileInfo);
1110 if(!d->data->fileEngine)
1111 return 0;
1112 return d->data->fileEngine->ownerId(QAbstractFileEngine::OwnerUser);
1113}
1114
1115/*!
1116 Returns the group of the file. On Windows, on systems where files
1117 do not have groups, or if an error occurs, an empty string is
1118 returned.
1119
1120 This function can be time consuming under Unix (in the order of
1121 milliseconds).
1122
1123 \sa groupId(), owner(), ownerId()
1124*/
1125
1126QString QFileInfo::group() const
1127{
1128 Q_D(const QFileInfo);
1129 if(!d->data->fileEngine)
1130 return QLatin1String("");
1131 return d->data->fileEngine->owner(QAbstractFileEngine::OwnerGroup);
1132}
1133
1134/*!
1135 Returns the id of the group the file belongs to.
1136
1137 On Windows and on systems where files do not have groups this
1138 function always returns (uint) -2.
1139
1140 \sa group(), owner(), ownerId()
1141*/
1142
1143uint QFileInfo::groupId() const
1144{
1145 Q_D(const QFileInfo);
1146 if(!d->data->fileEngine)
1147 return 0;
1148 return d->data->fileEngine->ownerId(QAbstractFileEngine::OwnerGroup);
1149}
1150
1151/*!
1152 Tests for file permissions. The \a permissions argument can be
1153 several flags of type QFile::Permissions OR-ed together to check
1154 for permission combinations.
1155
1156 On systems where files do not have permissions this function
1157 always returns true.
1158
1159 Example:
1160 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 10
1161
1162 \sa isReadable(), isWritable(), isExecutable()
1163*/
1164
1165bool QFileInfo::permission(QFile::Permissions permissions) const
1166{
1167 Q_D(const QFileInfo);
1168 if(!d->data->fileEngine)
1169 return false;
1170 return d->getFileFlags(QAbstractFileEngine::FileFlags((int)permissions)) == (uint)permissions;
1171}
1172
1173/*!
1174 Returns the complete OR-ed together combination of
1175 QFile::Permissions for the file.
1176*/
1177
1178QFile::Permissions QFileInfo::permissions() const
1179{
1180 Q_D(const QFileInfo);
1181 if(!d->data->fileEngine)
1182 return 0;
1183 return QFile::Permissions(d->getFileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask);
1184}
1185
1186
1187/*!
1188 Returns the file size in bytes. If the file does not exist or cannot be
1189 fetched, 0 is returned.
1190
1191 \sa exists()
1192*/
1193
1194qint64 QFileInfo::size() const
1195{
1196 Q_D(const QFileInfo);
1197 if(!d->data->fileEngine)
1198 return 0;
1199 if(!d->data->getCachedFlag(QFileInfoPrivate::CachedSize)) {
1200 d->data->setCachedFlag(QFileInfoPrivate::CachedSize);
1201 d->data->fileSize = d->data->fileEngine->size();
1202 }
1203 return d->data->fileSize;
1204}
1205
1206/*!
1207 Returns the date and time when the file was created.
1208
1209 On most Unix systems, this function returns the time of the last
1210 status change. A status change occurs when the file is created,
1211 but it also occurs whenever the user writes or sets inode
1212 information (for example, changing the file permissions).
1213
1214 If neither creation time nor "last status change" time are not
1215 available, returns the same as lastModified().
1216
1217 \sa lastModified() lastRead()
1218*/
1219
1220QDateTime QFileInfo::created() const
1221{
1222 Q_D(const QFileInfo);
1223 if(!d->data->fileEngine)
1224 return QDateTime();
1225 return d->getFileTime(QAbstractFileEngine::CreationTime);
1226}
1227
1228/*!
1229 Returns the date and time when the file was last modified.
1230
1231 \sa created() lastRead()
1232*/
1233
1234QDateTime QFileInfo::lastModified() const
1235{
1236 Q_D(const QFileInfo);
1237 if(!d->data->fileEngine)
1238 return QDateTime();
1239 return d->getFileTime(QAbstractFileEngine::ModificationTime);
1240}
1241
1242/*!
1243 Returns the date and time when the file was last read (accessed).
1244
1245 On platforms where this information is not available, returns the
1246 same as lastModified().
1247
1248 \sa created() lastModified()
1249*/
1250
1251QDateTime QFileInfo::lastRead() const
1252{
1253 Q_D(const QFileInfo);
1254 if(!d->data->fileEngine)
1255 return QDateTime();
1256 return d->getFileTime(QAbstractFileEngine::AccessTime);
1257}
1258
1259/*! \internal
1260 Detaches all internal data.
1261*/
1262
1263void QFileInfo::detach()
1264{
1265 Q_D(QFileInfo);
1266 d->detach();
1267}
1268
1269/*!
1270 Returns true if caching is enabled; otherwise returns false.
1271
1272 \sa setCaching(), refresh()
1273*/
1274
1275bool QFileInfo::caching() const
1276{
1277 Q_D(const QFileInfo);
1278 return d->data->cache_enabled;
1279}
1280
1281/*!
1282 If \a enable is true, enables caching of file information. If \a
1283 enable is false caching is disabled.
1284
1285 When caching is enabled, QFileInfo reads the file information from
1286 the file system the first time it's needed, but generally not
1287 later.
1288
1289 Caching is enabled by default.
1290
1291 \sa refresh(), caching()
1292*/
1293
1294void QFileInfo::setCaching(bool enable)
1295{
1296 Q_D(QFileInfo);
1297 detach();
1298 d->data->cache_enabled = enable;
1299}
1300
1301/*!
1302 \fn QString QFileInfo::baseName(bool complete)
1303
1304 Use completeBaseName() or the baseName() overload that takes no
1305 parameters instead.
1306*/
1307
1308/*!
1309 \fn QString QFileInfo::extension(bool complete = true) const
1310
1311 Use completeSuffix() or suffix() instead.
1312*/
1313
1314/*!
1315 \fn QString QFileInfo::absFilePath() const
1316
1317 Use absoluteFilePath() instead.
1318*/
1319
1320/*!
1321 \fn QString QFileInfo::dirPath(bool absPath) const
1322
1323 Use absolutePath() if the absolute path is wanted (\a absPath
1324 is true) or path() if it's not necessary (\a absPath is false).
1325*/
1326
1327/*!
1328 \fn bool QFileInfo::convertToAbs()
1329
1330 Use makeAbsolute() instead.
1331*/
1332
1333/*!
1334 \enum QFileInfo::Permission
1335
1336 \compat
1337
1338 \value ReadOwner
1339 \value WriteOwner
1340 \value ExeOwner
1341 \value ReadUser
1342 \value WriteUser
1343 \value ExeUser
1344 \value ReadGroup
1345 \value WriteGroup
1346 \value ExeGroup
1347 \value ReadOther
1348 \value WriteOther
1349 \value ExeOther
1350*/
1351
1352/*!
1353 \fn bool QFileInfo::permission(PermissionSpec permissions) const
1354 \compat
1355
1356 Use permission() instead.
1357*/
1358
1359/*!
1360 \typedef QFileInfoList
1361 \relates QFileInfo
1362
1363 Synonym for QList<QFileInfo>.
1364*/
1365
1366QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.