Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: from linuxdeepin/dtkcore #80

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions src/dci/private/ddcifileengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#else
DDciFileEngine *engine = new DDciFileEngine(fileName);
#endif

if (!engine->isValid()) {
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
delete engine;
Expand All @@ -56,8 +57,7 @@

// 共享同个线程内的同个 DDciFile
static thread_local QHash<QString, QWeakPointer<DDciFile>> sharedDciFile;
static void doDeleteSharedDciFile(const QString &path, DDciFile *file)
{
static void doDeleteSharedDciFile(const QString &path, DDciFile *file) {
int count = sharedDciFile.remove(path);
Q_ASSERT(count > 0);
delete file;
Expand All @@ -70,7 +70,8 @@
}

DDciFileShared shared(usePath ? new DDciFile(dciFilePath) : new DDciFile(),
std::bind(doDeleteSharedDciFile, dciFilePath, std::placeholders::_1));
std::bind(doDeleteSharedDciFile, dciFilePath,
std::placeholders::_1));
sharedDciFile[dciFilePath] = shared.toWeakRef();
return shared;
}
Expand All @@ -82,6 +83,7 @@
: QAbstractFileEngineIterator(filters, nameFilters)
#endif
{

}

#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
Expand All @@ -90,17 +92,16 @@
current = nextValid;
return DDciFileEngineIterator::currentFileName();
}
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool DDciFileEngineIterator::advance()
#else
bool DDciFileEngineIterator::hasNext() const
#else
bool DDciFileEngineIterator::advance()

Check warning on line 98 in src/dci/private/ddcifileengine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'advance' is never used.
#endif
{
if (!file) {
const auto paths = DDciFileEngine::resolvePath(path());
if (paths.first.isEmpty() || paths.second.isEmpty())
if (paths.first.isEmpty()
|| paths.second.isEmpty())
return false;

file = getDciFile(paths.first);
Expand All @@ -120,7 +121,7 @@
} else if (fileType == DDciFile::Symlink) {
if (filters.testFlag(QDir::NoSymLinks))
continue;
} else { // DDciFile::UnknowFile
} else { // DDciFile::UnknowFile
continue;
}

Expand Down Expand Up @@ -198,7 +199,8 @@
}
}

if ((openMode & QIODevice::ExistingOnly) || !(openMode & QIODevice::WriteOnly)) {
if ((openMode & QIODevice::ExistingOnly)
|| !(openMode & QIODevice::WriteOnly)) {
if (!file->exists(subfilePath)) {
setError(QFile::OpenError, "The file is not exists");
return false;
Expand All @@ -219,7 +221,8 @@
#endif

// 不存在时尝试新建
if (!file->exists(subfilePath) && !file->writeFile(subfilePath, QByteArray())) {
if (!file->exists(subfilePath)
&& !file->writeFile(subfilePath, QByteArray())) {
return false;
}
}
Expand Down Expand Up @@ -509,22 +512,24 @@
QString DDciFileEngine::fileName(QAbstractFileEngine::FileName file) const
{
switch (file) {
case AbsoluteName:
case CanonicalName:
case DefaultName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath + subfilePath);
case AbsolutePathName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath);
case BaseName:
return QFileInfo(subfilePath).baseName();
case AbsoluteName:
case CanonicalName:
case DefaultName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath + subfilePath);
case AbsolutePathName:
return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath);
case BaseName:
return QFileInfo(subfilePath).baseName();
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
case AbsoluteLinkTarget:
case AbsoluteLinkTarget:
#else
case LinkName:
case LinkName:
#endif
return this->file->type(subfilePath) == DDciFile::Symlink ? this->file->symlinkTarget(subfilePath) : QString();
default:
break;
return this->file->type(subfilePath) == DDciFile::Symlink
? this->file->symlinkTarget(subfilePath)
: QString();
default:
break;
}

return QString();
Expand All @@ -539,7 +544,8 @@
subfilePath.clear();

const auto paths = resolvePath(fullPath, QString(), false);
if (paths.first.isEmpty() || paths.second.isEmpty())
if (paths.first.isEmpty()
|| paths.second.isEmpty())
return;

