Skip to content
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
2 changes: 1 addition & 1 deletion include/dfm-io/dfm-io/dfmio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class DFMUtils
static qint64 deviceBytesFree(const QUrl &url);
static bool supportTrash(const QUrl &url);
static bool isGvfsFile(const QUrl &url);

// String comparison function for file names
static bool compareFileName(const QString &str1, const QString &str2);
static bool isInvalidCodecByPath(const char *path);

private:
static QMap<QString, QString>
Expand Down
67 changes: 55 additions & 12 deletions src/dfm-io/dfm-io/denumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

#include "utils/dlocalhelper.h"

#include <dfm-io/denumerator.h>

Check warning on line 9 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-io/denumerator.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 9 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-io/denumerator.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dfm-io/dfileinfo.h>

Check warning on line 10 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-io/dfileinfo.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 10 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-io/dfileinfo.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dfm-io/denumeratorfuture.h>

Check warning on line 11 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-io/denumeratorfuture.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 11 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-io/denumeratorfuture.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dfm-io/dfmio_utils.h>

Check warning on line 12 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-io/dfmio_utils.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 12 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-io/dfmio_utils.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QVariant>

Check warning on line 14 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QVariant> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QVariant> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QPointer>

Check warning on line 15 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPointer> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in src/dfm-io/dfm-io/denumerator.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QPointer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QtConcurrent>
#include <QDebug>
#include <qobjectdefs.h>
Expand Down Expand Up @@ -300,11 +301,12 @@

