refact: use QFileInfo to get File's timeInfo

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2023-09-11 13:01:20 +08:00 committed by Comix
parent dc96c21c7d
commit 3f38233306
4 changed files with 21 additions and 25 deletions

View File

@ -328,9 +328,9 @@ void ApplicationManager1Service::updateApplication(const QSharedPointer<Applicat
return; return;
} }
auto mtime = getFileTimeInfo(QFileInfo{desktopFile.sourceFileRef()}); auto timeInfo = getFileTimeInfo(QFileInfo{desktopFile.sourceFileRef()});
if (destApp->desktopFileSource().modified(std::get<1>(mtime))) { if (destApp->desktopFileSource().modified(timeInfo.mtime)) {
auto *newEntry = new (std::nothrow) DesktopEntry{}; auto *newEntry = new (std::nothrow) DesktopEntry{};
if (newEntry == nullptr) { if (newEntry == nullptr) {
qCritical() << "new DesktopEntry failed."; qCritical() << "new DesktopEntry failed.";

View File

@ -238,9 +238,9 @@ bool DesktopEntry::checkMainEntryValidation() const noexcept
if (type == it->end()) { if (type == it->end()) {
qWarning() << "No Type."; qWarning() << "No Type.";
for (auto tmp = it->constKeyValueBegin(); tmp != it->constKeyValueEnd(); ++tmp) { for (auto tmp = it->constKeyValueBegin(); tmp != it->constKeyValueEnd(); ++tmp) {
const auto& [k,v] = *tmp; const auto &[k, v] = *tmp;
qInfo() << "keyName:" << k; qInfo() << "keyName:" << k;
for (auto valIt = v.constKeyValueBegin() ; valIt != v.constKeyValueEnd(); ++valIt) { for (auto valIt = v.constKeyValueBegin(); valIt != v.constKeyValueEnd(); ++valIt) {
const auto &[key, value] = *valIt; const auto &[key, value] = *valIt;
qInfo() << key << value; qInfo() << key << value;
} }
@ -364,7 +364,7 @@ std::optional<DesktopFile> DesktopFile::searchDesktopFileById(const QString &app
return std::nullopt; return std::nullopt;
} }
bool DesktopFile::modified(std::size_t time) const noexcept bool DesktopFile::modified(qint64 time) const noexcept
{ {
return time != m_mtime; return time != m_mtime;
} }

View File

@ -47,8 +47,8 @@ struct DesktopFile
[[nodiscard]] QFile *sourceFile() const noexcept { return &sourceFileRef(); }; [[nodiscard]] QFile *sourceFile() const noexcept { return &sourceFileRef(); };
[[nodiscard]] QFile &sourceFileRef() const noexcept { return *m_fileSource; }; [[nodiscard]] QFile &sourceFileRef() const noexcept { return *m_fileSource; };
[[nodiscard]] const QString &desktopId() const noexcept { return m_desktopId; } [[nodiscard]] const QString &desktopId() const noexcept { return m_desktopId; }
[[nodiscard]] bool modified(std::size_t time) const noexcept; [[nodiscard]] bool modified(qint64 time) const noexcept;
[[nodiscard]] std::size_t createTime() const noexcept { return m_ctime; } [[nodiscard]] qint64 createTime() const noexcept { return m_ctime; }
static std::optional<DesktopFile> searchDesktopFileById(const QString &appId, DesktopErrorCode &err) noexcept; static std::optional<DesktopFile> searchDesktopFileById(const QString &appId, DesktopErrorCode &err) noexcept;
static std::optional<DesktopFile> searchDesktopFileByPath(const QString &desktopFilePath, DesktopErrorCode &err) noexcept; static std::optional<DesktopFile> searchDesktopFileByPath(const QString &desktopFilePath, DesktopErrorCode &err) noexcept;
@ -56,7 +56,7 @@ struct DesktopFile
static std::optional<DesktopFile> createTemporaryDesktopFile(std::unique_ptr<QFile> temporaryFile) noexcept; static std::optional<DesktopFile> createTemporaryDesktopFile(std::unique_ptr<QFile> temporaryFile) noexcept;
private: private:
DesktopFile(std::unique_ptr<QFile> source, QString fileId, std::size_t mtime, std::size_t ctime) DesktopFile(std::unique_ptr<QFile> source, QString fileId, qint64 mtime, qint64 ctime)
: m_mtime(mtime) : m_mtime(mtime)
, m_ctime(ctime) , m_ctime(ctime)
, m_fileSource(std::move(source)) , m_fileSource(std::move(source))
@ -64,8 +64,8 @@ private:
{ {
} }
std::size_t m_mtime; qint64 m_mtime;
std::size_t m_ctime; qint64 m_ctime;
std::unique_ptr<QFile> m_fileSource{nullptr}; std::unique_ptr<QFile> m_fileSource{nullptr};
QString m_desktopId{""}; QString m_desktopId{""};
}; };

View File

@ -468,23 +468,19 @@ ObjectMap dumpDBusObject(const QMap<QDBusObjectPath, QSharedPointer<T>> &map)
return objs; return objs;
} }
inline std::tuple<std::size_t, std::size_t, std::size_t> getFileTimeInfo(const QFileInfo &file) struct FileTimeInfo
{ {
struct stat buf; qint64 mtime;
qint64 ctime;
qint64 atime;
};
// TODO: use QFileInfo get timeInfo, and return a custom time structure. inline FileTimeInfo getFileTimeInfo(const QFileInfo &file)
if (auto ret = stat(file.absoluteFilePath().toLocal8Bit().constData(), &buf); ret == -1) { {
qWarning() << "get file" << file.absoluteFilePath() << "state failed:" << std::strerror(errno); auto mtime = file.lastModified().toMSecsSinceEpoch();
return std::make_tuple(0, 0, 0); auto atime = file.lastRead().toMSecsSinceEpoch();
} auto ctime = file.birthTime().toMSecsSinceEpoch();
return {mtime, ctime, atime};
constexpr std::size_t secToNano = 1e9;
auto mtime = buf.st_mtim.tv_sec * secToNano + buf.st_mtim.tv_nsec;
auto ctime = buf.st_ctim.tv_sec * secToNano + buf.st_ctim.tv_nsec;
auto atime = buf.st_atim.tv_sec * secToNano + buf.st_atim.tv_nsec;
return std::make_tuple(ctime, mtime, atime);
} }
#endif #endif