0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd::fs: Reorg path interface related.

This commit is contained in:
Jason Volk 2019-01-25 10:35:39 -08:00
parent f3cc3d945d
commit 2886924bf0
9 changed files with 165 additions and 133 deletions

View file

@ -17,7 +17,7 @@ namespace ircd::fs
size_t size(const fd &);
size_t block_size(const fd &);
ulong filesystem(const fd &);
ulong fstype(const fd &);
ulong device(const fd &);
}

View file

@ -11,6 +11,9 @@
#pragma once
#define HAVE_IRCD_FS_H
// Forward declarations for boost because it is not included here.
namespace boost::filesystem {}
/// Local filesystem interface.
///
/// IRCd has wrapped operations for the local filesystem to maintain a
@ -28,6 +31,11 @@ namespace ircd::fs
{
struct init;
struct error; // custom exception; still inherits from ircd::error
// Forward interface to boost::filesystem. We do not include boost
// from here; it is used internally only. Some exposed interfaces
// may make forward-declared references to boost symbols.
namespace filesystem = boost::filesystem;
}
#include "error.h"

View file

@ -11,6 +11,12 @@
#pragma once
#define HAVE_IRCD_FS_PATH_H
// Forward declarations for boost because it is not included here.
namespace boost::filesystem
{
struct path;
}
namespace ircd::fs
{
enum base :uint;
@ -19,15 +25,22 @@ namespace ircd::fs
extern const size_t NAME_MAX_LEN;
extern const size_t PATH_MAX_LEN;
const basepath &get(const base &) noexcept;
string_view make_path(const base &) noexcept;
std::string make_path(const base &, const string_view &);
std::string make_path(const vector_view<const string_view> &);
std::string make_path(const vector_view<const std::string> &);
filesystem::path _path(std::string);
filesystem::path _path(const string_view &);
filesystem::path _path(const vector_view<const string_view> &);
filesystem::path _path(const vector_view<const std::string> &);
string_view path(const base &) noexcept;
std::string path(const base &, const string_view &);
std::string path(const vector_view<const string_view> &);
std::string path(const vector_view<const std::string> &);
std::string path(const filesystem::path &);
size_t name_max_len(const string_view &path);
size_t path_max_len(const string_view &path);
long pathconf(const string_view &path, const int &arg);
string_view cwd(const mutable_buffer &buf);
std::string cwd();
}
@ -38,6 +51,8 @@ struct ircd::fs::basepath
{
string_view name;
string_view path;
static const basepath &get(const base &) noexcept;
};
/// Index of default paths. Must be aligned with the internal array in fs.cc.

View file