bool DEnumeratorPrivate::openDirByfts()
{
QString path = q->uri().path();
if (path != "/" && path.endsWith("/"))
path = path.left(path.length() - 1);
char *paths[2] = { nullptr, nullptr };
paths[0] = strdup(path.toUtf8().toStdString().data());
paths[0] = filePath(uri);
if (!paths[0]) {
error.setCode(DFMIOErrorCode::DFM_IO_ERROR_FAILED);
return false;
}
int (*compare)(const FTSENT **, const FTSENT **);
compare = nullptr;
if (sortRoleFlag == DEnumerator::SortRoleCompareFlag::kSortRoleCompareFileName) {
Expand Down Expand Up @@ -369,8 +371,7 @@
{
qInfo() << "start Async Iterator,uri = " << uri;
asyncStoped = false;
const QString &uriPath = uri.toString();
g_autoptr(GFile) gfile = g_file_new_for_uri(uriPath.toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(uri);

checkAndResetCancel();
EnumUriData *userData = new EnumUriData();
Expand All @@ -395,8 +396,7 @@
if (!gfileInfo)
continue;

auto path = uri.path() == "/" ? "/" + QString(g_file_info_get_name(gfileInfo)) : uri.path() + "/" + QString(g_file_info_get_name(gfileInfo));
nextUrl = QUrl::fromLocalFile(path);
nextUrl = buildUrl(uri, g_file_info_get_name(gfileInfo));

dfileInfoNext = DLocalHelper::createFileInfoByUri(nextUrl, g_file_info_dup(gfileInfo), queryAttributes.toStdString().c_str(),
enumLinks ? DFileInfo::FileQueryInfoFlags::kTypeNone : DFileInfo::FileQueryInfoFlags::kTypeNoFollowSymlinks);
Expand All @@ -417,7 +417,7 @@
for (auto gfileInfo : asyncInfos) {
if (!gfileInfo)
continue;
auto url = QUrl::fromLocalFile(uri.path() + "/" + QString(g_file_info_get_name(gfileInfo)));
auto url = buildUrl(uri, g_file_info_get_name(gfileInfo));

infoList.append(DLocalHelper::createFileInfoByUri(url, g_file_info_dup(gfileInfo), queryAttributes.toStdString().c_str(),
enumLinks ? DFileInfo::FileQueryInfoFlags::kTypeNone
Expand All @@ -433,6 +433,37 @@
queryAttributes = attributes;
}

char *DEnumeratorPrivate::filePath(const QUrl &url)
{
if (url.userInfo().startsWith("originPath::"))
return strdup(url.userInfo().replace("originPath::", "").toLatin1().constData());

QString path = url.path();
if (path != "/" && path.endsWith("/"))
path = path.left(path.length() - 1);
return strdup(path.toUtf8().constData());
}

QUrl DEnumeratorPrivate::buildUrl(const QUrl &url, const char *fileName)
{
auto path = url.path() == "/" ?
"/" + QString(fileName) :
url.path() + "/" + QString(fileName);
QUrl nextUrl = QUrl::fromLocalFile(path);

if (url.userInfo().startsWith("originPath::")) {
nextUrl.setUserInfo(url.userInfo() + QString::fromLatin1("/") + QString::fromLatin1(fileName));
} else if (DFMUtils::isInvalidCodecByPath(fileName)) {
auto org = url.path() == "/" ?
QString::fromLatin1("/") + QString::fromLatin1(fileName) :
QString::fromLatin1(url.path().toUtf8()) + QString::fromLatin1("/") +
QString::fromLatin1(fileName);
nextUrl.setUserInfo(QString::fromLatin1("originPath::") + org);
}

return nextUrl;
}

void DEnumeratorPrivate::enumUriAsyncCallBack(GObject *sourceObject, GAsyncResult *res, gpointer userData)
{
EnumUriData *data = static_cast<EnumUriData *>(userData);
Expand Down Expand Up @@ -661,9 +692,13 @@
g_autofree gchar *path = g_file_get_path(gfile);
if (path) {
d->nextUrl = QUrl::fromLocalFile(QString::fromLocal8Bit(path));
if (DFMUtils::isInvalidCodecByPath(path))
d->nextUrl.setUserInfo(QString::fromLatin1("originPath::") + QString::fromLatin1(path));
} else {
g_autofree gchar *uri = g_file_get_uri(gfile);
d->nextUrl = QUrl(QString::fromLocal8Bit(uri));
if (DFMUtils::isInvalidCodecByPath(uri))
d->nextUrl.setUserInfo(QString::fromLatin1("originPath::") + QString::fromLatin1(path));
}
d->dfileInfoNext = DLocalHelper::createFileInfoByUri(d->nextUrl, g_file_info_dup(gfileInfo), FILE_DEFAULT_ATTRIBUTES,
d->enumLinks ? DFileInfo::FileQueryInfoFlags::kTypeNone : DFileInfo::FileQueryInfoFlags::kTypeNoFollowSymlinks);
Expand Down Expand Up @@ -729,7 +764,7 @@
g_autoptr(GFileEnumerator) enumerator = nullptr;
g_autoptr(GError) gerror = nullptr;

g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);

d->checkAndResetCancel();
enumerator = g_file_enumerate_children(gfile,
Expand Down Expand Up @@ -782,8 +817,13 @@
QList<QSharedPointer<DEnumerator::SortFileInfo>> listFile;
QList<QSharedPointer<DEnumerator::SortFileInfo>> listDir;
QSet<QString> hideList;
const QUrl &urlHidden = QUrl::fromLocalFile(d->uri.path() + "/.hidden");
QUrl urlHidden = d->buildUrl(d->uri, ".hidden");
hideList = DLocalHelper::hideListFromUrl(urlHidden);
char *dirPath = d->filePath(d->uri);
if (!dirPath) {
qWarning() << "Failed to get file path for uri:" << d->uri;
return {};
}
while (1) {
FTSENT *ent = fts_read(d->fts);

Expand All @@ -796,7 +836,7 @@

unsigned short flag = ent->fts_info;

if (QString(ent->fts_path) == d->uri.path() || flag == FTS_DP)
if (strcmp(ent->fts_path, dirPath) == 0 || flag == FTS_DP)
continue;

d->insertSortFileInfoList(listFile, listDir, ent, d->fts, hideList);
Expand All @@ -805,6 +845,9 @@
fts_close(d->fts);
d->fts = nullptr;

// Clean up allocated memory
free(dirPath);

if (d->isMixDirAndFile)
return listFile;

Expand Down
20 changes: 9 additions & 11 deletions src/dfm-io/dfm-io/dfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ bool DFilePrivate::doOpen(DFile::OpenFlags mode)
}

const QUrl &&uri = q->uri();
g_autoptr(GFile) gfile = g_file_new_for_uri(uri.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(uri);
g_autoptr(GError) gerror = nullptr;
checkAndResetCancel();

Expand Down Expand Up @@ -699,8 +699,7 @@ bool DFile::isOpen() const

qint64 DFile::size() const
{
const QUrl &uri = d->uri;
g_autoptr(GFile) gfile = g_file_new_for_uri(uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);

g_autoptr(GError) gerror = nullptr;
d->checkAndResetCancel();
Expand All @@ -719,8 +718,7 @@ qint64 DFile::size() const

bool DFile::exists() const
{
const QUrl &uri = d->uri;
g_autoptr(GFile) gfile = g_file_new_for_uri(uri.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);
d->checkAndResetCancel();
return g_file_query_file_type(gfile, G_FILE_QUERY_INFO_NONE, d->cancellable) != G_FILE_TYPE_UNKNOWN;
}
Expand Down Expand Up @@ -772,7 +770,7 @@ DFile::Permissions DFile::permissions() const
{
DFile::Permissions retValue = DFile::Permission::kNoPermission;

g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);

g_autoptr(GError) gerror = nullptr;
d->checkAndResetCancel();
Expand Down Expand Up @@ -931,7 +929,7 @@ bool DFile::setPermissions(Permissions permission)
{
quint32 stMode = d->buildPermissions(permission);

g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);
g_autoptr(GError) gerror = nullptr;
d->checkAndResetCancel();
const std::string &attributeKey = DLocalHelper::attributeStringById(DFileInfo::AttributeID::kUnixMode);
Expand Down Expand Up @@ -1275,7 +1273,7 @@ DFileFuture *DFile::sizeAsync(int ioPriority, QObject *parent)
data->me = d.data();
data->future = future;

g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);
d->checkAndResetCancel();
const std::string &attributeKey = DLocalHelper::attributeStringById(DFileInfo::AttributeID::kStandardSize);
g_file_query_info_async(gfile, attributeKey.c_str(), G_FILE_QUERY_INFO_NONE, ioPriority, d->cancellable, DFilePrivate::sizeAsyncCallback, data);
Expand All @@ -1291,7 +1289,7 @@ DFileFuture *DFile::existsAsync(int ioPriority, QObject *parent)
data->me = d.data();
data->future = future;

g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);
d->checkAndResetCancel();
const std::string &attributeKey = DLocalHelper::attributeStringById(DFileInfo::AttributeID::kStandardType);
g_file_query_info_async(gfile, attributeKey.c_str(), G_FILE_QUERY_INFO_NONE, ioPriority, d->cancellable, d->existsAsyncCallback, data);
Expand All @@ -1307,7 +1305,7 @@ DFileFuture *DFile::permissionsAsync(int ioPriority, QObject *parent)
data->me = d.data();
data->future = future;

g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);
d->checkAndResetCancel();
const std::string &attributeKey = DLocalHelper::attributeStringById(DFileInfo::AttributeID::kUnixMode);
g_file_query_info_async(gfile, attributeKey.c_str(), G_FILE_QUERY_INFO_NONE, ioPriority, d->cancellable, d->permissionsAsyncCallback, data);
Expand All @@ -1322,7 +1320,7 @@ DFileFuture *DFile::setPermissionsAsync(Permissions permission, int ioPriority,
DFileFuture *future = new DFileFuture(parent);

quint32 stMode = d->buildPermissions(permission);
g_autoptr(GFile) gfile = g_file_new_for_uri(d->uri.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(d->uri);
d->checkAndResetCancel();
g_autoptr(GError) gerror = nullptr;
const std::string &attributeKey = DLocalHelper::attributeStringById(DFileInfo::AttributeID::kUnixMode);
Expand Down
4 changes: 1 addition & 3 deletions src/dfm-io/dfm-io/dfileinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ void DFileInfoPrivate::initNormal()
return;

const QUrl &url = q->uri();
const QString &urlStr = url.toString();

this->gfile = g_file_new_for_uri(urlStr.toLocal8Bit().data());
this->gfile = DLocalHelper::createGFile(url);;
}

void DFileInfoPrivate::attributeExtend(DFileInfo::MediaType type, QList<DFileInfo::AttributeExtendID> ids, DFileInfo::AttributeExtendFuncCallback callback)
Expand Down
29 changes: 22 additions & 7 deletions src/dfm-io/dfm-io/dfmio_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#include <gio-unix-2.0/gio/gunixmounts.h>
#include <glib/gstdio.h>

#include <QUrl>

Check warning on line 14 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QUrl> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QUrl> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QSet>

Check warning on line 15 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QSet> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QSet> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>

Check warning on line 16 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QByteArray>

Check warning on line 17 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QByteArray> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in src/dfm-io/dfm-io/dfmio_utils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QByteArray> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <fstab.h>
#include <sys/stat.h>
Expand All @@ -39,7 +40,7 @@
if (!url.isValid())
return QString();

g_autoptr(GFile) gfile = g_file_new_for_uri(url.toString().toStdString().c_str());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(url);
g_autoptr(GError) gerror = nullptr;
g_autoptr(GMount) gmount = g_file_find_enclosing_mount(gfile, nullptr, &gerror);
if (gmount) {
Expand All @@ -62,7 +63,7 @@
if (!url.isValid())
return QString();

g_autoptr(GFile) gfile = g_file_new_for_uri(url.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(url);
g_autoptr(GUnixMountEntry) mount = g_unix_mount_for(g_file_peek_path(gfile), nullptr);
if (mount)
return QString::fromLocal8Bit(g_unix_mount_get_device_path(mount));
Expand All @@ -74,7 +75,7 @@
if (!url.isValid())
return QString();

g_autoptr(GFile) gfile = g_file_new_for_uri(url.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(url);
g_autofree char *path = g_file_get_path(gfile);
if (!path)
return QString();
Expand All @@ -89,7 +90,7 @@
if (!url.isValid())
return QString();

g_autoptr(GFile) gfile = g_file_new_for_uri(url.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(url);
g_autofree char *path = g_file_get_path(gfile);
if (!path)
return QString();
Expand All @@ -111,7 +112,7 @@
return QUrl();
};

g_autoptr(GFile) file = g_file_new_for_uri(url.toString().toLocal8Bit().data());
g_autoptr(GFile) file = DLocalHelper::createGFile(url);
g_autoptr(GFile) fileParent = g_file_get_parent(file);
if (fileParent) {
if (localFirst) {
Expand All @@ -131,7 +132,7 @@
{
if (!url.isValid())
return false;
g_autoptr(GFile) gfile = g_file_new_for_uri(url.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(url);
g_autoptr(GMount) gmount = g_file_find_enclosing_mount(gfile, nullptr, nullptr);
if (gmount) {
g_autoptr(GDrive) gdrive = g_mount_get_drive(gmount);
Expand Down Expand Up @@ -326,7 +327,7 @@
if (file_stat.st_dev == home_stat.st_dev)
return true;

g_autoptr(GFile) gfile = g_file_new_for_uri(url.toString().toLocal8Bit().data());
g_autoptr(GFile) gfile = DLocalHelper::createGFile(url);
g_autofree char *path1 = g_file_get_path(gfile);
if (!path1)
return false;
Expand All @@ -350,6 +351,20 @@
return match.hasMatch();
}

bool DFMUtils::isInvalidCodecByPath(const char *path)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QTextCodec::ConverterState stat;
auto codec = QTextCodec::codecForLocale();

auto str = codec->toUnicode(path, static_cast<int>(strlen(path)), &stat);
return stat.invalidChars;
#else
QByteArray pathA(path);
return !pathA.isValidUtf8();
#endif
}

QMap<QString, QString> DFMUtils::fstabBindInfo()
{
static QMutex mutex;
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-io/dfm-io/doperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void DOperatorPrivate::setErrorFromGError(GError *gerror)

GFile *DOperatorPrivate::makeGFile(const QUrl &url)
{
return g_file_new_for_uri(url.toString().toLocal8Bit().data());
return DLocalHelper::createGFile(url);
}

void DOperatorPrivate::checkAndResetCancel()
Expand Down
3 changes: 2 additions & 1 deletion src/dfm-io/dfm-io/dwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include <dfm-io/dwatcher.h>
#include "utils/dlocalhelper.h"

#include "private/dwatcher_p.h"

Expand Down Expand Up @@ -184,7 +185,7 @@ bool DWatcher::start(int timeRate)
if (uri.scheme() == "file" && uri.path() == "/")
url.append("/");

d->gfile = g_file_new_for_uri(url.toStdString().c_str());
d->gfile = DLocalHelper::createGFile(url);

d->gmonitor = d->createMonitor(d->gfile, d->type);

Expand Down
2 changes: 2 additions & 0 deletions src/dfm-io/dfm-io/private/denumerator_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class DEnumeratorPrivate : public QObject, public QEnableSharedFromThis<DEnumera
bool hasNext();
QList<QSharedPointer<DFileInfo>> fileInfoList();
void setQueryAttributes(const QString &attributes);
char *filePath(const QUrl &url);
QUrl buildUrl(const QUrl &url, const char *fileName);

static void enumUriAsyncCallBack(GObject *sourceObject,
GAsyncResult *res,
Expand Down
Loading
Loading