Commit aba93a5d authored by aligungr's avatar aligungr

IO utils improvement

parent 155cddc1
......@@ -72,4 +72,5 @@ add_executable(nr-cli src/cli.cpp)
target_link_libraries(nr-cli pthread)
target_compile_options(nr-cli PRIVATE -Wall -Wextra -pedantic)
target_link_libraries(nr-cli utils)
\ No newline at end of file
target_link_libraries(nr-cli app)
target_link_libraries(nr-cli utils)
......@@ -7,6 +7,7 @@
//
#include "common.hpp"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <random>
......@@ -295,3 +296,8 @@ void utils::AssertNodeName(const std::string &str)
"' contains illegal character: " + std::string(1, c));
}
}
bool utils::IsNumeric(const std::string &str)
{
return !str.empty() && std::all_of(str.begin(), str.end(), [](char c) { return (c >= '0' && c <= '9'); });
}
......@@ -32,6 +32,7 @@ int ParseInt(const char *str);
uint64_t Random64();
void Sleep(int ms);
bool IsRoot();
bool IsNumeric(const std::string& str);
void AssertNodeName(const std::string &str);
template <typename T>
......
......@@ -28,5 +28,6 @@ struct cons
// Others
static constexpr const char *CMD_SERVER_IP = "127.0.0.1";
static constexpr const char* PROC_TABLE_DIR = "/tmp/UERANSIM.proc-table/";
static constexpr const char *PROC_TABLE_DIR = "/tmp/UERANSIM.proc-table/";
static constexpr const char *PROCESS_DIR = "/proc/";
};
......@@ -26,13 +26,13 @@ std::string ReadAllText(const std::string &file)
void CreateDirectory(const std::string &path)
{
if (!std::filesystem::exists(path))
if (!Exists(path))
{
if (!std::filesystem::is_directory(path))
throw std::runtime_error("Required path '" + path + "' exists but not a directory.");
std::filesystem::create_directory(path);
std::filesystem::permissions(path, std::filesystem::perms::all);
RelaxPermissions(path);
}
}
......@@ -41,4 +41,26 @@ bool Exists(const std::string &path)
return std::filesystem::exists(path);
}
void WriteAllText(const std::string &path, const std::string &content)
{
std::ofstream ofs{};
ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
ofs.open(path);
ofs << content;
ofs.close();
RelaxPermissions(path);
}
void RelaxPermissions(const std::string &path)
{
std::filesystem::permissions(path, std::filesystem::perms::all);
}
bool Remove(const std::string &path)
{
std::error_code err{};
std::filesystem::remove_all(path, err);
return (bool)err;
}
} // namespace io
......@@ -13,8 +13,14 @@ namespace io
void CreateDirectory(const std::string &path);
bool Exists(const std::string &path);
std::string ReadAllText(const std::string &file);
bool Exists(const std::string &path);
void WriteAllText(const std::string &path, const std::string &content);
void RelaxPermissions(const std::string &path);
bool Remove(const std::string &path);
} // namespace io
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment