0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 10:08:56 +02:00

ircd: Populate path:: with boost::filesystem wrapping.

This commit is contained in:
Jason Volk 2016-08-31 01:03:44 -07:00
parent 4b62a704bf
commit 4b9dc5bff4
2 changed files with 118 additions and 5 deletions

View file

@ -43,6 +43,9 @@
namespace ircd {
namespace path {
IRCD_EXCEPTION(ircd::error, error)
IRCD_EXCEPTION(error, filesystem_error)
constexpr auto DPATH = IRCD_PREFIX;
constexpr auto BINPATH = IRCD_PREFIX "/bin";
constexpr auto MODPATH = RB_MODULE_DIR;
@ -81,6 +84,16 @@ enum index
const char *get(index) noexcept;
const char *name(index) noexcept;
bool exists(const std::string &path);
bool is_dir(const std::string &path);
bool is_reg(const std::string &path);
std::vector<std::string> ls(const std::string &path);
std::vector<std::string> ls_recursive(const std::string &path);
std::string cwd();
void chdir(const std::string &path);
} // namespace path
} // namespace ircd
#endif // __cplusplus

View file

@ -23,6 +23,10 @@
* USA
*/
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
namespace ircd {
namespace path {
@ -52,8 +56,107 @@ std::array<ent, num_of<index>()> paths
{ "bandb", DBPATH },
}};
} // namespace path
} // namespace ircd
void
ircd::path::chdir(const std::string &path)
try
{
fs::current_path(path);
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
std::string
ircd::path::cwd()
try
{
return fs::current_path().string();
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
std::vector<std::string>
ircd::path::ls_recursive(const std::string &path)
try
{
fs::recursive_directory_iterator it(path);
const fs::recursive_directory_iterator end;
std::vector<std::string> ret(std::distance(it, end));
std::transform(it, end, begin(ret), []
(const auto &ent)
{
return ent.path().string();
});
return ret;
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
std::vector<std::string>
ircd::path::ls(const std::string &path)
try
{
fs::directory_iterator it(path);
const fs::directory_iterator end;
std::vector<std::string> ret(std::distance(it, end));
std::transform(it, end, begin(ret), []
(const auto &ent)
{
return ent.path().string();
});
return ret;
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
bool
ircd::path::is_reg(const std::string &path)
try
{
return fs::is_regular_file(path);
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
bool
ircd::path::is_dir(const std::string &path)
try
{
return fs::is_directory(path);
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
bool
ircd::path::exists(const std::string &path)
try
{
return fs::exists(path);
}
catch(const fs::filesystem_error &e)
{
throw filesystem_error("%s", e.what());
}
const char *
get(index index)
ircd::path::get(index index)
noexcept try
{
return std::get<PATH>(paths.at(index)).c_str();
@ -64,7 +167,7 @@ catch(const std::out_of_range &e)
}
const char *
name(index index)
ircd::path::name(index index)
noexcept try
{
return std::get<NAME>(paths.at(index)).c_str();
@ -73,6 +176,3 @@ catch(const std::out_of_range &e)
{
return nullptr;
}
} // namespace path
} // namespace ircd