diff --git a/include/ircd/fs.h b/include/ircd/fs.h index 1aefe048c..6fa365c59 100644 --- a/include/ircd/fs.h +++ b/include/ircd/fs.h @@ -95,4 +95,8 @@ namespace ircd::fs bool mkdir(const std::string &path); std::string read(const std::string &name); + string_view read(const std::string &name, const mutable_raw_buffer &buf); + bool write(const std::string &name, const const_raw_buffer &buf); + bool append(const std::string &name, const const_raw_buffer &buf); + bool overwrite(const std::string &name, const const_raw_buffer &buf); } diff --git a/ircd/fs.cc b/ircd/fs.cc index dfdac4c03..a2c71c133 100644 --- a/ircd/fs.cc +++ b/ircd/fs.cc @@ -60,6 +60,34 @@ std::array()> paths } // namespace fs } // namespace ircd +bool +ircd::fs::write(const std::string &path, + const const_raw_buffer &buf) +{ + if(fs::exists(path)) + return false; + + return overwrite(path, buf); +} + +bool +ircd::fs::overwrite(const std::string &path, + const const_raw_buffer &buf) +{ + std::ofstream file{path, std::ios::trunc}; + file.write(reinterpret_cast(data(buf)), size(buf)); + return true; +} + +bool +ircd::fs::append(const std::string &path, + const const_raw_buffer &buf) +{ + std::ofstream file{path, std::ios::app}; + file.write(reinterpret_cast(data(buf)), size(buf)); + return true; +} + std::string ircd::fs::read(const std::string &path) { @@ -70,6 +98,18 @@ ircd::fs::read(const std::string &path) return std::string{b, e}; } +ircd::string_view +ircd::fs::read(const std::string &path, + const mutable_raw_buffer &buf) +{ + std::ifstream file{path}; + if(!file.good()) + return {}; + + file.read(reinterpret_cast(data(buf)), size(buf)); + return { reinterpret_cast(data(buf)), file.gcount() }; +} + void ircd::fs::chdir(const std::string &path) try