2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-11 06:30:31 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-02-06 06:23:34 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
2017-10-12 02:50:13 +02:00
|
|
|
#include <ircd/asio.h>
|
2016-08-31 10:03:44 +02:00
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
#include "aio.h"
|
|
|
|
#endif
|
|
|
|
|
2017-11-26 20:56:08 +01:00
|
|
|
namespace ircd::fs
|
2016-08-14 05:35:06 +02:00
|
|
|
{
|
2017-11-26 20:56:08 +01:00
|
|
|
using namespace boost::filesystem;
|
2016-08-14 05:35:06 +02:00
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
enum { NAME, PATH };
|
2017-11-26 20:56:08 +01:00
|
|
|
using ent = std::pair<std::string, std::string>;
|
|
|
|
extern const std::array<ent, num_of<index>()> paths;
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
/// Non-null when aio is available for use
|
|
|
|
decltype(ircd::fs::aioctx)
|
|
|
|
ircd::fs::aioctx
|
|
|
|
{};
|
|
|
|
|
2017-11-26 20:56:08 +01:00
|
|
|
decltype(ircd::fs::paths)
|
|
|
|
ircd::fs::paths
|
2016-08-31 08:25:25 +02:00
|
|
|
{{
|
|
|
|
{ "prefix", DPATH },
|
|
|
|
{ "binary dir", BINPATH },
|
|
|
|
{ "config", ETCPATH },
|
|
|
|
{ "log", LOGPATH },
|
|
|
|
{ "libexec dir", PKGLIBEXECDIR },
|
|
|
|
{ "modules", MODPATH },
|
|
|
|
{ "ircd.conf", CPATH },
|
|
|
|
{ "ircd binary", SPATH },
|
2016-09-24 03:21:08 +02:00
|
|
|
{ "db", DBPATH },
|
2016-08-31 08:25:25 +02:00
|
|
|
}};
|
2016-08-14 05:35:06 +02:00
|
|
|
|
2018-01-10 01:48:05 +01:00
|
|
|
ircd::fs::init::init()
|
|
|
|
{
|
2018-01-11 06:30:31 +01:00
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
assert(!aioctx);
|
|
|
|
aioctx = new aio{};
|
2018-01-24 20:50:50 +01:00
|
|
|
#else
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"No support for asynchronous local filesystem IO..."
|
|
|
|
};
|
2018-01-11 06:30:31 +01:00
|
|
|
#endif
|
2018-01-10 01:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-01-11 06:30:31 +01:00
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
delete aioctx;
|
|
|
|
aioctx = nullptr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(!aioctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/read.h
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
string_view read__std(const string_view &path, const mutable_buffer &, const read_opts &);
|
2018-01-11 06:30:31 +01:00
|
|
|
std::string read__std(const string_view &path, const read_opts &);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::read_opts
|
|
|
|
const ircd::fs::read_opts_default
|
|
|
|
{};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ircd::fs interface linkage
|
|
|
|
//
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::fs::read(const string_view &path,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
if(likely(aioctx))
|
|
|
|
return read__aio(path, opts);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return read__std(path, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::read(const string_view &path,
|
2018-02-03 08:20:26 +01:00
|
|
|
const mutable_buffer &buf,
|
2018-01-11 06:30:31 +01:00
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
if(likely(aioctx))
|
|
|
|
return read__aio(path, buf, opts);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return read__std(path, buf, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// std read
|
|
|
|
//
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::fs::read__std(const string_view &path,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
std::ifstream file{std::string{path}};
|
|
|
|
std::noskipws(file);
|
|
|
|
std::istream_iterator<char> b{file};
|
|
|
|
std::istream_iterator<char> e{};
|
|
|
|
return std::string{b, e};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::read__std(const string_view &path,
|
2018-02-03 08:20:26 +01:00
|
|
|
const mutable_buffer &buf,
|
2018-01-11 06:30:31 +01:00
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
std::ifstream file{std::string{path}};
|
|
|
|
file.exceptions(file.failbit | file.badbit);
|
|
|
|
file.seekg(opts.offset, file.beg);
|
2018-02-03 08:20:26 +01:00
|
|
|
file.read(data(buf), size(buf));
|
2018-01-15 07:18:18 +01:00
|
|
|
return
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
data(buf), size_t(file.gcount())
|
2018-01-11 06:30:31 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/write.h
|
|
|
|
//
|
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
namespace ircd::fs
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-02-06 08:28:33 +01:00
|
|
|
string_view write__std(const string_view &path, const const_buffer &, const write_opts &);
|
2017-10-02 11:58:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
ircd::fs::write_opts
|
|
|
|
const ircd::fs::write_opts_default
|
|
|
|
{};
|
2017-12-21 21:27:22 +01:00
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::write(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-02-06 08:28:33 +01:00
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
if(likely(aioctx))
|
|
|
|
return write__aio(path, buf, opts);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return write__std(path, buf, opts);
|
2017-10-02 11:58:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::write__std(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-02-06 08:28:33 +01:00
|
|
|
const auto open_mode
|
|
|
|
{
|
|
|
|
opts.append?
|
|
|
|
std::ios::app:
|
|
|
|
|
|
|
|
opts.overwrite?
|
|
|
|
std::ios::trunc:
|
|
|
|
|
|
|
|
std::ios::out
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ofstream file
|
|
|
|
{
|
|
|
|
std::string{path}, open_mode
|
|
|
|
};
|
|
|
|
|
|
|
|
file.seekp(opts.offset, file.beg);
|
2018-02-03 08:20:26 +01:00
|
|
|
file.write(data(buf), size(buf));
|
2018-02-06 08:28:33 +01:00
|
|
|
return buf;
|
2017-10-02 11:58:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 10:03:44 +02:00
|
|
|
void
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::chdir(const std::string &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
fs::current_path(path);
|
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
|
|
|
}
|
|
|
|
|
2017-08-16 21:15:23 +02:00
|
|
|
bool
|
|
|
|
ircd::fs::mkdir(const std::string &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return fs::create_directory(path);
|
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
|
|
|
}
|
|
|
|
|
2016-08-31 10:03:44 +02:00
|
|
|
std::string
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::cwd()
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return fs::current_path().string();
|
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::ls_recursive(const std::string &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const fs::recursive_directory_iterator end;
|
2017-08-23 23:43:25 +02:00
|
|
|
fs::recursive_directory_iterator it(path);
|
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
std::transform(it, end, std::back_inserter(ret), []
|
2016-08-31 10:03:44 +02:00
|
|
|
(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>
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::ls(const std::string &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2017-03-31 00:46:01 +02:00
|
|
|
static const fs::directory_iterator end;
|
2017-08-23 23:43:25 +02:00
|
|
|
fs::directory_iterator it(path);
|
2017-03-31 00:46:01 +02:00
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
std::transform(it, end, std::back_inserter(ret), []
|
2016-08-31 10:03:44 +02:00
|
|
|
(const auto &ent)
|
|
|
|
{
|
|
|
|
return ent.path().string();
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
2017-12-21 01:33:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::size(const string_view &path)
|
|
|
|
{
|
|
|
|
return ircd::fs::size(std::string{path});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::size(const std::string &path)
|
|
|
|
{
|
|
|
|
return fs::file_size(path);
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::is_reg(const std::string &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return fs::is_regular_file(path);
|
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::is_dir(const std::string &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return fs::is_directory(path);
|
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::exists(const std::string &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2017-10-03 13:02:39 +02:00
|
|
|
return boost::filesystem::exists(path);
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
catch(const fs::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error("%s", e.what());
|
|
|
|
}
|
|
|
|
|
2016-09-24 03:44:30 +02:00
|
|
|
std::string
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::make_path(const std::initializer_list<std::string> &list)
|
2016-09-24 03:44:30 +02:00
|
|
|
{
|
|
|
|
fs::path ret;
|
|
|
|
for(const auto &s : list)
|
|
|
|
ret /= fs::path(s);
|
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:35:06 +02:00
|
|
|
const char *
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::get(index index)
|
2016-08-14 05:35:06 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
2016-08-31 08:25:25 +02:00
|
|
|
return std::get<PATH>(paths.at(index)).c_str();
|
2016-08-14 05:35:06 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
2016-07-25 10:57:54 +02:00
|
|
|
{
|
2016-08-14 05:35:06 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-25 10:57:54 +02:00
|
|
|
|
2016-08-14 05:35:06 +02:00
|
|
|
const char *
|
2017-03-31 00:46:01 +02:00
|
|
|
ircd::fs::name(index index)
|
2016-08-14 05:35:06 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
2016-08-31 08:25:25 +02:00
|
|
|
return std::get<NAME>(paths.at(index)).c_str();
|
2016-08-14 05:35:06 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|