From cd7247c49b84ebdb3fce8570e976840678e341c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Sat, 9 Nov 2024 17:42:44 -0500 Subject: [PATCH] std::filesystem: always use non-throw version when possible Change-Id: Ibc5410929a4f7b5e56433d883763bed5400dd2ea --- src/fileutils.cpp | 20 ++++++++++++++------ src/jamidht/jamiaccount.cpp | 4 +++- src/media/media_encoder.cpp | 3 ++- src/plugin/jamipluginmanager.cpp | 11 +++++++---- src/plugin/pluginsutils.cpp | 3 ++- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/fileutils.cpp b/src/fileutils.cpp index 821e97d8d..4efe51b9b 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -322,9 +322,13 @@ std::vector<uint8_t> loadCacheFile(const std::filesystem::path& path, std::chrono::system_clock::duration maxAge) { // last_write_time throws exception if file doesn't exist - auto writeTime = std::filesystem::last_write_time(path); - if (decltype(writeTime)::clock::now() - writeTime > maxAge) - throw std::runtime_error("file too old"); + std::error_code ec; + auto writeTime = std::filesystem::last_write_time(path, ec); + if (ec) + throw std::runtime_error("unable to get last write time of file"); + auto now = decltype(writeTime)::clock::now(); + if (now - writeTime > maxAge) + throw std::runtime_error("file too old " + dht::print_time_relative(now, writeTime)); JAMI_LOG("Loading cache file '{}'", path); return dhtnet::fileutils::loadFile(path); @@ -334,9 +338,13 @@ std::string loadCacheTextFile(const std::filesystem::path& path, std::chrono::system_clock::duration maxAge) { // last_write_time throws exception if file doesn't exist - auto writeTime = std::filesystem::last_write_time(path); - if (decltype(writeTime)::clock::now() - writeTime > maxAge) - throw std::runtime_error("file too old"); + std::error_code ec; + auto writeTime = std::filesystem::last_write_time(path, ec); + if (ec) + throw std::runtime_error("unable to get last write time of file"); + auto now = decltype(writeTime)::clock::now(); + if (now - writeTime > maxAge) + throw std::runtime_error("file too old " + dht::print_time_relative(now, writeTime)); JAMI_LOG("Loading cache file '{}'", path); return loadTextFile(path); diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index aee2c12b5..bb2c6f052 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -4239,7 +4239,9 @@ JamiAccount::sendFile(const std::string& conversationId, "Unable to create symlink for file transfer {} - {}. Copy file", filelinkPath, path); - if (!std::filesystem::copy_file(path, filelinkPath)) { + std::error_code ec; + auto success = std::filesystem::copy_file(path, filelinkPath, ec); + if (ec || !success) { JAMI_ERROR("Unable to copy file for file transfer {} - {}", filelinkPath, path); diff --git a/src/media/media_encoder.cpp b/src/media/media_encoder.cpp index a3e654831..9e834c7d2 100644 --- a/src/media/media_encoder.cpp +++ b/src/media/media_encoder.cpp @@ -1140,7 +1140,8 @@ MediaEncoder::readConfig(AVCodecContext* encoderCtx) { auto path = fileutils::get_config_dir() / "encoder.json"; std::string name = encoderCtx->codec->name; - if (std::filesystem::is_regular_file(path)) { + std::error_code ec; + if (std::filesystem::is_regular_file(path, ec)) { JAMI_WARN("encoder.json file found, default settings will be erased"); try { Json::Value root; diff --git a/src/plugin/jamipluginmanager.cpp b/src/plugin/jamipluginmanager.cpp index 65a2b1f40..27bc57f40 100644 --- a/src/plugin/jamipluginmanager.cpp +++ b/src/plugin/jamipluginmanager.cpp @@ -127,10 +127,11 @@ bool JamiPluginManager::checkPluginCertificatePublicKey(const std::string& oldJplPath, const std::string& newJplPath) { std::map<std::string, std::string> oldDetails = PluginUtils::parseManifestFile(PluginUtils::manifestPath(oldJplPath), oldJplPath); + std::error_code ec; if ( oldDetails.empty() || - !std::filesystem::is_regular_file(oldJplPath + DIR_SEPARATOR_CH + oldDetails["id"] + ".crt") || - !std::filesystem::is_regular_file(newJplPath) + !std::filesystem::is_regular_file(oldJplPath + DIR_SEPARATOR_CH + oldDetails["id"] + ".crt", ec) || + !std::filesystem::is_regular_file(newJplPath, ec) ) return false; try { @@ -170,7 +171,8 @@ bool JamiPluginManager::checkPluginSignatureFile(const std::string& jplPath) { // check if the file exists - if (!std::filesystem::is_regular_file(jplPath)){ + std::error_code ec; + if (!std::filesystem::is_regular_file(jplPath, ec)){ return false; } try { @@ -255,7 +257,8 @@ int JamiPluginManager::installPlugin(const std::string& jplPath, bool force) { int r {SUCCESS}; - if (std::filesystem::is_regular_file(jplPath)) { + std::error_code ec; + if (std::filesystem::is_regular_file(jplPath, ec)) { try { auto manifestMap = PluginUtils::readPluginManifestFromArchive(jplPath); const std::string& name = manifestMap["id"]; diff --git a/src/plugin/pluginsutils.cpp b/src/plugin/pluginsutils.cpp index 10d98961d..0d66a5eee 100644 --- a/src/plugin/pluginsutils.cpp +++ b/src/plugin/pluginsutils.cpp @@ -351,7 +351,8 @@ getLocales(const std::string& rootPath, const std::string& lang) std::map<std::string, std::string> processLocaleFile(const std::string& preferenceLocaleFilePath) { - if (!std::filesystem::is_regular_file(preferenceLocaleFilePath)) { + std::error_code ec; + if (!std::filesystem::is_regular_file(preferenceLocaleFilePath, ec)) { return {}; } std::ifstream file(preferenceLocaleFilePath); -- GitLab