source: trunk/src/corelib/io/qabstractfileengine.cpp@ 168

Last change on this file since 168 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 36.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qabstractfileengine.h"
43#include "private/qabstractfileengine_p.h"
44#include "qdatetime.h"
45#include "qmutex.h"
46#include "qvariant.h"
47// built-in handlers
48#include "qfsfileengine.h"
49#include "qdiriterator.h"
50
51QT_BEGIN_NAMESPACE
52
53/*!
54 \class QAbstractFileEngineHandler
55 \reentrant
56
57 \brief The QAbstractFileEngineHandler class provides a way to register
58 custom file engines with your application.
59
60 \ingroup io
61 \since 4.1
62
63 QAbstractFileEngineHandler is a factory for creating QAbstractFileEngine
64 objects (file engines), which are used internally by QFile, QFileInfo, and
65 QDir when working with files and directories.
66
67 When you open a file, Qt chooses a suitable file engine by passing the
68 file name from QFile or QDir through an internal list of registered file
69 engine handlers. The first handler to recognize the file name is used to
70 create the engine. Qt provides internal file engines for working with
71 regular files and resources, but you can also register your own
72 QAbstractFileEngine subclasses.
73
74 To install an application-specific file engine, you subclass
75 QAbstractFileEngineHandler and reimplement create(). When you instantiate
76 the handler (e.g. by creating an instance on the stack or on the heap), it
77 will automatically register with Qt. (The latest registered handler takes
78 precedence over existing handlers.)
79
80 For example:
81
82 \snippet doc/src/snippets/code/src_corelib_io_qabstractfileengine.cpp 0
83
84 When the handler is destroyed, it is automatically removed from Qt.
85
86 The most common approach to registering a handler is to create an instance
87 as part of the start-up phase of your application. It is also possible to
88 limit the scope of the file engine handler to a particular area of
89 interest (e.g. a special file dialog that needs a custom file engine). By
90 creating the handler inside a local scope, you can precisely control the
91 area in which your engine will be applied without disturbing file
92 operations in other parts of your application.
93
94 \sa QAbstractFileEngine, QAbstractFileEngine::create()
95*/
96
97/*
98 All application-wide handlers are stored in this list. The mutex must be
99 acquired to ensure thread safety.
100 */
101Q_GLOBAL_STATIC_WITH_ARGS(QMutex, fileEngineHandlerMutex, (QMutex::Recursive))
102static bool qt_abstractfileenginehandlerlist_shutDown = false;
103class QAbstractFileEngineHandlerList : public QList<QAbstractFileEngineHandler *>
104{
105public:
106 ~QAbstractFileEngineHandlerList()
107 {
108 QMutexLocker locker(fileEngineHandlerMutex());
109 qt_abstractfileenginehandlerlist_shutDown = true;
110 }
111};
112Q_GLOBAL_STATIC(QAbstractFileEngineHandlerList, fileEngineHandlers)
113
114/*!
115 Constructs a file handler and registers it with Qt. Once created this
116 handler's create() function will be called (along with all the other
117 handlers) for any paths used. The most recently created handler that
118 recognizes the given path (i.e. that returns a QAbstractFileEngine) is
119 used for the new path.
120
121 \sa create()
122 */
123QAbstractFileEngineHandler::QAbstractFileEngineHandler()
124{
125 QMutexLocker locker(fileEngineHandlerMutex());
126 fileEngineHandlers()->prepend(this);
127}
128
129/*!
130 Destroys the file handler. This will automatically unregister the handler
131 from Qt.
132 */
133QAbstractFileEngineHandler::~QAbstractFileEngineHandler()
134{
135 QMutexLocker locker(fileEngineHandlerMutex());
136 // Remove this handler from the handler list only if the list is valid.
137 if (!qt_abstractfileenginehandlerlist_shutDown)
138 fileEngineHandlers()->removeAll(this);
139}
140
141/*!
142 \fn QAbstractFileEngine *QAbstractFileEngineHandler::create(const QString &fileName) const
143
144 Creates a file engine for file \a fileName. Returns 0 if this
145 file handler cannot handle \a fileName.
146
147 Example:
148
149 \snippet doc/src/snippets/code/src_corelib_io_qabstractfileengine.cpp 1
150
151 \sa QAbstractFileEngine::create()
152*/
153
154/*!
155 Creates and returns a QAbstractFileEngine suitable for processing \a
156 fileName.
157
158 You should not need to call this function; use QFile, QFileInfo or
159 QDir directly instead.
160
161 If you reimplemnt this function, it should only return file
162 engines that knows how to handle \a fileName; otherwise, it should
163 return 0.
164
165 \sa QAbstractFileEngineHandler
166*/
167QAbstractFileEngine *QAbstractFileEngine::create(const QString &fileName)
168{
169 QMutexLocker locker(fileEngineHandlerMutex());
170
171 // check for registered handlers that can load the file
172 for (int i = 0; i < fileEngineHandlers()->size(); i++) {
173 if (QAbstractFileEngine *ret = fileEngineHandlers()->at(i)->create(fileName))
174 return ret;
175 }
176
177#ifdef QT_BUILD_CORE_LIB
178 if (!fileName.startsWith(QLatin1Char('/'))) {
179 int prefixSeparator = fileName.indexOf(QLatin1Char(':'));
180 if (prefixSeparator > 1) {
181 QString prefix = fileName.left(prefixSeparator);
182 QString fileNameWithoutPrefix = fileName.mid(prefixSeparator + 1).prepend(QLatin1Char('/'));
183 const QStringList &paths = QDir::searchPaths(prefix);
184 for (int i = 0; i < paths.count(); i++) {
185 QString path = paths.at(i);
186 path.append(fileNameWithoutPrefix);
187 QAbstractFileEngine *engine = create(path);
188 if (engine && (engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::ExistsFlag)) {
189 return engine;
190 }
191 delete engine;
192 }
193 }
194 }
195#endif
196
197#ifdef QT_NO_FSFILEENGINE
198 return 0;
199#else
200 // fall back to regular file engine
201 return new QFSFileEngine(fileName);
202#endif
203}
204
205/*!
206 \class QAbstractFileEngine
207 \reentrant
208
209 \brief The QAbstractFileEngine class provides an abstraction for accessing
210 the filesystem.
211
212 \ingroup io
213 \since 4.1
214
215 The QDir, QFile, and QFileInfo classes all make use of a
216 QAbstractFileEngine internally. If you create your own QAbstractFileEngine
217 subclass (and register it with Qt by creating a QAbstractFileEngineHandler
218 subclass), your file engine will be used when the path is one that your
219 file engine handles.
220
221 A QAbstractFileEngine refers to one file or one directory. If the referent
222 is a file, the setFileName(), rename(), and remove() functions are
223 applicable. If the referent is a directory the mkdir(), rmdir(), and
224 entryList() functions are applicable. In all cases the caseSensitive(),
225 isRelativePath(), fileFlags(), ownerId(), owner(), and fileTime()
226 functions are applicable.
227
228 A QAbstractFileEngine subclass can be created to do synchronous network I/O
229 based file system operations, local file system operations, or to operate
230 as a resource system to access file based resources.
231
232 \sa QAbstractFileEngineHandler
233*/
234
235/*!
236 \enum QAbstractFileEngine::FileName
237
238 These values are used to request a file name in a particular
239 format.
240
241 \value DefaultName The same filename that was passed to the
242 QAbstractFileEngine.
243 \value BaseName The name of the file excluding the path.
244 \value PathName The path to the file excluding the base name.
245 \value AbsoluteName The absolute path to the file (including
246 the base name).
247 \value AbsolutePathName The absolute path to the file (excluding
248 the base name).
249 \value LinkName The full file name of the file that this file is a
250 link to. (This will be empty if this file is not a link.)
251 \value CanonicalName Often very similar to LinkName. Will return the true path to the file.
252 \value CanonicalPathName Same as CanonicalName, excluding the base name.
253 \value BundleName Returns the name of the bundle implies BundleType is set.
254
255 \sa fileName(), setFileName()
256*/
257
258/*!
259 \enum QAbstractFileEngine::FileFlag
260
261 The permissions and types of a file, suitable for OR'ing together.
262
263 \value ReadOwnerPerm The owner of the file has permission to read
264 it.
265 \value WriteOwnerPerm The owner of the file has permission to
266 write to it.
267 \value ExeOwnerPerm The owner of the file has permission to
268 execute it.
269 \value ReadUserPerm The current user has permission to read the
270 file.
271 \value WriteUserPerm The current user has permission to write to
272 the file.
273 \value ExeUserPerm The current user has permission to execute the
274 file.
275 \value ReadGroupPerm Members of the current user's group have
276 permission to read the file.
277 \value WriteGroupPerm Members of the current user's group have
278 permission to write to the file.
279 \value ExeGroupPerm Members of the current user's group have
280 permission to execute the file.
281 \value ReadOtherPerm All users have permission to read the file.
282 \value WriteOtherPerm All users have permission to write to the
283 file.
284 \value ExeOtherPerm All users have permission to execute the file.
285
286 \value LinkType The file is a link to another file (or link) in
287 the file system (i.e. not a file or directory).
288 \value FileType The file is a regular file to the file system
289 (i.e. not a link or directory)
290 \value BundleType The file is a Mac OS X bundle implies DirectoryType
291 \value DirectoryType The file is a directory in the file system
292 (i.e. not a link or file).
293
294 \value HiddenFlag The file is hidden.
295 \value ExistsFlag The file actually exists in the file system.
296 \value RootFlag The file or the file pointed to is the root of the filesystem.
297 \value LocalDiskFlag The file resides on the local disk and can be passed to standard file functions.
298 \value Refresh Passing this flag will force the file engine to refresh all flags.
299
300 \omitvalue PermsMask
301 \omitvalue TypesMask
302 \omitvalue FlagsMask
303 \omitvalue FileInfoAll
304
305 \sa fileFlags(), setFileName()
306*/
307
308/*!
309 \enum QAbstractFileEngine::FileTime
310
311 These are used by the fileTime() function.
312
313 \value CreationTime When the file was created.
314 \value ModificationTime When the file was most recently modified.
315 \value AccessTime When the file was most recently accessed (e.g.
316 read or written to).
317
318 \sa setFileName()
319*/
320
321/*!
322 \enum QAbstractFileEngine::FileOwner
323
324 \value OwnerUser The user who owns the file.
325 \value OwnerGroup The group who owns the file.
326
327 \sa owner(), ownerId(), setFileName()
328*/
329
330/*!
331 Constructs a new QAbstractFileEngine that does not refer to any file or directory.
332
333 \sa setFileName()
334 */
335QAbstractFileEngine::QAbstractFileEngine() : d_ptr(new QAbstractFileEnginePrivate)
336{
337 d_ptr->q_ptr = this;
338}
339
340/*!
341 \internal
342
343 Constructs a QAbstractFileEngine.
344 */
345QAbstractFileEngine::QAbstractFileEngine(QAbstractFileEnginePrivate &dd) : d_ptr(&dd)
346{
347 d_ptr->q_ptr = this;
348}
349
350/*!
351 Destroys the QAbstractFileEngine.
352 */
353QAbstractFileEngine::~QAbstractFileEngine()
354{
355 delete d_ptr;
356 d_ptr = 0;
357}
358
359/*!
360 \fn bool QAbstractFileEngine::open(QIODevice::OpenMode mode)
361
362 Opens the file in the specified \a mode. Returns true if the file
363 was successfully opened; otherwise returns false.
364
365 The \a mode is an OR combination of QIODevice::OpenMode and
366 QIODevice::HandlingMode values.
367*/
368bool QAbstractFileEngine::open(QIODevice::OpenMode openMode)
369{
370 Q_UNUSED(openMode);
371 return false;
372}
373
374/*!
375 Closes the file, returning true if successful; otherwise returns false.
376
377 The default implementation always returns false.
378*/
379bool QAbstractFileEngine::close()
380{
381 return false;
382}
383
384/*!
385 Flushes the open file, returning true if successful; otherwise returns
386 false.
387
388 The default implementation always returns false.
389*/
390bool QAbstractFileEngine::flush()
391{
392 return false;
393}
394
395/*!
396 Returns the size of the file.
397*/
398qint64 QAbstractFileEngine::size() const
399{
400 return 0;
401}
402
403/*!
404 Returns the current file position.
405
406 This is the position of the data read/write head of the file.
407*/
408qint64 QAbstractFileEngine::pos() const
409{
410 return 0;
411}
412
413/*!
414 \fn bool QAbstractFileEngine::seek(qint64 offset)
415
416 Sets the file position to the given \a offset. Returns true if
417 the position was successfully set; otherwise returns false.
418
419 The offset is from the beginning of the file, unless the
420 file is sequential.
421
422 \sa isSequential()
423*/
424bool QAbstractFileEngine::seek(qint64 pos)
425{
426 Q_UNUSED(pos);
427 return false;
428}
429
430/*!
431 Returns true if the file is a sequential access device; returns
432 false if the file is a direct access device.
433
434 Operations involving size() and seek(int) are not valid on
435 sequential devices.
436*/
437bool QAbstractFileEngine::isSequential() const
438{
439 return false;
440}
441
442/*!
443 Requests that the file is deleted from the file system. If the
444 operation succeeds return true; otherwise return false.
445
446 This virtual function must be reimplemented by all subclasses.
447
448 \sa setFileName() rmdir()
449 */
450bool QAbstractFileEngine::remove()
451{
452 return false;
453}
454
455/*!
456 Copies the contents of this file to a file with the name \a newName.
457 Returns true on success; otherwise, false is returned.
458*/
459bool QAbstractFileEngine::copy(const QString &newName)
460{
461 Q_UNUSED(newName);
462 return false;
463}
464
465/*!
466 Requests that the file be renamed to \a newName in the file
467 system. If the operation succeeds return true; otherwise return
468 false.
469
470 This virtual function must be reimplemented by all subclasses.
471
472 \sa setFileName()
473 */
474bool QAbstractFileEngine::rename(const QString &newName)
475{
476 Q_UNUSED(newName);
477 return false;
478}
479
480/*!
481 Creates a link from the file currently specified by fileName() to
482 \a newName. What a link is depends on the underlying filesystem
483 (be it a shortcut on Windows or a symbolic link on Unix). Returns
484 true if successful; otherwise returns false.
485*/
486bool QAbstractFileEngine::link(const QString &newName)
487{
488 Q_UNUSED(newName);
489 return false;
490}
491
492/*!
493 Requests that the directory \a dirName be created. If
494 \a createParentDirectories is true, then any sub-directories in \a dirName
495 that don't exist must be created. If \a createParentDirectories is false then
496 any sub-directories in \a dirName must already exist for the function to
497 succeed. If the operation succeeds return true; otherwise return
498 false.
499
500 This virtual function must be reimplemented by all subclasses.
501
502 \sa setFileName() rmdir() isRelativePath()
503 */
504bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
505{
506 Q_UNUSED(dirName);
507 Q_UNUSED(createParentDirectories);
508 return false;
509}
510
511/*!
512 Requests that the directory \a dirName is deleted from the file
513 system. When \a recurseParentDirectories is true, then any empty
514 parent-directories in \a dirName must also be deleted. If
515 \a recurseParentDirectories is false, only the \a dirName leaf-node
516 should be deleted. In most file systems a directory cannot be deleted
517 using this function if it is non-empty. If the operation succeeds
518 return true; otherwise return false.
519
520 This virtual function must be reimplemented by all subclasses.
521
522 \sa setFileName() remove() mkdir() isRelativePath()
523 */
524bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
525{
526 Q_UNUSED(dirName);
527 Q_UNUSED(recurseParentDirectories);
528 return false;
529}
530
531/*!
532 Requests that the file be set to size \a size. If \a size is larger
533 than the current file then it is filled with 0's, if smaller it is
534 simply truncated. If the operations succceeds return true; otherwise
535 return false;
536
537 This virtual function must be reimplemented by all subclasses.
538
539 \sa size()
540*/
541bool QAbstractFileEngine::setSize(qint64 size)
542{
543 Q_UNUSED(size);
544 return false;
545}
546
547/*!
548 Should return true if the underlying file system is case-sensitive;
549 otherwise return false.
550
551 This virtual function must be reimplemented by all subclasses.
552 */
553bool QAbstractFileEngine::caseSensitive() const
554{
555 return false;
556}
557
558/*!
559 Return true if the file referred to by this file engine has a
560 relative path; otherwise return false.
561
562 This virtual function must be reimplemented by all subclasses.
563
564 \sa setFileName()
565 */
566bool QAbstractFileEngine::isRelativePath() const
567{
568 return false;
569}
570
571/*!
572 Requests that a list of all the files matching the \a filters
573 list based on the \a filterNames in the file engine's directory
574 are returned.
575
576 Should return an empty list if the file engine refers to a file
577 rather than a directory, or if the directory is unreadable or does
578 not exist or if nothing matches the specifications.
579
580 This virtual function must be reimplemented by all subclasses.
581
582 \sa setFileName()
583 */
584QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
585{
586 QStringList ret;
587 QDirIterator it(fileName(), filterNames, filters);
588 while (it.hasNext()) {
589 it.next();
590 ret << it.fileName();
591 }
592 return ret;
593}
594
595/*!
596 This function should return the set of OR'd flags that are true
597 for the file engine's file, and that are in the \a type's OR'd
598 members.
599
600 In your reimplementation you can use the \a type argument as an
601 optimization hint and only return the OR'd set of members that are
602 true and that match those in \a type; in other words you can
603 ignore any members not mentioned in \a type, thus avoiding some
604 potentially expensive lookups or system calls.
605
606 This virtual function must be reimplemented by all subclasses.
607
608 \sa setFileName()
609*/
610QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
611{
612 Q_UNUSED(type);
613 return 0;
614}
615
616/*!
617 Requests that the file's permissions be set to \a perms. The argument
618 perms will be set to the OR-ed together combination of
619 QAbstractFileEngine::FileInfo, with only the QAbstractFileEngine::PermsMask being
620 honored. If the operations succceeds return true; otherwise return
621 false;
622
623 This virtual function must be reimplemented by all subclasses.
624
625 \sa size()
626*/
627bool QAbstractFileEngine::setPermissions(uint perms)
628{
629 Q_UNUSED(perms);
630 return false;
631}
632
633/*!
634 Return the file engine's current file name in the format
635 specified by \a file.
636
637 If you don't handle some \c FileName possibilities, return the
638 file name set in setFileName() when an unhandled format is
639 requested.
640
641 This virtual function must be reimplemented by all subclasses.
642
643 \sa setFileName(), FileName
644 */
645QString QAbstractFileEngine::fileName(FileName file) const
646{
647 Q_UNUSED(file);
648 return QString();
649}
650
651/*!
652 If \a owner is \c OwnerUser return the ID of the user who owns
653 the file. If \a owner is \c OwnerGroup return the ID of the group
654 that own the file. If you can't determine the owner return -2.
655
656 This virtual function must be reimplemented by all subclasses.
657
658 \sa owner() setFileName(), FileOwner
659 */
660uint QAbstractFileEngine::ownerId(FileOwner owner) const
661{
662 Q_UNUSED(owner);
663 return 0;
664}
665
666/*!
667 If \a owner is \c OwnerUser return the name of the user who owns
668 the file. If \a owner is \c OwnerGroup return the name of the group
669 that own the file. If you can't determine the owner return
670 QString().
671
672 This virtual function must be reimplemented by all subclasses.
673
674 \sa ownerId() setFileName(), FileOwner
675 */
676QString QAbstractFileEngine::owner(FileOwner owner) const
677{
678 Q_UNUSED(owner);
679 return QString();
680}
681
682/*!
683 If \a time is \c CreationTime, return when the file was created.
684 If \a time is \c ModificationTime, return when the file was most
685 recently modified. If \a time is \c AccessTime, return when the
686 file was most recently accessed (e.g. read or written).
687 If the time cannot be determined return QDateTime() (an invalid
688 date time).
689
690 This virtual function must be reimplemented by all subclasses.
691
692 \sa setFileName(), QDateTime, QDateTime::isValid(), FileTime
693 */
694QDateTime QAbstractFileEngine::fileTime(FileTime time) const
695{
696 Q_UNUSED(time);
697 return QDateTime();
698}
699
700/*!
701 Sets the file engine's file name to \a file. This file name is the
702 file that the rest of the virtual functions will operate on.
703
704 This virtual function must be reimplemented by all subclasses.
705
706 \sa rename()
707 */
708void QAbstractFileEngine::setFileName(const QString &file)
709{
710 Q_UNUSED(file);
711}
712
713/*!
714 Returns the native file handle for this file engine. This handle must be
715 used with care; its value and type are platform specific, and using it
716 will most likely lead to non-portable code.
717*/
718int QAbstractFileEngine::handle() const
719{
720 return -1;
721}
722
723/*!
724 \since 4.3
725
726 Returns true if the current position is at the end of the file; otherwise,
727 returns false.
728
729 This function bases its behavior on calling extension() with
730 AtEndExtension. If the engine does not support this extension, false is
731 returned.
732
733 \sa extension(), supportsExtension(), QFile::atEnd()
734*/
735bool QAbstractFileEngine::atEnd() const
736{
737 return const_cast<QAbstractFileEngine *>(this)->extension(AtEndExtension);
738}
739
740/*!
741 \since 4.4
742
743 Maps \a size bytes of the file into memory starting at \a offset.
744 Returns a pointer to the memory if successful; otherwise returns false
745 if, for example, an error occurs.
746
747 This function bases its behavior on calling extension() with