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
|
2019-01-02 03:35:09 +01:00
|
|
|
#include <RB_INC_SYS_STATFS_H
|
|
|
|
#include <RB_INC_SYS_STATVFS_H
|
2018-02-06 06:23:34 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
2019-02-16 02:25:19 +01:00
|
|
|
#include <RB_INC_SYS_SYSMACROS_H
|
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
|
2019-01-23 22:20:38 +01:00
|
|
|
#include "fs_aio.h"
|
2018-01-11 06:30:31 +01:00
|
|
|
#endif
|
|
|
|
|
2017-11-26 20:56:08 +01:00
|
|
|
namespace ircd::fs
|
2016-08-14 05:35:06 +02:00
|
|
|
{
|
2018-12-30 00:27:58 +01:00
|
|
|
static uint posix_flags(const std::ios::openmode &mode);
|
|
|
|
static const char *path_str(const string_view &);
|
|
|
|
static void debug_paths();
|
2017-11-26 20:56:08 +01:00
|
|
|
}
|
|
|
|
|
2018-08-12 20:56:53 +02:00
|
|
|
//
|
2018-12-30 00:27:58 +01:00
|
|
|
// init
|
2018-08-12 20:56:53 +02:00
|
|
|
//
|
|
|
|
|
2018-12-30 00:27:58 +01:00
|
|
|
ircd::fs::init::init()
|
|
|
|
:_aio_{}
|
2018-08-12 20:56:53 +02:00
|
|
|
{
|
2018-12-30 00:27:58 +01:00
|
|
|
debug_paths();
|
2018-08-12 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:27:58 +01:00
|
|
|
ircd::fs::init::~init()
|
|
|
|
noexcept
|
2018-08-12 20:56:53 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs.h / misc
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::mkdir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::create_directories(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::remove(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(std::nothrow_t,
|
|
|
|
const string_view &path)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::remove(_path(path), ec);
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:27:58 +01:00
|
|
|
bool
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::rename(const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
filesystem::rename(_path(old), _path(new_));
|
2018-12-30 00:27:58 +01:00
|
|
|
return true;
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::rename(std::nothrow_t,
|
|
|
|
const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
2019-01-25 19:35:39 +01:00
|
|
|
filesystem::rename(_path(old), _path(new_), ec);
|
2018-11-28 03:09:12 +01:00
|
|
|
return !ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
2018-12-30 00:27:58 +01:00
|
|
|
ircd::fs::ls_r(const string_view &path)
|
2018-11-28 03:09:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const filesystem::recursive_directory_iterator end;
|
|
|
|
filesystem::recursive_directory_iterator it
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
_path(path)
|
2018-11-28 03:09:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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-16 05:12:39 +01:00
|
|
|
throw error{e};
|
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
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
_path(path)
|
2018-11-28 03:09:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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-16 05:12:39 +01:00
|
|
|
throw error{e};
|
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
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::file_size(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
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
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::is_regular_file(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::is_dir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::is_directory(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::exists(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::exists(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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]
|
2018-12-23 01:21:27 +01:00
|
|
|
(ctx::ctx *const &interruptor)
|
2018-03-23 23:37:01 +01:00
|
|
|
{
|
|
|
|
fd.cancel();
|
|
|
|
}};
|
|
|
|
|
2018-12-23 05:55:52 +01:00
|
|
|
size_t len; continuation
|
2018-03-23 23:37:01 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
continuation::asio_predicate, interruption, [&len, &fd, &sb]
|
|
|
|
(auto &yield)
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
len = boost::asio::async_read_until(fd, sb, '\n', yield);
|
|
|
|
}
|
2018-03-23 23:37:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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-12-14 01:54:09 +01:00
|
|
|
// fs/sync.h
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
|
2018-12-14 01:54:09 +01:00
|
|
|
ircd::fs::sync_opts
|
|
|
|
const ircd::fs::sync_opts_default;
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-12-14 03:03:17 +01:00
|
|
|
void
|
|
|
|
ircd::fs::sync(const fd &fd,
|
|
|
|
const off_t &offset,
|
|
|
|
const size_t &length,
|
|
|
|
const sync_opts &opts)
|
|
|
|
{
|
|
|
|
return sync(fd, opts);
|
|
|
|
}
|
|
|
|
|
2018-12-12 19:55:56 +01:00
|
|
|
void
|
|
|
|
ircd::fs::sync(const fd &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-12-12 19:55:56 +01:00
|
|
|
{
|
2018-12-18 01:02:05 +01:00
|
|
|
const ctx::slice_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::sync(fd:%d)", int(fd)
|
|
|
|
};
|
|
|
|
|
2018-12-12 19:55:56 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
syscall(::syncfs, fd);
|
|
|
|
#else
|
|
|
|
syscall(::sync);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-14 03:03:17 +01:00
|
|
|
void
|
|
|
|
ircd::fs::flush(const fd &fd,
|
|
|
|
const off_t &offset,
|
|
|
|
const size_t &length,
|
|
|
|
const sync_opts &opts)
|
|
|
|
{
|
|
|
|
return flush(fd, opts);
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
void
|
2018-12-14 02:46:08 +01:00
|
|
|
ircd::fs::flush(const fd &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-08-19 07:58:43 +02:00
|
|
|
{
|
2018-12-18 01:02:05 +01:00
|
|
|
const ctx::slice_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::flush(fd:%d, {metadata:%b aio:%b:%b})",
|
|
|
|
int(fd),
|
|
|
|
opts.metadata,
|
|
|
|
opts.aio,
|
2018-12-29 00:02:02 +01:00
|
|
|
opts.metadata? aio::support_fdsync : aio::support_fsync
|
2018-12-18 01:02:05 +01:00
|
|
|
};
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2018-12-29 00:02:02 +01:00
|
|
|
if(aio::system && opts.aio)
|
2018-12-14 02:46:08 +01:00
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
if(!opts.metadata && aio::support_fdsync)
|
2018-12-14 02:46:08 +01:00
|
|
|
return aio::fdsync(fd, opts);
|
2018-08-19 07:58:43 +02:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
if(aio::support_fsync)
|
2018-12-14 02:46:08 +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-12-14 02:46:08 +01:00
|
|
|
if(!opts.metadata)
|
|
|
|
return void(syscall(::fdatasync, fd));
|
|
|
|
|
2018-12-18 01:02:05 +01:00
|
|
|
return void(syscall(::fsync, fd));
|
2018-08-19 07:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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-12-18 22:41:45 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
static size_t read(const fd &, const const_iovec_view &, const read_opts &);
|
|
|
|
}
|
|
|
|
|
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-12-29 00:02:02 +01:00
|
|
|
if(likely(aio::system) && opts.aio)
|
2018-11-28 03:09:12 +01:00
|
|
|
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-12-19 21:24:16 +01:00
|
|
|
/// Read from file descriptor fd into buffers. The number of bytes read into
|
|
|
|
/// the buffers is returned. By default (via read_opts.all) this call will
|
|
|
|
/// loop internally until the buffers are full or EOF. To allow for a partial
|
|
|
|
/// read(), disable read_opts.all. Note that to maintain alignments (i.e when
|
|
|
|
/// direct-io or for special files read_opts.all must be false). By default
|
|
|
|
/// (via read_opts.interruptible) this call can throw if the syscall was
|
|
|
|
/// interrupted before reading any bytes.
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2018-11-29 02:26:25 +01:00
|
|
|
size_t
|
2018-12-21 22:19:34 +01:00
|
|
|
__attribute__((stack_protect))
|
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-12-18 22:41:45 +01:00
|
|
|
const read_opts &opts_)
|
|
|
|
{
|
2018-12-21 22:19:34 +01:00
|
|
|
if(unlikely(bufs.size() > info::iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
|
|
|
"Buffer count of %zu exceeds IOV_MAX of %zu",
|
|
|
|
bufs.size(),
|
|
|
|
info::iov_max
|
|
|
|
};
|
|
|
|
|
2018-12-18 22:41:45 +01:00
|
|
|
size_t ret(0);
|
2018-12-21 19:28:51 +01:00
|
|
|
read_opts opts(opts_);
|
2018-12-21 22:19:34 +01:00
|
|
|
assert(bufs.size() <= info::iov_max);
|
2018-12-21 19:28:51 +01:00
|
|
|
struct ::iovec iovbuf[bufs.size()]; do
|
2018-12-18 22:41:45 +01:00
|
|
|
{
|
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
const size_t off(opts.offset - opts_.offset);
|
|
|
|
assert(off <= buffers::size(bufs));
|
|
|
|
assert(ret <= buffers::size(bufs));
|
2018-12-21 22:19:34 +01:00
|
|
|
const auto iov
|
|
|
|
{
|
|
|
|
make_iov({iovbuf, bufs.size()}, bufs, ret)
|
|
|
|
};
|
|
|
|
|
2018-12-18 22:41:45 +01:00
|
|
|
ret += read(fd, iov, opts);
|
|
|
|
if(!opts_.all)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(off >= ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
opts.offset = opts_.offset + ret;
|
|
|
|
}
|
|
|
|
while(ret < buffers::size(bufs));
|
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
assert(ret <= buffers::size(bufs));
|
|
|
|
return ret;
|
|
|
|
}
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2018-12-18 22:41:45 +01:00
|
|
|
|
2018-12-19 21:24:16 +01:00
|
|
|
/// Lowest-level read() call. This call only conducts a single operation
|
|
|
|
/// (no looping) and can return a partial read(). It does have branches
|
|
|
|
/// for various read_opts. The arguments involve `struct ::iovec` which
|
|
|
|
/// we do not expose to the ircd.h API; thus this function is internal to
|
|
|
|
/// ircd::fs. There is no reason to use this function in lieu of the public
|
|
|
|
/// fs::read() suite.
|
2018-12-18 22:41:45 +01:00
|
|
|
size_t
|
|
|
|
ircd::fs::read(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
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-12-29 00:02:02 +01:00
|
|
|
if(aio::system && opts.aio)
|
2018-12-18 22:41:45 +01:00
|
|
|
return aio::read(fd, iov, opts);
|
2018-05-30 11:13:06 +02:00
|
|
|
#endif
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-12-18 23:17:38 +01:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
opts.interruptible?
|
|
|
|
syscall(::preadv, fd, iov.data(), iov.size(), opts.offset):
|
|
|
|
syscall_nointr(::preadv, fd, iov.data(), iov.size(), opts.offset)
|
|
|
|
};
|
|
|
|
|
|
|
|
return size_t(ret);
|
2018-05-30 11:13:06 +02:00
|
|
|
}
|
2018-01-11 06:30:31 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/write.h
|
|
|
|
//
|
|
|
|
|
2018-12-18 23:01:05 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
static size_t write(const fd &, const const_iovec_view &, const write_opts &);
|
|
|
|
}
|
|
|
|
|
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-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2018-11-29 02:10:48 +01:00
|
|
|
size_t
|
2018-12-21 22:19:34 +01:00
|
|
|
__attribute__((stack_protect))
|
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-12-18 23:01:05 +01:00
|
|
|
const write_opts &opts_)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-12-21 22:19:34 +01:00
|
|
|
if(unlikely(bufs.size() > info::iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
|
|
|
"Buffer count of %zu exceeds IOV_MAX of %zu",
|
|
|
|
bufs.size(),
|
|
|
|
info::iov_max
|
|
|
|
};
|
|
|
|
|
2018-12-18 23:01:05 +01:00
|
|
|
write_opts opts(opts_);
|
2018-12-21 19:28:51 +01:00
|
|
|
size_t off(opts.offset - opts_.offset);
|
2018-12-21 22:19:34 +01:00
|
|
|
assert(bufs.size() <= info::iov_max);
|
2018-12-21 19:28:51 +01:00
|
|
|
struct ::iovec iovbuf[bufs.size()]; do
|
2018-12-18 23:01:05 +01:00
|
|
|
{
|
2018-12-21 22:19:34 +01:00
|
|
|
const auto iov
|
|
|
|
{
|
|
|
|
make_iov({iovbuf, bufs.size()}, bufs, off)
|
|
|
|
};
|
|
|
|
|
2018-12-18 23:01:05 +01:00
|
|
|
opts.offset += write(fd, iov, opts);
|
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
off = opts.offset - opts_.offset;
|
|
|
|
assert(off <= buffers::size(bufs));
|
|
|
|
}
|
|
|
|
while(opts.all && off < buffers::size(bufs));
|
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
assert(ssize_t(off) == opts.offset - opts_.offset);
|
|
|
|
assert(!opts.all || off == buffers::size(bufs));
|
|
|
|
return off;
|
|
|
|
}
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2018-12-18 22:40:46 +01:00
|
|
|
|
2018-12-19 21:24:16 +01:00
|
|
|
/// Lowest-level write() call. This call only conducts a single operation
|
|
|
|
/// (no looping) and can return early with a partial write(). It does have
|
|
|
|
/// branches for various write_opts. The arguments involve `struct ::iovec`
|
|
|
|
/// which we do not expose to the ircd.h API; thus this function is internal to
|
|
|
|
/// ircd::fs. There is no reason to use this function in lieu of the public
|
|
|
|
/// fs::read() suite.
|
2018-12-18 23:01:05 +01:00
|
|
|
size_t
|
|
|
|
ircd::fs::write(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-05-30 11:24:35 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2018-12-29 00:02:02 +01:00
|
|
|
if(likely(aio::system) && opts.aio)
|
2018-12-18 22:40:46 +01:00
|
|
|
return aio::write(fd, iov, opts);
|
2018-05-30 11:24:35 +02:00
|
|
|
#endif
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2018-12-18 23:17:38 +01:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
opts.interruptible?
|
|
|
|
syscall(::pwritev, fd, iov.data(), iov.size(), opts.offset):
|
|
|
|
syscall_nointr(::pwritev, fd, iov.data(), iov.size(), opts.offset)
|
|
|
|
};
|
|
|
|
|
|
|
|
return size_t(ret);
|
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-21 21:58:53 +01:00
|
|
|
//
|
|
|
|
// These symbols can be overriden by ircd/aio.cc if it is compiled and linked;
|
|
|
|
// otherwise on non-supporting platforms these will be the defaults here.
|
|
|
|
//
|
2018-12-21 21:09:44 +01:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::support)
|
2018-12-21 21:58:53 +01:00
|
|
|
extern __attribute__((weak))
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::support;
|
2018-12-21 21:09:44 +01:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::support_fsync)
|
2018-12-21 21:58:53 +01:00
|
|
|
extern __attribute__((weak))
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::support_fsync;
|
2018-12-21 21:58:53 +01:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::support_fdsync)
|
2018-12-21 21:58:53 +01:00
|
|
|
extern __attribute__((weak))
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::support_fdsync;
|
2018-12-21 21:58:53 +01:00
|
|
|
|
|
|
|
decltype(ircd::fs::aio::MAX_REQPRIO)
|
|
|
|
extern __attribute__((weak))
|
|
|
|
ircd::fs::aio::MAX_REQPRIO;
|
|
|
|
|
|
|
|
decltype(ircd::fs::aio::MAX_EVENTS)
|
|
|
|
extern __attribute__((weak))
|
|
|
|
ircd::fs::aio::MAX_EVENTS;
|
2018-12-21 21:09:44 +01:00
|
|
|
|
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-21 21:58:53 +01:00
|
|
|
decltype(ircd::fs::aio::max_events)
|
|
|
|
ircd::fs::aio::max_events
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max_events" },
|
|
|
|
{ "default", long(aio::MAX_EVENTS) },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fs::aio::max_submit)
|
|
|
|
ircd::fs::aio::max_submit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max_submit" },
|
2018-12-28 00:05:21 +01:00
|
|
|
{ "default", 32L },
|
2018-12-21 21:58:53 +01:00
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
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
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::system)
|
|
|
|
ircd::fs::aio::system;
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2018-12-27 00:49:08 +01:00
|
|
|
#ifndef IRCD_USE_AIO
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::init::init()
|
|
|
|
{
|
|
|
|
assert(!context);
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"No support for asynchronous local filesystem IO..."
|
|
|
|
};
|
|
|
|
}
|
2018-12-27 00:49:08 +01:00
|
|
|
#endif
|
2018-11-28 03:09:12 +01:00
|
|
|
|
2018-12-27 00:49:08 +01:00
|
|
|
#ifndef IRCD_USE_AIO
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(!context);
|
|
|
|
}
|
2018-12-27 00:49:08 +01:00
|
|
|
#endif
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/fd.h
|
|
|
|
//
|
2018-05-30 10:38:20 +02:00
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
static long pathconf(const fd &, const int &arg);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
ulong
|
|
|
|
ircd::fs::device(const fd &fd)
|
|
|
|
{
|
|
|
|
struct stat st{0};
|
|
|
|
syscall(::fstat, fd, &st);
|
|
|
|
return st.st_dev;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ulong
|
|
|
|
ircd::fs::device(const fd &fd)
|
|
|
|
{
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
0, "Please implement this definition"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STATFS_H
|
|
|
|
ulong
|
2019-01-25 19:35:39 +01:00
|
|
|
ircd::fs::fstype(const fd &fd)
|
2019-01-02 03:35:09 +01:00
|
|
|
{
|
|
|
|
struct statfs f{0};
|
|
|
|
syscall(::fstatfs, fd, &f);
|
|
|
|
return f.f_type;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ulong
|
2019-01-25 19:35:39 +01:00
|
|
|
ircd::fs::fstype(const fd &fd)
|
2019-01-02 03:35:09 +01:00
|
|
|
{
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
0, "Please implement this definition"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
long
|
|
|
|
ircd::fs::pathconf(const fd &fd,
|
|
|
|
const int &arg)
|
2018-08-21 07:44:39 +02:00
|
|
|
{
|
2019-01-02 03:35:09 +01:00
|
|
|
return syscall(::fpathconf, fd, arg);
|
2018-08-21 07:44:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
|
|
|
// 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
|
|
|
{
|
2019-02-12 19:50:36 +01:00
|
|
|
const ctx::slice_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::fs::fd(): open(2): %s", path
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/device.h
|
|
|
|
//
|
|
|
|
|
2019-01-02 04:11:27 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs(const mutable_buffer &out,
|
|
|
|
const ulong &id,
|
|
|
|
const string_view &relpath)
|
|
|
|
{
|
|
|
|
const string_view path{fmt::sprintf
|
|
|
|
{
|
2019-02-08 03:46:41 +01:00
|
|
|
path_scratch, "/sys/dev/block/%s/%s",
|
|
|
|
sysfs_id(name_scratch, id),
|
2019-01-02 04:11:27 +01:00
|
|
|
relpath
|
|
|
|
}};
|
|
|
|
|
|
|
|
fs::read_opts opts;
|
|
|
|
opts.aio = false;
|
|
|
|
return fs::read(path, out, opts);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs(const mutable_buffer &out,
|
|
|
|
const ulong &id,
|
|
|
|
const string_view &relpath)
|
|
|
|
{
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2019-01-02 04:11:27 +01:00
|
|
|
{
|
|
|
|
"sysfs(5) is not available."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs_id(const mutable_buffer &out,
|
|
|
|
const ulong &id)
|
|
|
|
{
|
|
|
|
return sysfs_id(out, dev::id(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs_id(const mutable_buffer &out,
|
|
|
|
const major_minor &id)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "%lu:%lu", id.first, id.second
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong
|
|
|
|
ircd::fs::dev::id(const major_minor &id)
|
|
|
|
{
|
|
|
|
return gnu_dev_makedev(id.first, id.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::dev::major_minor
|
|
|
|
ircd::fs::dev::id(const ulong &id)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
gnu_dev_major(id), gnu_dev_minor(id)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-30 05:02:22 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/opts.h
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::fs::opts_default)
|
|
|
|
ircd::fs::opts_default
|
|
|
|
{};
|
|
|
|
|
2018-11-29 01:53:04 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/iov.h
|
|
|
|
//
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::make_iov(const iovec_view &iov,
|
2018-12-18 21:28:28 +01:00
|
|
|
const mutable_buffers &bufs,
|
|
|
|
const size_t &offset)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
const size_t max
|
|
|
|
{
|
|
|
|
std::min(iov.size(), bufs.size())
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0), off(0);
|
|
|
|
for(; i < max; off += size(bufs[i++]))
|
|
|
|
if(size(bufs[i]) > offset - off)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
off = offset - off;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-02 00:33:30 +01:00
|
|
|
|
2018-12-18 21:28:28 +01:00
|
|
|
if(i < max)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
assert(off <= size(bufs[i]));
|
|
|
|
iov.at(i) =
|
|
|
|
{
|
|
|
|
data(bufs[i]) + off, size(bufs[i]) - off
|
|
|
|
};
|
|
|
|
|
|
|
|
for(++i; i < max; ++i)
|
|
|
|
iov.at(i) =
|
|
|
|
{
|
|
|
|
data(bufs[i]), size(bufs[i])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const const_iovec_view ret{iov.data(), i};
|
|
|
|
assert(bytes(ret) <= buffer::buffers::size(bufs));
|
|
|
|
return ret;
|
2018-12-02 00:33:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::make_iov(const iovec_view &iov,
|
2018-12-18 21:28:28 +01:00
|
|
|
const const_buffers &bufs,
|
|
|
|
const size_t &offset)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
const size_t max
|
|
|
|
{
|
|
|
|
std::min(iov.size(), bufs.size())
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0), off(0);
|
|
|
|
for(; i < max; off += size(bufs[i++]))
|
|
|
|
if(size(bufs[i]) > offset - off)
|
|
|
|
{
|
|
|
|
off = offset - off;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(i < max)
|
|
|
|
{
|
|
|
|
assert(off <= size(bufs[i]));
|
2018-12-02 00:33:30 +01:00
|
|
|
iov.at(i) =
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
const_cast<char *>(data(bufs[i])) + off, size(bufs[i]) - off
|
2018-11-29 01:53:04 +01:00
|
|
|
};
|
|
|
|
|
2018-12-18 21:28:28 +01:00
|
|
|
for(++i; i < max; ++i)
|
|
|
|
iov.at(i) =
|
|
|
|
{
|
|
|
|
const_cast<char *>(data(bufs[i])), size(bufs[i])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const const_iovec_view ret{iov.data(), i};
|
|
|
|
assert(bytes(ret) <= buffer::buffers::size(bufs));
|
|
|
|
return ret;
|
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-12-30 00:27:58 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/path.h
|
|
|
|
//
|
|
|
|
|
2019-02-08 03:46:41 +01:00
|
|
|
/// Default maximum path string length (for all filesystems & platforms).
|
|
|
|
decltype(ircd::fs::NAME_MAX_LEN)
|
|
|
|
ircd::fs::NAME_MAX_LEN
|
|
|
|
{
|
|
|
|
#ifdef NAME_MAX
|
|
|
|
NAME_MAX
|
|
|
|
#elif defined(_POSIX_NAME_MAX)
|
|
|
|
_POSIX_NAME_MAX
|
|
|
|
#else
|
|
|
|
255
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Default maximum path string length (for all filesystems & platforms).
|
|
|
|
decltype(ircd::fs::PATH_MAX_LEN)
|
|
|
|
ircd::fs::PATH_MAX_LEN
|
|
|
|
{
|
|
|
|
#ifdef PATH_MAX
|
|
|
|
PATH_MAX
|
|
|
|
#elif defined(_POSIX_PATH_MAX)
|
|
|
|
_POSIX_PATH_MAX
|
|
|
|
#else
|
|
|
|
4096
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
thread_local char _path_scratch[PATH_MAX_LEN];
|
|
|
|
thread_local char _name_scratch[NAME_MAX_LEN];
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::fs::path_scratch)
|
|
|
|
ircd::fs::path_scratch
|
|
|
|
{
|
|
|
|
_path_scratch
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fs::name_scratch)
|
|
|
|
ircd::fs::name_scratch
|
|
|
|
{
|
|
|
|
_name_scratch
|
|
|
|
};
|
|
|
|
|
2018-12-30 00:27:58 +01:00
|
|
|
std::string
|
|
|
|
ircd::fs::cwd()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &cur
|
|
|
|
{
|
|
|
|
filesystem::current_path()
|
|
|
|
};
|
|
|
|
|
|
|
|
return cur.string();
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::cwd(const mutable_buffer &buf)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &cur
|
|
|
|
{
|
|
|
|
filesystem::current_path()
|
|
|
|
};
|
|
|
|
|
|
|
|
return strlcpy(buf, cur.native());
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
#ifdef _PC_PATH_MAX
|
|
|
|
size_t
|
|
|
|
ircd::fs::path_max_len(const string_view &path)
|
|
|
|
{
|
|
|
|
return pathconf(path, _PC_PATH_MAX);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
ircd::fs::path_max_len(const string_view &path)
|
|
|
|
{
|
|
|
|
return PATH_MAX_LEN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _PC_NAME_MAX
|
|
|
|
size_t
|
|
|
|
ircd::fs::name_max_len(const string_view &path)
|
|
|
|
{
|
|
|
|
return pathconf(path, _PC_NAME_MAX);
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_SYS_STATFS_H)
|
|
|
|
size_t
|
|
|
|
ircd::fs::name_max_len(const string_view &path)
|
|
|
|
{
|
|
|
|
struct statfs f{0};
|
|
|
|
syscall(::statfs, path_str(path), &f);
|
|
|
|
return f.f_namelen;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
ircd::fs::name_max_len(const string_view &path)
|
|
|
|
{
|
|
|
|
return NAME_MAX_LEN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
long
|
|
|
|
ircd::fs::pathconf(const string_view &path,
|
|
|
|
const int &arg)
|
|
|
|
{
|
|
|
|
return syscall(::pathconf, path_str(path), arg);
|
|
|
|
}
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::filename(const mutable_buffer &buf,
|
|
|
|
const string_view &p)
|
|
|
|
{
|
|
|
|
return path(buf, _path(p).filename());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::extension(const mutable_buffer &buf,
|
|
|
|
const string_view &p)
|
|
|
|
{
|
|
|
|
return path(buf, _path(p).extension());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::extension(const mutable_buffer &buf,
|
|
|
|
const string_view &p,
|
|
|
|
const string_view &replace)
|
|
|
|
{
|
|
|
|
return path(buf, _path(p).replace_extension(_path(replace)));
|
|
|
|
}
|
|
|
|
|
2019-02-08 05:22:55 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::relative(const mutable_buffer &buf,
|
|
|
|
const string_view &root,
|
|
|
|
const string_view &p)
|
|
|
|
{
|
|
|
|
return path(buf, relative(_path(p), _path(root)));
|
|
|
|
}
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
bool
|
|
|
|
ircd::fs::is_relative(const string_view &p)
|
|
|
|
{
|
|
|
|
return _path(p).is_relative();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::is_absolute(const string_view &p)
|
|
|
|
{
|
|
|
|
return _path(p).is_absolute();
|
|
|
|
}
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
//
|
|
|
|
// fs::path()
|
|
|
|
//
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::path(const mutable_buffer &buf,
|
|
|
|
const filesystem::path &path)
|
2019-01-25 19:35:39 +01:00
|
|
|
{
|
2019-01-25 20:26:35 +01:00
|
|
|
return strlcpy(buf, path.c_str());
|
2019-01-25 19:35:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::path(const mutable_buffer &buf,
|
|
|
|
const vector_view<const std::string> &list)
|
2019-01-25 19:35:39 +01:00
|
|
|
{
|
2019-01-25 20:26:35 +01:00
|
|
|
return strlcpy(buf, _path(list).c_str());
|
2019-01-25 19:35:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::path(const mutable_buffer &buf,
|
|
|
|
const vector_view<const string_view> &list)
|
2019-01-25 19:35:39 +01:00
|
|
|
{
|
2019-01-25 20:26:35 +01:00
|
|
|
return strlcpy(buf, _path(list).c_str());
|
2019-01-25 19:35:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::path(const mutable_buffer &buf,
|
|
|
|
const base &base,
|
2019-01-25 19:35:39 +01:00
|
|
|
const string_view &rest)
|
|
|
|
{
|
|
|
|
const auto p
|
|
|
|
{
|
|
|
|
_path(std::initializer_list<const string_view>
|
|
|
|
{
|
|
|
|
path(base),
|
|
|
|
rest,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-01-25 20:26:35 +01:00
|
|
|
return strlcpy(buf, p.c_str());
|
2019-01-25 19:35:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::path(const base &base)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return basepath::get(base).path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fs::_path()
|
|
|
|
//
|
|
|
|
|
|
|
|
boost::filesystem::path
|
|
|
|
ircd::fs::_path(const vector_view<const std::string> &list)
|
2018-12-30 00:27:58 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
filesystem::path ret;
|
|
|
|
for(const auto &s : list)
|
2019-01-25 19:35:39 +01:00
|
|
|
ret /= s;
|
2018-12-30 00:27:58 +01:00
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
boost::filesystem::path
|
|
|
|
ircd::fs::_path(const vector_view<const string_view> &list)
|
2018-12-30 00:27:58 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
filesystem::path ret;
|
|
|
|
for(const auto &s : list)
|
2019-01-25 19:35:39 +01:00
|
|
|
ret /= _path(s);
|
2018-12-30 00:27:58 +01:00
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
boost::filesystem::path
|
|
|
|
ircd::fs::_path(const string_view &s)
|
2018-12-30 00:27:58 +01:00
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return _path(std::string{s});
|
2018-12-30 00:27:58 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
boost::filesystem::path
|
|
|
|
ircd::fs::_path(std::string s)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::path{std::move(s)};
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fs::basepath
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
2018-12-30 00:27:58 +01:00
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
extern const std::array<basepath, num_of<base>()> basepaths;
|
2018-12-30 00:27:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
decltype(ircd::fs::basepaths)
|
|
|
|
ircd::fs::basepaths
|
|
|
|
{{
|
|
|
|
{ "installation prefix", RB_PREFIX },
|
|
|
|
{ "binary directory", RB_BIN_DIR },
|
|
|
|
{ "configuration directory", RB_CONF_DIR },
|
|
|
|
{ "data directory", RB_DATA_DIR },
|
|
|
|
{ "database directory", RB_DB_DIR },
|
|
|
|
{ "log directory", RB_LOG_DIR },
|
|
|
|
{ "module directory", RB_MODULE_DIR },
|
|
|
|
}};
|
|
|
|
|
2018-12-30 00:27:58 +01:00
|
|
|
const ircd::fs::basepath &
|
2019-01-25 19:35:39 +01:00
|
|
|
ircd::fs::basepath::get(const base &base)
|
2018-12-30 00:27:58 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return basepaths.at(base);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2018-12-16 05:12:39 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// error::error
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::error::error(const boost::filesystem::filesystem_error &code)
|
|
|
|
:std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(code)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const string_view &msg
|
|
|
|
{
|
|
|
|
// Strip this prefix off the message to simplify it for our user.
|
|
|
|
lstrip(code.what(), "boost::filesystem::")
|
|
|
|
};
|
|
|
|
|
|
|
|
copy(this->buf, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::error::error(const std::system_error &code)
|
|
|
|
:std::system_error{code}
|
|
|
|
{
|
|
|
|
string(this->buf, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::error::error(const std::error_code &code)
|
|
|
|
:std::system_error{code}
|
|
|
|
{
|
|
|
|
string(this->buf, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::fs::error::what()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return this->ircd::error::what();
|
|
|
|
}
|
2018-12-30 00:27:58 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Internal utils
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::debug_paths()
|
|
|
|
{
|
2019-01-02 03:35:09 +01:00
|
|
|
thread_local char buf[PATH_MAX_LEN + 1];
|
2018-12-30 00:27:58 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Current working directory: `%s'", cwd(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
for_each<base>([](const base &base)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Working %s is `%s'",
|
2019-01-25 19:35:39 +01:00
|
|
|
basepath::get(base).name,
|
|
|
|
basepath::get(base).path,
|
2018-12-30 00:27:58 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::fs::path_str(const string_view &s)
|
|
|
|
{
|
2019-01-02 03:35:09 +01:00
|
|
|
thread_local char buf[PATH_MAX_LEN + 1];
|
2018-12-30 00:27:58 +01:00
|
|
|
return data(strlcpy(buf, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
|
|
|
ircd::fs::posix_flags(const std::ios::openmode &mode)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|