dciFilePath = paths.first;
Expand All @@ -560,17 +566,17 @@
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QAbstractFileEngine::IteratorUniquePtr
DDciFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
{
return QAbstractFileEngine::IteratorUniquePtr(new DDciFileEngineIterator(filters, filterNames));
}
QAbstractFileEngine::IteratorUniquePtr DDciFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
#else
DDciFileEngine::Iterator *DDciFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
#endif
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return QAbstractFileEngine::IteratorUniquePtr(new DDciFileEngineIterator(filters, filterNames));
#else
return new DDciFileEngineIterator(filters, filterNames);
}
#endif
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QAbstractFileEngine::IteratorUniquePtr DDciFileEngine::endEntryList()
Expand Down Expand Up @@ -621,7 +627,9 @@
return flushToFile(&file, writeFile);
}

QPair<QString, QString> DDciFileEngine::resolvePath(const QString &fullPath, const QString &realFilePath, bool needRealFileExists)
QPair<QString, QString> DDciFileEngine::resolvePath(const QString &fullPath,
const QString &realFilePath,
bool needRealFileExists)
{
if (!fullPath.startsWith(QStringLiteral(DCI_FILE_SCHEME) + realFilePath))
return {};
Expand Down Expand Up @@ -663,8 +671,8 @@
return {};

subfilePath = QDir::cleanPath(formatFullPath.mid(schemeLength + dciFilePath.length()));
qCDebug(
logFE(), "The DCI file path is: \"%s\", the subfile path is: \"%s\"", qPrintable(dciFilePath), qPrintable(subfilePath));
qCDebug(logFE(), "The DCI file path is: \"%s\", the subfile path is: \"%s\"",
qPrintable(dciFilePath), qPrintable(subfilePath));
Q_ASSERT(!subfilePath.isEmpty());

return qMakePair(dciFilePath, subfilePath);
Expand Down
18 changes: 10 additions & 8 deletions src/dci/private/ddcifileengine_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ using DDciFileShared = QSharedPointer<DDciFile>;
class DDciFileEngineIterator : public QAbstractFileEngineIterator
{
friend class DDciFileEngine;

public:
DDciFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters);

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool advance() override;
#else
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
QString next() override;
bool hasNext() const override;
#else
bool advance() override;
#endif

QString currentFileName() const override;
Expand All @@ -59,7 +58,6 @@ class DDciFileEngineIterator : public QAbstractFileEngineIterator
class DDciFileEngine : public QAbstractFileEngine
{
friend class DDciFileEngineIterator;

public:
explicit DDciFileEngine(const QString &fullPath);
~DDciFileEngine();
Expand Down Expand Up @@ -112,6 +110,7 @@ class DDciFileEngine : public QAbstractFileEngine
#endif

typedef DDciFileEngineIterator Iterator;

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
IteratorUniquePtr beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames) override;
IteratorUniquePtr endEntryList() override;
Expand All @@ -123,7 +122,8 @@ class DDciFileEngine : public QAbstractFileEngine
qint64 read(char *data, qint64 maxlen) override;
qint64 write(const char *data, qint64 len) override;

bool extension(Extension extension, const ExtensionOption *option = 0, ExtensionReturn *output = 0) override;
bool extension(Extension extension, const ExtensionOption *option = 0,
ExtensionReturn *output = 0) override;
bool supportsExtension(Extension extension) const override;

bool cloneTo(QAbstractFileEngine *target) override;
Expand All @@ -138,8 +138,9 @@ class DDciFileEngine : public QAbstractFileEngine
* 是 DCI 文件的内部路径。
* 函数返回的第一个数据是"真实文件路径"。
*/
static QPair<QString, QString>
resolvePath(const QString &fullPath, const QString &realFilePath = QString(), bool needRealFileExists = true);
static QPair<QString, QString> resolvePath(const QString &fullPath,
const QString &realFilePath = QString(),
bool needRealFileExists = true);

DDciFileShared file;
QString dciFilePath;
Expand All @@ -150,4 +151,5 @@ class DDciFileEngine : public QAbstractFileEngine
QBuffer *fileBuffer = nullptr;
};


DCORE_END_NAMESPACE