0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 08:12:37 +01:00

ircd::fs: Refactor base paths into conf::items.

This commit is contained in:
Jason Volk 2020-04-18 21:13:13 -07:00
parent e7f2a5c1f6
commit a6cf4c3c91
5 changed files with 158 additions and 115 deletions

View file

@ -19,8 +19,6 @@ namespace boost::filesystem
namespace ircd::fs
{
enum class base :uint;
struct basepath;
using path_views = vector_view<const string_view>;
using path_strings = vector_view<const std::string>;
@ -36,8 +34,6 @@ namespace ircd::fs
filesystem::path _path(const path_views &);
filesystem::path _path(const path_strings &);
string_view path(const base &) noexcept;
string_view path(const mutable_buffer &, const base &, const string_view &);
string_view path(const mutable_buffer &, const path_views &);
string_view path(const mutable_buffer &, const path_strings &);
string_view path(const mutable_buffer &, const filesystem::path &);
@ -62,37 +58,24 @@ namespace ircd::fs
std::string cwd();
}
/// A compile-time installation base-path. We have several of these in an
/// internal array accessible with get(enum base) or make_path(enum base).
/// These can still be modified at runtime by setting a new platform-dependent
/// string with set(enum base); do so with care.
struct ircd::fs::basepath
/// Configuration items storing the base paths used at runtime for program
/// operation. The defaults are generated at ./configure time and obtained
/// from macros in config.h. As conf items, these values may be overriden by
/// environment variables and may be updated by conf loads from the database.
///
namespace ircd::fs::base
{
string_view name;
string_view path;
static const basepath &get(const base &) noexcept;
static string_view set(const base &, const string_view &); // (returns old value)
};
/// Index of default paths. Must be aligned with the internal array in fs.cc.
/// Note that even though the PREFIX is accessible here, custom installations
/// may use entirely different paths for other components; most installations
/// use the package-target name as a path component.
enum class ircd::fs::base
:uint
{
PREFIX, ///< Installation prefix (from ./configure --prefix)
BIN, ///< Binary directory (e.g. $prefix/bin)
CONF, ///< Configuration directory (e.g. $prefix/etc)
DATA, ///< Read-only data directory (e.g. $prefix/share)
DB, ///< Database directory (e.g. $prefix/var/db)
LOG, ///< Logfile directory (e.g. $prefix/var/log)
LIB, ///< Shared library directory (e.g. $prefix/lib)
MODULES, ///< Modules directory (e.g. $prefix/lib/modules)
_NUM_
};
extern conf::item<std::string> prefix; // e.g. /usr
extern conf::item<std::string> bin; // e.g. /usr/bin
extern conf::item<std::string> etc; // e.g. /etc
extern conf::item<std::string> include; // e.g. /usr/include/ircd
extern conf::item<std::string> lib; // e.g. /usr/lib
extern conf::item<std::string> modules; // e.g. /usr/lib/modules/ircd
extern conf::item<std::string> share; // e.g. /usr/share/ircd
extern conf::item<std::string> run; // e.g. /var/run/ircd
extern conf::item<std::string> log; // e.g. /var/log/ircd
extern conf::item<std::string> db; // e.g. /var/db/ircd
}
template<class... A>
std::string

View file