@ -180,7 +180,7 @@ try
{
const auto dbdir
{
fs::make_path(fs::DB)
fs::path(fs::DB)
};
if(fs::mkdir(dbdir))
@ -224,7 +224,7 @@ try
{
log, "Direct-IO is not supported in the database directory `%s'"
"; Concurrent database queries will not be possible.",
fs::get(fs::DB)
fs::basepath::get(fs::DB)
};
}
catch(const std::exception &e)
@ -246,7 +246,7 @@ ircd::db::direct_io_test_file_path()
"SUPPORTS_DIRECT_IO"_sv
};
return fs::make_path(fs::DB, test_file_name);
return fs::path(fs::DB, test_file_name);
}
decltype(ircd::db::compressions)
@ -3265,10 +3265,10 @@ ircd::db::database::sst::dump::dump(db::column column,
{
const string_view path_parts[]
{
fs::make_path(fs::DB), db::name(d), db::name(c)
fs::path(fs::DB), db::name(d), db::name(c)
};
path = fs::make_path(path_parts);
path = fs::path(path_parts);
}
rocksdb::Options opts(d.d->GetOptions(c));
@ -7245,7 +7245,7 @@ ircd::db::available()
{
const auto &prefix
{
fs::make_path(fs::DB)
fs::path(fs::DB)
};
const auto dirs
@ -7309,7 +7309,7 @@ ircd::db::path(const string_view &name,
{
const auto &prefix
{
fs::make_path(fs::DB)
fs::path(fs::DB)
};
const string_view parts[]
@ -7317,7 +7317,7 @@ ircd::db::path(const string_view &name,
prefix, name, lex_cast(checkpoint)
};
return fs::make_path(parts);
return fs::path(parts);
}
std::pair<ircd::string_view, uint64_t>

View file

@ -18,13 +18,8 @@
#include "fs_aio.h"
#endif
namespace filesystem = boost::filesystem;
namespace ircd::fs
{
static filesystem::path path(std::string);
static filesystem::path path(const string_view &);
static filesystem::path path(const vector_view<const string_view> &);
static uint posix_flags(const std::ios::openmode &mode);
static const char *path_str(const string_view &);
static void debug_paths();
@ -80,7 +75,7 @@ bool
ircd::fs::mkdir(const string_view &path)
try
{
return filesystem::create_directories(fs::path(path));
return filesystem::create_directories(_path(path));
}
catch(const filesystem::filesystem_error &e)
{
@ -91,7 +86,7 @@ bool
ircd::fs::remove(const string_view &path)
try
{
return filesystem::remove(fs::path(path));
return filesystem::remove(_path(path));
}
catch(const filesystem::filesystem_error &e)
{
@ -103,7 +98,7 @@ ircd::fs::remove(std::nothrow_t,
const string_view &path)
{
boost::system::error_code ec;
return filesystem::remove(fs::path(path), ec);
return filesystem::remove(_path(path), ec);
}
bool
@ -111,7 +106,7 @@ ircd::fs::rename(const string_view &old,
const string_view &new_)
try
{
filesystem::rename(path(old), path(new_));
filesystem::rename(_path(old), _path(new_));
return true;
}
catch(const filesystem::filesystem_error &e)
@ -125,7 +120,7 @@ ircd::fs::rename(std::nothrow_t,
const string_view &new_)
{
boost::system::error_code ec;
filesystem::rename(path(old), path(new_), ec);
filesystem::rename(_path(old), _path(new_), ec);
return !ec;
}
@ -136,7 +131,7 @@ try
const filesystem::recursive_directory_iterator end;
filesystem::recursive_directory_iterator it
{
fs::path(path)
_path(path)
};
std::vector<std::string> ret;
@ -160,7 +155,7 @@ try
static const filesystem::directory_iterator end;
filesystem::directory_iterator it
{
fs::path(path)
_path(path)
};
std::vector<std::string> ret;
@ -181,7 +176,7 @@ size_t
ircd::fs::size(const string_view &path)
try
{
return filesystem::file_size(fs::path(path));
return filesystem::file_size(_path(path));
}
catch(const filesystem::filesystem_error &e)
{
@ -192,7 +187,7 @@ bool
ircd::fs::is_reg(const string_view &path)
try
{
return filesystem::is_regular_file(fs::path(path));
return filesystem::is_regular_file(_path(path));
}
catch(const filesystem::filesystem_error &e)
{
@ -203,7 +198,7 @@ bool
ircd::fs::is_dir(const string_view &path)
try
{
return filesystem::is_directory(fs::path(path));
return filesystem::is_directory(_path(path));
}
catch(const filesystem::filesystem_error &e)
{
@ -214,7 +209,7 @@ bool
ircd::fs::exists(const string_view &path)
try
{
return filesystem::exists(fs::path(path));
return filesystem::exists(_path(path));
}
catch(const filesystem::filesystem_error &e)
{
@ -1015,7 +1010,7 @@ ircd::fs::device(const fd &fd)
#ifdef HAVE_SYS_STATFS_H
ulong
ircd::fs::filesystem(const fd &fd)
ircd::fs::fstype(const fd &fd)
{
struct statfs f{0};
syscall(::fstatfs, fd, &f);
@ -1023,7 +1018,7 @@ ircd::fs::filesystem(const fd &fd)
}
#else
ulong
ircd::fs::filesystem(const fd &fd)
ircd::fs::fstype(const fd &fd)
{
static_assert
(
@ -1337,25 +1332,6 @@ ircd::fs::bytes(const const_iovec_view &iov)
// fs/path.h
//
namespace ircd::fs
{
extern const std::array<basepath, num_of<base>()> basepaths;
static long pathconf(const string_view &path, const int &arg);
}
decltype(ircd::fs::basepaths)
ircd::fs::basepaths
{{
{ "installation prefix", RB_PREFIX },
{ "binary directory", RB_BIN_DIR },
{ "configuration directory", RB_CONF_DIR },
{ "data directory", RB_DATA_DIR },
{ "database directory", RB_DB_DIR },
{ "log directory", RB_LOG_DIR },
{ "module directory", RB_MODULE_DIR },
}};
std::string
ircd::fs::cwd()
try
@ -1431,60 +1407,130 @@ ircd::fs::pathconf(const string_view &path,
return syscall(::pathconf, path_str(path), arg);
}
std::string
ircd::fs::make_path(const vector_view<const std::string> &list)
try
{
filesystem::path ret;
for(const auto &s : list)
ret /= path(s);
//
// fs::path()
//
return ret.string();
}
catch(const filesystem::filesystem_error &e)
std::string
ircd::fs::path(const filesystem::path &path)
{
throw error{e};
return path.string();
}
std::string
ircd::fs::make_path(const vector_view<const string_view> &list)
try
ircd::fs::path(const vector_view<const std::string> &list)
{
filesystem::path ret;
for(const auto &s : list)
ret /= path(s);
return ret.string();
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
return _path(list).string();
}
std::string
ircd::fs::make_path(const base &base,
const string_view &rest)
try
ircd::fs::path(const vector_view<const string_view> &list)
{
filesystem::path ret;
ret /= make_path(base);
ret /= path(rest);
return ret.string();
return _path(list).string();
}
catch(const filesystem::filesystem_error &e)
std::string
ircd::fs::path(const base &base,
const string_view &rest)
{
throw error{e};
const auto p
{
_path(std::initializer_list<const string_view>
{
path(base),
rest,
})
};
return p.string();
}
ircd::string_view
ircd::fs::make_path(const base &base)
ircd::fs::path(const base &base)
noexcept
{
return get(base).path;
return basepath::get(base).path;
}
//
// fs::_path()
//
boost::filesystem::path
ircd::fs::_path(const vector_view<const std::string> &list)
try
{
filesystem::path ret;
for(const auto &s : list)
ret /= s;
return ret.string();
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
boost::filesystem::path
ircd::fs::_path(const vector_view<const string_view> &list)
try
{
filesystem::path ret;
for(const auto &s : list)
ret /= _path(s);
return ret.string();
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
boost::filesystem::path
ircd::fs::_path(const string_view &s)
try
{
return _path(std::string{s});
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
boost::filesystem::path
ircd::fs::_path(std::string s)
try
{
return filesystem::path{std::move(s)};
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
//
// fs::basepath
//
namespace ircd::fs
{
extern const std::array<basepath, num_of<base>()> basepaths;
}
decltype(ircd::fs::basepaths)
ircd::fs::basepaths
{{
{ "installation prefix", RB_PREFIX },
{ "binary directory", RB_BIN_DIR },
{ "configuration directory", RB_CONF_DIR },
{ "data directory", RB_DATA_DIR },
{ "database directory", RB_DB_DIR },
{ "log directory", RB_LOG_DIR },
{ "module directory", RB_MODULE_DIR },
}};
const ircd::fs::basepath &
ircd::fs::get(const base &base)
ircd::fs::basepath::get(const base &base)
noexcept
{
return basepaths.at(base);
@ -1563,8 +1609,8 @@ ircd::fs::debug_paths()
log::debug
{
"Working %s is `%s'",
get(base).name,
get(base).path,
basepath::get(base).name,
basepath::get(base).path,
};
});
}
@ -1598,40 +1644,3 @@ ircd::fs::posix_flags(const std::ios::openmode &mode)
ret |= ret & O_RDWR && ret & (O_TRUNC | O_APPEND)? O_CREAT : 0;
return ret;
}
filesystem::path
ircd::fs::path(const vector_view<const string_view> &list)
try
{
filesystem::path ret;
for(const auto &s : list)
ret /= path(s);
return ret.string();
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
filesystem::path
ircd::fs::path(const string_view &s)
try
{
return path(std::string{s});
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
filesystem::path
ircd::fs::path(std::string s)
try
{
return filesystem::path(std::move(s));
}
catch(const filesystem::filesystem_error &e)
{
throw error{e};
}

View file

@ -63,7 +63,7 @@ ircd::log::mkdir()
{
const auto &dir
{
fs::make_path(fs::LOG)
fs::path(fs::LOG)
};
if(fs::exists(dir))
@ -140,7 +140,7 @@ catch(const std::exception &e)
std::string
ircd::log::file_path(const level &lev)
{
return fs::make_path(fs::LOG, reflect(lev));
return fs::path(fs::LOG, reflect(lev));
}
void

View file

@ -881,7 +881,7 @@ namespace ircd::mods
decltype(ircd::mods::modroot)
ircd::mods::modroot
{
fs::make_path(fs::MODULES)
fs::path(fs::MODULES)
};
decltype(ircd::mods::paths)

View file

@ -3390,7 +3390,7 @@ console_cmd__db__list(opt &out, const string_view &line)
{
const auto name
{
replace(lstrip(lstrip(path, fs::make_path(fs::DB)), '/'), "/", ":")
replace(lstrip(lstrip(path, fs::path(fs::DB)), '/'), "/", ":")
};
const auto &d

View file

@ -77,17 +77,17 @@ init_my_tls_crt()
const std::string private_key_file
{
fs::make_path(private_key_path_parts)
fs::path(private_key_path_parts)
};
const std::string public_key_file
{
fs::make_path(public_key_path_parts)
fs::path(public_key_path_parts)
};
const std::string cert_file
{
fs::make_path(certificate_path_parts)
fs::path(certificate_path_parts)
};
if(!fs::exists(private_key_file))
@ -104,7 +104,7 @@ init_my_tls_crt()
/*
const std::string dhparam_file
{
fs::make_path(dhparam_path_parts)
fs::path(dhparam_path_parts)
};
if(!fs::exists(dhparam_file))
@ -242,7 +242,7 @@ init_my_ed25519()
const std::string sk_file
{
fs::make_path(path_parts)
fs::path(path_parts)
};
if(fs::exists(sk_file))