refactor: Replace os::bts_data_path w/ SystemConfig::data_path

This commit is contained in:
John Colvin committed 2023-03-20 13:26:44 -04:00
1 parent b7fae8a6f5
commit 17402ce94a
16 files changed
+75 -74

No files matched your search

+24 -18
View File
@@ -1,5 +1,8 @@
#include <boost/filesystem/directory.hpp>
#ifndef _WIN32
#include "Puid.h"
#include "lib/config/config.h"
#include "lib/config/protos/system_config.pb.h"
#include "../os/os.h"
#include "../utils/bts_assert.h"
@@ -23,6 +26,10 @@
namespace bts {
namespace utils {
using bts::config::SystemConfig;
using DirectoryIterator = boost::filesystem::directory_iterator;
using Path = boost::filesystem::path;
// use a single global lock for simplicity. we can do a per domain
// lock if we ever need it. we need a lock because posix locks files
// per process not per thread.
@@ -76,15 +83,13 @@ void init_store(
}
}
std::string find_file(std::string const &path, std::string const &ending) {
for (boost::filesystem::directory_iterator p =
boost::filesystem::directory_iterator(path);
p != boost::filesystem::directory_iterator(); p++) {
std::string fn(p->path().string());
if (boost::algorithm::ends_with(fn, ending))
return fn;
Path find_file(std::string const &path, std::string const &ending) {
for (DirectoryIterator p(path); p != DirectoryIterator(); p++) {
if (boost::algorithm::ends_with(p->path().string(), ending)) {
return p->path();
}
}
return std::string();
return {};
}
// boost::filesystem::copy_file is broken for C++ 11 unless one compiles boost
@@ -154,20 +159,21 @@ uint64_t reserve_ids(std::string const &scope, int n_ids) {
// we risk race conditions if the directory is not there.
// but that's OK, this is rare and easily manageable.
std::string path = os::bts_data_path() + "/puids/" + scope;
std::string lock_path = path + "/" + lock_file_name;
std::string counter_path = path + "/" + counter_file_name;
const auto path =
Path{config::get<config::SystemConfig>().data_path()} / "puids" / scope;
auto lock_path = path / lock_file_name;
auto counter_path = path / counter_file_name;
init_store(path, lock_path, counter_path);
init_store(path.string(), lock_path.string(), counter_path.string());
// W1
boost::interprocess::file_lock f_lock(lock_path.c_str());
boost::interprocess::scoped_lock<boost::interprocess::file_lock> scope_lock(
f_lock);
std::string tmp_file = find_file(path, ".tmp");
std::string alt_file = find_file(path, ".alt");
std::string counter_file = find_file(path, counter_file_name);
auto tmp_file = find_file(path.string(), ".tmp");
auto alt_file = find_file(path.string(), ".alt");
const auto counter_file = find_file(path.string(), counter_file_name);
// r1?
if (counter_file.size() == 0 && tmp_file.size() && alt_file.size()) {
@@ -194,7 +200,7 @@ uint64_t reserve_ids(std::string const &scope, int n_ids) {
} else {
// W2, copy counter file to a temp file
tmp_file = path + "/" + Uuid().to_text_string() + ".tmp";
tmp_file = path / (Uuid().to_text_string() + ".tmp");
copy_file(counter_path, tmp_file);
// W3, W4 update temp file, close and flush
@@ -203,7 +209,7 @@ uint64_t reserve_ids(std::string const &scope, int n_ids) {
std::ofstream(tmp_file.c_str()) << counter_value + n_ids;
// W5, rename counter file to an alt file (renames are atomic)
alt_file = path + "/" + Uuid().to_text_string() + ".alt";
alt_file = path / (Uuid().to_text_string() + ".alt");
boost::filesystem::rename(counter_path, alt_file);
// W6, rename temp file to counter file
@@ -282,4 +288,4 @@ Puid gen_puid(std::string const &scope) {
} // namespace utils
} // namespace bts
#endif
#endif