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

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

trunk: Merged in qt 4.6.2 sources.

File size: 37.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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
587 if (!d->data->fileEngine) {
588 return QLatin1String("");
589 } else if (d->data->fileName.isEmpty()) {
590 qWarning("QFileInfo::absolutePath: Constructed with empty filename");
591 return QLatin1String("");
592 }
593 return d->getFileName(QAbstractFileEngine::AbsolutePathName);
594}
595
596/*!
597 Returns the file's path canonical path (excluding the file name),
598 i.e. an absolute path without symbolic links or redundant "." or ".." elements.
599
600 If the file does not exist, canonicalPath() returns an empty string.
601
602 \sa path(), absolutePath()
603*/
604
605QString QFileInfo::canonicalPath() const
606{
607 Q_D(const QFileInfo);
608 if(!d->data->fileEngine)
609 return QLatin1String("");
610 return d->getFileName(QAbstractFileEngine::CanonicalPathName);
611}
612
613
614/*!
615 Returns the file's path. This doesn't include the file name.
616
617 Note that, if this QFileInfo object is given a path ending in a
618 slash, the name of the file is considered empty and this function
619 will return the entire path.
620
621 \sa filePath(), absolutePath(), canonicalPath(), dir(), fileName(), isRelative()
622*/
623
624QString QFileInfo::path() const
625{
626 Q_D(const QFileInfo);
627 if(!d->data->fileEngine)
628 return QLatin1String("");
629 return d->getFileName(QAbstractFileEngine::PathName);
630}
631
632/*!
633 \fn bool QFileInfo::isAbsolute() const
634
635 Returns true if the file path name is absolute, otherwise returns
636 false if the path is relative.
637
638 \sa isRelative()
639*/
640
641/*!
642 Returns true if the file path name is relative, otherwise returns
643 false if the path is absolute (e.g. under Unix a path is absolute
644 if it begins with a "/").
645
646 \sa isAbsolute()
647*/
648
649bool QFileInfo::isRelative() const
650{
651 Q_D(const QFileInfo);
652 if(!d->data->fileEngine)
653 return true;
654 return d->data->fileEngine->isRelativePath();
655}
656
657
658/*!
659 Converts the file's path to an absolute path if it is not already in that form.
660 Returns true to indicate that the path was converted; otherwise returns false
661 to indicate that the path was already absolute.
662
663 \sa filePath(), isRelative()
664*/
665
666bool QFileInfo::makeAbsolute()
667{
668 Q_D(QFileInfo);
669 if(!d->data->fileEngine || !d->data->fileEngine->isRelativePath())
670 return false;
671 QString absFileName = d->getFileName(QAbstractFileEngine::AbsoluteName);
672 d->initFileEngine(absFileName);
673 return true;
674}
675
676/*!
677 Returns true if the file exists; otherwise returns false.
678
679 \note If the file is a symlink that points to a non existing
680 file, false is returned.
681*/
682
683bool QFileInfo::exists() const
684{
685 Q_D(const QFileInfo);
686 if(!d->data->fileEngine)
687 return false;
688 return d->getFileFlags(QAbstractFileEngine::ExistsFlag);
689}
690
691/*!
692 Refreshes the information about the file, i.e. reads in information
693 from the file system the next time a cached property is fetched.
694
695 \note On Windows CE, there might be a delay for the file system driver
696 to detect changes on the file.
697*/
698
699void QFileInfo::refresh()
700{
701 Q_D(QFileInfo);
702 d->reset();
703}
704
705/*!
706 Returns the file name, including the path (which may be absolute
707 or relative).
708
709 \sa absoluteFilePath(), canonicalFilePath(), isRelative()
710*/
711
712QString QFileInfo::filePath() const
713{
714 Q_D(const QFileInfo);
715 if(!d->data->fileEngine)
716 return QLatin1String("");
717 return d->getFileName(QAbstractFileEngine::DefaultName);
718}
719
720/*!
721 Returns the name of the file, excluding the path.
722
723 Example:
724 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 3
725
726 Note that, if this QFileInfo object is given a path ending in a
727 slash, the name of the file is considered empty.
728
729 \sa isRelative(), filePath(), baseName(), extension()
730*/
731
732QString QFileInfo::fileName() const
733{
734 Q_D(const QFileInfo);
735 if(!d->data->fileEngine)
736 return QLatin1String("");
737 return d->getFileName(QAbstractFileEngine::BaseName);
738}
739
740/*!
741 \since 4.3
742 Returns the name of the bundle.
743
744 On Mac OS X this returns the proper localized name for a bundle if the
745 path isBundle(). On all other platforms an empty QString is returned.
746
747 Example:
748 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 4
749
750 \sa isBundle(), filePath(), baseName(), extension()
751*/
752
753QString QFileInfo::bundleName() const
754{
755 Q_D(const QFileInfo);
756 if(!d->data->fileEngine)
757 return QLatin1String("");
758 return d->getFileName(QAbstractFileEngine::BundleName);
759}
760
761/*!
762 Returns the base name of the file without the path.
763
764 The base name consists of all characters in the file up to (but
765 not including) the \e first '.' character.
766
767 Example:
768 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 5
769
770
771 The base name of a file is computed equally on all platforms, independent
772 of file naming conventions (e.g., ".bashrc" on Unix has an empty base
773 name, and the suffix is "bashrc").
774
775 \sa fileName(), suffix(), completeSuffix(), completeBaseName()
776*/
777
778QString QFileInfo::baseName() const
779{
780 Q_D(const QFileInfo);
781 if(!d->data->fileEngine)
782 return QLatin1String("");
783 return d->getFileName(QAbstractFileEngine::BaseName).section(QLatin1Char('.'), 0, 0);
784}
785
786/*!
787 Returns the complete base name of the file without the path.
788
789 The complete base name consists of all characters in the file up
790 to (but not including) the \e last '.' character.
791
792 Example:
793 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 6
794
795 \sa fileName(), suffix(), completeSuffix(), baseName()
796*/
797
798QString QFileInfo::completeBaseName() const
799{
800 Q_D(const QFileInfo);
801 if(!d->data->fileEngine)
802 return QLatin1String("");
803 QString name = d->getFileName(QAbstractFileEngine::BaseName);
804 int index = name.lastIndexOf(QLatin1Char('.'));
805 return (index == -1) ? name : name.left(index);
806}
807
808/*!
809 Returns the complete suffix of the file.
810
811 The complete suffix consists of all characters in the file after
812 (but not including) the first '.'.
813
814 Example:
815 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 7
816
817 \sa fileName(), suffix(), baseName(), completeBaseName()
818*/
819
820QString QFileInfo::completeSuffix() const
821{
822 Q_D(const QFileInfo);
823 if(!d->data->fileEngine)
824 return QLatin1String("");
825 QString fileName = d->getFileName(QAbstractFileEngine::BaseName);
826 int firstDot = fileName.indexOf(QLatin1Char('.'));
827 if (firstDot == -1)
828 return QLatin1String("");
829 return fileName.mid(firstDot + 1);
830}
831
832/*!
833 Returns the suffix of the file.
834
835 The suffix consists of all characters in the file after (but not
836 including) the last '.'.
837
838 Example:
839 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 8
840
841 The suffix of a file is computed equally on all platforms, independent of
842 file naming conventions (e.g., ".bashrc" on Unix has an empty base name,
843 and the suffix is "bashrc").
844
845 \sa fileName(), completeSuffix(), baseName(), completeBaseName()
846*/
847
848QString QFileInfo::suffix() const
849{
850 Q_D(const QFileInfo);
851 if(!d->data->fileEngine)
852 return QLatin1String("");
853 QString fileName = d->getFileName(QAbstractFileEngine::BaseName);
854 int lastDot = fileName.lastIndexOf(QLatin1Char('.'));
855 if (lastDot == -1)
856 return QLatin1String("");
857 return fileName.mid(lastDot + 1);
858}
859
860
861/*!
862 Returns the path of the object's parent directory as a QDir object.
863
864 \bold{Note:} The QDir returned always corresponds to the object's
865 parent directory, even if the QFileInfo represents a directory.
866
867 For each of the follwing, dir() returns a QDir for
868 \c{"~/examples/191697"}.
869
870 \snippet doc/src/snippets/fileinfo/main.cpp 0
871
872 For each of the follwing, dir() returns a QDir for
873 \c{"."}.
874
875 \snippet doc/src/snippets/fileinfo/main.cpp 1
876
877 \sa absolutePath(), filePath(), fileName(), isRelative(), absoluteDir()
878*/
879
880QDir QFileInfo::dir() const
881{
882 // ### Qt5: Maybe rename this to parentDirectory(), considering what it actually do?
883 return QDir(path());
884}
885
886/*!
887 Returns the file's absolute path as a QDir object.
888
889 \sa dir(), filePath(), fileName(), isRelative()
890*/
891
892QDir QFileInfo::absoluteDir() const
893{
894 return QDir(absolutePath());
895}
896
897#ifdef QT3_SUPPORT
898/*!
899 Use absoluteDir() or the dir() overload that takes no parameters
900 instead.
901*/
902QDir QFileInfo::dir(bool absPath) const
903{
904 if(absPath)
905 return absoluteDir();
906 return dir();
907}
908#endif //QT3_SUPPORT
909
910/*!
911 Returns true if the user can read the file; otherwise returns false.
912
913 \sa isWritable(), isExecutable(), permission()
914*/
915
916bool QFileInfo::isReadable() const
917{
918 Q_D(const QFileInfo);
919 if(!d->data->fileEngine)
920 return false;
921 return d->hasAccess(QFileInfoPrivate::ReadAccess);
922}
923
924/*!
925 Returns true if the user can write to the file; otherwise returns false.
926
927 \sa isReadable(), isExecutable(), permission()
928*/
929
930bool QFileInfo::isWritable() const
931{
932 Q_D(const QFileInfo);
933 if(!d->data->fileEngine)
934 return false;
935 return d->hasAccess(QFileInfoPrivate::WriteAccess);
936}
937
938/*!
939 Returns true if the file is executable; otherwise returns false.
940
941 \sa isReadable(), isWritable(), permission()
942*/
943
944bool QFileInfo::isExecutable() const
945{
946 Q_D(const QFileInfo);
947 if(!d->data->fileEngine)
948 return false;
949 return d->hasAccess(QFileInfoPrivate::ExecuteAccess);
950}
951
952/*!
953 Returns true if this is a `hidden' file; otherwise returns false.
954
955 \bold{Note:} This function returns true for the special entries
956 "." and ".." on Unix, even though QDir::entryList threats them as shown.
957*/
958bool QFileInfo::isHidden() const
959{
960 Q_D(const QFileInfo);
961 if(!d->data->fileEngine)
962 return false;
963 return d->getFileFlags(QAbstractFileEngine::HiddenFlag);
964}
965
966/*!
967 Returns true if this object points to a file or to a symbolic
968 link to a file. Returns false if the
969 object points to something which isn't a file, such as a directory.
970
971 \sa isDir(), isSymLink(), isBundle()
972*/
973
974bool QFileInfo::isFile() const
975{
976 Q_D(const QFileInfo);
977 if(!d->data->fileEngine)
978 return false;
979 return d->getFileFlags(QAbstractFileEngine::FileType);
980}
981
982/*!
983 Returns true if this object points to a directory or to a symbolic
984 link to a directory; otherwise returns false.
985
986 \sa isFile(), isSymLink(), isBundle()
987*/
988
989bool QFileInfo::isDir() const
990{
991 Q_D(const QFileInfo);
992 if(!d->data->fileEngine)
993 return false;
994 return d->getFileFlags(QAbstractFileEngine::DirectoryType);
995}
996
997
998/*!
999 \since 4.3
1000 Returns true if this object points to a bundle or to a symbolic
1001 link to a bundle on Mac OS X; otherwise returns false.
1002
1003 \sa isDir(), isSymLink(), isFile()
1004*/
1005
1006bool QFileInfo::isBundle() const
1007{
1008 Q_D(const QFileInfo);
1009 if(!d->data->fileEngine)
1010 return false;
1011 return d->getFileFlags(QAbstractFileEngine::BundleType);
1012}
1013
1014/*!
1015 Returns true if this object points to a symbolic link (or to a
1016 shortcut on Windows); otherwise returns false.
1017
1018 On Unix (including Mac OS X), opening a symlink effectively opens
1019 the \l{symLinkTarget()}{link's target}. On Windows, it opens the \c
1020 .lnk file itself.
1021
1022 Example:
1023
1024 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 9
1025
1026 \note If the symlink points to a non existing file, exists() returns
1027 false.
1028
1029 \sa isFile(), isDir(), symLinkTarget()
1030*/
1031
1032bool QFileInfo::isSymLink() const
1033{
1034 Q_D(const QFileInfo);
1035 if(!d->data->fileEngine)
1036 return false;
1037 return d->getFileFlags(QAbstractFileEngine::LinkType);
1038}
1039
1040/*!
1041 Returns true if the object points to a directory or to a symbolic
1042 link to a directory, and that directory is the root directory; otherwise
1043 returns false.
1044*/
1045
1046bool QFileInfo::isRoot() const
1047{
1048 Q_D(const QFileInfo);
1049 if (!d->data->fileEngine)
1050 return true;
1051 return d->getFileFlags(QAbstractFileEngine::RootFlag);
1052}
1053
1054/*!
1055 \fn QString QFileInfo::symLinkTarget() const
1056 \since 4.2
1057
1058 Returns the absolute path to the file or directory a symlink (or shortcut
1059 on Windows) points to, or a an empty string if the object isn't a symbolic
1060 link.
1061
1062 This name may not represent an existing file; it is only a string.
1063 QFileInfo::exists() returns true if the symlink points to an
1064 existing file.
1065
1066 \sa exists(), isSymLink(), isDir(), isFile()
1067*/
1068
1069/*!
1070 \obsolete
1071
1072 Use symLinkTarget() instead.
1073*/
1074QString QFileInfo::readLink() const
1075{
1076 Q_D(const QFileInfo);
1077 if(!d->data->fileEngine)
1078 return QLatin1String("");
1079 return d->getFileName(QAbstractFileEngine::LinkName);
1080}
1081
1082/*!
1083 Returns the owner of the file. On systems where files
1084 do not have owners, or if an error occurs, an empty string is
1085 returned.
1086
1087 This function can be time consuming under Unix (in the order of
1088 milliseconds).
1089
1090 \sa ownerId(), group(), groupId()
1091*/
1092
1093QString QFileInfo::owner() const
1094{
1095 Q_D(const QFileInfo);
1096 if(!d->data->fileEngine)
1097 return QLatin1String("");
1098 return d->data->fileEngine->owner(QAbstractFileEngine::OwnerUser);
1099}
1100
1101/*!
1102 Returns the id of the owner of the file.
1103
1104 On Windows and on systems where files do not have owners this
1105 function returns ((uint) -2).
1106
1107 \sa owner(), group(), groupId()
1108*/
1109
1110uint QFileInfo::ownerId() const
1111{
1112 Q_D(const QFileInfo);
1113 if(!d->data->fileEngine)
1114 return 0;
1115 return d->data->fileEngine->ownerId(QAbstractFileEngine::OwnerUser);
1116}
1117
1118/*!
1119 Returns the group of the file. On Windows, on systems where files
1120 do not have groups, or if an error occurs, an empty string is
1121 returned.
1122
1123 This function can be time consuming under Unix (in the order of
1124 milliseconds).
1125
1126 \sa groupId(), owner(), ownerId()
1127*/
1128
1129QString QFileInfo::group() const
1130{
1131 Q_D(const QFileInfo);
1132 if(!d->data->fileEngine)
1133 return QLatin1String("");
1134 return d->data->fileEngine->owner(QAbstractFileEngine::OwnerGroup);
1135}
1136
1137/*!
1138 Returns the id of the group the file belongs to.
1139
1140 On Windows and on systems where files do not have groups this
1141 function always returns (uint) -2.
1142
1143 \sa group(), owner(), ownerId()
1144*/
1145
1146uint QFileInfo::groupId() const
1147{
1148 Q_D(const QFileInfo);
1149 if(!d->data->fileEngine)
1150 return 0;
1151 return d->data->fileEngine->ownerId(QAbstractFileEngine::OwnerGroup);
1152}
1153
1154/*!
1155 Tests for file permissions. The \a permissions argument can be
1156 several flags of type QFile::Permissions OR-ed together to check
1157 for permission combinations.
1158
1159 On systems where files do not have permissions this function
1160 always returns true.
1161
1162 Example:
1163 \snippet doc/src/snippets/code/src_corelib_io_qfileinfo.cpp 10
1164
1165 \sa isReadable(), isWritable(), isExecutable()
1166*/
1167
1168bool QFileInfo::permission(QFile::Permissions permissions) const
1169{
1170 Q_D(const QFileInfo);
1171 if(!d->data->fileEngine)
1172 return false;
1173 return d->getFileFlags(QAbstractFileEngine::FileFlags((int)permissions)) == (uint)permissions;
1174}
1175
1176/*!
1177 Returns the complete OR-ed together combination of
1178 QFile::Permissions for the file.
1179*/
1180
1181QFile::Permissions QFileInfo::permissions() const
1182{
1183 Q_D(const QFileInfo);
1184 if(!d->data->fileEngine)
1185 return 0;
1186 return QFile::Permissions(d->getFileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask);
1187}
1188
1189
1190/*!
1191 Returns the file size in bytes. If the file does not exist or cannot be
1192 fetched, 0 is returned.
1193
1194 \sa exists()
1195*/
1196
1197qint64 QFileInfo::size() const
1198{
1199 Q_D(const QFileInfo);
1200 if(!d->data->fileEngine)
1201 return 0;
1202 if(!d->data->getCachedFlag(QFileInfoPrivate::CachedSize)) {
1203 d->data->setCachedFlag(QFileInfoPrivate::CachedSize);
1204 d->data->fileSize = d->data->fileEngine->size();
1205 }
1206 return d->data->fileSize;
1207}
1208
1209/*!
1210 Returns the date and time when the file was created.
1211
1212 On most Unix systems, this function returns the time of the last
1213 status change. A status change occurs when the file is created,
1214 but it also occurs whenever the user writes or sets inode
1215 information (for example, changing the file permissions).
1216
1217 If neither creation time nor "last status change" time are not
1218 available, returns the same as lastModified().
1219
1220 \sa lastModified() lastRead()
1221*/
1222
1223QDateTime QFileInfo::created() const
1224{
1225 Q_D(const QFileInfo);
1226 if(!d->data->fileEngine)
1227 return QDateTime();
1228 return d->getFileTime(QAbstractFileEngine::CreationTime);
1229}
1230
1231/*!
1232 Returns the date and time when the file was last modified.
1233
1234 \sa created() lastRead()
1235*/
1236
1237QDateTime QFileInfo::lastModified() const
1238{
1239 Q_D(const QFileInfo);
1240 if(!d->data->fileEngine)
1241 return QDateTime();
1242 return d->getFileTime(QAbstractFileEngine::ModificationTime);
1243}
1244
1245/*!
1246 Returns the date and time when the file was last read (accessed).
1247
1248 On platforms where this information is not available, returns the
1249 same as lastModified().
1250
1251 \sa created() lastModified()
1252*/
1253
1254QDateTime QFileInfo::lastRead() const
1255{
1256 Q_D(const QFileInfo);
1257 if(!d->data->fileEngine)
1258 return QDateTime();
1259 return d->getFileTime(QAbstractFileEngine::AccessTime);
1260}
1261
1262/*! \internal
1263 Detaches all internal data.
1264*/
1265
1266void QFileInfo::detach()
1267{
1268 Q_D(QFileInfo);
1269 d->detach();
1270}
1271
1272/*!
1273 Returns true if caching is enabled; otherwise returns false.
1274
1275 \sa setCaching(), refresh()
1276*/
1277
1278bool QFileInfo::caching() const
1279{
1280 Q_D(const QFileInfo);
1281 return d->data->cache_enabled;
1282}
1283
1284/*!
1285 If \a enable is true, enables caching of file information. If \a
1286 enable is false caching is disabled.
1287
1288 When caching is enabled, QFileInfo reads the file information from
1289 the file system the first time it's needed, but generally not
1290 later.
1291
1292 Caching is enabled by default.
1293
1294 \sa refresh(), caching()
1295*/
1296
1297void QFileInfo::setCaching(bool enable)
1298{
1299 Q_D(QFileInfo);
1300 detach();
1301 d->data->cache_enabled = enable;
1302}
1303
1304/*!
1305 \fn QString QFileInfo::baseName(bool complete)
1306
1307 Use completeBaseName() or the baseName() overload that takes no
1308 parameters instead.
1309*/
1310
1311/*!
1312 \fn QString QFileInfo::extension(bool complete = true) const
1313
1314 Use completeSuffix() or suffix() instead.
1315*/
1316
1317/*!
1318 \fn QString QFileInfo::absFilePath() const
1319
1320 Use absoluteFilePath() instead.
1321*/
1322
1323/*!
1324 \fn QString QFileInfo::dirPath(bool absPath) const
1325
1326 Use absolutePath() if the absolute path is wanted (\a absPath
1327 is true) or path() if it's not necessary (\a absPath is false).
1328*/
1329
1330/*!
1331 \fn bool QFileInfo::convertToAbs()
1332
1333 Use makeAbsolute() instead.
1334*/
1335
1336/*!
1337 \enum QFileInfo::Permission
1338
1339 \compat
1340
1341 \value ReadOwner
1342 \value WriteOwner
1343 \value ExeOwner
1344 \value ReadUser
1345 \value WriteUser
1346 \value ExeUser
1347 \value ReadGroup
1348 \value WriteGroup
1349 \value ExeGroup
1350 \value ReadOther
1351 \value WriteOther
1352 \value ExeOther
1353*/
1354
1355/*!
1356 \fn bool QFileInfo::permission(PermissionSpec permissions) const
1357 \compat
1358
1359 Use permission() instead.
1360*/
1361
1362/*!
1363 \typedef QFileInfoList
1364 \relates QFileInfo
1365
1366 Synonym for QList<QFileInfo>.
1367*/
1368
1369QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.