Get rid of IterDir

It's a gross implementation, and even if it wasn't, using it takes more
code than not using it.
pull/2228/head
Jason Rhinelander 5 months ago
parent f41bcd00c6
commit 7417c59286
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262

@ -12,6 +12,7 @@
#include <llarp/router_contact.hpp>
#include <llarp/service/name.hpp>
#include <llarp/util/file.hpp>
#include <llarp/util/fs.hpp>
#include <llarp/util/logging.hpp>
#include <llarp/util/str.hpp>
@ -1377,22 +1378,19 @@ namespace llarp
const auto overridesDir = GetOverridesDir(data_dir);
if (fs::exists(overridesDir))
{
util::IterDir(overridesDir, [&](const fs::path& overrideFile) {
if (overrideFile.extension() == ".ini")
{
ConfigParser parser;
if (not parser.load_file(overrideFile))
throw std::runtime_error{"cannot load '" + overrideFile.u8string() + "'"};
parser.iter_all_sections([&](std::string_view section, const SectionValues& values) {
for (const auto& pair : values)
{
conf.add_config_value(section, pair.first, pair.second);
}
});
}
return true;
});
for (const auto& f : fs::directory_iterator{overridesDir})
{
if (not f.is_regular_file() or f.path().extension() != ".ini")
continue;
ConfigParser parser;
if (not parser.load_file(f.path()))
throw std::runtime_error{"cannot load '" + f.path().u8string() + "'"};
parser.iter_all_sections([&](std::string_view section, const SectionValues& values) {
for (const auto& [k, v] : values)
conf.add_config_value(section, k, v);
});
}
}
}

@ -248,7 +248,6 @@ namespace llarp
std::optional<int64_t> id) -> std::shared_ptr<oxen::quic::Stream> {
if (id && *id == 0)
{
log::critical(logcat, "Stream constructor constructing BTStream (ID:{})", id);
auto s = e.make_shared<oxen::quic::BTRequestStream>(
c, e, [](oxen::quic::Stream& s, uint64_t error_code) {
log::warning(
@ -261,8 +260,6 @@ namespace llarp
return s;
}
log::critical(logcat, "Stream constructor constructing Stream (ID:{})!", id);
return e.make_shared<oxen::quic::Stream>(c, e);
} */);
}

@ -10,7 +10,6 @@
#include <unordered_map>
#include <utility>
static const char skiplist_subdirs[] = "0123456789abcdef";
static const std::string RC_FILE_EXT = ".signed";
namespace llarp
@ -31,19 +30,6 @@ namespace llarp
if (not fs::is_directory(nodedbDir))
throw std::runtime_error{fmt::format("nodedb {} is not a directory", nodedbDir)};
for (const char& ch : skiplist_subdirs)
{
// this seems to be a problem on all targets
// perhaps cpp17::fs is just as screwed-up
// attempting to create a folder with no name
// what does this mean...?
if (!ch)
continue;
fs::path sub = nodedbDir / std::string(&ch, 1);
fs::create_directory(sub);
}
}
NodeDB::NodeDB(fs::path root, std::function<void(std::function<void()>)> diskCaller, Router* r)
@ -169,17 +155,9 @@ namespace llarp
}
fs::path
NodeDB::get_path_by_pubkey(RouterID pubkey) const
NodeDB::get_path_by_pubkey(const RouterID& pubkey) const
{
std::string hexString = oxenc::to_hex(pubkey.begin(), pubkey.end());
std::string skiplistDir;
const llarp::RouterID r{pubkey};
std::string fname = r.ToString();
skiplistDir += hexString[0];
fname += RC_FILE_EXT;
return _root / skiplistDir / fname;
return _root / (pubkey.ToString() + RC_FILE_EXT);
}
bool
@ -875,48 +853,26 @@ namespace llarp
if (_root.empty())
return;
std::set<fs::path> purge;
std::vector<fs::path> purge;
const auto now = time_now_ms();
for (const char& ch : skiplist_subdirs)
for (const auto& f : fs::directory_iterator{_root})
{
if (!ch)
if (not f.is_regular_file() or f.path().extension() != RC_FILE_EXT)
continue;
std::string p;
p += ch;
fs::path sub = _root / p;
llarp::util::IterDir(sub, [&](const fs::path& f) -> bool {
// skip files that are not suffixed with .signed
if (not(fs::is_regular_file(f) and f.extension() == RC_FILE_EXT))
return true;
RemoteRC rc{};
if (not rc.read(f))
{
// try loading it, purge it if it is junk
purge.emplace(f);
return true;
}
if (rc.is_expired(now))
{
// rc expired dont load it and purge it later
purge.emplace(f);
return true;
}
RemoteRC rc{};
const auto& rid = rc.router_id();
if (not rc.read(f) or rc.is_expired(now))
// try loading it, purge it if it is junk or expired
purge.push_back(f);
auto [itr, b] = known_rcs.insert(std::move(rc));
rc_lookup.emplace(rid, *itr);
known_rids.insert(rid);
const auto& rid = rc.router_id();
return true;
});
auto [itr, b] = known_rcs.insert(std::move(rc));
rc_lookup.emplace(rid, *itr);
known_rids.insert(rid);
}
if (not purge.empty())

@ -187,7 +187,7 @@ namespace llarp
/// get filename of an RC file given its public ident key
fs::path
get_path_by_pubkey(RouterID pk) const;
get_path_by_pubkey(const RouterID& pk) const;
public:
explicit NodeDB(

@ -25,7 +25,7 @@ namespace llarp::util
template <
typename Char,
std::enable_if_t<sizeof(Char) == 1 and not std::is_same_v<Char, char>, int> = 1>
inline size_t
size_t
file_to_buffer(const fs::path& filename, Char* buffer, size_t buffer_size)
{
return file_to_buffer(filename, reinterpret_cast<char*>(buffer), buffer_size);
@ -38,7 +38,7 @@ namespace llarp::util
/// Same as above, but works via char-like buffer
template <typename Char, std::enable_if_t<sizeof(Char) == 1, int> = 0>
inline void
void
buffer_to_file(const fs::path& filename, const Char* buffer, size_t buffer_size)
{
return buffer_to_file(
@ -73,31 +73,4 @@ namespace llarp::util
return std::make_optional<T>(pathname, mode);
}
template <typename PathVisitor>
static void
IterDir(const fs::path& path, PathVisitor visit)
{
DIR* d = opendir(path.string().c_str());
if (d == nullptr)
return;
struct dirent* ent = nullptr;
std::set<fs::path> entries;
do
{
ent = readdir(d);
if (not ent)
break;
if (ent->d_name[0] == '.')
continue;
entries.emplace(path / fs::path{ent->d_name});
} while (ent);
closedir(d);
for (const auto& p : entries)
{
if (not visit(p))
return;
}
}
} // namespace llarp::util

Loading…
Cancel
Save