From 0eee25973810d3392bdf092f51fe1db1fd8e94cc Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 8 Nov 2026 08:35:03 -0500 Subject: [PATCH] always use std::filesystem::path for file path and in ReadDir --- Makefile.mingw | 1 - libi2pd/FS.cpp | 73 +++++++++++++------------------- libi2pd/FS.h | 12 +----- libi2pd/Family.cpp | 9 ++-- libi2pd/Garlic.cpp | 6 +-- libi2pd/Reseed.cpp | 9 ++-- libi2pd_client/ClientContext.cpp | 6 +-- 7 files changed, 47 insertions(+), 69 deletions(-) diff --git a/Makefile.mingw b/Makefile.mingw index dadbd0c6..353a73ae 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -27,7 +27,6 @@ ifeq ($(USE_WINXP_FLAGS), yes) endif LDLIBS += \ - $(MINGW_PREFIX)/lib/libboost_filesystem-mt.a \ $(MINGW_PREFIX)/lib/libboost_program_options-mt.a \ $(MINGW_PREFIX)/lib/libboost_json-mt.a \ $(MINGW_PREFIX)/lib/libboost_url-mt.a \ diff --git a/libi2pd/FS.cpp b/libi2pd/FS.cpp index cffee648..53375a9f 100644 --- a/libi2pd/FS.cpp +++ b/libi2pd/FS.cpp @@ -10,9 +10,6 @@ #include #if defined(MAC_OSX) -#if !STD_FILESYSTEM -#include -#endif #include #endif @@ -31,16 +28,10 @@ #include "Log.h" #include "Garlic.h" -#if STD_FILESYSTEM -#include -namespace fs_lib = std::filesystem; -#else -#include -namespace fs_lib = boost::filesystem; -#endif - -namespace i2p { -namespace fs { +namespace i2p +{ +namespace fs +{ std::string appName = "i2pd"; std::string dataDir = ""; std::string certsDir = ""; @@ -106,7 +97,7 @@ namespace fs { exit(1); } else - dataDir = (fs_lib::path(commonAppData)/appName).string (); + dataDir = (std::filesystem::path(commonAppData)/appName).string (); #else dataDir = "/var/lib/" + appName; #endif @@ -130,10 +121,10 @@ namespace fs { } else { - auto execPath = fs_lib::path(localAppData).parent_path(); + auto execPath = std::filesystem::path(localAppData).parent_path(); // if config file exists in .exe's folder use it - if(fs_lib::exists(execPath/"i2pd.conf")) // TODO: magic string + if(std::filesystem::exists(execPath/"i2pd.conf")) // TODO: magic string dataDir = execPath.string (); else // otherwise %appdata% { @@ -147,7 +138,7 @@ namespace fs { exit(1); } else - dataDir = (fs_lib::path(localAppData)/appName).string(); + dataDir = (std::filesystem::path(localAppData)/appName).string(); } } return; @@ -167,7 +158,7 @@ namespace fs { #if defined(ANDROID) const char * ext = getenv("EXTERNAL_STORAGE"); if (!ext) ext = "/sdcard"; - if (fs_lib::exists(ext)) + if (std::filesystem::exists(ext)) { dataDir = std::string (ext) + "/" + appName; return; @@ -200,46 +191,47 @@ namespace fs { } bool Init() { - if (!fs_lib::exists(dataDir)) - fs_lib::create_directory(dataDir); + if (!std::filesystem::exists(dataDir)) + std::filesystem::create_directory(dataDir); std::string destinations = DataDirPath("destinations"); - if (!fs_lib::exists(destinations)) - fs_lib::create_directory(destinations); + if (!std::filesystem::exists(destinations)) + std::filesystem::create_directory(destinations); std::string tags = DataDirPath("tags"); - if (!fs_lib::exists(tags)) - fs_lib::create_directory(tags); + if (!std::filesystem::exists(tags)) + std::filesystem::create_directory(tags); else i2p::garlic::CleanUpTagsFiles (); return true; } - bool ReadDir(const std::string & path, std::vector & files) { - if (!fs_lib::exists(path)) + bool ReadDir(const std::filesystem::path& path, std::vector& files) + { + if (!std::filesystem::exists(path)) return false; - fs_lib::directory_iterator it(path); - fs_lib::directory_iterator end; + std::filesystem::directory_iterator it(path); + std::filesystem::directory_iterator end; for ( ; it != end; it++) { - if (!fs_lib::is_regular_file(it->status())) + if (!std::filesystem::is_regular_file(it->status())) continue; - files.push_back(it->path().string()); + files.push_back(it->path()); } return true; } bool Exists(const std::string & path) { - return fs_lib::exists(path); + return std::filesystem::exists(path); } uint32_t GetLastUpdateTime (const std::string & path) { - if (!fs_lib::exists(path)) + if (!std::filesystem::exists(path)) return 0; -#if STD_FILESYSTEM + std::error_code ec; auto t = std::filesystem::last_write_time (path, ec); if (ec) return 0; @@ -250,25 +242,20 @@ namespace fs { t - decltype(t)::clock::now() + std::chrono::system_clock::now()); #endif return std::chrono::system_clock::to_time_t(sctp); -#else - boost::system::error_code ec; - auto t = boost::filesystem::last_write_time (path, ec); - return ec ? 0 : t; -#endif } bool Remove(const std::string & path) { - if (!fs_lib::exists(path)) + if (!std::filesystem::exists(path)) return false; - return fs_lib::remove(path); + return std::filesystem::remove(path); } bool Rename (std::string_view from, std::string_view to) { try { - fs_lib::rename (from, to); + std::filesystem::rename (from, to); } catch (std::exception& ex) { @@ -280,9 +267,9 @@ namespace fs { bool CreateDirectory (const std::string& path) { - if (fs_lib::exists(path) && fs_lib::is_directory (fs_lib::status (path))) + if (std::filesystem::exists(path) && std::filesystem::is_directory (std::filesystem::status (path))) return true; - return fs_lib::create_directory(path); + return std::filesystem::create_directory(path); } std::filesystem::path CreatePrivateFile (const std::filesystem::path& path) diff --git a/libi2pd/FS.h b/libi2pd/FS.h index f8044093..12491300 100644 --- a/libi2pd/FS.h +++ b/libi2pd/FS.h @@ -18,16 +18,6 @@ #include #include -#ifndef STD_FILESYSTEM -# if (_WIN32 && __GNUG__) // MinGW GCC somehow incorrectly converts paths -# define STD_FILESYSTEM 0 -# elif (!TARGET_OS_SIMULATOR && __has_include()) // supports std::filesystem -# define STD_FILESYSTEM 1 -# else -# define STD_FILESYSTEM 0 -# endif -#endif - namespace i2p { namespace fs @@ -134,7 +124,7 @@ namespace fs * @param files Vector to store found files * @return true on success and false if directory not exists */ - bool ReadDir(const std::string & path, std::vector & files); + bool ReadDir(const std::filesystem::path& path, std::vector & files); /** * @brief Remove file with given path diff --git a/libi2pd/Family.cpp b/libi2pd/Family.cpp index df0025ee..66b0f0cd 100644 --- a/libi2pd/Family.cpp +++ b/libi2pd/Family.cpp @@ -83,22 +83,23 @@ namespace data { std::string certDir = i2p::fs::GetCertsDir() + i2p::fs::dirSep + "family"; - std::vector files; + std::vector files; int numCertificates = 0; - if (!i2p::fs::ReadDir(certDir, files)) { + if (!i2p::fs::ReadDir(certDir, files)) + { LogPrint(eLogWarning, "Family: Can't load family certificates from ", certDir); return; } for (const auto& file: files) { - if (!file.ends_with (".crt")) + if (file.extension () != ".crt") { LogPrint(eLogWarning, "Family: ignoring file ", file); continue; } - LoadCertificate (file); + LoadCertificate (file.string ()); numCertificates++; } LogPrint (eLogInfo, "Family: ", numCertificates, " certificates loaded"); diff --git a/libi2pd/Garlic.cpp b/libi2pd/Garlic.cpp index 36853219..173b9e23 100644 --- a/libi2pd/Garlic.cpp +++ b/libi2pd/Garlic.cpp @@ -1014,12 +1014,12 @@ namespace garlic void CleanUpTagsFiles () { - std::vector files; + std::vector files; i2p::fs::ReadDir (i2p::fs::DataDirPath("tags"), files); uint32_t ts = i2p::util::GetSecondsSinceEpoch (); for (auto it: files) - if (ts >= i2p::fs::GetLastUpdateTime (it) + INCOMING_TAGS_EXPIRATION_TIMEOUT) - i2p::fs::Remove (it); + if (ts >= i2p::fs::GetLastUpdateTime (it.string ()) + INCOMING_TAGS_EXPIRATION_TIMEOUT) + i2p::fs::Remove (it.string ()); } void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len, diff --git a/libi2pd/Reseed.cpp b/libi2pd/Reseed.cpp index db240c2f..f056c42d 100644 --- a/libi2pd/Reseed.cpp +++ b/libi2pd/Reseed.cpp @@ -591,22 +591,23 @@ namespace data { std::string certDir = i2p::fs::GetCertsDir() + i2p::fs::dirSep + "reseed"; - std::vector files; + std::vector files; int numCertificates = 0; - if (!i2p::fs::ReadDir(certDir, files)) { + if (!i2p::fs::ReadDir(certDir, files)) + { LogPrint(eLogWarning, "Reseed: Can't load reseed certificates from ", certDir); return; } for (const auto& file : files) { - if (!file.ends_with (".crt")) + if (file.extension () != ".crt") { LogPrint(eLogWarning, "Reseed: Ignoring file ", file); continue; } - LoadCertificate (file); + LoadCertificate (file.string ()); numCertificates++; } LogPrint (eLogInfo, "Reseed: ", numCertificates, " certificates loaded"); diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index 4c546c13..e02db87e 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -607,14 +607,14 @@ namespace client if (i2p::fs::Exists (tunDir)) { - std::vector files; + std::vector files; if (i2p::fs::ReadDir (tunDir, files)) { for (auto& it: files) { - if (!it.ends_with (".conf")) continue; + if (it.extension () != ".conf") continue; LogPrint(eLogDebug, "Clients: Tunnels extra config file: ", it); - ReadTunnels (it, numClientTunnels, numServerTunnels); + ReadTunnels (it.string (), numClientTunnels, numServerTunnels); } } }