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-10-18 18:26:04 +02:00
|
|
|
#include <RB_INC_SYS_STAT_H
|
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
|
|
|
|
|
2018-02-27 05:35:09 +01:00
|
|
|
namespace filesystem = boost::filesystem;
|
|
|
|
|
2017-11-26 20:56:08 +01:00
|
|
|
namespace ircd::fs
|
2016-08-14 05:35:06 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
filesystem::path path(std::string);
|
|
|
|
filesystem::path path(const string_view &);
|
2018-05-25 04:11:05 +02:00
|
|
|
filesystem::path path(const vector_view<const string_view> &);
|
2017-11-26 20:56:08 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-01-10 01:48:05 +01:00
|
|
|
ircd::fs::init::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-01-11 06:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-12 20:56:53 +02:00
|
|
|
//
|
|
|
|
// Compile-time path index
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
struct sysent;
|
|
|
|
extern const std::array<struct sysent, num_of<index>()> syspaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ircd::fs::sysent
|
|
|
|
{
|
|
|
|
string_view name;
|
|
|
|
string_view path;
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fs::syspaths)
|
|
|
|
ircd::fs::syspaths
|
|
|
|
{{
|
2018-08-13 01:44:55 +02:00
|
|
|
{ "installation prefix", RB_PREFIX },
|
|
|
|
{ "binary directory", RB_BIN_DIR },
|
|
|
|
{ "configuration directory", RB_CONF_DIR },
|
2018-09-14 05:59:52 +02:00
|
|
|
{ "data directory", RB_DATA_DIR },
|
2018-08-13 01:44:55 +02:00
|
|
|
{ "database directory", RB_DB_DIR },
|
|
|
|
{ "log directory", RB_LOG_DIR },
|
|
|
|
{ "module directory", RB_MODULE_DIR },
|
2018-08-12 20:56:53 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::get(index index)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
return syspaths.at(index).path;
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::name(index index)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
return syspaths.at(index).name;
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs.h / misc
|
|
|
|
//
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::fs::cwd()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::current_path().string();
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::chdir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
filesystem::current_path(fs::path(path));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
e, "`%s' :%s", path, e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::mkdir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::create_directories(fs::path(path));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
e, "`%s' :%s", path, e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::remove(fs::path(path));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
e, "`%s' :%s", path, e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(std::nothrow_t,
|
|
|
|
const string_view &path)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
return filesystem::remove(fs::path(path), ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::rename(const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
filesystem::rename(path(old), path(new_));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
e, "`%s' -> `%s' :%s", old, new_, e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::rename(std::nothrow_t,
|
|
|
|
const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
filesystem::rename(path(old), path(new_), ec);
|
|
|
|
return !ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
ircd::fs::ls_recursive(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const filesystem::recursive_directory_iterator end;
|
|
|
|
filesystem::recursive_directory_iterator it
|
|
|
|
{
|
|
|
|
fs::path(path)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
std::transform(it, end, std::back_inserter(ret), []
|
|
|
|
(const auto &ent)
|
|
|
|
{
|
|
|
|
return ent.path().string();
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-09 00:43:59 +01:00
|
|
|
const auto &ec
|
|
|
|
{
|
|
|
|
make_error_code(e.code())
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
e, "`%s': %s", path, ec.message()
|
|
|
|
};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
ircd::fs::ls(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const filesystem::directory_iterator end;
|
|
|
|
filesystem::directory_iterator it
|
|
|
|
{
|
|
|
|
fs::path(path)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
std::transform(it, end, std::back_inserter(ret), []
|
|
|
|
(const auto &ent)
|
|
|
|
{
|
|
|
|
return ent.path().string();
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-09 00:43:59 +01:00
|
|
|
const auto &ec
|
|
|
|
{
|
|
|
|
make_error_code(e.code())
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
e, "`%s': %s", path, ec.message()
|
|
|
|
};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::size(const string_view &path)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
|
|
|
return filesystem::file_size(fs::path(path));
|
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::is_reg(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::is_regular_file(fs::path(path));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::is_dir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::is_directory(fs::path(path));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::exists(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::exists(fs::path(path));
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::fs::make_path(const vector_view<const std::string> &list)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
|
|
|
filesystem::path ret;
|
|
|
|
for(const auto &s : list)
|
|
|
|
ret /= path(s);
|
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::fs::make_path(const vector_view<const string_view> &list)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
|
|
|
filesystem::path ret;
|
|
|
|
for(const auto &s : list)
|
|
|
|
ret /= path(s);
|
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
filesystem::path
|
|
|
|
ircd::fs::path(const vector_view<const string_view> &list)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
|
|
|
filesystem::path ret;
|
|
|
|
for(const auto &s : list)
|
|
|
|
ret /= path(s);
|
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
filesystem::path
|
|
|
|
ircd::fs::path(const string_view &s)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
|
|
|
return path(std::string{s});
|
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
filesystem::path
|
|
|
|
ircd::fs::path(std::string s)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
|
|
|
return filesystem::path(std::move(s));
|
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
2018-12-03 21:43:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/support.h
|
|
|
|
//
|
|
|
|
|
2018-12-03 21:58:42 +01:00
|
|
|
bool
|
|
|
|
ircd::fs::support::fallocate(const string_view &path,
|
|
|
|
const write_opts &wopts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const fd::opts opts
|
|
|
|
{
|
|
|
|
std::ios::out
|
|
|
|
};
|
|
|
|
|
|
|
|
fs::fd fd
|
|
|
|
{
|
|
|
|
path, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
fs::allocate(fd, info::page_size, wopts);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
const auto &ec(e.code());
|
|
|
|
if(system_category(ec)) switch(ec.value())
|
|
|
|
{
|
|
|
|
case int(std::errc::invalid_argument):
|
|
|
|
case int(std::errc::operation_not_supported):
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2018-12-03 21:43:42 +01:00
|
|
|
bool
|
|
|
|
ircd::fs::support::direct_io(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fd::opts opts{std::ios::out};
|
|
|
|
opts.direct = true;
|
|
|
|
fd{path, opts};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
const auto &ec(e.code());
|
|
|
|
if(system_category(ec)) switch(ec.value())
|
|
|
|
{
|
|
|
|
case int(std::errc::invalid_argument):
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// True if AIO is supported by this build and runtime.
|
|
|
|
decltype(ircd::fs::support::aio)
|
|
|
|
ircd::fs::support::aio
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/// True if IOCB_CMD_FSYNC is supported by AIO. If this is false then
|
|
|
|
/// fs::fsync_opts::async=true flag is ignored.
|
|
|
|
decltype(ircd::fs::support::aio_fsync)
|
|
|
|
ircd::fs::support::aio_fsync
|
|
|
|
{
|
|
|
|
false //TODO: Detect kernel support
|
|
|
|
};
|
|
|
|
|
|
|
|
/// True if IOCB_CMD_FDSYNC is supported by AIO. If this is false then
|
|
|
|
/// fs::fsync_opts::async=true flag is ignored.
|
|
|
|
decltype(ircd::fs::support::aio_fdsync)
|
|
|
|
ircd::fs::support::aio_fdsync
|
|
|
|
{
|
|
|
|
false //TODO: Detect kernel support
|
|
|
|
};
|
|
|
|
|
2018-03-23 23:37:01 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/stdin.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::stdin::readline(const mutable_buffer &buf)
|
2018-12-11 23:32:51 +01:00
|
|
|
try
|
2018-03-23 23:37:01 +01:00
|
|
|
{
|
|
|
|
boost::asio::posix::stream_descriptor fd
|
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios::get(), dup(STDIN_FILENO)
|
2018-03-23 23:37:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
boost::asio::streambuf sb
|
|
|
|
{
|
|
|
|
size(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto interruption{[&fd]
|
|
|
|
(ctx::ctx *const &interruptor) noexcept
|
|
|
|
{
|
|
|
|
fd.cancel();
|
|
|
|
}};
|
|
|
|
|
|
|
|
const size_t len
|
|
|
|
{
|
|
|
|
boost::asio::async_read_until(fd, sb, '\n', yield_context{to_asio{interruption}})
|
|
|
|
};
|
|
|
|
|
|
|
|
std::istream is{&sb};
|
|
|
|
is.get(data(buf), size(buf), '\n');
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(buf), size_t(is.gcount())
|
|
|
|
};
|
|
|
|
}
|
2018-12-11 23:32:51 +01:00
|
|
|
catch(boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
throw_system_error(e);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2018-03-23 23:37:01 +01:00
|
|
|
|
2018-06-03 18:47:42 +02:00
|
|
|
//
|
|
|
|
// tty
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::stdin::tty::tty()
|
|
|
|
:fd{[]
|
|
|
|
{
|
|
|
|
thread_local char buf[256];
|
|
|
|
syscall(::ttyname_r, STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
return fd
|
|
|
|
{
|
|
|
|
string_view{buf}, std::ios_base::out
|
|
|
|
};
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::stdin::tty::write(const string_view &buf)
|
|
|
|
{
|
|
|
|
return syscall(::write, int(*this), buf.data(), buf.size());
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2018-08-19 07:58:43 +02:00
|
|
|
// fs/fsync.h
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
ircd::fs::fsync_opts
|
2018-11-29 19:18:43 +01:00
|
|
|
const ircd::fs::fsync_opts_default;
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-12-12 19:55:56 +01:00
|
|
|
void
|
|
|
|
ircd::fs::sync(const fd &fd,
|
|
|
|
const fsync_opts &opts)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
syscall(::syncfs, fd);
|
|
|
|
#else
|
|
|
|
syscall(::sync);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
void
|
|
|
|
ircd::fs::fsync(const fd &fd,
|
|
|
|
const fsync_opts &opts)
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_AIO
|
2018-12-03 21:43:42 +01:00
|
|
|
if(aio::context && opts.async && support::aio_fsync)
|
2018-11-28 03:09:12 +01:00
|
|
|
return aio::fsync(fd, opts);
|
2018-08-19 07:58:43 +02:00
|
|
|
#endif
|
2018-11-29 19:18:43 +01:00
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
syscall(::fsync, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::fdsync(const fd &fd,
|
|
|
|
const fsync_opts &opts)
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_AIO
|
2018-12-03 21:43:42 +01:00
|
|
|
if(aio::context && opts.async && support::aio_fdsync)
|
2018-11-28 03:09:12 +01:00
|
|
|
return aio::fdsync(fd, opts);
|
2018-08-19 07:58:43 +02:00
|
|
|
#endif
|
2018-11-29 19:18:43 +01:00
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
syscall(::fdatasync, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/prefetch.h
|
|
|
|
//
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
2018-08-19 07:58:43 +02:00
|
|
|
// fs/read.h
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
ircd::fs::read_opts
|
|
|
|
const ircd::fs::read_opts_default
|
|
|
|
{};
|
|
|
|
|
2018-06-02 20:44:53 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
void
|
|
|
|
ircd::fs::prefetch(const fd &fd,
|
|
|
|
const size_t &count,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
const auto flags
|
|
|
|
{
|
|
|
|
syscall(::fcntl, fd, F_GETFL)
|
|
|
|
};
|
|
|
|
|
2018-08-19 08:08:55 +02:00
|
|
|
if(~flags & O_DIRECT)
|
|
|
|
{
|
|
|
|
syscall(::readahead, fd, opts.offset, count);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-02 20:44:53 +02:00
|
|
|
|
2018-08-19 08:08:55 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2018-11-28 03:09:12 +01:00
|
|
|
if(likely(aio::context))
|
|
|
|
aio::prefetch(fd, count, opts);
|
2018-08-19 08:08:55 +02:00
|
|
|
#endif
|
2018-06-02 20:44:53 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
ircd::fs::prefetch(const fd &fd,
|
|
|
|
const size_t &count,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
std::string
|
|
|
|
ircd::fs::read(const string_view &path,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
2018-05-30 11:13:06 +02:00
|
|
|
const fd fd
|
|
|
|
{
|
|
|
|
path
|
|
|
|
};
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-05-30 11:13:06 +02:00
|
|
|
return read(fd, opts);
|
|
|
|
}
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-05-30 11:13:06 +02:00
|
|
|
std::string
|
|
|
|
ircd::fs::read(const fd &fd,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
return string(size(fd), [&fd, &opts]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return read(fd, buf, opts);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-25 03:34:46 +02:00
|
|
|
ircd::const_buffer
|
2018-01-11 06:30:31 +01:00
|
|
|
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)
|
2018-11-29 02:26:25 +01:00
|
|
|
{
|
|
|
|
const mutable_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return mutable_buffer
|
|
|
|
{
|
|
|
|
data(buf), read(path, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::read(const fd &fd,
|
|
|
|
const mutable_buffer &buf,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
const mutable_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return mutable_buffer
|
|
|
|
{
|
|
|
|
data(buf), read(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::read(const string_view &path,
|
|
|
|
const mutable_buffers &bufs,
|
|
|
|
const read_opts &opts)
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2018-05-30 11:13:06 +02:00
|
|
|
const fd fd
|
|
|
|
{
|
|
|
|
path
|
|
|
|
};
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-11-29 02:26:25 +01:00
|
|
|
return read(fd, bufs, opts);
|
2018-05-30 11:13:06 +02:00
|
|
|
}
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-11-29 02:26:25 +01:00
|
|
|
size_t
|
2018-05-30 11:13:06 +02:00
|
|
|
ircd::fs::read(const fd &fd,
|
2018-11-29 02:26:25 +01:00
|
|
|
const mutable_buffers &bufs,
|
2018-05-30 11:13:06 +02:00
|
|
|
const read_opts &opts)
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2018-05-30 11:13:06 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2018-11-28 03:09:12 +01:00
|
|
|
if(likely(aio::context))
|
2018-11-29 02:26:25 +01:00
|
|
|
return aio::read(fd, bufs, opts);
|
2018-05-30 11:13:06 +02:00
|
|
|
#endif
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-11-29 02:26:25 +01:00
|
|
|
const auto iov(make_iov(bufs));
|
|
|
|
return size_t(syscall(::preadv, fd, iov.data(), iov.size(), opts.offset));
|
2018-05-30 11:13:06 +02:00
|
|
|
}
|
2018-01-11 06:30:31 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/write.h
|
|
|
|
//
|
|
|
|
|
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-08-24 06:24:35 +02:00
|
|
|
void
|
|
|
|
ircd::fs::allocate(const fd &fd,
|
|
|
|
const size_t &size,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-08-24 07:35:01 +02:00
|
|
|
int mode{0};
|
|
|
|
mode |= opts.keep_size? FALLOC_FL_KEEP_SIZE : 0;
|
|
|
|
syscall(::fallocate, fd, mode, opts.offset, size);
|
2018-08-24 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 06:24:19 +02:00
|
|
|
void
|
|
|
|
ircd::fs::truncate(const string_view &path,
|
|
|
|
const size_t &size,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const fd fd
|
|
|
|
{
|
|
|
|
path, std::ios::out | std::ios::trunc
|
|
|
|
};
|
|
|
|
|
|
|
|
return truncate(fd, size, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::truncate(const fd &fd,
|
|
|
|
const size_t &size,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
syscall(::ftruncate, fd, size);
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:23:42 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::overwrite(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-11-29 02:21:34 +01:00
|
|
|
const const_buffers bufs
|
2018-08-24 06:23:42 +02:00
|
|
|
{
|
2018-11-29 02:21:34 +01:00
|
|
|
&buf, 1
|
2018-08-24 06:23:42 +02:00
|
|
|
};
|
|
|
|
|
2018-11-29 02:21:34 +01:00
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), overwrite(path, bufs, opts)
|
|
|
|
};
|
2018-08-24 06:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::overwrite(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-11-29 02:21:34 +01:00
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), overwrite(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::overwrite(const string_view &path,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const fd fd
|
|
|
|
{
|
|
|
|
path, std::ios::out | std::ios::trunc
|
|
|
|
};
|
|
|
|
|
|
|
|
return overwrite(fd, bufs, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::overwrite(const fd &fd,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
return write(fd, bufs, opts);
|
2018-08-24 06:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 06:24:56 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::append(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-11-29 02:16:26 +01:00
|
|
|
const const_buffers bufs
|
2018-08-24 06:24:56 +02:00
|
|
|
{
|
2018-11-29 02:16:26 +01:00
|
|
|
&buf, 1
|
2018-08-24 06:24:56 +02:00
|
|
|
};
|
|
|
|
|
2018-11-29 02:16:26 +01:00
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), append(path, bufs, opts)
|
|
|
|
};
|
2018-08-24 06:24:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 11:40:20 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::append(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
2018-11-29 02:16:26 +01:00
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), append(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::append(const string_view &path,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const fd fd
|
|
|
|
{
|
|
|
|
path, std::ios::out | std::ios::app
|
|
|
|
};
|
|
|
|
|
|
|
|
return append(fd, bufs, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::append(const fd &fd,
|
|
|
|
const const_buffers &bufs,
|
2018-05-30 11:40:20 +02:00
|
|
|
const write_opts &opts_)
|
|
|
|
{
|
|
|
|
auto opts(opts_);
|
2018-08-24 06:24:56 +02:00
|
|
|
if(!opts.offset)
|
|
|
|
opts.offset = syscall(::lseek, fd, 0, SEEK_END);
|
|
|
|
|
2018-11-29 02:16:26 +01:00
|
|
|
return write(fd, bufs, opts);
|
2018-05-30 11:40:20 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 03:34:46 +02:00
|
|
|
ircd::const_buffer
|
2018-02-06 08:28:33 +01:00
|
|
|
ircd::fs::write(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
2018-11-29 02:10:48 +01:00
|
|
|
{
|
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), write(path, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::write(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), write(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::write(const string_view &path,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-05-30 11:24:35 +02:00
|
|
|
const fd fd
|
|
|
|
{
|
|
|
|
path, std::ios::out
|
|
|
|
};
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2018-11-29 02:10:48 +01:00
|
|
|
return write(fd, bufs, opts);
|
2018-05-30 11:24:35 +02:00
|
|
|
}
|
2017-10-02 11:58:53 +02:00
|
|
|
|
2018-11-29 02:10:48 +01:00
|
|
|
size_t
|
2018-05-30 11:24:35 +02:00
|
|
|
ircd::fs::write(const fd &fd,
|
2018-11-29 02:10:48 +01:00
|
|
|
const const_buffers &bufs,
|
2018-05-30 11:24:35 +02:00
|
|
|
const write_opts &opts)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-05-30 11:24:35 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2018-11-28 03:09:12 +01:00
|
|
|
if(likely(aio::context))
|
2018-11-29 02:10:48 +01:00
|
|
|
return aio::write(fd, bufs, opts);
|
2018-05-30 11:24:35 +02:00
|
|
|
#endif
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2018-11-29 02:10:48 +01:00
|
|
|
const auto iov(make_iov(bufs));
|
|
|
|
return size_t(syscall(::pwritev, fd, iov.data(), iov.size(), opts.offset));
|
2018-05-30 11:24:35 +02:00
|
|
|
}
|
2017-10-02 11:58:53 +02:00
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2018-11-28 03:09:12 +01:00
|
|
|
// fs/aio.h
|
|
|
|
//
|
|
|
|
|
2018-12-09 00:17:13 +01:00
|
|
|
/// Conf item to control whether AIO is enabled or bypassed.
|
|
|
|
decltype(ircd::fs::aio::enable)
|
|
|
|
ircd::fs::aio::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.enable" },
|
|
|
|
{ "default", true },
|
2018-12-09 01:15:22 +01:00
|
|
|
{ "persist", false },
|
2018-12-09 00:17:13 +01:00
|
|
|
};
|
|
|
|
|
2018-12-02 01:06:33 +01:00
|
|
|
/// Global stats structure
|
|
|
|
decltype(ircd::fs::aio::stats)
|
|
|
|
ircd::fs::aio::stats;
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
/// Non-null when aio is available for use
|
|
|
|
decltype(ircd::fs::aio::context)
|
2018-12-02 01:06:33 +01:00
|
|
|
ircd::fs::aio::context;
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
ircd::fs::aio::init::init()
|
|
|
|
{
|
|
|
|
assert(!context);
|
2018-12-09 00:17:13 +01:00
|
|
|
if(!bool(aio::enable))
|
2018-12-02 00:55:53 +01:00
|
|
|
return;
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
context = new kernel{};
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ircd::fs::aio::init::init()
|
|
|
|
{
|
|
|
|
assert(!context);
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"No support for asynchronous local filesystem IO..."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
ircd::fs::aio::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
delete context;
|
|
|
|
context = nullptr;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ircd::fs::aio::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(!context);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/fd.h
|
|
|
|
//
|
|
|
|
// TODO: x-platform
|
2018-05-30 10:38:20 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
2018-08-12 20:56:53 +02:00
|
|
|
thread_local char path_buf[PATH_MAX];
|
2018-05-30 10:38:20 +02:00
|
|
|
static const char *path_str(const string_view &);
|
2018-09-13 12:01:06 +02:00
|
|
|
static uint posix_flags(const std::ios::openmode &mode);
|
2018-05-30 10:38:20 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 00:17:13 +01:00
|
|
|
decltype(ircd::fs::fd::opts::direct_io_enable)
|
|
|
|
ircd::fs::fd::opts::direct_io_enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.fd.direct_io_enable" },
|
|
|
|
{ "default", true },
|
2018-12-09 01:15:22 +01:00
|
|
|
{ "persist", false },
|
2018-12-09 00:17:13 +01:00
|
|
|
};
|
|
|
|
|
2018-10-18 18:28:10 +02:00
|
|
|
#ifdef __linux__
|
2018-08-29 04:31:58 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::block_size(const fd &fd)
|
|
|
|
{
|
2018-10-18 18:28:10 +02:00
|
|
|
return 512UL;
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_SYS_STAT_H)
|
|
|
|
size_t
|
|
|
|
ircd::fs::block_size(const fd &fd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
syscall(::fstat, fd, &st);
|
|
|
|
return st.st_blksize;
|
2018-08-29 04:31:58 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
ircd::fs::block_size(const fd &fd)
|
|
|
|
{
|
|
|
|
return info::page_size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-21 07:44:39 +02:00
|
|
|
/// This is not a standard UUID of any sort; this is custom, and intended for
|
|
|
|
/// rocksdb (though rocksdb has no requirement for its format specifically).
|
|
|
|
/// The format is plain-text, fs major and minor number, inode number, and
|
|
|
|
/// a three letter file type; all obtained from fstat(2).
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::uuid(const fd &fd,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
syscall(::fstat, fd, &stat);
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%u-%u-%lu-%s",
|
2018-09-02 08:51:54 +02:00
|
|
|
gnu_dev_major(stat.st_dev),
|
|
|
|
gnu_dev_minor(stat.st_dev),
|
2018-08-21 07:44:39 +02:00
|
|
|
stat.st_ino,
|
|
|
|
S_ISREG(stat.st_mode)? "reg":
|
|
|
|
S_ISDIR(stat.st_mode)? "dir":
|
|
|
|
"???"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-30 11:12:19 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::size(const fd &fd)
|
|
|
|
{
|
2018-08-24 06:25:05 +02:00
|
|
|
const off_t cur
|
|
|
|
{
|
|
|
|
syscall(::lseek, fd, 0, SEEK_CUR)
|
|
|
|
};
|
|
|
|
|
|
|
|
const off_t end
|
|
|
|
{
|
|
|
|
syscall(::lseek, fd, 0, SEEK_END)
|
|
|
|
};
|
|
|
|
|
|
|
|
syscall(::lseek, fd, cur, SEEK_SET);
|
|
|
|
return end;
|
|
|
|
}
|
2018-05-30 11:12:19 +02:00
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
uint
|
2018-09-13 12:01:06 +02:00
|
|
|
ircd::fs::posix_flags(const std::ios::openmode &mode)
|
2018-05-30 10:38:20 +02:00
|
|
|
{
|
|
|
|
static const auto rdwr
|
|
|
|
{
|
|
|
|
std::ios::in | std::ios::out
|
|
|
|
};
|
|
|
|
|
|
|
|
uint ret{0};
|
|
|
|
if((mode & rdwr) == rdwr)
|
|
|
|
ret |= O_RDWR;
|
|
|
|
else if(mode & std::ios::out)
|
|
|
|
ret |= O_WRONLY;
|
|
|
|
else
|
|
|
|
ret |= O_RDONLY;
|
|
|
|
|
|
|
|
ret |= mode & std::ios::trunc? O_TRUNC : 0;
|
|
|
|
ret |= mode & std::ios::app? O_APPEND : 0;
|
|
|
|
ret |= ret & O_WRONLY? O_CREAT : 0;
|
|
|
|
ret |= ret & O_RDWR && ret & (O_TRUNC | O_APPEND)? O_CREAT : 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::fs::path_str(const string_view &s)
|
|
|
|
{
|
|
|
|
return data(strlcpy(path_buf, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fd::opts
|
|
|
|
//
|
|
|
|
|
2018-09-13 12:01:06 +02:00
|
|
|
ircd::fs::fd::opts::opts(const std::ios::openmode &mode)
|
2018-11-02 07:35:52 +01:00
|
|
|
:mode
|
|
|
|
{
|
|
|
|
mode
|
|
|
|
}
|
|
|
|
,flags
|
2018-05-30 10:38:20 +02:00
|
|
|
{
|
|
|
|
posix_flags(mode)
|
|
|
|
}
|
|
|
|
,mask
|
|
|
|
{
|
|
|
|
flags & O_CREAT?
|
|
|
|
S_IRUSR | S_IWUSR:
|
|
|
|
0U
|
|
|
|
}
|
|
|
|
,ate
|
|
|
|
{
|
|
|
|
bool(mode & std::ios::ate)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fd::fd
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::fd::fd(const string_view &path)
|
|
|
|
:fd{path, opts{}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd::fd(const string_view &path,
|
|
|
|
const opts &opts)
|
2018-05-30 13:12:27 +02:00
|
|
|
:fdno{[&path, &opts]
|
|
|
|
() -> int
|
2018-05-30 10:38:20 +02:00
|
|
|
{
|
2018-08-24 07:53:13 +02:00
|
|
|
uint flags(opts.flags);
|
|
|
|
flags |= opts.direct? O_DIRECT : 0UL;
|
|
|
|
flags |= opts.cloexec? O_CLOEXEC : 0UL;
|
2018-05-30 13:21:27 +02:00
|
|
|
flags &= opts.nocreate? ~O_CREAT : flags;
|
2018-05-30 13:12:27 +02:00
|
|
|
|
|
|
|
const mode_t &mode(opts.mask);
|
2018-08-24 07:53:13 +02:00
|
|
|
assert((flags & ~O_CREAT) || mode != 0);
|
|
|
|
|
2018-05-30 13:12:27 +02:00
|
|
|
const char *const &p(path_str(path));
|
|
|
|
return syscall(::open, p, flags, mode);
|
|
|
|
}()}
|
2018-05-30 10:38:20 +02:00
|
|
|
{
|
|
|
|
if(opts.ate)
|
|
|
|
syscall(::lseek, fdno, 0, SEEK_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd::fd(fd &&o)
|
|
|
|
noexcept
|
|
|
|
:fdno
|
|
|
|
{
|
|
|
|
std::move(o.fdno)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
o.fdno = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd &
|
|
|
|
ircd::fs::fd::operator=(fd &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~fd();
|
|
|
|
fdno = std::move(o.fdno);
|
|
|
|
o.fdno = -1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd::~fd()
|
|
|
|
noexcept(false)
|
|
|
|
{
|
|
|
|
if(fdno < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
syscall(::close, fdno);
|
|
|
|
}
|
|
|
|
|
2018-11-29 01:53:04 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/iov.h
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
thread_local std::array<struct ::iovec, IOV_MAX> _iov_;
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
ircd::fs::const_iovec_view
|
2018-11-29 02:10:17 +01:00
|
|
|
ircd::fs::make_iov(const mutable_buffers &bufs)
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
2018-12-02 00:33:30 +01:00
|
|
|
if(unlikely(bufs.size() > _iov_.size()))
|
2018-12-02 00:21:06 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
2018-12-02 00:33:30 +01:00
|
|
|
"Buffer count of %zu exceeds IOV_MAX of %zu",
|
2018-12-02 00:21:06 +01:00
|
|
|
bufs.size(),
|
2018-12-02 00:33:30 +01:00
|
|
|
_iov_.size()
|
2018-12-02 00:21:06 +01:00
|
|
|
};
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
return make_iov(iovec_view(_iov_.data(), _iov_.size()), bufs);
|
2018-11-29 01:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
ircd::fs::const_iovec_view
|
2018-11-29 02:10:17 +01:00
|
|
|
ircd::fs::make_iov(const const_buffers &bufs)
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
2018-12-02 00:33:30 +01:00
|
|
|
if(unlikely(bufs.size() > _iov_.size()))
|
2018-12-02 00:21:06 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
2018-12-02 00:33:30 +01:00
|
|
|
"Buffer count of %zu exceeds IOV_MAX of %zu",
|
2018-12-02 00:21:06 +01:00
|
|
|
bufs.size(),
|
2018-12-02 00:33:30 +01:00
|
|
|
_iov_.size()
|
2018-12-02 00:21:06 +01:00
|
|
|
};
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
return make_iov(iovec_view(_iov_.data(), _iov_.size()), bufs);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::make_iov(const iovec_view &iov,
|
|
|
|
const mutable_buffers &bufs)
|
|
|
|
{
|
2018-11-29 01:53:04 +01:00
|
|
|
size_t i(0);
|
2018-12-02 00:33:30 +01:00
|
|
|
for(; i < bufs.size() && i < iov.size(); ++i)
|
2018-11-29 01:53:04 +01:00
|
|
|
_iov_.at(i) =
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
|
|
|
buffer::data(bufs[i]), buffer::size(bufs[i])
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_iovec_view
|
|
|
|
{
|
|
|
|
iov.data(), i
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::make_iov(const iovec_view &iov,
|
|
|
|
const const_buffers &bufs)
|
|
|
|
{
|
|
|
|
size_t i(0);
|
|
|
|
for(; i < bufs.size() && i < iov.size(); ++i)
|
|
|
|
iov.at(i) =
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
|
|
|
const_cast<char *>(buffer::data(bufs[i])), buffer::size(bufs[i])
|
|
|
|
};
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
return const_iovec_view
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
2018-12-02 00:33:30 +01:00
|
|
|
iov.data(), i
|
2018-11-29 01:53:04 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-12-02 00:33:30 +01:00
|
|
|
ircd::fs::bytes(const const_iovec_view &iov)
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
|
|
|
return std::accumulate(begin(iov), end(iov), size_t(0), []
|
|
|
|
(auto ret, const auto &iov)
|
|
|
|
{
|
|
|
|
return ret += iov.iov_len;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:27:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-23 12:31:36 +02:00
|
|
|
//
|
|
|
|
// fs/error.h
|
|
|
|
//
|
|
|
|
|
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
const boost::system::error_code &ec
|
|
|
|
{
|
|
|
|
e.code()
|
|
|
|
};
|
|
|
|
|
|
|
|
return make_error_code(ec);
|
|
|
|
}
|