@ -166,7 +166,7 @@ try
{
const auto dbdir
{
fs::path(fs::base::DB)
fs::path(fs::base::db)
};
if(!fs::is_dir(dbdir) && (ircd::read_only || ircd::write_avoid))

View file

@ -60,6 +60,139 @@ ircd::fs::name_scratch
_name_scratch
};
/// e.g. / default=RB_PREFIX
/// env=ircd_fs_base_prefix
decltype(ircd::fs::base::prefix)
ircd::fs::base::prefix
{
{
{ "name", "ircd.fs.base.prefix" },
{ "default", RB_PREFIX },
{ "help", "directory prefix" },
},
nullptr
};
/// e.g. /usr/bin default=RB_BIN_DIR
/// env=ircd_fs_base_bin
decltype(ircd::fs::base::bin)
ircd::fs::base::bin
{
{
{ "name", "ircd.fs.base.bin" },
{ "default", RB_BIN_DIR },
{ "help", "binary directory" },
},
nullptr
};
/// e.g. /etc default=RB_CONF_DIR
/// env=$ircd_fs_base_etc env=$CONFIGURATION_DIRECTORY
decltype(ircd::fs::base::etc)
ircd::fs::base::etc
{
{
{ "name", "ircd.fs.base.etc" },
{ "default", RB_CONF_DIR },
{ "help", "configuration directory" },
}, []
{
string_view env;
if((env = getenv("CONFIGURATION_DIRECTORY")))
etc._value = env;
}
};
/// e.g. /usr/lib default=RB_LIB_DIR
/// env=$ircd_fs_base_lib
decltype(ircd::fs::base::lib)
ircd::fs::base::lib
{
{
{ "name", "ircd.fs.base.lib" },
{ "default", RB_LIB_DIR },
{ "help", "library directory" },
},
nullptr
};
/// e.g. /usr/lib/modules/construct default=RB_MODULE_DIR
/// env=$ircd_fs_base_modules
decltype(ircd::fs::base::modules)
ircd::fs::base::modules
{
{
{ "name", "ircd.fs.base.modules" },
{ "default", RB_MODULE_DIR },
{ "help", "modules directory" },
},
nullptr
};
/// e.g. /usr/share/construct default=RB_DATA_DIR
/// env=$ircd_fs_base_share
decltype(ircd::fs::base::share)
ircd::fs::base::share
{
{
{ "name", "ircd.fs.base.share" },
{ "default", RB_DATA_DIR },
{ "help", "read-only data directory" },
},
nullptr
};
/// e.g. /var/run/construct default=RB_RUN_DIR
/// env=$ircd_fs_base_run env=$RUNTIME_DIRECTORY
decltype(ircd::fs::base::run)
ircd::fs::base::run
{
{
{ "name", "ircd.fs.base.run" },
{ "default", RB_RUN_DIR },
{ "help", "runtime directory" },
}, []
{
string_view env;
if((env = getenv("RUNTIME_DIRECTORY")))
run._value = env;
}
};
/// e.g. /var/log/construct default=RB_LOG_DIR
/// env=$ircd_fs_base_log env=$LOGS_DIRECTORY
decltype(ircd::fs::base::log)
ircd::fs::base::log
{
{
{ "name", "ircd.fs.base.log" },
{ "default", RB_LOG_DIR },
{ "help", "logging directory" },
}, []
{
string_view env;
if((env = getenv("LOGS_DIRECTORY")))
log._value = env;
}
};
/// e.g. /var/db/construct default=RB_DB_DIR
/// env=$ircd_fs_base_db env=$STATE_DIRECTORY
decltype(ircd::fs::base::db)
ircd::fs::base::db
{
{
{ "name", "ircd.fs.base.db" },
{ "default", RB_DB_DIR },
{ "help", "database directory" },
}, []
{
string_view env;
if((env = getenv("STATE_DIRECTORY")))
db._value = env;
}
};
std::string
ircd::fs::cwd()
try
@ -225,30 +358,6 @@ ircd::fs::path(const mutable_buffer &buf,
return strlcpy(buf, _path(list).c_str());
}
ircd::string_view
ircd::fs::path(const mutable_buffer &buf,
const base &base,
const string_view &rest)
{
const auto p
{
_path(std::initializer_list<const string_view>
{
path(base),
rest,
})
};
return strlcpy(buf, p.c_str());
}
ircd::string_view
ircd::fs::path(const base &base)
noexcept
{
return basepath::get(base).path;
}
//
// fs::_path()
//
@ -304,50 +413,3 @@ catch(const filesystem::filesystem_error &e)
{
throw error{e};
}
//
// fs::basepath
//
namespace ircd::fs
{
extern 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 },
{ "library directory", RB_LIB_DIR },
{ "module directory", RB_MODULE_DIR },
}};
ircd::string_view
ircd::fs::basepath::set(const base &base,
const string_view &path)
{
log::debug
{
log, "Updating base path #%u '%s' from `%s' to `%s'",
uint(base),
basepaths.at(uint(base)).name,
basepaths.at(uint(base)).path,
path,
};
const string_view ret(basepaths.at(uint(base)).path);
basepaths.at(uint(base)).path = path;
return ret;
}
const ircd::fs::basepath &
ircd::fs::basepath::get(const base &base)
noexcept
{
return basepaths.at(uint(base));
}

View file

@ -145,15 +145,10 @@ ircd::log::fini()
void
ircd::log::mkdir()
{
const auto &dir
{
fs::path(fs::base::LOG)
};
if(fs::exists(dir))
if(fs::exists(fs::base::log))
return;
fs::mkdir(dir);
fs::mkdir(fs::base::log);
}
//
@ -228,7 +223,10 @@ catch(const std::exception &e)
std::string
ircd::log::file_path(const level &lev)
{
return fs::path_string(fs::base::LOG, reflect(lev));
return fs::path_string(fs::path_views
{
fs::base::log, reflect(lev)
});
}
void

View file

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