Skip to content

Commit

Permalink
Use std::filesystem::path
Browse files Browse the repository at this point in the history
  • Loading branch information
Keita Iwabuchi committed Dec 19, 2023
1 parent 7e312f1 commit 4669446
Show file tree
Hide file tree
Showing 29 changed files with 410 additions and 378 deletions.
2 changes: 1 addition & 1 deletion bench/bfs/run_bfs_bench_metall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) {
// metall::logger::set_log_level(metall::logger::level::verbose);

metall::manager manager(metall::open_read_only,
option.graph_file_name_list[0].c_str());
option.graph_file_name_list[0]);
auto adj_list =
manager.find<adjacency_list_type>(option.graph_key_name.c_str()).first;

Expand Down
2 changes: 1 addition & 1 deletion bench/bfs/run_bfs_bench_metall_multiple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) {
std::vector<metall::manager *> managers;
for (const auto &file_name : option.graph_file_name_list) {
managers.emplace_back(
new metall::manager(metall::open_read_only, file_name.c_str()));
new metall::manager(metall::open_read_only, file_name));
}

auto adj_list = adjacency_list_type(option.graph_key_name, managers.begin(),
Expand Down
2 changes: 1 addition & 1 deletion bench/simple_alloc/run_simple_allocation_bench_metall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
int main(int argc, char *argv[]) {
const auto option = simple_alloc_bench::parse_option(argc, argv);
{
metall::manager manager(metall::create_only, option.datastore_path.c_str());
metall::manager manager(metall::create_only, option.datastore_path);
simple_alloc_bench::run_bench(option, manager.get_allocator<std::byte>());
}
metall::manager::remove(option.datastore_path.c_str());
Expand Down
45 changes: 26 additions & 19 deletions include/metall/basic_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#define METALL_BASIC_MANAGER_HPP

#include <cstddef>
#include <string>
#include <memory>
#include <filesystem>

#include <metall/tags.hpp>
#include <metall/stl_allocator.hpp>
Expand Down Expand Up @@ -116,7 +118,7 @@ class basic_manager {

/// \brief Opens an existing data store.
/// \param base_path Path to a data store.
basic_manager(open_only_t, const char *base_path) noexcept {
basic_manager(open_only_t, const std::filesystem::path &base_path) noexcept {
try {
m_kernel = std::make_unique<manager_kernel_type>();
m_kernel->open(base_path);
Expand All @@ -130,7 +132,8 @@ class basic_manager {
/// \brief Opens an existing data store with the read only mode.
/// Write accesses will cause segmentation fault.
/// \param base_path Path to a data store.
basic_manager(open_read_only_t, const char *base_path) noexcept {
basic_manager(open_read_only_t,
const std::filesystem::path &base_path) noexcept {
try {
m_kernel = std::make_unique<manager_kernel_type>();
m_kernel->open_read_only(base_path);
Expand All @@ -143,7 +146,8 @@ class basic_manager {

/// \brief Creates a new data store (an existing data store will be
/// overwritten). \param base_path Path to create a data store.
basic_manager(create_only_t, const char *base_path) noexcept {
basic_manager(create_only_t,
const std::filesystem::path &base_path) noexcept {
try {
m_kernel = std::make_unique<manager_kernel_type>();
m_kernel->create(base_path);
Expand All @@ -157,7 +161,7 @@ class basic_manager {
/// \brief Creates a new data store (an existing data store will be
/// overwritten). \param base_path Path to create a data store. \param
/// capacity Maximum total allocation size.
basic_manager(create_only_t, const char *base_path,
basic_manager(create_only_t, const std::filesystem::path &base_path,
const size_type capacity) noexcept {
try {
m_kernel = std::make_unique<manager_kernel_type>();
Expand Down Expand Up @@ -940,7 +944,8 @@ class basic_manager {
/// if it is available. \param num_max_copy_threads The maximum number of copy
/// threads to use. If <= 0 is given, the value is automatically determined.
/// \return Returns true on success; other false.
bool snapshot(const char_type *destination_dir_path, const bool clone = true,
bool snapshot(const std::filesystem::path &destination_dir_path,
const bool clone = true,
const int num_max_copy_threads = 0) noexcept {
if (!check_sanity()) {
return false;
Expand Down Expand Up @@ -968,8 +973,8 @@ class basic_manager {
/// \param num_max_copy_threads The maximum number of copy threads to use.
/// If <= 0 is given, the value is automatically determined.
/// \return If succeeded, returns true; other false.
static bool copy(const char_type *source_dir_path,
const char_type *destination_dir_path,
static bool copy(const std::filesystem::path &source_dir_path,
const std::filesystem::path &destination_dir_path,
const bool clone = true,
const int num_max_copy_threads = 0) noexcept {
try {
Expand All @@ -995,8 +1000,8 @@ class basic_manager {
/// If <= 0 is given, the value is automatically determined.
/// \return Returns an object of std::future.
/// If succeeded, its get() returns true; other false.
static auto copy_async(const char_type *source_dir_path,
const char_type *destination_dir_path,
static auto copy_async(const std::filesystem::path &source_dir_path,
const std::filesystem::path &destination_dir_path,
const bool clone = true,
const int num_max_copy_threads = 0) noexcept {
try {
Expand All @@ -1015,7 +1020,7 @@ class basic_manager {
///
/// \param dir_path Path to a data store to remove. \return If
/// succeeded, returns true; other false.
static bool remove(const char_type *dir_path) noexcept {
static bool remove(const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::remove(dir_path);
} catch (...) {
Expand All @@ -1032,7 +1037,8 @@ class basic_manager {
/// \param dir_path Path to a data store to remove.
/// \return Returns an object of std::future.
/// If succeeded, its get() returns true; other false
static std::future<bool> remove_async(const char_type *dir_path) noexcept {
static std::future<bool> remove_async(
const std::filesystem::path &dir_path) noexcept {
try {
return std::async(std::launch::async, remove, dir_path);
} catch (...) {
Expand All @@ -1054,7 +1060,7 @@ class basic_manager {
/// \param dir_path Path to a data store.
/// \return Returns true if it exists and is consistent; otherwise, returns
/// false.
static bool consistent(const char_type *dir_path) noexcept {
static bool consistent(const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::consistent(dir_path);
} catch (...) {
Expand Down Expand Up @@ -1086,7 +1092,7 @@ class basic_manager {
///
/// \param dir_path Path to a data store.
/// \return UUID in the std::string format; returns an empty string on error.
static std::string get_uuid(const char_type *dir_path) noexcept {
static std::string get_uuid(const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::get_uuid(dir_path);
} catch (...) {
Expand Down Expand Up @@ -1118,7 +1124,7 @@ class basic_manager {
///
/// \param dir_path Path to a data store.
/// \return Returns a version number; returns 0 on error.
static version_type get_version(const char_type *dir_path) noexcept {
static version_type get_version(const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::get_version(dir_path);
} catch (...) {
Expand Down Expand Up @@ -1161,7 +1167,7 @@ class basic_manager {
/// \param dir_path Path to a data store. \param description An std::string
/// object that holds a description. \return Returns true on success;
/// otherwise, false.
static bool set_description(const char *dir_path,
static bool set_description(const std::filesystem::path &dir_path,
const std::string &description) noexcept {
try {
return manager_kernel_type::set_description(dir_path, description);
Expand Down Expand Up @@ -1203,7 +1209,7 @@ class basic_manager {
/// to an std::string object to store a description if it exists. \return
/// Returns true on success; returns false on error. Trying to get a
/// non-existent description is not considered as an error.
static bool get_description(const char *dir_path,
static bool get_description(const std::filesystem::path &dir_path,
std::string *description) noexcept {
try {
return manager_kernel_type::get_description(dir_path, description);
Expand All @@ -1222,7 +1228,7 @@ class basic_manager {
/// \param dir_path Path to a data store. \return Returns an instance
/// of named_object_attribute_accessor_type.
static named_object_attribute_accessor_type access_named_object_attribute(
const char *dir_path) noexcept {
const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::access_named_object_attribute(dir_path);
} catch (...) {
Expand All @@ -1239,7 +1245,7 @@ class basic_manager {
/// \param dir_path Path to a data store. \return Returns an instance
/// of unique_object_attribute_accessor_type.
static unique_object_attribute_accessor_type access_unique_object_attribute(
const char *dir_path) noexcept {
const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::access_unique_object_attribute(dir_path);
} catch (...) {
Expand All @@ -1256,7 +1262,8 @@ class basic_manager {
/// \param dir_path Path to a data store. \return Returns an
/// instance of anonymous_object_attribute_accessor_type.
static anonymous_object_attribute_accessor_type
access_anonymous_object_attribute(const char *dir_path) noexcept {
access_anonymous_object_attribute(
const std::filesystem::path &dir_path) noexcept {
try {
return manager_kernel_type::access_anonymous_object_attribute(dir_path);
} catch (...) {
Expand Down
50 changes: 25 additions & 25 deletions include/metall/detail/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ inline bool os_fsync(const int fd) {
return true;
}

inline bool fsync(const std::string &path) {
inline bool fsync(const fs::path &path) {
const int fd = ::open(path.c_str(), O_RDONLY);
if (fd == -1) {
logger::perror(logger::level::error, __FILE__, __LINE__, "open");
Expand All @@ -68,7 +68,7 @@ inline bool fsync(const std::string &path) {
return ret;
}

inline bool fsync_recursive(const std::string &path) {
inline bool fsync_recursive(const fs::path &path) {
fs::path p(path);
p = fs::canonical(p);
while (true) {
Expand Down Expand Up @@ -135,8 +135,7 @@ inline bool extend_file_size(const int fd, const size_t file_size,
return ret;
}

inline bool extend_file_size(const std::string &file_path,
const size_t file_size,
inline bool extend_file_size(const fs::path &file_path, const size_t file_size,
const bool fill_with_zero = false) {
const int fd = ::open(file_path.c_str(), O_RDWR);
if (fd == -1) {
Expand All @@ -152,30 +151,31 @@ inline bool extend_file_size(const std::string &file_path,

/// \brief Check if a file, any kinds of file including directory, exists
/// \warning This implementation could return a wrong result due to metadata
/// cache on NFS. The following code could fail: if (mpi_rank == 1)
/// file_exist(path); // NFS creates metadata cache mpi_barrier(); if (mpi_rank
/// == 0) create_directory(path); mpi_barrier(); if (mpi_rank == 1)
/// cache on NFS. The following code could fail:
/// if (mpi_rank == 1)
/// file_exist(path); // NFS creates metadata cache
/// mpi_barrier();
/// if (mpi_rank == 0)
/// create_directory(path);
/// mpi_barrier();
/// if (mpi_rank == 1)
/// assert(file_exist(path)); // Could fail due to the cached metadata.
inline bool file_exist(const std::string &file_name) {
std::string fixed_string(file_name);
while (fixed_string.back() == '/') {
fixed_string.pop_back();
}
return (::access(fixed_string.c_str(), F_OK) == 0);
inline bool file_exist(const fs::path &file_name) {
return fs::exists(file_name);
}

/// \brief Check if a directory exists
/// \warning This implementation could return a wrong result due to metadata
/// cache on NFS.
inline bool directory_exist(const std::string &dir_path) {
inline bool directory_exist(const fs::path &dir_path) {
struct stat stat_buf;
if (::stat(dir_path.c_str(), &stat_buf) == -1) {
return false;
}
return S_ISDIR(stat_buf.st_mode);
}

inline bool create_file(const std::string &file_path) {
inline bool create_file(const fs::path &file_path) {
const int fd =
::open(file_path.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
Expand All @@ -191,8 +191,8 @@ inline bool create_file(const std::string &file_path) {
/// \brief Creates directories recursively.
/// \return Returns true if the directory was created or already exists.
/// Otherwise, returns false.
inline bool create_directory(const std::string &dir_path) {
std::string fixed_string = dir_path;
inline bool create_directory(const fs::path &dir_path) {
fs::path fixed_string = dir_path;
// MEMO: GCC bug 87846 (fixed in v8.3)
// "Calling std::filesystem::create_directories with a path with a trailing
// separator (e.g. "./a/b/") does not create any directory."
Expand Down Expand Up @@ -227,7 +227,7 @@ inline bool create_directory(const std::string &dir_path) {
return success;
}

inline ssize_t get_file_size(const std::string &file_path) {
inline ssize_t get_file_size(const fs::path &file_path) {
std::ifstream ifs(file_path, std::ifstream::binary | std::ifstream::ate);
ssize_t size = ifs.tellg();
if (size == -1) {
Expand All @@ -242,10 +242,10 @@ inline ssize_t get_file_size(const std::string &file_path) {
/// \brief
/// Note that, according to GCC,
/// the file system may use some blocks for internal record keeping
inline ssize_t get_actual_file_size(const std::string &file_path) {
inline ssize_t get_actual_file_size(const fs::path &file_path) {
struct stat stat_buf;
if (::stat(file_path.c_str(), &stat_buf) != 0) {
std::string s("stat (" + file_path + ")");
std::string s("stat (" + file_path.string() + ")");
logger::perror(logger::level::error, __FILE__, __LINE__, s.c_str());
return -1;
}
Expand All @@ -255,7 +255,7 @@ inline ssize_t get_actual_file_size(const std::string &file_path) {
/// \brief Remove a file or directory
/// \return Upon successful completion, returns true; otherwise, false is
/// returned. If the file or directory does not exist, true is returned.
inline bool remove_file(const std::string &path) {
inline bool remove_file(const fs::path &path) {
std::filesystem::path p(path);
std::error_code ec;
[[maybe_unused]] const auto num_removed = std::filesystem::remove_all(p, ec);
Expand Down Expand Up @@ -283,8 +283,8 @@ inline bool free_file_space([[maybe_unused]] const int fd,

namespace file_copy_detail {

inline bool copy_file_dense(const std::string &source_path,
const std::string &destination_path) {
inline bool copy_file_dense(const fs::path &source_path,
const fs::path &destination_path) {
bool success = true;
try {
if (!fs::copy_file(source_path, destination_path,
Expand All @@ -308,8 +308,8 @@ inline bool copy_file_dense(const std::string &source_path,
}

#ifdef __linux__
inline bool copy_file_sparse_linux(const std::string &source_path,
const std::string &destination_path) {
inline bool copy_file_sparse_linux(const fs::path &source_path,
const fs::path &destination_path) {
std::string command("cp --sparse=auto " + source_path + " " +
destination_path);
const int status = std::system(command.c_str());
Expand Down
Loading

0 comments on commit 4669446

Please sign in to comment.