Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qabstractfileengine.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "private/qabstractfileengine_p.h"
5#include "private/qfsfileengine_p.h"
6#ifdef QT_BUILD_CORE_LIB
7#include "private/qresource_p.h"
8#endif
9#include "qdatetime.h"
10#include "qreadwritelock.h"
11#include "qvariant.h"
12// built-in handlers
13#include "qdirlisting.h"
14#include "qstringbuilder.h"
15
16#include <QtCore/private/qfilesystementry_p.h>
17#include <QtCore/private/qfilesystemmetadata_p.h>
18#include <QtCore/private/qfilesystemengine_p.h>
19
21
22using namespace Qt::StringLiterals;
23
24static QString appendSlashIfNeeded(const QString &path)
25{
26 if (!path.isEmpty() && !path.endsWith(u'/')
27#ifdef Q_OS_ANDROID
28 && !path.startsWith("content:/"_L1)
29#endif
30 )
31 return QString{path + u'/'};
32 return path;
33}
34
35QAbstractFileEnginePrivate::~QAbstractFileEnginePrivate()
36 = default;
37
38/*!
39 \class QAbstractFileEngineHandler
40 \inmodule QtCore
41 \reentrant
42 \internal
43
44 \brief The QAbstractFileEngineHandler class provides a way to register
45 custom file engines with your application.
46
47 \ingroup io
48 \since 4.1
49
50 QAbstractFileEngineHandler is a factory for creating QAbstractFileEngine
51 objects (file engines), which are used internally by QFile, QFileInfo, and
52 QDir when working with files and directories.
53
54 When you open a file, Qt chooses a suitable file engine by passing the
55 file name from QFile or QDir through an internal list of registered file
56 engine handlers. The first handler to recognize the file name is used to
57 create the engine. Qt provides internal file engines for working with
58 regular files and resources, but you can also register your own
59 QAbstractFileEngine subclasses.
60
61 To install an application-specific file engine, you subclass
62 QAbstractFileEngineHandler and reimplement create(). When you instantiate
63 the handler (e.g. by creating an instance on the stack or on the heap), it
64 will automatically register with Qt. (The latest registered handler takes
65 precedence over existing handlers.)
66
67 For example:
68
69 \snippet code/src_corelib_io_qabstractfileengine.cpp 0
70
71 When the handler is destroyed, it is automatically removed from Qt.
72
73 The most common approach to registering a handler is to create an instance
74 as part of the start-up phase of your application. It is also possible to
75 limit the scope of the file engine handler to a particular area of
76 interest (e.g. a special file dialog that needs a custom file engine). By
77 creating the handler inside a local scope, you can precisely control the
78 area in which your engine will be applied without disturbing file
79 operations in other parts of your application.
80
81 \sa QAbstractFileEngine, QAbstractFileEngine::create()
82*/
83
84Q_CONSTINIT static QBasicAtomicInt qt_file_engine_handlers_in_use = Q_BASIC_ATOMIC_INITIALIZER(false);
85
86/*
87 All application-wide handlers are stored in this list. The mutex must be
88 acquired to ensure thread safety.
89 */
90Q_GLOBAL_STATIC(QReadWriteLock, fileEngineHandlerMutex, QReadWriteLock::Recursive)
91Q_CONSTINIT static bool qt_abstractfileenginehandlerlist_shutDown = false;
93{
95public:
97
99 {
100 QWriteLocker locker(fileEngineHandlerMutex());
101 qt_abstractfileenginehandlerlist_shutDown = true;
102 }
103};
104Q_GLOBAL_STATIC(QAbstractFileEngineHandlerList, fileEngineHandlers)
105
106/*!
107 Constructs a file handler and registers it with Qt. Once created this
108 handler's create() function will be called (along with all the other
109 handlers) for any paths used. The most recently created handler that
110 recognizes the given path (i.e. that returns a QAbstractFileEngine) is
111 used for the new path.
112
113 \sa create()
114 */
115QAbstractFileEngineHandler::QAbstractFileEngineHandler()
116{
117 QWriteLocker locker(fileEngineHandlerMutex());
118 qt_file_engine_handlers_in_use.storeRelaxed(true);
119 fileEngineHandlers()->prepend(this);
120}
121
122/*!
123 Destroys the file handler. This will automatically unregister the handler
124 from Qt.
125 */
126QAbstractFileEngineHandler::~QAbstractFileEngineHandler()
127{
128 QWriteLocker locker(fileEngineHandlerMutex());
129 // Remove this handler from the handler list only if the list is valid.
130 if (!qt_abstractfileenginehandlerlist_shutDown) {
131 QAbstractFileEngineHandlerList *handlers = fileEngineHandlers();
132 handlers->removeOne(this);
133 if (handlers->isEmpty())
134 qt_file_engine_handlers_in_use.storeRelaxed(false);
135 }
136}
137
138/*
139 \internal
140
141 Handles calls to custom file engine handlers.
142*/
143std::unique_ptr<QAbstractFileEngine> qt_custom_file_engine_handler_create(const QString &path)
144{
145 if (qt_file_engine_handlers_in_use.loadRelaxed()) {
146 QReadLocker locker(fileEngineHandlerMutex());
147
148 // check for registered handlers that can load the file
149 for (QAbstractFileEngineHandler *handler : std::as_const(*fileEngineHandlers())) {
150 if (auto engine = handler->create(path))
151 return engine;
152 }
153 }
154
155 return nullptr;
156}
157
158/*!
159 \fn std::unique_ptr<QAbstractFileEngine> QAbstractFileEngineHandler::create(const QString &fileName) const
160
161 If this file handler can handle \a fileName, this method creates a file
162 engine and returns it wrapped in a std::unique_ptr; otherwise returns
163 nullptr.
164
165 Example:
166
167 \snippet code/src_corelib_io_qabstractfileengine.cpp 1
168
169 \sa QAbstractFileEngine::create()
170*/
171
172/*!
173 Creates and returns a QAbstractFileEngine suitable for processing \a
174 fileName.
175
176 You should not need to call this function; use QFile, QFileInfo or
177 QDir directly instead.
178
179 If you reimplemnt this function, it should only return file
180 engines that knows how to handle \a fileName; otherwise, it should
181 return 0.
182
183 \sa QAbstractFileEngineHandler
184*/
185std::unique_ptr<QAbstractFileEngine> QAbstractFileEngine::create(const QString &fileName)
186{
187 QFileSystemEntry entry(fileName);
188 QFileSystemMetaData metaData;
189 auto engine = QFileSystemEngine::createLegacyEngine(entry, metaData);
190
191#ifndef QT_NO_FSFILEENGINE
192 if (!engine) // fall back to regular file engine
193 engine = std::make_unique<QFSFileEngine>(entry.filePath());
194#endif
195
196 return engine;
197}
198
199/*!
200 \class QAbstractFileEngine
201 \inmodule QtCore
202 \reentrant
203 \internal
204
205 \brief The QAbstractFileEngine class provides an abstraction for accessing
206 the filesystem.
207
208 \ingroup io
209 \since 4.1
210
211 The QDir, QFile, and QFileInfo classes all make use of a
212 QAbstractFileEngine internally. If you create your own QAbstractFileEngine
213 subclass (and register it with Qt by creating a QAbstractFileEngineHandler
214 subclass), your file engine will be used when the path is one that your
215 file engine handles.
216
217 A QAbstractFileEngine refers to one file or one directory. If the referent
218 is a file, the setFileName(), rename(), and remove() functions are
219 applicable. If the referent is a directory the mkdir(), rmdir(), and
220 entryList() functions are applicable. In all cases the caseSensitive(),
221 isRelativePath(), fileFlags(), ownerId(), owner(), and fileTime()
222 functions are applicable.
223
224 A QAbstractFileEngine subclass can be created to do synchronous network I/O
225 based file system operations, local file system operations, or to operate
226 as a resource system to access file based resources.
227
228 \sa QAbstractFileEngineHandler
229*/
230
231/*!
232 \enum QAbstractFileEngine::FileName
233
234 These values are used to request a file name in a particular
235 format.
236
237 \value DefaultName The same filename that was passed to the
238 QAbstractFileEngine.
239 \value BaseName The name of the file excluding the path.
240 \value PathName The path to the file excluding the base name.
241 \value AbsoluteName The absolute path to the file (including
242 the base name).
243 \value AbsolutePathName The absolute path to the file (excluding
244 the base name).
245 \value AbsoluteLinkTarget The full file name of the file that this file is a
246 link to. (This will be empty if this file is not a link.)
247 \value RawLinkPath The raw link path of the file that this file is a
248 link to. (This will be empty if this file is not a link.)
249 \value CanonicalName Often very similar to AbsoluteLinkTarget. Will return the true path to the file.
250 \value CanonicalPathName Same as CanonicalName, excluding the base name.
251 \value BundleName Returns the name of the bundle implies BundleType is set.
252 \value JunctionName The full name of the directory that this NTFS junction
253 is linked to. (This will be empty if this file is not an NTFS junction.)
254
255 \omitvalue NFileNames
256
257 \sa fileName(), setFileName()
258*/
259
260/*!
261 \enum QAbstractFileEngine::FileFlag
262
263 The permissions and types of a file, suitable for OR'ing together.
264
265 \value ReadOwnerPerm The owner of the file has permission to read
266 it.
267 \value WriteOwnerPerm The owner of the file has permission to
268 write to it.
269 \value ExeOwnerPerm The owner of the file has permission to
270 execute it.
271 \value ReadUserPerm The current user has permission to read the
272 file.
273 \value WriteUserPerm The current user has permission to write to
274 the file.
275 \value ExeUserPerm The current user has permission to execute the
276 file.
277 \value ReadGroupPerm Members of the current user's group have
278 permission to read the file.
279 \value WriteGroupPerm Members of the current user's group have
280 permission to write to the file.
281 \value ExeGroupPerm Members of the current user's group have
282 permission to execute the file.
283 \value ReadOtherPerm All users have permission to read the file.
284 \value WriteOtherPerm All users have permission to write to the
285 file.
286 \value ExeOtherPerm All users have permission to execute the file.
287
288 \value LinkType The file is a link to another file (or link) in
289 the file system (i.e. not a file or directory).
290 \value FileType The file is a regular file to the file system
291 (i.e. not a link or directory)
292 \value BundleType \macos and iOS: the file is a bundle; implies DirectoryType
293 \value DirectoryType The file is a directory in the file system
294 (i.e. not a link or file).
295
296 \value HiddenFlag The file is hidden.
297 \value ExistsFlag The file actually exists in the file system.
298 \value RootFlag The file or the file pointed to is the root of the filesystem.
299 \value LocalDiskFlag The file resides on the local disk and can be passed to standard file functions.
300 \value Refresh Passing this flag will force the file engine to refresh all flags.
301
302 \omitvalue PermsMask
303 \omitvalue TypesMask
304 \omitvalue FlagsMask
305 \omitvalue FileInfoAll
306
307 \sa fileFlags(), setFileName()
308*/
309
310/*!
311 \enum QAbstractFileEngine::FileOwner
312
313 \value OwnerUser The user who owns the file.
314 \value OwnerGroup The group who owns the file.
315
316 \sa owner(), ownerId(), setFileName()
317*/
318
319/*!
320 Constructs a new QAbstractFileEngine that does not refer to any file or directory.
321
322 \sa setFileName()
323 */
324QAbstractFileEngine::QAbstractFileEngine() : d_ptr(new QAbstractFileEnginePrivate(this))
325{
326}
327
328/*!
329 \internal
330
331 Constructs a QAbstractFileEngine.
332 */
333QAbstractFileEngine::QAbstractFileEngine(QAbstractFileEnginePrivate &dd) : d_ptr(&dd)
334{
335 Q_ASSERT(d_ptr->q_ptr == this);
336}
337
338/*!
339 Destroys the QAbstractFileEngine.
340 */
341QAbstractFileEngine::~QAbstractFileEngine()
342{
343}
344
345/*!
346 \fn bool QAbstractFileEngine::open(QIODevice::OpenMode mode)
347
348 Opens the file in the specified \a mode. Returns \c true if the file
349 was successfully opened; otherwise returns \c false.
350
351 The \a mode is an OR combination of QIODevice::OpenMode and
352 QIODevice::HandlingMode values.
353
354 If the file is created as a result of this call, its permissions are
355 set according to \a permissision. Null value means an implementation-
356 specific default.
357*/
358bool QAbstractFileEngine::open(QIODevice::OpenMode openMode,
359 std::optional<QFile::Permissions> permissions)
360{
361 Q_UNUSED(openMode);
362 Q_UNUSED(permissions);
363 return false;
364}
365
366/*!
367 Closes the file, returning true if successful; otherwise returns \c false.
368
369 The default implementation always returns \c false.
370*/
371bool QAbstractFileEngine::close()
372{
373 return false;
374}
375
376/*!
377 \since 5.1
378
379 Flushes and syncs the file to disk.
380
381 Returns \c true if successful; otherwise returns \c false.
382 The default implementation always returns \c false.
383*/
384bool QAbstractFileEngine::syncToDisk()
385{
386 return false;
387}
388
389/*!
390 Flushes the open file, returning true if successful; otherwise returns
391 false.
392
393 The default implementation always returns \c false.
394*/
395bool QAbstractFileEngine::flush()
396{
397 return false;
398}
399
400/*!
401 Returns the size of the file.
402*/
403qint64 QAbstractFileEngine::size() const
404{
405 return 0;
406}
407
408/*!
409 Returns the current file position.
410
411 This is the position of the data read/write head of the file.
412*/
413qint64 QAbstractFileEngine::pos() const
414{
415 return 0;
416}
417
418/*!
419 \fn bool QAbstractFileEngine::seek(qint64 offset)
420
421 Sets the file position to the given \a offset. Returns \c true if
422 the position was successfully set; otherwise returns \c false.
423
424 The offset is from the beginning of the file, unless the
425 file is sequential.
426
427 \sa isSequential()
428*/
429bool QAbstractFileEngine::seek(qint64 pos)
430{
431 Q_UNUSED(pos);
432 return false;
433}
434
435/*!
436 Returns \c true if the file is a sequential access device; returns
437 false if the file is a direct access device.
438
439 Operations involving size() and seek(qint64) are not valid on
440 sequential devices.
441*/
442bool QAbstractFileEngine::isSequential() const
443{
444 return false;
445}
446
447/*!
448 Requests that the file is deleted from the file system. If the
449 operation succeeds return true; otherwise return false.
450
451 \sa setFileName(), rmdir()
452 */
453bool QAbstractFileEngine::remove()
454{
455 return false;
456}
457
458/*!
459 Copies the contents of this file to a file with the name \a newName.
460 Returns \c true on success; otherwise, false is returned.
461*/
462bool QAbstractFileEngine::copy(const QString &newName)
463{
464 Q_UNUSED(newName);
465 return false;
466}
467
468/*!
469 Requests that the file be renamed to \a newName in the file
470 system. If the operation succeeds return true; otherwise return
471 false.
472
473 \sa setFileName()
474 */
475bool QAbstractFileEngine::rename(const QString &newName)
476{
477 Q_UNUSED(newName);
478 return false;
479}
480
481/*!
482 \since 5.1
483
484 Requests that the file be renamed to \a newName in the file
485 system. If the new name already exists, it must be overwritten.
486 If the operation succeeds, returns \c true; otherwise returns
487 false.
488
489 \sa setFileName()
490 */
491bool QAbstractFileEngine::renameOverwrite(const QString &newName)
492{
493 Q_UNUSED(newName);
494 return false;
495}
496
497/*!
498 Creates a link from the file currently specified by fileName() to
499 \a newName. What a link is depends on the underlying filesystem
500 (be it a shortcut on Windows or a symbolic link on Unix). Returns
501 true if successful; otherwise returns \c false.
502*/
503bool QAbstractFileEngine::link(const QString &newName)
504{
505 Q_UNUSED(newName);
506 return false;
507}
508
509/*!
510 Requests that the directory \a dirName be created with the specified \a permissions.
511 If \a createParentDirectories is true, then any sub-directories in \a dirName
512 that don't exist must be created. If \a createParentDirectories is false then
513 any sub-directories in \a dirName must already exist for the function to
514 succeed. If the operation succeeds return true; otherwise return
515 false.
516
517 If \a permissions is null then implementation-specific default permissions are
518 used.
519
520 \sa setFileName(), rmdir(), isRelativePath()
521 */
522bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories,
523 std::optional<QFile::Permissions> permissions) const
524{
525 Q_UNUSED(dirName);
526 Q_UNUSED(createParentDirectories);
527 Q_UNUSED(permissions);
528 return false;
529}
530
531/*!
532 Requests that the directory \a dirName is deleted from the file
533 system. When \a recurseParentDirectories is true, then any empty
534 parent-directories in \a dirName must also be deleted. If
535 \a recurseParentDirectories is false, only the \a dirName leaf-node
536 should be deleted. In most file systems a directory cannot be deleted
537 using this function if it is non-empty. If the operation succeeds
538 return true; otherwise return false.
539
540 \sa setFileName(), remove(), mkdir(), isRelativePath()
541 */
542bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
543{
544 Q_UNUSED(dirName);
545 Q_UNUSED(recurseParentDirectories);
546 return false;
547}
548
549/*!
550 Requests that the file be set to size \a size. If \a size is larger
551 than the current file then it is filled with 0's, if smaller it is
552 simply truncated. If the operations succceeds return true; otherwise
553 return false;
554
555 \sa size()
556*/
557bool QAbstractFileEngine::setSize(qint64 size)
558{
559 Q_UNUSED(size);
560 return false;
561}
562
563/*!
564 Should return true if the underlying file system is case-sensitive;
565 otherwise return false.
566 */
567bool QAbstractFileEngine::caseSensitive() const
568{
569 return false;
570}
571
572/*!
573 Return true if the file referred to by this file engine has a
574 relative path; otherwise return false.
575
576 \sa setFileName()
577 */
578bool QAbstractFileEngine::isRelativePath() const
579{
580 return false;
581}
582
583/*!
584 Requests that a list of all the files matching the \a filters
585 list based on the \a filterNames in the file engine's directory
586 are returned.
587
588 Should return an empty list if the file engine refers to a file
589 rather than a directory, or if the directory is unreadable or does
590 not exist or if nothing matches the specifications.
591
592 \sa setFileName()
593 */
594QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
595{
596 QStringList ret;
597#ifdef QT_BOOTSTRAPPED
598 Q_UNUSED(filters);
599 Q_UNUSED(filterNames);
600 Q_UNREACHABLE_RETURN(ret);
601#else
602 for (const auto &dirEntry : QDirListing(fileName(), filterNames, filters.toInt()))
603 ret.emplace_back(dirEntry.fileName());
604 return ret;
605#endif
606}
607
608QStringList QAbstractFileEngine::entryList(QDirListing::IteratorFlags filters,
609 const QStringList &filterNames) const
610{
611 QStringList ret;
612#ifdef QT_BOOTSTRAPPED
613 Q_UNUSED(filters);
614 Q_UNUSED(filterNames);
615 Q_UNREACHABLE_RETURN(ret);
616#else
617 for (const auto &dirEntry : QDirListing(fileName(), filterNames, filters))
618 ret.emplace_back(dirEntry.fileName());
619 return ret;
620#endif
621}
622
623/*!
624 This function should return the set of OR'd flags that are true
625 for the file engine's file, and that are in the \a type's OR'd
626 members.
627
628 In your reimplementation you can use the \a type argument as an
629 optimization hint and only return the OR'd set of members that are
630 true and that match those in \a type; in other words you can
631 ignore any members not mentioned in \a type, thus avoiding some
632 potentially expensive lookups or system calls.
633
634 \sa setFileName()
635*/
636QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
637{
638 Q_UNUSED(type);
639 return {};
640}
641
642/*!
643 Requests that the file's permissions be set to \a perms. The argument
644 perms will be set to the OR-ed together combination of
645 QAbstractFileEngine::FileInfo, with only the QAbstractFileEngine::PermsMask being
646 honored. If the operations succceeds return true; otherwise return
647 false;
648
649 \sa size()
650*/
651bool QAbstractFileEngine::setPermissions(uint perms)
652{
653 Q_UNUSED(perms);
654 return false;
655}
656
657/*!
658 \since 5.9
659
660 Return an identifier that (hopefully) uniquely identifies this file in the
661 system. Returns an invalid QByteArray() if that cannot be calculated.
662*/
663QByteArray QAbstractFileEngine::id() const
664{
665 return QByteArray();
666}
667
668/*!
669 Return the file engine's current file name in the format
670 specified by \a file.
671
672 If you don't handle some \c FileName possibilities, return the
673 file name set in setFileName() when an unhandled format is
674 requested.
675
676 \sa setFileName(), FileName
677 */
678QString QAbstractFileEngine::fileName(FileName file) const
679{
680 Q_UNUSED(file);
681 return QString();
682}
683
684/*!
685 If \a owner is \c OwnerUser return the ID of the user who owns
686 the file. If \a owner is \c OwnerGroup return the ID of the group
687 that own the file. If you can't determine the owner return -2.
688
689 \sa owner(), setFileName(), FileOwner
690 */
691uint QAbstractFileEngine::ownerId(FileOwner owner) const
692{
693 Q_UNUSED(owner);
694 return 0;
695}
696
697/*!
698 If \a owner is \c OwnerUser return the name of the user who owns
699 the file. If \a owner is \c OwnerGroup return the name of the group
700 that own the file. If you can't determine the owner return
701 QString().
702
703 \sa ownerId(), setFileName(), FileOwner
704 */
705QString QAbstractFileEngine::owner(FileOwner owner) const
706{
707 Q_UNUSED(owner);
708 return QString();
709}
710
711
712/*!
713 \since 5.10
714
715 Sets the file \a time to \a newDate, returning true if successful;
716 otherwise returns false.
717
718 \sa fileTime()
719*/
720bool QAbstractFileEngine::setFileTime(const QDateTime &newDate, QFile::FileTime time)
721{
722 Q_UNUSED(newDate);
723 Q_UNUSED(time);
724 return false;
725}
726
727/*!
728 If \a time is \c BirthTime, return when the file was born (created). If \a
729 time is \c MetadataChangeTime, return when the file's metadata was last
730 changed. If \a time is \c ModificationTime, return when the file was most
731 recently modified. If \a time is \c AccessTime, return when the file was
732 most recently accessed (e.g. read or written). If the time cannot be
733 determined return QDateTime() (an invalid date time).
734
735 \sa setFileName(), QDateTime, QDateTime::isValid(), FileTime
736 */
737QDateTime QAbstractFileEngine::fileTime(QFile::FileTime time) const
738{
739 Q_UNUSED(time);
740 return QDateTime();
741}
742
743/*!
744 Sets the file engine's file name to \a file. This file name is the
745 file that the rest of the virtual functions will operate on.
746
747 \sa rename()
748 */
749void QAbstractFileEngine::setFileName(const QString &file)
750{
751 Q_UNUSED(file);
752}
753
754/*!
755 Returns the native file handle for this file engine. This handle must be
756 used with care; its value and type are platform specific, and using it
757 will most likely lead to non-portable code.
758*/
759int QAbstractFileEngine::handle() const
760{
761 return -1;
762}
763
764/*!
765 \since 4.3
766
767 Returns \c true if the current position is at the end of the file; otherwise,
768 returns \c false.
769
770 This function bases its behavior on calling extension() with
771 AtEndExtension. If the engine does not support this extension, false is
772 returned.
773
774 \sa extension(), supportsExtension(), QFile::atEnd()
775*/
776bool QAbstractFileEngine::atEnd() const
777{
778 return const_cast<QAbstractFileEngine *>(this)->extension(AtEndExtension);
779}
780
781/*!
782 \since 4.4
783
784 Maps \a size bytes of the file into memory starting at \a offset.
785 Returns a pointer to the memory if successful; otherwise returns \c false
786 if, for example, an error occurs.
787
788 This function bases its behavior on calling extension() with
789 MapExtensionOption. If the engine does not support this extension, 0 is
790 returned.
791
792 \a flags is currently not used, but could be used in the future.
793
794 \sa unmap(), supportsExtension()
795 */
796
797uchar *QAbstractFileEngine::map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)
798{
799 const MapExtensionOption option(offset, size, flags);
800 MapExtensionReturn r;
801 if (!extension(MapExtension, &option, &r))
802 return nullptr;
803 return r.address;
804}
805
806/*!
807 \since 4.4
808
809 Unmaps the memory \a address. Returns \c true if the unmap succeeds; otherwise
810 returns \c false.
811
812 This function bases its behavior on calling extension() with
813 UnMapExtensionOption. If the engine does not support this extension, false is
814 returned.
815
816 \sa map(), supportsExtension()
817 */
818bool QAbstractFileEngine::unmap(uchar *address)
819{
820 const UnMapExtensionOption options(address);
821 return extension(UnMapExtension, &options);
822}
823
824/*!
825 \since 5.10
826
827 Duplicates the contents of this file (starting from the current position)
828 to the file specified by the engine \a target.
829
830 Returns \c true on success; otherwise, \c false is returned.
831 */
832bool QAbstractFileEngine::cloneTo(QAbstractFileEngine *target)
833{
834 Q_UNUSED(target);
835 return false;
836}
837
838/*!
839 \since 4.3
840 \class QAbstractFileEngineIterator
841 \inmodule QtCore
842 \brief The QAbstractFileEngineIterator class provides an iterator
843 interface for custom file engines.
844 \internal
845
846 If all you want is to iterate over entries in a directory, see
847 QDirListing instead. This class is useful only for custom file engine
848 authors.
849
850 QAbstractFileEngineIterator is a unidirectional single-use virtual
851 iterator that plugs into QDirListing, providing transparent proxy
852 iteration for custom file engines (for example, QResourceFileEngine).
853
854 You can subclass QAbstractFileEngineIterator to provide an iterator when
855 writing your own file engine. To plug the iterator into your file system,
856 you simply return an instance of this subclass from a reimplementation of
857 QAbstractFileEngine::beginEntryList().
858
859 Example:
860
861 \snippet code/src_corelib_io_qabstractfileengine.cpp 2
862
863 QAbstractFileEngineIterator is associated with a path, name filters, and
864 entry filters. The path is the directory that the iterator lists entries
865 in. The name filters and entry filters are provided for file engines that
866 can optimize directory listing at the iterator level (e.g., network file
867 systems that need to minimize network traffic), but they can also be
868 ignored by the iterator subclass; QAbstractFileEngineIterator already
869 provides the required filtering logics in the matchesFilters() function.
870 You can call dirName() to get the directory name, nameFilters() to get a
871 stringlist of name filters, and filters() to get the entry filters.
872
873 The pure virtual function advance(), as its name implies, advances the
874 iterator to the next entry in the current directory; if the operation
875 was successful this method returns \c true, otherwise it returns \c
876 false. You have to reimplement this function in your sub-class to work
877 with your file engine implementation.
878
879 The pure virtual function currentFileName() returns the name of the
880 current entry without advancing the iterator. The currentFilePath()
881 function is provided for convenience; it returns the full path of the
882 current entry.
883
884 Here is an example of how to implement an iterator that returns each of
885 three fixed entries in sequence.
886
887 \snippet code/src_corelib_io_qabstractfileengine.cpp 3
888
889 Note: QAbstractFileEngineIterator does not deal with QDir::IteratorFlags;
890 it simply returns entries for a single directory.
891
892 \sa QDirListing
893*/
894
895/*!
896 \typedef QAbstractFileEngine::Iterator
897 \since 4.3
898
899 Synonym for QAbstractFileEngineIterator.
900*/
901
902/*!
903 \typedef QAbstractFileEngine::IteratorUniquePtr
904 \since 6.8
905
906 Synonym for std::unique_ptr<Iterator> (that is a
907 std::unique_ptr<QAbstractFileEngineIterator>).
908*/
909
910/*!
911 Constructs a QAbstractFileEngineIterator, using the entry filters \a
912 filters, and wildcard name filters \a nameFilters.
913*/
914QAbstractFileEngineIterator::QAbstractFileEngineIterator(const QString &path, QDir::Filters filters,
915 const QStringList &nameFilters)
916 : m_filters(filters),
917 m_nameFilters(nameFilters),
918 m_path(appendSlashIfNeeded(path))
919{
920}
921
922QAbstractFileEngineIterator::QAbstractFileEngineIterator(const QString &path,
923 QDirListing::IteratorFlags filters,
924 const QStringList &nameFilters)
925 : m_listingFilters(filters),
926 m_nameFilters(nameFilters),
927 m_path(appendSlashIfNeeded(path))
928{
929}
930
931/*!
932 Destroys the QAbstractFileEngineIterator.
933
934 \sa QDirListing
935*/
936QAbstractFileEngineIterator::~QAbstractFileEngineIterator()
937{
938}
939
940/*!
941
942 Returns the path for this iterator. The path is set by beginEntryList().
943 The path should't be changed once iteration begins.
944
945 \sa nameFilters(), filters()
946*/
947QString QAbstractFileEngineIterator::path() const
948{
949 return m_path;
950}
951
952/*!
953 Returns the name filters for this iterator.
954
955 \sa QDir::nameFilters(), filters(), path()
956*/
957QStringList QAbstractFileEngineIterator::nameFilters() const
958{
959 return m_nameFilters;
960}
961
962/*!
963 Returns the entry filters for this iterator.
964
965 \sa QDir::filter(), nameFilters(), path()
966*/
967QDir::Filters QAbstractFileEngineIterator::filters() const
968{
969 return m_filters;
970}
971
972/*!
973 \fn QString QAbstractFileEngineIterator::currentFileName() const = 0
974
975 This pure virtual function returns the name of the current directory
976 entry, excluding the path.
977
978 \sa currentFilePath()
979*/
980
981/*!
982 Returns the path to the current directory entry. It's the same as
983 prepending path() to the return value of currentFileName().
984
985 \sa currentFileName()
986*/
987QString QAbstractFileEngineIterator::currentFilePath() const
988{
989 QString name = currentFileName();
990 if (name.isNull())
991 return name;
992
993 return path() + name;
994}
995
996/*!
997 The virtual function returns a QFileInfo for the current directory
998 entry. This function is provided for convenience. It can also be slightly
999 faster than creating a QFileInfo object yourself, as the object returned
1000 by this function might contain cached information that QFileInfo otherwise
1001 would have to access through the file engine.
1002
1003 \sa currentFileName()
1004*/
1005QFileInfo QAbstractFileEngineIterator::currentFileInfo() const
1006{
1007 QString path = currentFilePath();
1008 if (m_fileInfo.filePath() != path)
1009 m_fileInfo.setFile(path);
1010
1011 // return a shallow copy
1012 return m_fileInfo;
1013}
1014
1015/*!
1016 \fn virtual bool QAbstractFileEngineIterator::advance() = 0
1017
1018 This pure virtual function advances the iterator to the next directory
1019 entry; if the operation was successful this method returns \c true,
1020 otherwise it returs \c false.
1021
1022 This function can optionally make use of nameFilters() and filters() to
1023 optimize its performance.
1024
1025 Reimplement this function in a subclass to advance the iterator.
1026*/
1027
1028/*!
1029 Returns a QAbstractFileEngine::IteratorUniquePtr, that can be used
1030 to iterate over the entries in \a path, using \a filters for entry
1031 filtering and \a filterNames for name filtering. This function is called
1032 by QDirListing to initiate directory iteration.
1033
1034 \sa QDirListing
1035*/
1036QAbstractFileEngine::IteratorUniquePtr
1037QAbstractFileEngine::beginEntryList(const QString &path, QDirListing::IteratorFlags filters,
1038 const QStringList &filterNames)
1039{
1040 Q_UNUSED(path);
1041 Q_UNUSED(filters);
1042 Q_UNUSED(filterNames);
1043 return {};
1044}
1045
1046/*!
1047 Reads a number of characters from the file into \a data. At most
1048 \a maxlen characters will be read.
1049
1050 Returns -1 if a fatal error occurs, or 0 if there are no bytes to
1051 read.
1052*/
1053qint64 QAbstractFileEngine::read(char *data, qint64 maxlen)
1054{
1055 Q_UNUSED(data);
1056 Q_UNUSED(maxlen);
1057 return -1;
1058}
1059
1060/*!
1061 Writes \a len bytes from \a data to the file. Returns the number
1062 of characters written on success; otherwise returns -1.
1063*/
1064qint64 QAbstractFileEngine::write(const char *data, qint64 len)
1065{
1066 Q_UNUSED(data);
1067 Q_UNUSED(len);
1068 return -1;
1069}
1070
1071/*!
1072 This function reads one line, terminated by a '\\n' character, from the
1073 file info \a data. At most \a maxlen characters will be read. The
1074 end-of-line character is included.
1075*/
1076qint64 QAbstractFileEngine::readLine(char *data, qint64 maxlen)
1077{
1078 qint64 readSoFar = 0;
1079 while (readSoFar < maxlen) {
1080 char c;
1081 qint64 readResult = read(&c, 1);
1082 if (readResult <= 0)
1083 return (readSoFar > 0) ? readSoFar : -1;
1084 ++readSoFar;
1085 *data++ = c;
1086 if (c == '\n')
1087 return readSoFar;
1088 }
1089 return readSoFar;
1090}
1091
1092/*!
1093 \enum QAbstractFileEngine::Extension
1094 \since 4.3
1095
1096 This enum describes the types of extensions that the file engine can
1097 support. Before using these extensions, you must verify that the extension
1098 is supported (i.e., call supportsExtension()).
1099
1100 \value AtEndExtension Whether the current file position is at the end of
1101 the file or not. This extension allows file engines that implement local
1102 buffering to report end-of-file status without having to check the size of
1103 the file. It is also useful for sequential files, where the size of the
1104 file cannot be used to determine whether or not you have reached the end.
1105 This extension returns \c true if the file is at the end; otherwise it returns
1106 false. The input and output arguments to extension() are ignored.
1107
1108 \value FastReadLineExtension Whether the file engine provides a
1109 fast implementation for readLine() or not. If readLine() remains
1110 unimplemented in the file engine, QAbstractFileEngine will provide
1111 an implementation based on calling read() repeatedly. If
1112 supportsExtension() returns \c false for this extension, however,
1113 QIODevice can provide a faster implementation by making use of its
1114 internal buffer. For engines that already provide a fast readLine()
1115 implementation, returning false for this extension can avoid
1116 unnecessary double-buffering in QIODevice.
1117
1118 \value MapExtension Whether the file engine provides the ability to map
1119 a file to memory.
1120
1121 \value UnMapExtension Whether the file engine provides the ability to
1122 unmap memory that was previously mapped.
1123*/
1124
1125/*!
1126 \class QAbstractFileEngine::ExtensionOption
1127 \inmodule QtCore
1128 \since 4.3
1129 \brief provides an extended input argument to QAbstractFileEngine's
1130 extension support.
1131
1132 \sa QAbstractFileEngine::extension()
1133*/
1134
1135/*!
1136 \class QAbstractFileEngine::ExtensionReturn
1137 \inmodule QtCore
1138 \since 4.3
1139 \brief provides an extended output argument to QAbstractFileEngine's
1140 extension support.
1141
1142 \sa QAbstractFileEngine::extension()
1143*/
1144
1145/*!
1146 \since 4.3
1147
1148 This virtual function can be reimplemented in a QAbstractFileEngine
1149 subclass to provide support for extensions. The \a option argument is
1150 provided as input to the extension, and this function can store output
1151 results in \a output.
1152
1153 The behavior of this function is determined by \a extension; see the
1154 Extension documentation for details.
1155
1156 You can call supportsExtension() to check if an extension is supported by
1157 the file engine.
1158
1159 By default, no extensions are supported, and this function returns \c false.
1160
1161 \sa supportsExtension(), Extension
1162*/
1163bool QAbstractFileEngine::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
1164{
1165 Q_UNUSED(extension);
1166 Q_UNUSED(option);
1167 Q_UNUSED(output);
1168 return false;
1169}
1170
1171/*!
1172 \since 4.3
1173
1174 This virtual function returns \c true if the file engine supports \a
1175 extension; otherwise, false is returned. By default, no extensions are
1176 supported.
1177
1178 \sa extension()
1179*/
1180bool QAbstractFileEngine::supportsExtension(Extension extension) const
1181{
1182 Q_UNUSED(extension);
1183 return false;
1184}
1185
1186/*!
1187 Returns the QFile::FileError that resulted from the last failed
1188 operation. If QFile::UnspecifiedError is returned, QFile will
1189 use its own idea of the error status.
1190
1191 \sa QFile::FileError, errorString()
1192 */
1193QFile::FileError QAbstractFileEngine::error() const
1194{
1195 Q_D(const QAbstractFileEngine);
1196 return d->fileError;
1197}
1198
1199/*!
1200 Returns the human-readable message appropriate to the current error
1201 reported by error(). If no suitable string is available, an
1202 empty string is returned.
1203
1204 \sa error()
1205 */
1206QString QAbstractFileEngine::errorString() const
1207{
1208 Q_D(const QAbstractFileEngine);
1209 return d->errorString;
1210}
1211
1212/*!
1213 Sets the error type to \a error, and the error string to \a errorString.
1214 Call this function to set the error values returned by the higher-level
1215 classes.
1216
1217 \sa QFile::error(), QIODevice::errorString(), QIODevice::setErrorString()
1218*/
1219void QAbstractFileEngine::setError(QFile::FileError error, const QString &errorString)
1220{
1221 Q_D(QAbstractFileEngine);
1222 d->fileError = error;
1223 d->errorString = errorString;
1224}
1225
1226QT_END_NAMESPACE
Combined button and popup list for selecting options.
static QString appendSlashIfNeeded(const QString &path)