0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-05 01:58:35 +02:00

ircd::fs: Fix namespace ambiguities; various interface updates.

This commit is contained in:
Jason Volk 2018-02-26 20:35:09 -08:00
parent 5c5a5d3c83
commit 121be689e8
2 changed files with 110 additions and 55 deletions

View file

@ -68,21 +68,19 @@ namespace ircd::fs
const char *get(index) noexcept; const char *get(index) noexcept;
const char *name(index) noexcept; const char *name(index) noexcept;
std::string make_path(const std::initializer_list<std::string> &); std::string make_path(const std::initializer_list<string_view> &);
bool exists(const std::string &path); bool exists(const string_view &path);
bool is_dir(const std::string &path); bool is_dir(const string_view &path);
bool is_reg(const std::string &path); bool is_reg(const string_view &path);
size_t size(const std::string &path);
size_t size(const string_view &path); size_t size(const string_view &path);
std::vector<std::string> ls(const std::string &path); std::vector<std::string> ls(const string_view &path);
std::vector<std::string> ls_recursive(const std::string &path); std::vector<std::string> ls_recursive(const string_view &path);
std::string cwd(); std::string cwd();
void chdir(const std::string &path); void chdir(const string_view &path);
bool mkdir(const std::string &path); bool mkdir(const string_view &path);
extern aio *aioctx; extern aio *aioctx;
} }

View file

@ -15,13 +15,17 @@
#include "aio.h" #include "aio.h"
#endif #endif
namespace filesystem = boost::filesystem;
namespace ircd::fs namespace ircd::fs
{ {
using namespace boost::filesystem;
enum { NAME, PATH }; enum { NAME, PATH };
using ent = std::pair<std::string, std::string>; using ent = std::pair<std::string, std::string>;
extern const std::array<ent, num_of<index>()> paths; extern const std::array<ent, num_of<index>()> paths;
filesystem::path path(std::string);
filesystem::path path(const string_view &);
filesystem::path path(const std::initializer_list<string_view> &);
} }
/// Non-null when aio is available for use /// Non-null when aio is available for use
@ -195,44 +199,63 @@ ircd::fs::write__std(const string_view &path,
} }
void void
ircd::fs::chdir(const std::string &path) ircd::fs::chdir(const string_view &path)
try try
{ {
fs::current_path(path); filesystem::current_path(fs::path(path));
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
bool bool
ircd::fs::mkdir(const std::string &path) ircd::fs::mkdir(const string_view &path)
try try
{ {
return fs::create_directory(path); return filesystem::create_directory(fs::path(path));
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
std::string std::string
ircd::fs::cwd() ircd::fs::cwd()
try try
{ {
return fs::current_path().string(); return filesystem::current_path().string();
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
}
catch(const filesystem::filesystem_error &e)
{
throw filesystem_error
{
"%s", e.what()
};
} }
std::vector<std::string> std::vector<std::string>
ircd::fs::ls_recursive(const std::string &path) ircd::fs::ls_recursive(const string_view &path)
try try
{ {
const fs::recursive_directory_iterator end; const filesystem::recursive_directory_iterator end;
fs::recursive_directory_iterator it(path); filesystem::recursive_directory_iterator it
{
fs::path(path)
};
std::vector<std::string> ret; std::vector<std::string> ret;
std::transform(it, end, std::back_inserter(ret), [] std::transform(it, end, std::back_inserter(ret), []
@ -243,17 +266,23 @@ try
return ret; return ret;
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
std::vector<std::string> std::vector<std::string>
ircd::fs::ls(const std::string &path) ircd::fs::ls(const string_view &path)
try try
{ {
static const fs::directory_iterator end; static const filesystem::directory_iterator end;
fs::directory_iterator it(path); filesystem::directory_iterator it
{
fs::path(path)
};
std::vector<std::string> ret; std::vector<std::string> ret;
std::transform(it, end, std::back_inserter(ret), [] std::transform(it, end, std::back_inserter(ret), []
@ -264,66 +293,94 @@ try
return ret; return ret;
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
size_t size_t
ircd::fs::size(const string_view &path) ircd::fs::size(const string_view &path)
{ {
return ircd::fs::size(std::string{path}); return filesystem::file_size(fs::path(path));
}
size_t
ircd::fs::size(const std::string &path)
{
return fs::file_size(path);
} }
bool bool
ircd::fs::is_reg(const std::string &path) ircd::fs::is_reg(const string_view &path)
try try
{ {
return fs::is_regular_file(path); return filesystem::is_regular_file(fs::path(path));
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
bool bool
ircd::fs::is_dir(const std::string &path) ircd::fs::is_dir(const string_view &path)
try try
{ {
return fs::is_directory(path); return filesystem::is_directory(fs::path(path));
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
bool bool
ircd::fs::exists(const std::string &path) ircd::fs::exists(const string_view &path)
try try
{ {
return boost::filesystem::exists(path); return filesystem::exists(fs::path(path));
} }
catch(const fs::filesystem_error &e) catch(const filesystem::filesystem_error &e)
{ {
throw filesystem_error("%s", e.what()); throw filesystem_error
{
"%s", e.what()
};
} }
std::string std::string
ircd::fs::make_path(const std::initializer_list<std::string> &list) ircd::fs::make_path(const std::initializer_list<string_view> &list)
{ {
fs::path ret; filesystem::path ret;
for(const auto &s : list) for(const auto &s : list)
ret /= fs::path(s); ret /= path(s);
return ret.string(); return ret.string();
} }
filesystem::path
ircd::fs::path(const std::initializer_list<string_view> &list)
{
filesystem::path ret;
for(const auto &s : list)
ret /= path(s);
return ret.string();
}
filesystem::path
ircd::fs::path(const string_view &s)
{
return path(std::string{s});
}
filesystem::path
ircd::fs::path(std::string s)
{
return filesystem::path(std::move(s));
}
const char * const char *
ircd::fs::get(index index) ircd::fs::get(index index)
noexcept try noexcept try