From e20cf4c9608f99cc4ab61d324d259e11cea4dc00 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 7 Nov 2026 17:42:12 -0500 Subject: [PATCH] use std::filesystem::path in HashedStorage --- libi2pd/FS.cpp | 72 +++++++++++++++++----------------- libi2pd/FS.h | 44 ++++++++++----------- libi2pd/NetDb.cpp | 13 +++--- libi2pd/NetDb.hpp | 2 +- libi2pd/Profiling.cpp | 4 +- libi2pd_client/AddressBook.cpp | 24 +++++------- 6 files changed, 74 insertions(+), 85 deletions(-) diff --git a/libi2pd/FS.cpp b/libi2pd/FS.cpp index 43ec52c8..cffee648 100644 --- a/libi2pd/FS.cpp +++ b/libi2pd/FS.cpp @@ -296,36 +296,38 @@ namespace fs { return path; } - void HashedStorage::SetPlace(const std::string &path) { - root = path + i2p::fs::dirSep + name; + void HashedStorage::SetPlace (const std::filesystem::path& p) + { + root = p/name; } bool HashedStorage::Init(std::span chars) { try { - if (!fs_lib::exists(root)) - fs_lib::create_directories(root); + if (!std::filesystem::exists (root)) + std::filesystem::create_directories(root); for (auto ch: chars) { - auto p = root + i2p::fs::dirSep + prefix1 + ch; - if (fs_lib::exists(p)) + auto p = root/(prefix1 + ch); + if (std::filesystem::exists(p)) continue; #if TARGET_OS_SIMULATOR // ios simulator fs says it is case sensitive, but it is not - boost::system::error_code ec; - if (fs_lib::create_directory(p, ec)) + std::error_code ec; + if (std::filesystem::create_directory(p, ec)) continue; - switch (ec.value()) { - case boost::system::errc::file_exists: - case boost::system::errc::success: + switch (ec.value()) + { + case std::errc::file_exists: + case std::errc::success: continue; default: - throw boost::system::system_error( ec, __func__ ); + throw std::system_error( ec, __func__ ); } #else - if (fs_lib::create_directory(p)) + if (std::filesystem::create_directory(p)) continue; /* ^ throws exception on failure */ #endif return false; @@ -340,43 +342,43 @@ namespace fs { return true; } - std::string HashedStorage::Path(const std::string & ident) const { + std::filesystem::path HashedStorage::Path(const std::string& ident) const + { std::string safe_ident = ident; std::replace(safe_ident.begin(), safe_ident.end(), '/', '-'); std::replace(safe_ident.begin(), safe_ident.end(), '\\', '-'); - std::stringstream t(""); - t << this->root << i2p::fs::dirSep; - t << prefix1 << safe_ident[0] << i2p::fs::dirSep; - t << prefix2 << safe_ident << "." << suffix; - - return t.str(); + return root/(prefix1 + safe_ident[0])/(prefix2 + safe_ident + "." + suffix); } - void HashedStorage::Remove(const std::string & ident) { - std::string path = Path(ident); - if (!fs_lib::exists(path)) + void HashedStorage::Remove(const std::string& ident) + { + auto p = Path (ident); + if (!std::filesystem::exists(p)) return; - fs_lib::remove(path); + std::filesystem::remove(p); } - void HashedStorage::Traverse(std::vector & files) { - Iterate([&files] (const std::string & fname) { - files.push_back(fname); - }); + std::vector HashedStorage::Traverse () + { + std::vector files; + Iterate([&files] (const std::filesystem::path& p) + { + files.push_back (p); + }); + return files; } void HashedStorage::Iterate(FilenameVisitor v) { - fs_lib::path p(root); - fs_lib::recursive_directory_iterator it(p); - fs_lib::recursive_directory_iterator end; + std::filesystem::recursive_directory_iterator it (root); + std::filesystem::recursive_directory_iterator end; - for ( ; it != end; it++) { - if (!fs_lib::is_regular_file( it->status() )) + for ( ; it != end; it++) + { + if (!std::filesystem::is_regular_file (it->status())) continue; - const std::string & t = it->path().string(); - v(t); + v(it->path ()); } } } // fs diff --git a/libi2pd/FS.h b/libi2pd/FS.h index 5bacdd73..f8044093 100644 --- a/libi2pd/FS.h +++ b/libi2pd/FS.h @@ -50,36 +50,36 @@ namespace fs * std::vector files; * h.Traverse(files); <- finds all files in storage and saves in given vector */ - class HashedStorage + class HashedStorage final { - protected: - - std::string root; /**< path to storage with it's name included */ - std::string name; /**< name of the storage */ - std::string prefix1; /**< hashed directory prefix */ - std::string prefix2; /**< prefix of file in storage */ - std::string suffix; /**< suffix of file in storage (extension) */ - public: - typedef std::function FilenameVisitor; - HashedStorage(const char *n, const char *p1, const char *p2, const char *s): + using FilenameVisitor = std::function; + HashedStorage(const std::filesystem::path& n, std::string_view p1, std::string_view p2, std::string_view s): name(n), prefix1(p1), prefix2(p2), suffix(s) {}; /** create subdirs in storage */ bool Init(std::span chars); - const std::string & GetRoot() const { return root; } - const std::string & GetName() const { return name; } + const std::filesystem::path& GetRoot() const { return root; } + const std::filesystem::path& GetName() const { return name; } /** set directory where to place storage directory */ - void SetPlace(const std::string & path); + void SetPlace (const std::filesystem::path& p); /** path to file with given ident */ - std::string Path(const std::string & ident) const; + std::filesystem::path Path(const std::string& ident) const; /** remove file by ident */ - void Remove(const std::string & ident); - /** find all files in storage and store list in provided vector */ - void Traverse(std::vector & files); + void Remove(const std::string& ident); + /** find all files in storage and return */ + std::vector Traverse (); /** visit every file in this storage with a visitor */ void Iterate(FilenameVisitor v); + + private: + + std::filesystem::path root; /**< path to storage with it's name included */ + std::filesystem::path name; /**< name of the storage */ + std::string prefix1; /**< hashed directory prefix */ + std::string prefix2; /**< prefix of file in storage */ + std::string suffix; /**< suffix of file in storage (extension) */ }; /** @brief Returns current application name, default 'i2pd' */ @@ -186,13 +186,9 @@ namespace fs } template - std::string StorageRootPath (const Storage& storage, Filename... filenames) + std::filesystem::path StorageRootPath (const Storage& storage, Filename... filenames) { - std::stringstream s(""); - s << storage.GetRoot (); - _ExpandPath(s, filenames...); - - return s.str(); + return storage.GetRoot ()/(filenames/...); } } // fs diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 3fd7186d..93043557 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -531,9 +531,9 @@ namespace data i2p::transport::transports.SendMessages(ih, std::move (requests)); } - bool NetDb::LoadRouterInfo (const std::string& path, uint64_t ts) + bool NetDb::LoadRouterInfo (const std::filesystem::path& p, uint64_t ts) { - auto r = std::make_shared(path); + auto r = std::make_shared(p); if (r->GetRouterIdentity () && !r->IsUnreachable () && r->HasValidAddresses () && ts < r->GetTimestamp () + 24*60*60*NETDB_MAX_OFFLINE_EXPIRATION_TIMEOUT*1000LL && // too old (r->GetVersion () >= NETDB_MIN_ALLOWED_VERSION || r->IsHighBandwidth ())) // old version @@ -547,8 +547,8 @@ namespace data } else { - LogPrint(eLogWarning, "NetDb: RI from ", path, " is invalid or too old. Delete"); - i2p::fs::Remove(path); + LogPrint(eLogWarning, "NetDb: RI from ", p, " is invalid or too old. Delete"); + i2p::fs::Remove(p); } return true; } @@ -562,7 +562,7 @@ namespace data void NetDb::VisitStoredRouterInfos(RouterInfoVisitor v) { - m_Storage.Iterate([v] (const std::string & filename) + m_Storage.Iterate([v] (const std::filesystem::path& filename) { auto ri = std::make_shared(filename); v(ri); @@ -629,8 +629,7 @@ namespace data m_Floodfills.Clear (); uint64_t ts = i2p::util::GetMillisecondsSinceEpoch(); - std::vector files; - m_Storage.Traverse(files); + auto files = m_Storage.Traverse (); for (const auto& path : files) LoadRouterInfo (path, ts); diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 4c6b1576..5db16564 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -150,7 +150,7 @@ namespace data private: void Load (); - bool LoadRouterInfo (const std::string& path, uint64_t ts); + bool LoadRouterInfo (const std::filesystem::path& p, uint64_t ts); void SaveUpdated (); void PersistRouters (std::list > >&& update, std::list&& remove); diff --git a/libi2pd/Profiling.cpp b/libi2pd/Profiling.cpp index 0f83b2b2..54f8c0de 100644 --- a/libi2pd/Profiling.cpp +++ b/libi2pd/Profiling.cpp @@ -353,9 +353,7 @@ namespace data static void DeleteFilesFromDisk () { - std::vector files; - g_ProfilesStorage.Traverse(files); - + auto files = g_ProfilesStorage.Traverse (); struct stat st; std::time_t now = std::time(nullptr); for (const auto& path: files) diff --git a/libi2pd_client/AddressBook.cpp b/libi2pd_client/AddressBook.cpp index 7a9340de..c5424775 100644 --- a/libi2pd_client/AddressBook.cpp +++ b/libi2pd_client/AddressBook.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "Base.h" @@ -28,13 +29,6 @@ #include "BoostStream.h" #include "AddressBook.h" -#if STD_FILESYSTEM -#include -namespace fs_lib = std::filesystem; -#else -#include -namespace fs_lib = boost::filesystem; -#endif namespace i2p { @@ -67,12 +61,12 @@ namespace client private: - int LoadFromFile (const std::string& filename, Addresses& addresses); // returns -1 if can't open file, otherwise number of records + int LoadFromFile (const std::filesystem::path& filename, Addresses& addresses); // returns -1 if can't open file, otherwise number of records private: i2p::fs::HashedStorage storage; - std::string etagsPath, indexPath, localPath; + std::filesystem::path etagsPath, indexPath, localPath; bool m_IsPersist = false; std::string m_HostsFile; // file to dump hosts.txt, empty if not used std::unordered_map, uint64_t> > m_FullAddressCache; // ident hash -> (full ident buffer, last access timestamp) @@ -182,7 +176,7 @@ namespace client storage.Remove( ident.ToBase32() ); } - int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, Addresses& addresses) + int AddressBookFilesystemStorage::LoadFromFile (const std::filesystem::path& filename, Addresses& addresses) { int num = 0; std::ifstream f (filename, std::ifstream::in); // in text mode @@ -290,7 +284,7 @@ namespace client void AddressBookFilesystemStorage::SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) { - std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt"; + auto fname = etagsPath/(subscription.ToBase32 () + ".txt"); std::ofstream f (fname, std::ofstream::out | std::ofstream::trunc); if (f) { @@ -301,7 +295,7 @@ namespace client bool AddressBookFilesystemStorage::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) { - std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt"; + auto fname = etagsPath/(subscription.ToBase32 () + ".txt"); std::ifstream f (fname, std::ofstream::in); if (!f || f.eof ()) return false; std::getline (f, etag); @@ -313,11 +307,11 @@ namespace client void AddressBookFilesystemStorage::ResetEtags () { LogPrint (eLogError, "Addressbook: Resetting eTags"); - for (fs_lib::directory_iterator it (etagsPath); it != fs_lib::directory_iterator (); ++it) + for (std::filesystem::directory_iterator it (etagsPath); it != std::filesystem::directory_iterator (); ++it) { - if (!fs_lib::is_regular_file (it->status ())) + if (!std::filesystem::is_regular_file (it->status ())) continue; - fs_lib::remove (it->path ()); + std::filesystem::remove (it->path ()); } }