From 4c9d4491df45322fe5b04c1fd5394fd55126d66b Mon Sep 17 00:00:00 2001 From: ComixHe Date: Thu, 31 Aug 2023 17:59:09 +0800 Subject: [PATCH] refact: remove unnecessary open operation fix AM can't access some dirs due to systemd 254's sandboxing option: PrivateUser is true by default. Signed-off-by: ComixHe --- ...org.deepin.dde.ApplicationManager1.service.in | 2 ++ src/dbus/applicationmanager1service.cpp | 2 +- src/desktopentry.cpp | 9 ++------- src/global.h | 16 ++++------------ 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/misc/systemd/user/org.deepin.dde.ApplicationManager1.service.in b/misc/systemd/user/org.deepin.dde.ApplicationManager1.service.in index f607453..de652bc 100644 --- a/misc/systemd/user/org.deepin.dde.ApplicationManager1.service.in +++ b/misc/systemd/user/org.deepin.dde.ApplicationManager1.service.in @@ -11,3 +11,5 @@ BusName=org.deepin.dde.ApplicationManager1 ExecStart=@CMAKE_INSTALL_FULL_BINDIR@/dde-application-manager-reborn Restart=always Environment=QT_LOGGING_RULES="*.debug=false" +PrivateUsers=false +# turn off PrivateUser to prevent AM can't access some directory. eg. "/persistent/linglong" diff --git a/src/dbus/applicationmanager1service.cpp b/src/dbus/applicationmanager1service.cpp index 3d01562..87937e3 100644 --- a/src/dbus/applicationmanager1service.cpp +++ b/src/dbus/applicationmanager1service.cpp @@ -270,7 +270,7 @@ void ApplicationManager1Service::updateApplication(const QSharedPointerdesktopFileSource().modified(std::get<1>(mtime))) { auto *newEntry = new (std::nothrow) DesktopEntry{}; diff --git a/src/desktopentry.cpp b/src/desktopentry.cpp index 6826023..20f96b0 100644 --- a/src/desktopentry.cpp +++ b/src/desktopentry.cpp @@ -259,7 +259,7 @@ bool DesktopEntry::checkMainEntryValidation() const noexcept std::optional DesktopFile::createTemporaryDesktopFile(std::unique_ptr temporaryFile) noexcept { - auto [ctime, mtime, _] = getFileTimeInfo(*temporaryFile); + auto [ctime, mtime, _] = getFileTimeInfo(QFileInfo{*temporaryFile}); if (mtime == 0) { qWarning() << "create temporary file failed."; return std::nullopt; @@ -330,12 +330,7 @@ std::optional DesktopFile::searchDesktopFileByPath(const QString &d auto filePtr = std::make_unique(std::move(path)); - auto [ctime, mtime, _] = getFileTimeInfo(*filePtr); - - if (mtime == 0) { - err = DesktopErrorCode::OpenFailed; - return std::nullopt; - } + auto [ctime, mtime, _] = getFileTimeInfo(QFileInfo{*filePtr}); err = DesktopErrorCode::NoError; return DesktopFile{std::move(filePtr), std::move(id), mtime, ctime}; diff --git a/src/global.h b/src/global.h index 0dedb0b..4ef49af 100644 --- a/src/global.h +++ b/src/global.h @@ -88,7 +88,7 @@ void applyIteratively(QList dirs, T &&func) QList dirList{std::move(dirs)}; while (!dirList.isEmpty()) { - const auto dir = dirList.takeFirst(); + const auto &dir = dirList.takeFirst(); if (!dir.exists()) { qWarning() << "apply function to an non-existent directory:" << dir.absolutePath() << ", skip."; @@ -467,20 +467,12 @@ ObjectMap dumpDBusObject(const QMap> &map) return objs; } -inline std::tuple getFileTimeInfo(QFile &file) +inline std::tuple getFileTimeInfo(const QFileInfo &file) { struct stat buf; - QFileInfo info{file}; - if (!file.isOpen()) { - if (auto ret = file.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text); !ret) { - qWarning() << "open file" << info.absoluteFilePath() << "failed."; - return std::make_tuple(0, 0, 0); - } - } - - if (auto ret = stat(info.absoluteFilePath().toLocal8Bit().data(), &buf); ret == -1) { - qWarning() << "get file" << info.absoluteFilePath() << "state failed:" << std::strerror(errno); + if (auto ret = stat(file.absoluteFilePath().toLocal8Bit().constData(), &buf); ret == -1) { + qWarning() << "get file" << file.absoluteFilePath() << "state failed:" << std::strerror(errno); return std::make_tuple(0, 0, 0); }