Qt 4.8
qabstractfileengine.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractfileengine.h"
43 #include "private/qabstractfileengine_p.h"
44 #ifdef QT_BUILD_CORE_LIB
45 #include "private/qresource_p.h"
46 #endif
47 #include "qdatetime.h"
48 #include "qreadwritelock.h"
49 #include "qvariant.h"
50 // built-in handlers
51 #include "qfsfileengine.h"
52 #include "qdiriterator.h"
53 #include "qstringbuilder.h"
54 
55 #include <QtCore/private/qfilesystementry_p.h>
56 #include <QtCore/private/qfilesystemmetadata_p.h>
57 #include <QtCore/private/qfilesystemengine_p.h>
58 
60 
108 static bool qt_file_engine_handlers_in_use = false;
109 
110 /*
111  All application-wide handlers are stored in this list. The mutex must be
112  acquired to ensure thread safety.
113  */
117 {
118 public:
120  {
121  QWriteLocker locker(fileEngineHandlerMutex());
123  }
124 };
126 
127 
137 {
138  QWriteLocker locker(fileEngineHandlerMutex());
140  fileEngineHandlers()->prepend(this);
141 }
142 
148 {
149  QWriteLocker locker(fileEngineHandlerMutex());
150  // Remove this handler from the handler list only if the list is valid.
152  QAbstractFileEngineHandlerList *handlers = fileEngineHandlers();
153  handlers->removeOne(this);
154  if (handlers->isEmpty())
156  }
157 }
158 
159 /*
160  \ìnternal
161 
162  Handles calls to custom file engine handlers.
163 */
165 {
166  QAbstractFileEngine *engine = 0;
167 
169  QReadLocker locker(fileEngineHandlerMutex());
170 
171  // check for registered handlers that can load the file
172  QAbstractFileEngineHandlerList *handlers = fileEngineHandlers();
173  for (int i = 0; i < handlers->size(); i++) {
174  if ((engine = handlers->at(i)->create(path)))
175  break;
176  }
177  }
178 
179  return engine;
180 }
181 
209 {
210  QFileSystemEntry entry(fileName);
211  QFileSystemMetaData metaData;
213 
214 #ifndef QT_NO_FSFILEENGINE
215  if (!engine)
216  // fall back to regular file engine
217  return new QFSFileEngine(entry.filePath());
218 #endif
219 
220  return engine;
221 }
222 
368 {
369  d_ptr->q_ptr = this;
370 }
371 
381 {
382  d_ptr->q_ptr = this;
383 }
384 
389 {
390 }
391 
404 bool QAbstractFileEngine::open(QIODevice::OpenMode openMode)
405 {
406  Q_UNUSED(openMode);
407  return false;
408 }
409 
416 {
417  return false;
418 }
419 
427 {
428  return false;
429 }
430 
435 {
436  return 0;
437 }
438 
445 {
446  return 0;
447 }
448 
464 {
465  Q_UNUSED(pos);
466  return false;
467 }
468 
477 {
478  return false;
479 }
480 
490 {
491  return false;
492 }
493 
498 bool QAbstractFileEngine::copy(const QString &newName)
499 {
500  Q_UNUSED(newName);
501  return false;
502 }
503 
514 {
515  Q_UNUSED(newName);
516  return false;
517 }
518 
525 bool QAbstractFileEngine::link(const QString &newName)
526 {
527  Q_UNUSED(newName);
528  return false;
529 }
530 
543 bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
544 {
545  Q_UNUSED(dirName);
546  Q_UNUSED(createParentDirectories);
547  return false;
548 }
549 
563 bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
564 {
565  Q_UNUSED(dirName);
566  Q_UNUSED(recurseParentDirectories);
567  return false;
568 }
569 
581 {
582  Q_UNUSED(size);
583  return false;
584 }
585 
593 {
594  return false;
595 }
596 
606 {
607  return false;
608 }
609 
623 QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
624 {
625  QStringList ret;
626  QDirIterator it(fileName(), filterNames, filters);
627  while (it.hasNext()) {
628  it.next();
629  ret << it.fileName();
630  }
631  return ret;
632 }
633 
649 QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
650 {
651  Q_UNUSED(type);
652  return 0;
653 }
654 
667 {
668  Q_UNUSED(perms);
669  return false;
670 }
671 
685 {
686  Q_UNUSED(file);
687  return QString();
688 }
689 
700 {
701  Q_UNUSED(owner);
702  return 0;
703 }
704 
716 {
717  Q_UNUSED(owner);
718  return QString();
719 }
720 
734 {
735  Q_UNUSED(time);
736  return QDateTime();
737 }
738 
748 {
749  Q_UNUSED(file);
750 }
751 
758 {
759  return -1;
760 }
761 
778 {
779  return const_cast<QAbstractFileEngine *>(this)->extension(AtEndExtension);
780 }
781 
802 {
803  MapExtensionOption option;
804  option.offset = offset;
805  option.size = size;
806  option.flags = flags;
808  if (!extension(MapExtension, &option, &r))
809  return 0;
810  return r.address;
811 }
812 
829 {
830  UnMapExtensionOption options;
831  options.address = address;
832  return extension(UnMapExtension, &options);
833 }
834 
914 {
915 public:
917  QDir::Filters filters;
920 };
921 
927  const QStringList &nameFilters)
929 {
931  d->filters = filters;
932 }
933 
940 {
941 }
942 
950 {
951  return d->path;
952 }
953 
964 {
965  d->path = path;
966 }
967 
974 {
975  return d->nameFilters;
976 }
977 
984 {
985  return d->filters;
986 }
987 
1007 {
1009  if (!name.isNull()) {
1010  QString tmp = path();
1011  if (!tmp.isEmpty()) {
1012  if (!tmp.endsWith(QLatin1Char('/')))
1013  tmp.append(QLatin1Char('/'));
1014  name.prepend(tmp);
1015  }
1016  }
1017  return name;
1018 }
1019 
1030 {
1032  if (d->fileInfo.filePath() != path)
1033  d->fileInfo.setFile(path);
1034 
1035  // return a shallow copy
1036  return d->fileInfo;
1037 }
1038 
1052 {
1053  Q_UNUSED(type)
1054  return QVariant();
1055 }
1056 
1098 {
1099  Q_UNUSED(filters);
1100  Q_UNUSED(filterNames);
1101  return 0;
1102 }
1103 
1108 {
1109  return 0;
1110 }
1111 
1120 {
1121  Q_UNUSED(data);
1122  Q_UNUSED(maxlen);
1123  return -1;
1124 }
1125 
1131 {
1132  Q_UNUSED(data);
1133  Q_UNUSED(len);
1134  return -1;
1135 }
1136 
1143 {
1144  qint64 readSoFar = 0;
1145  while (readSoFar < maxlen) {
1146  char c;
1147  qint64 readResult = read(&c, 1);
1148  if (readResult <= 0)
1149  return (readSoFar > 0) ? readSoFar : -1;
1150  ++readSoFar;
1151  *data++ = c;
1152  if (c == '\n')
1153  return readSoFar;
1154  }
1155  return readSoFar;
1156 }
1157 
1240 {
1241  Q_UNUSED(extension);
1242  Q_UNUSED(option);
1243  Q_UNUSED(output);
1244  return false;
1245 }
1246 
1260 {
1261  Q_UNUSED(extension);
1262  return false;
1263 }
1264 
1273 {
1274  Q_D(const QAbstractFileEngine);
1275  return d->fileError;
1276 }
1277 
1286 {
1287  Q_D(const QAbstractFileEngine);
1288  return d->errorString;
1289 }
1290 
1299 {
1301  d->fileError = error;
1302  d->errorString = errorString;
1303 }
1304 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setError(QFile::FileError error, const QString &str)
Sets the error type to error, and the error string to errorString.
double d
Definition: qnumeric_p.h:62
virtual FileFlags fileFlags(FileFlags type=FileInfoAll) const
This function should return the set of OR&#39;d flags that are true for the file engine&#39;s file...
QString path() const
Returns the path for this iterator.
virtual bool copy(const QString &newName)
Copies the contents of this file to a file with the name newName.
virtual bool open(QIODevice::OpenMode openMode)
Opens the file in the specified mode.
int type
Definition: qmetatype.cpp:239
unsigned char c[8]
Definition: qnumeric_p.h:62
virtual bool setSize(qint64 size)
Requests that the file be set to size size.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual ~QAbstractFileEngine()
Destroys the QAbstractFileEngine.
bool hasNext() const
Returns true if there is at least one more entry in the directory; otherwise, false is returned...
virtual bool flush()
Flushes the open file, returning true if successful; otherwise returns false.
virtual QString fileName(FileName file=DefaultName) const
Return the file engine&#39;s current file name in the format specified by file.
#define it(className, varName)
virtual bool setPermissions(uint perms)
Requests that the file&#39;s permissions be set to perms.
virtual bool isRelativePath() const
Return true if the file referred to by this file engine has a relative path; otherwise return false...
bool atEnd() const
Returns true if the current position is at the end of the file; otherwise, returns false...
virtual ~QAbstractFileEngineHandler()
Destroys the file handler.
#define error(msg)
virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const
Requests that the directory dirName is deleted from the file system.
QString & prepend(QChar c)
Definition: qstring.h:261
virtual QString owner(FileOwner) const
If owner is OwnerUser return the name of the user who owns the file.
virtual uint ownerId(FileOwner) const
If owner is OwnerUser return the ID of the user who owns the file.
virtual QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const
Requests that a list of all the files matching the filters list based on the filterNames in the file ...
virtual bool close()
Closes the file, returning true if successful; otherwise returns false.
FileName
These values are used to request a file name in a particular format.
virtual bool remove()
Requests that the file is deleted from the file system.
The QDirIterator class provides an iterator for directory entrylists.
Definition: qdiriterator.h:54
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
QAbstractFileEngine * qt_custom_file_engine_handler_create(const QString &path)
virtual void setFileName(const QString &file)
Sets the file engine&#39;s file name to file.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
provides an extended output argument to QAbstractFileEngine&#39;s extension support.
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
bool removeOne(const T &t)
Removes the first occurrence of value in the list and returns true on success; otherwise returns fals...
Definition: qlist.h:796
unsigned char uchar
Definition: qglobal.h:994
QString fileName() const
Returns the file name for the current directory entry, without the path prepended.
virtual ~QAbstractFileEngineIterator()
Destroys the QAbstractFileEngineIterator.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual bool seek(qint64 pos)
Sets the file position to the given offset.
Extension
This enum describes the types of extensions that the file engine can support.
virtual bool caseSensitive() const
Should return true if the underlying file system is case-sensitive; otherwise return false...
QScopedPointer< QAbstractFileEngineIteratorPrivate > d
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
provides an extended input argument to QAbstractFileEngine&#39;s extension support.
void setFile(const QString &file)
Sets the file that the QFileInfo provides information about to file.
Definition: qfileinfo.cpp:468
The QAbstractFileEngine class provides an abstraction for accessing the filesystem.
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
QFile::FileError error() const
Returns the QFile::FileError that resulted from the last failed operation.
virtual bool supportsExtension(Extension extension) const
This virtual function returns true if the file engine supports extension; otherwise, false is returned.
QAbstractFileEngine()
Constructs a new QAbstractFileEngine that does not refer to any file or directory.
virtual int handle() const
Returns the native file handle for this file engine.
__int64 qint64
Definition: qglobal.h:942
static QAbstractFileEngine * create(const QString &fileName)
Creates and returns a QAbstractFileEngine suitable for processing fileName.
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
bool unmap(uchar *ptr)
Unmaps the memory address.
FileTime
These are used by the fileTime() function.
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
QDir::Filters filters() const
Returns the entry filters for this iterator.
virtual qint64 write(const char *data, qint64 len)
Writes len bytes from data to the file.
QStringList nameFilters() const
Returns the name filters for this iterator.
void setPath(const QString &path)
Sets the iterator path to path.
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
virtual Iterator * beginEntryList(QDir::Filters filters, const QStringList &filterNames)
Returns an instance of a QAbstractFileEngineIterator using filters for entry filtering and filterName...
static const char *const filters[3]
QString & append(QChar c)
Definition: qstring.cpp:1777
QString next()
Advances the iterator to the next entry, and returns the file path of this new entry.
virtual qint64 read(char *data, qint64 maxlen)
Reads a number of characters from the file into data.
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
virtual Iterator * endEntryList()
The QReadWriteLock class provides read-write locking.
QScopedPointer< QAbstractFileEnginePrivate > d_ptr
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
virtual qint64 readLine(char *data, qint64 maxlen)
This function reads one line, terminated by a &#39; &#39; character, from the file info data.
QString errorString() const
Returns the human-readable message appropriate to the current error reported by error().
The QAbstractFileEngineIterator class provides an iterator interface for custom file engines...
virtual bool isSequential() const
Returns true if the file is a sequential access device; returns false if the file is a direct access ...
QString filePath() const
virtual QVariant entryInfo(EntryInfoType type) const
Returns the entry info type for this iterator&#39;s current directory entry as a QVariant.
virtual QString currentFileName() const =0
This pure virtual function returns the name of the current directory entry, excluding the path...
QString currentFilePath() const
Returns the path to the current directory entry.
virtual bool extension(Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0)
This virtual function can be reimplemented in a QAbstractFileEngine subclass to provide support for e...
uchar * map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)
Maps size bytes of the file into memory starting at offset.
virtual QDateTime fileTime(FileTime time) const
If time is CreationTime, return when the file was created.
static QAbstractFileEngine * resolveEntryAndCreateLegacyEngine(QFileSystemEntry &entry, QFileSystemMetaData &data)
Resolves the entry (see QDir::searchPaths) and returns an engine for it, but never a QFSFileEngine...
virtual bool mkdir(const QString &dirName, bool createParentDirectories) const
Requests that the directory dirName be created.
MemoryMapFlags
This enum describes special options that may be used by the map() function.
Definition: qfile.h:180
static bool qt_abstractfileenginehandlerlist_shutDown
FileError
This enum describes the errors that may be returned by the error() function.
Definition: qfile.h:74
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
virtual qint64 pos() const
Returns the current file position.
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
#define class
QAbstractFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters)
Constructs a QAbstractFileEngineIterator, using the entry filters filters, and wildcard name filters ...
QString filePath() const
Returns the file name, including the path (which may be absolute or relative).
Definition: qfileinfo.cpp:707
virtual qint64 size() const
Returns the size of the file.
EntryInfoType
This enum describes the different types of information that can be requested through the QAbstractFil...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QString fileName(const QString &fileUrl)
virtual bool rename(const QString &newName)
Requests that the file be renamed to newName in the file system.
static bool qt_file_engine_handlers_in_use
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
Declares a global static variable with the specified type and name.
Definition: qglobal.h:1982
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual QFileInfo currentFileInfo() const
The virtual function returns a QFileInfo for the current directory entry.
virtual bool link(const QString &newName)
Creates a link from the file currently specified by fileName() to newName.
The QAbstractFileEngineHandler class provides a way to register custom file engines with your applica...
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
The QFSFileEngine class implements Qt&#39;s default file engine.
Definition: qfsfileengine.h:60
virtual QAbstractFileEngine * create(const QString &fileName) const =0
Creates a file engine for file fileName.