1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qplatformdefs.h"
43 #include "qfileinfo.h"
46 #include "qfileinfo_p.h"
50 QString QFileInfoPrivate::getFileName(QAbstractFileEngine::FileName name) const
52 if (cache_enabled && !fileNames[(int)name].isNull())
53 return fileNames[(int)name];
56 if (fileEngine == 0) { // local file; use the QFileSystemEngine directly
58 case QAbstractFileEngine::CanonicalName:
59 case QAbstractFileEngine::CanonicalPathName: {
60 QFileSystemEntry entry = QFileSystemEngine::canonicalName(fileEntry, metaData);
61 if (cache_enabled) { // be smart and store both
62 fileNames[QAbstractFileEngine::CanonicalName] = entry.filePath();
63 fileNames[QAbstractFileEngine::CanonicalPathName] = entry.path();
65 if (name == QAbstractFileEngine::CanonicalName)
66 ret = entry.filePath();
71 case QAbstractFileEngine::LinkName:
72 ret = QFileSystemEngine::getLinkTarget(fileEntry, metaData).filePath();
74 case QAbstractFileEngine::BundleName:
75 ret = QFileSystemEngine::bundleName(fileEntry);
77 case QAbstractFileEngine::AbsoluteName:
78 case QAbstractFileEngine::AbsolutePathName: {
79 QFileSystemEntry entry = QFileSystemEngine::absoluteName(fileEntry);
80 if (cache_enabled) { // be smart and store both
81 fileNames[QAbstractFileEngine::AbsoluteName] = entry.filePath();
82 fileNames[QAbstractFileEngine::AbsolutePathName] = entry.path();
84 if (name == QAbstractFileEngine::AbsoluteName)
85 ret = entry.filePath();
93 ret = fileEngine->fileName(name);
96 ret = QLatin1String("");
98 fileNames[(int)name] = ret;
102 QString QFileInfoPrivate::getFileOwner(QAbstractFileEngine::FileOwner own) const
104 if (cache_enabled && !fileOwners[(int)own].isNull())
105 return fileOwners[(int)own];
107 if (fileEngine == 0) {
109 case QAbstractFileEngine::OwnerUser:
110 ret = QFileSystemEngine::resolveUserName(fileEntry, metaData);
112 case QAbstractFileEngine::OwnerGroup:
113 ret = QFileSystemEngine::resolveGroupName(fileEntry, metaData);
117 ret = fileEngine->owner(own);
120 ret = QLatin1String("");
122 fileOwners[(int)own] = ret;
126 uint QFileInfoPrivate::getFileFlags(QAbstractFileEngine::FileFlags request) const
128 Q_ASSERT(fileEngine); // should never be called when using the native FS
129 // We split the testing into tests for for LinkType, BundleType, PermsMask
131 // Tests for file permissions on Windows can be slow, expecially on network
132 // paths and NTFS drives.
133 // In order to determine if a file is a symlink or not, we have to lstat().
134 // If we're not interested in that information, we might as well avoid one
135 // extra syscall. Bundle detecton on Mac can be slow, expecially on network
136 // paths, so we separate out that as well.
138 QAbstractFileEngine::FileFlags req = 0;
139 uint cachedFlags = 0;
141 if (request & (QAbstractFileEngine::FlagsMask | QAbstractFileEngine::TypesMask)) {
142 if (!getCachedFlag(CachedFileFlags)) {
143 req |= QAbstractFileEngine::FlagsMask;
144 req |= QAbstractFileEngine::TypesMask;
145 req &= (~QAbstractFileEngine::LinkType);
146 req &= (~QAbstractFileEngine::BundleType);
148 cachedFlags |= CachedFileFlags;
151 if (request & QAbstractFileEngine::LinkType) {
152 if (!getCachedFlag(CachedLinkTypeFlag)) {
153 req |= QAbstractFileEngine::LinkType;
154 cachedFlags |= CachedLinkTypeFlag;
158 if (request & QAbstractFileEngine::BundleType) {
159 if (!getCachedFlag(CachedBundleTypeFlag)) {
160 req |= QAbstractFileEngine::BundleType;
161 cachedFlags |= CachedBundleTypeFlag;
166 if (request & QAbstractFileEngine::PermsMask) {
167 if (!getCachedFlag(CachedPerms)) {
168 req |= QAbstractFileEngine::PermsMask;
169 cachedFlags |= CachedPerms;
175 req &= (~QAbstractFileEngine::Refresh);
177 req |= QAbstractFileEngine::Refresh;
179 QAbstractFileEngine::FileFlags flags = fileEngine->fileFlags(req);
180 fileFlags |= uint(flags);
181 setCachedFlag(cachedFlags);
184 return fileFlags & request;
187 QDateTime &QFileInfoPrivate::getFileTime(QAbstractFileEngine::FileTime request) const
189 Q_ASSERT(fileEngine); // should never be called when using the native FS
193 if (request == QAbstractFileEngine::CreationTime)
195 else if (request == QAbstractFileEngine::ModificationTime)
199 if (!getCachedFlag(cf)) {
200 fileTimes[request] = fileEngine->fileTime(request);
203 return fileTimes[request];
206 //************* QFileInfo
211 \brief The QFileInfo class provides system-independent file information.
216 QFileInfo provides information about a file's name and position
217 (path) in the file system, its access rights and whether it is a
218 directory or symbolic link, etc. The file's size and last
219 modified/read times are also available. QFileInfo can also be
220 used to obtain information about a Qt \l{resource
223 A QFileInfo can point to a file with either a relative or an
224 absolute file path. Absolute file paths begin with the directory
225 separator "/" (or with a drive specification on Windows). Relative
226 file names begin with a directory name or a file name and specify
227 a path relative to the current working directory. An example of an
228 absolute path is the string "/tmp/quartz". A relative path might
229 look like "src/fatlib". You can use the function isRelative() to
230 check whether a QFileInfo is using a relative or an absolute file
231 path. You can call the function makeAbsolute() to convert a
232 relative QFileInfo's path to an absolute path.
234 The file that the QFileInfo works on is set in the constructor or
235 later with setFile(). Use exists() to see if the file exists and
236 size() to get its size.
238 The file's type is obtained with isFile(), isDir() and
239 isSymLink(). The symLinkTarget() function provides the name of the file
240 the symlink points to.
242 On Unix (including Mac OS X), the symlink has the same size() has
243 the file it points to, because Unix handles symlinks
244 transparently; similarly, opening a symlink using QFile
245 effectively opens the link's target. For example:
247 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 0
249 On Windows, symlinks (shortcuts) are \c .lnk files. The reported
250 size() is that of the symlink (not the link's target), and
251 opening a symlink using QFile opens the \c .lnk file. For
254 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 1
256 Elements of the file's name can be extracted with path() and
257 fileName(). The fileName()'s parts can be extracted with
258 baseName(), suffix() or completeSuffix(). QFileInfo objects to
259 directories created by Qt classes will not have a trailing file
260 separator. If you wish to use trailing separators in your own file
261 info objects, just append one to the file name given to the constructors
264 The file's dates are returned by created(), lastModified() and
265 lastRead(). Information about the file's access permissions is
266 obtained with isReadable(), isWritable() and isExecutable(). The
267 file's ownership is available from owner(), ownerId(), group() and
268 groupId(). You can examine a file's permissions and ownership in a
269 single statement using the permission() function.
271 \section1 Performance Issues
273 Some of QFileInfo's functions query the file system, but for
274 performance reasons, some functions only operate on the
275 file name itself. For example: To return the absolute path of
276 a relative file name, absolutePath() has to query the file system.
277 The path() function, however, can work on the file name directly,
280 \note To speed up performance, QFileInfo caches information about
283 To speed up performance, QFileInfo caches information about the
284 file. Because files can be changed by other users or programs, or
285 even by other parts of the same program, there is a function that
286 refreshes the file information: refresh(). If you want to switch
287 off a QFileInfo's caching and force it to access the file system
288 every time you request information from it call setCaching(false).
296 QFileInfo::QFileInfo(QFileInfoPrivate *p) : d_ptr(p)
301 Constructs an empty QFileInfo object.
303 Note that an empty QFileInfo object contain no file reference.
307 QFileInfo::QFileInfo() : d_ptr(new QFileInfoPrivate())
312 Constructs a new QFileInfo that gives information about the given
313 file. The \a file can also include an absolute or relative path.
315 \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()
317 QFileInfo::QFileInfo(const QString &file) : d_ptr(new QFileInfoPrivate(file))
322 Constructs a new QFileInfo that gives information about file \a
325 If the \a file has a relative path, the QFileInfo will also have a
330 QFileInfo::QFileInfo(const QFile &file) : d_ptr(new QFileInfoPrivate(file.fileName()))
335 Constructs a new QFileInfo that gives information about the given
336 \a file in the directory \a dir.
338 If \a dir has a relative path, the QFileInfo will also have a
341 If \a file is an absolute path, then the directory specified
342 by \a dir will be disregarded.
346 QFileInfo::QFileInfo(const QDir &dir, const QString &file)
347 : d_ptr(new QFileInfoPrivate(dir.filePath(file)))
352 Constructs a new QFileInfo that is a copy of the given \a fileinfo.
354 QFileInfo::QFileInfo(const QFileInfo &fileinfo)
355 : d_ptr(fileinfo.d_ptr)
361 Destroys the QFileInfo and frees its resources.
364 QFileInfo::~QFileInfo()
369 \fn bool QFileInfo::operator!=(const QFileInfo &fileinfo) const
371 Returns true if this QFileInfo object refers to a different file
372 than the one specified by \a fileinfo; otherwise returns false.
378 Returns true if this QFileInfo object refers to a file in the same
379 location as \a fileinfo; otherwise returns false.
381 Note that the result of comparing two empty QFileInfo objects,
382 containing no file references, is undefined.
384 \warning This will not compare two different symbolic links
385 pointing to the same file.
387 \warning Long and short file names that refer to the same file on Windows
388 are treated as if they referred to different files.
392 bool QFileInfo::operator==(const QFileInfo &fileinfo) const
394 Q_D(const QFileInfo);
395 // ### Qt 5: understand long and short file names on Windows
396 // ### (GetFullPathName()).
397 if (fileinfo.d_ptr == d_ptr)
399 if (d->isDefaultConstructed || fileinfo.d_ptr->isDefaultConstructed)
402 // Assume files are the same if path is the same
403 if (d->fileEntry.filePath() == fileinfo.d_ptr->fileEntry.filePath())
406 Qt::CaseSensitivity sensitive;
407 if (d->fileEngine == 0 || fileinfo.d_ptr->fileEngine == 0) {
408 if (d->fileEngine != fileinfo.d_ptr->fileEngine) // one is native, the other is a custom file-engine
411 sensitive = QFileSystemEngine::isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
413 if (d->fileEngine->caseSensitive() != fileinfo.d_ptr->fileEngine->caseSensitive())
415 sensitive = d->fileEngine->caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
418 if (fileinfo.size() != size()) //if the size isn't the same...
421 // Fallback to expensive canonical path computation
422 return canonicalFilePath().compare(fileinfo.canonicalFilePath(), sensitive) == 0;
426 Makes a copy of the given \a fileinfo and assigns it to this QFileInfo.
428 QFileInfo &QFileInfo::operator=(const QFileInfo &fileinfo)
430 d_ptr = fileinfo.d_ptr;
435 Sets the file that the QFileInfo provides information about to \a
438 The \a file can also include an absolute or relative file path.
439 Absolute paths begin with the directory separator (e.g. "/" under
440 Unix) or a drive specification (under Windows). Relative file
441 names begin with a directory name or a file name and specify a
442 path relative to the current directory.
445 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 2
447 \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()
449 void QFileInfo::setFile(const QString &file)
451 bool caching = d_ptr.constData()->cache_enabled;
452 *this = QFileInfo(file);
453 d_ptr->cache_enabled = caching;
459 Sets the file that the QFileInfo provides information about to \a
462 If \a file includes a relative path, the QFileInfo will also have
467 void QFileInfo::setFile(const QFile &file)
469 setFile(file.fileName());
475 Sets the file that the QFileInfo provides information about to \a
476 file in directory \a dir.
478 If \a file includes a relative path, the QFileInfo will also
479 have a relative path.
483 void QFileInfo::setFile(const QDir &dir, const QString &file)
485 setFile(dir.filePath(file));
489 Returns an absolute path including the file name.
491 The absolute path name consists of the full path and the file
492 name. On Unix this will always begin with the root, '/',
493 directory. On Windows this will always begin 'D:/' where D is a
494 drive letter, except for network shares that are not mapped to a
495 drive letter, in which case the path will begin '//sharename/'.
496 QFileInfo will uppercase drive letters. Note that QDir does not do
497 this. The code snippet below shows this.
499 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp newstuff
501 This function returns the same as filePath(), unless isRelative()
502 is true. In contrast to canonicalFilePath(), symbolic links or
503 redundant "." or ".." elements are not necessarily removed.
505 If the QFileInfo is empty it returns QDir::currentPath().
507 \sa filePath(), canonicalFilePath(), isRelative()
509 QString QFileInfo::absoluteFilePath() const
511 Q_D(const QFileInfo);
512 if (d->isDefaultConstructed)
513 return QLatin1String("");
514 return d->getFileName(QAbstractFileEngine::AbsoluteName);
518 Returns the canonical path including the file name, i.e. an absolute
519 path without symbolic links or redundant "." or ".." elements.
521 If the file does not exist, canonicalFilePath() returns an empty
524 \sa filePath(), absoluteFilePath(), dir()
526 QString QFileInfo::canonicalFilePath() const
528 Q_D(const QFileInfo);
529 if (d->isDefaultConstructed)
530 return QLatin1String("");
531 return d->getFileName(QAbstractFileEngine::CanonicalName);
536 Returns a file's path absolute path. This doesn't include the
539 On Unix the absolute path will always begin with the root, '/',
540 directory. On Windows this will always begin 'D:/' where D is a
541 drive letter, except for network shares that are not mapped to a
542 drive letter, in which case the path will begin '//sharename/'.
544 In contrast to canonicalPath() symbolic links or redundant "." or
545 ".." elements are not necessarily removed.
547 \warning If the QFileInfo object was created with an empty QString,
548 the behavior of this function is undefined.
550 \sa absoluteFilePath(), path(), canonicalPath(), fileName(), isRelative()
552 QString QFileInfo::absolutePath() const
554 Q_D(const QFileInfo);
556 if (d->isDefaultConstructed) {
557 return QLatin1String("");
558 } else if (d->fileEntry.isEmpty()) {
559 qWarning("QFileInfo::absolutePath: Constructed with empty filename");
560 return QLatin1String("");
562 return d->getFileName(QAbstractFileEngine::AbsolutePathName);
566 Returns the file's path canonical path (excluding the file name),
567 i.e. an absolute path without symbolic links or redundant "." or ".." elements.
569 If the file does not exist, canonicalPath() returns an empty string.
571 \sa path(), absolutePath()
573 QString QFileInfo::canonicalPath() const
575 Q_D(const QFileInfo);
576 if (d->isDefaultConstructed)
577 return QLatin1String("");
578 return d->getFileName(QAbstractFileEngine::CanonicalPathName);
582 Returns the file's path. This doesn't include the file name.
584 Note that, if this QFileInfo object is given a path ending in a
585 slash, the name of the file is considered empty and this function
586 will return the entire path.
588 \sa filePath(), absolutePath(), canonicalPath(), dir(), fileName(), isRelative()
590 QString QFileInfo::path() const
592 Q_D(const QFileInfo);
593 if (d->isDefaultConstructed)
594 return QLatin1String("");
595 return d->fileEntry.path();
599 \fn bool QFileInfo::isAbsolute() const
601 Returns true if the file path name is absolute, otherwise returns
602 false if the path is relative.
608 Returns true if the file path name is relative, otherwise returns
609 false if the path is absolute (e.g. under Unix a path is absolute
610 if it begins with a "/").
614 bool QFileInfo::isRelative() const
616 Q_D(const QFileInfo);
617 if (d->isDefaultConstructed)
619 if (d->fileEngine == 0)
620 return d->fileEntry.isRelative();
621 return d->fileEngine->isRelativePath();
625 Converts the file's path to an absolute path if it is not already in that form.
626 Returns true to indicate that the path was converted; otherwise returns false
627 to indicate that the path was already absolute.
629 \sa filePath(), isRelative()
631 bool QFileInfo::makeAbsolute()
633 if (d_ptr.constData()->isDefaultConstructed
634 || !d_ptr.constData()->fileEntry.isRelative())
637 setFile(absoluteFilePath());
642 Returns true if the file exists; otherwise returns false.
644 \note If the file is a symlink that points to a non existing
645 file, false is returned.
647 bool QFileInfo::exists() const
649 Q_D(const QFileInfo);
650 if (d->isDefaultConstructed)
652 if (d->fileEngine == 0) {
653 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::ExistsAttribute))
654 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::ExistsAttribute);
655 return d->metaData.exists();
657 return d->getFileFlags(QAbstractFileEngine::ExistsFlag);
661 Refreshes the information about the file, i.e. reads in information
662 from the file system the next time a cached property is fetched.
664 \note On Windows CE, there might be a delay for the file system driver
665 to detect changes on the file.
667 void QFileInfo::refresh()
674 Returns the file name, including the path (which may be absolute
677 \sa absoluteFilePath(), canonicalFilePath(), isRelative()
679 QString QFileInfo::filePath() const
681 Q_D(const QFileInfo);
682 if (d->isDefaultConstructed)
683 return QLatin1String("");
684 return d->fileEntry.filePath();
688 Returns the name of the file, excluding the path.
691 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 3
693 Note that, if this QFileInfo object is given a path ending in a
694 slash, the name of the file is considered empty.
696 \sa isRelative(), filePath(), baseName(), extension()
698 QString QFileInfo::fileName() const
700 Q_D(const QFileInfo);
701 if (d->isDefaultConstructed)
702 return QLatin1String("");
703 return d->fileEntry.fileName();
708 Returns the name of the bundle.
710 On Mac OS X this returns the proper localized name for a bundle if the
711 path isBundle(). On all other platforms an empty QString is returned.
714 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 4
716 \sa isBundle(), filePath(), baseName(), extension()
718 QString QFileInfo::bundleName() const
720 Q_D(const QFileInfo);
721 if (d->isDefaultConstructed)
722 return QLatin1String("");
723 return d->getFileName(QAbstractFileEngine::BundleName);
727 Returns the base name of the file without the path.
729 The base name consists of all characters in the file up to (but
730 not including) the \e first '.' character.
733 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 5
736 The base name of a file is computed equally on all platforms, independent
737 of file naming conventions (e.g., ".bashrc" on Unix has an empty base
738 name, and the suffix is "bashrc").
740 \sa fileName(), suffix(), completeSuffix(), completeBaseName()
742 QString QFileInfo::baseName() const
744 Q_D(const QFileInfo);
745 if (d->isDefaultConstructed)
746 return QLatin1String("");
747 return d->fileEntry.baseName();
751 Returns the complete base name of the file without the path.
753 The complete base name consists of all characters in the file up
754 to (but not including) the \e last '.' character.
757 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 6
759 \sa fileName(), suffix(), completeSuffix(), baseName()
761 QString QFileInfo::completeBaseName() const
763 Q_D(const QFileInfo);
764 if (d->isDefaultConstructed)
765 return QLatin1String("");
766 return d->fileEntry.completeBaseName();
770 Returns the complete suffix of the file.
772 The complete suffix consists of all characters in the file after
773 (but not including) the first '.'.
776 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 7
778 \sa fileName(), suffix(), baseName(), completeBaseName()
780 QString QFileInfo::completeSuffix() const
782 Q_D(const QFileInfo);
783 if (d->isDefaultConstructed)
784 return QLatin1String("");
785 return d->fileEntry.completeSuffix();
789 Returns the suffix of the file.
791 The suffix consists of all characters in the file after (but not
792 including) the last '.'.
795 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 8
797 The suffix of a file is computed equally on all platforms, independent of
798 file naming conventions (e.g., ".bashrc" on Unix has an empty base name,
799 and the suffix is "bashrc").
801 \sa fileName(), completeSuffix(), baseName(), completeBaseName()
803 QString QFileInfo::suffix() const
805 Q_D(const QFileInfo);
806 if (d->isDefaultConstructed)
807 return QLatin1String("");
808 return d->fileEntry.suffix();
813 Returns the path of the object's parent directory as a QDir object.
815 \b{Note:} The QDir returned always corresponds to the object's
816 parent directory, even if the QFileInfo represents a directory.
818 For each of the following, dir() returns a QDir for
819 \c{"~/examples/191697"}.
821 \snippet doc/src/snippets/fileinfo/main.cpp 0
823 For each of the following, dir() returns a QDir for
826 \snippet doc/src/snippets/fileinfo/main.cpp 1
828 \sa absolutePath(), filePath(), fileName(), isRelative(), absoluteDir()
830 QDir QFileInfo::dir() const
832 Q_D(const QFileInfo);
833 // ### Qt5: Maybe rename this to parentDirectory(), considering what it actually do?
834 return QDir(d->fileEntry.path());
838 Returns the file's absolute path as a QDir object.
840 \sa dir(), filePath(), fileName(), isRelative()
842 QDir QFileInfo::absoluteDir() const
844 return QDir(absolutePath());
848 Returns true if the user can read the file; otherwise returns false.
850 \sa isWritable(), isExecutable(), permission()
852 bool QFileInfo::isReadable() const
854 Q_D(const QFileInfo);
855 if (d->isDefaultConstructed)
857 if (d->fileEngine == 0) {
858 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::UserReadPermission))
859 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::UserReadPermission);
860 return (d->metaData.permissions() & QFile::ReadUser) != 0;
862 return d->getFileFlags(QAbstractFileEngine::ReadUserPerm);
866 Returns true if the user can write to the file; otherwise returns false.
868 \sa isReadable(), isExecutable(), permission()
870 bool QFileInfo::isWritable() const
872 Q_D(const QFileInfo);
873 if (d->isDefaultConstructed)
875 if (d->fileEngine == 0) {
876 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::UserWritePermission))
877 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::UserWritePermission);
878 return (d->metaData.permissions() & QFile::WriteUser) != 0;
880 return d->getFileFlags(QAbstractFileEngine::WriteUserPerm);
884 Returns true if the file is executable; otherwise returns false.
886 \sa isReadable(), isWritable(), permission()
888 bool QFileInfo::isExecutable() const
890 Q_D(const QFileInfo);
891 if (d->isDefaultConstructed)
893 if (d->fileEngine == 0) {
894 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::UserExecutePermission))
895 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::UserExecutePermission);
896 return (d->metaData.permissions() & QFile::ExeUser) != 0;
898 return d->getFileFlags(QAbstractFileEngine::ExeUserPerm);
902 Returns true if this is a `hidden' file; otherwise returns false.
904 \b{Note:} This function returns true for the special entries
905 "." and ".." on Unix, even though QDir::entryList threats them as shown.
907 bool QFileInfo::isHidden() const
909 Q_D(const QFileInfo);
910 if (d->isDefaultConstructed)
912 if (d->fileEngine == 0) {
913 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::HiddenAttribute))
914 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::HiddenAttribute);
915 return d->metaData.isHidden();
917 return d->getFileFlags(QAbstractFileEngine::HiddenFlag);
922 Returns true if the file path can be used directly with native APIs.
923 Returns false if the file is otherwise supported by a virtual file system
924 inside Qt, such as \l{the Qt Resource System}.
926 \b{Note:} Native paths may still require conversion of path separators
927 and character encoding, depending on platform and input requirements of the
930 \sa QDir::toNativeSeparators(), QFile::encodeName(), filePath(),
931 absoluteFilePath(), canonicalFilePath()
933 bool QFileInfo::isNativePath() const
935 Q_D(const QFileInfo);
936 if (d->isDefaultConstructed)
938 if (d->fileEngine == 0)
940 return d->getFileFlags(QAbstractFileEngine::LocalDiskFlag);
944 Returns true if this object points to a file or to a symbolic
945 link to a file. Returns false if the
946 object points to something which isn't a file, such as a directory.
948 \sa isDir(), isSymLink(), isBundle()
950 bool QFileInfo::isFile() const
952 Q_D(const QFileInfo);
953 if (d->isDefaultConstructed)
955 if (d->fileEngine == 0) {
956 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::FileType))
957 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::FileType);
958 return d->metaData.isFile();
960 return d->getFileFlags(QAbstractFileEngine::FileType);
964 Returns true if this object points to a directory or to a symbolic
965 link to a directory; otherwise returns false.
967 \sa isFile(), isSymLink(), isBundle()
969 bool QFileInfo::isDir() const
971 Q_D(const QFileInfo);
972 if (d->isDefaultConstructed)
974 if (d->fileEngine == 0) {
975 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::DirectoryType))
976 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::DirectoryType);
977 return d->metaData.isDirectory();
979 return d->getFileFlags(QAbstractFileEngine::DirectoryType);
985 Returns true if this object points to a bundle or to a symbolic
986 link to a bundle on Mac OS X; otherwise returns false.
988 \sa isDir(), isSymLink(), isFile()
990 bool QFileInfo::isBundle() const
992 Q_D(const QFileInfo);
993 if (d->isDefaultConstructed)
995 if (d->fileEngine == 0) {
996 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::BundleType))
997 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::BundleType);
998 return d->metaData.isBundle();
1000 return d->getFileFlags(QAbstractFileEngine::BundleType);
1004 Returns true if this object points to a symbolic link (or to a
1005 shortcut on Windows); otherwise returns false.
1007 On Unix (including Mac OS X), opening a symlink effectively opens
1008 the \l{symLinkTarget()}{link's target}. On Windows, it opens the \c
1013 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 9
1015 \note If the symlink points to a non existing file, exists() returns
1018 \sa isFile(), isDir(), symLinkTarget()
1020 bool QFileInfo::isSymLink() const
1022 Q_D(const QFileInfo);
1023 if (d->isDefaultConstructed)
1025 if (d->fileEngine == 0) {
1026 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::LegacyLinkType))
1027 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::LegacyLinkType);
1028 return d->metaData.isLegacyLink();
1030 return d->getFileFlags(QAbstractFileEngine::LinkType);
1034 Returns true if the object points to a directory or to a symbolic
1035 link to a directory, and that directory is the root directory; otherwise
1038 bool QFileInfo::isRoot() const
1040 Q_D(const QFileInfo);
1041 if (d->isDefaultConstructed)
1043 if (d->fileEngine == 0) {
1044 if (d->fileEntry.isRoot()) {
1045 #if defined(Q_OS_WIN)
1046 //the path is a drive root, but the drive may not exist
1047 //for backward compatibility, return true only if the drive exists
1048 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::ExistsAttribute))
1049 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::ExistsAttribute);
1050 return d->metaData.exists();
1057 return d->getFileFlags(QAbstractFileEngine::RootFlag);
1061 \fn QString QFileInfo::symLinkTarget() const
1064 Returns the absolute path to the file or directory a symlink (or shortcut
1065 on Windows) points to, or a an empty string if the object isn't a symbolic
1068 This name may not represent an existing file; it is only a string.
1069 QFileInfo::exists() returns true if the symlink points to an
1072 \sa exists(), isSymLink(), isDir(), isFile()
1078 Use symLinkTarget() instead.
1080 QString QFileInfo::readLink() const
1082 Q_D(const QFileInfo);
1083 if (d->isDefaultConstructed)
1084 return QLatin1String("");
1085 return d->getFileName(QAbstractFileEngine::LinkName);
1089 Returns the owner of the file. On systems where files
1090 do not have owners, or if an error occurs, an empty string is
1093 This function can be time consuming under Unix (in the order of
1096 \sa ownerId(), group(), groupId()
1098 QString QFileInfo::owner() const
1100 Q_D(const QFileInfo);
1101 if (d->isDefaultConstructed)
1102 return QLatin1String("");
1103 return d->getFileOwner(QAbstractFileEngine::OwnerUser);
1107 Returns the id of the owner of the file.
1109 On Windows and on systems where files do not have owners this
1110 function returns ((uint) -2).
1112 \sa owner(), group(), groupId()
1114 uint QFileInfo::ownerId() const
1116 Q_D(const QFileInfo);
1117 if (d->isDefaultConstructed)
1119 if (d->fileEngine == 0) {
1120 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::UserId))
1121 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::UserId);
1122 return d->metaData.userId();
1124 return d->fileEngine->ownerId(QAbstractFileEngine::OwnerUser);
1128 Returns the group of the file. On Windows, on systems where files
1129 do not have groups, or if an error occurs, an empty string is
1132 This function can be time consuming under Unix (in the order of
1135 \sa groupId(), owner(), ownerId()
1137 QString QFileInfo::group() const
1139 Q_D(const QFileInfo);
1140 if (d->isDefaultConstructed)
1141 return QLatin1String("");
1142 return d->getFileOwner(QAbstractFileEngine::OwnerGroup);
1146 Returns the id of the group the file belongs to.
1148 On Windows and on systems where files do not have groups this
1149 function always returns (uint) -2.
1151 \sa group(), owner(), ownerId()
1153 uint QFileInfo::groupId() const
1155 Q_D(const QFileInfo);
1156 if (d->isDefaultConstructed)
1158 if (d->fileEngine == 0) {
1159 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::GroupId))
1160 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::GroupId);
1161 return d->metaData.groupId();
1163 return d->fileEngine->ownerId(QAbstractFileEngine::OwnerGroup);
1167 Tests for file permissions. The \a permissions argument can be
1168 several flags of type QFile::Permissions OR-ed together to check
1169 for permission combinations.
1171 On systems where files do not have permissions this function
1172 always returns true.
1175 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 10
1177 \sa isReadable(), isWritable(), isExecutable()
1179 bool QFileInfo::permission(QFile::Permissions permissions) const
1181 Q_D(const QFileInfo);
1182 if (d->isDefaultConstructed)
1184 if (d->fileEngine == 0) {
1185 // the QFileSystemMetaData::MetaDataFlag and QFile::Permissions overlap, so just static cast.
1186 QFileSystemMetaData::MetaDataFlag permissionFlags = static_cast<QFileSystemMetaData::MetaDataFlag>((int)permissions);
1187 if (!d->cache_enabled || !d->metaData.hasFlags(permissionFlags))
1188 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, permissionFlags);
1189 return (d->metaData.permissions() & permissions) == permissions;
1191 return d->getFileFlags(QAbstractFileEngine::FileFlags((int)permissions)) == (uint)permissions;
1195 Returns the complete OR-ed together combination of
1196 QFile::Permissions for the file.
1198 QFile::Permissions QFileInfo::permissions() const
1200 Q_D(const QFileInfo);
1201 if (d->isDefaultConstructed)
1203 if (d->fileEngine == 0) {
1204 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::Permissions))
1205 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::Permissions);
1206 return d->metaData.permissions();
1208 return QFile::Permissions(d->getFileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask);
1213 Returns the file size in bytes. If the file does not exist or cannot be
1214 fetched, 0 is returned.
1218 qint64 QFileInfo::size() const
1220 Q_D(const QFileInfo);
1221 if (d->isDefaultConstructed)
1223 if (d->fileEngine == 0) {
1224 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::SizeAttribute))
1225 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::SizeAttribute);
1226 return d->metaData.size();
1228 if (!d->getCachedFlag(QFileInfoPrivate::CachedSize)) {
1229 d->setCachedFlag(QFileInfoPrivate::CachedSize);
1230 d->fileSize = d->fileEngine->size();
1236 Returns the date and time when the file was created.
1238 On most Unix systems, this function returns the time of the last
1239 status change. A status change occurs when the file is created,
1240 but it also occurs whenever the user writes or sets inode
1241 information (for example, changing the file permissions).
1243 If neither creation time nor "last status change" time are not
1244 available, returns the same as lastModified().
1246 \sa lastModified() lastRead()
1248 QDateTime QFileInfo::created() const
1250 Q_D(const QFileInfo);
1251 if (d->isDefaultConstructed)
1253 if (d->fileEngine == 0) {
1254 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::CreationTime))
1255 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::CreationTime);
1256 return d->metaData.creationTime();
1258 return d->getFileTime(QAbstractFileEngine::CreationTime);
1262 Returns the date and time when the file was last modified.
1264 \sa created() lastRead()
1266 QDateTime QFileInfo::lastModified() const
1268 Q_D(const QFileInfo);
1269 if (d->isDefaultConstructed)
1271 if (d->fileEngine == 0) {
1272 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::ModificationTime))
1273 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::ModificationTime);
1274 return d->metaData.modificationTime();
1276 return d->getFileTime(QAbstractFileEngine::ModificationTime);
1280 Returns the date and time when the file was last read (accessed).
1282 On platforms where this information is not available, returns the
1283 same as lastModified().
1285 \sa created() lastModified()
1287 QDateTime QFileInfo::lastRead() const
1289 Q_D(const QFileInfo);
1290 if (d->isDefaultConstructed)
1292 if (d->fileEngine == 0) {
1293 if (!d->cache_enabled || !d->metaData.hasFlags(QFileSystemMetaData::AccessTime))
1294 QFileSystemEngine::fillMetaData(d->fileEntry, d->metaData, QFileSystemMetaData::AccessTime);
1295 return d->metaData.accessTime();
1297 return d->getFileTime(QAbstractFileEngine::AccessTime);
1302 QFileInfoPrivate* QFileInfo::d_func()
1304 return d_ptr.data();
1308 Returns true if caching is enabled; otherwise returns false.
1310 \sa setCaching(), refresh()
1312 bool QFileInfo::caching() const
1314 Q_D(const QFileInfo);
1315 return d->cache_enabled;
1319 If \a enable is true, enables caching of file information. If \a
1320 enable is false caching is disabled.
1322 When caching is enabled, QFileInfo reads the file information from
1323 the file system the first time it's needed, but generally not
1326 Caching is enabled by default.
1328 \sa refresh(), caching()
1330 void QFileInfo::setCaching(bool enable)
1333 d->cache_enabled = enable;
1337 \typedef QFileInfoList
1340 Synonym for QList<QFileInfo>.