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
|
|
|
|
|
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-01-11 06:30:31 +01:00
|
|
|
/// Non-null when aio is available for use
|
|
|
|
decltype(ircd::fs::aioctx)
|
|
|
|
ircd::fs::aioctx
|
|
|
|
{};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{{
|
|
|
|
{ "prefix", RB_PREFIX },
|
|
|
|
{ "binary dir", RB_BIN_DIR },
|
|
|
|
{ "database", RB_DB_DIR },
|
|
|
|
{ "conf", RB_CONF_DIR },
|
|
|
|
{ "log", RB_LOG_DIR },
|
|
|
|
{ "modules", RB_MODULE_DIR },
|
|
|
|
}};
|
|
|
|
|
|
|
|
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-03-23 23:37:01 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/stdin.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::stdin::readline(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
assert(ircd::ios);
|
|
|
|
boost::asio::posix::stream_descriptor fd
|
|
|
|
{
|
|
|
|
*ircd::ios, dup(STDIN_FILENO)
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/read.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::read_opts
|
|
|
|
const ircd::fs::read_opts_default
|
|
|
|
{};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ircd::fs interface linkage
|
|
|
|
//
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(flags & O_DIRECT)
|
|
|
|
return; //TODO: AIO prefetch scheme
|
|
|
|
|
|
|
|
syscall(::readahead, fd, opts.offset, count);
|
|
|
|
}
|
|
|
|
#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-02-27 05:45:40 +01:00
|
|
|
try
|
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-05-30 11:13:06 +02:00
|
|
|
return read(fd, opts);
|
|
|
|
}
|
|
|
|
catch(const filesystem_error &)
|
|
|
|
{
|
|
|
|
throw;
|
2018-01-11 06:30:31 +01:00
|
|
|
}
|
2018-02-27 05:45:40 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
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-02-27 05:45:40 +01:00
|
|
|
try
|
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-05-30 11:13:06 +02:00
|
|
|
return read(fd, buf, opts);
|
|
|
|
}
|
|
|
|
catch(const filesystem_error &)
|
|
|
|
{
|
|
|
|
throw;
|
2018-01-11 06:30:31 +01:00
|
|
|
}
|
2018-02-27 05:45:40 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-05-30 11:13:06 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::read(const fd &fd,
|
|
|
|
const mutable_buffer &buf,
|
|
|
|
const read_opts &opts)
|
|
|
|
try
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2018-05-30 11:13:06 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
if(likely(aioctx))
|
|
|
|
return read__aio(fd, buf, opts);
|
|
|
|
#endif
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-01-15 07:18:18 +01:00
|
|
|
return
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2018-05-30 11:13:06 +02:00
|
|
|
data(buf),
|
|
|
|
size_t(syscall(::pread, fd, data(buf), size(buf), opts.offset))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const filesystem_error &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
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-05-30 11:40:20 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::append(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts_)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(opts_.offset == 0);
|
|
|
|
|
|
|
|
auto opts(opts_);
|
|
|
|
opts.offset = syscall(::lseek, fd, 0, SEEK_END);
|
|
|
|
return write(fd, buf, opts);
|
|
|
|
}
|
|
|
|
catch(const filesystem_error &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-02-27 05:45:40 +01:00
|
|
|
try
|
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-05-30 11:24:35 +02:00
|
|
|
return write(fd, buf, opts);
|
|
|
|
}
|
|
|
|
catch(const filesystem_error &)
|
|
|
|
{
|
|
|
|
throw;
|
2017-10-02 11:58:53 +02:00
|
|
|
}
|
2018-02-27 05:45:40 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2017-10-02 11:58:53 +02:00
|
|
|
|
2018-04-25 03:34:46 +02:00
|
|
|
ircd::const_buffer
|
2018-05-30 11:24:35 +02:00
|
|
|
ircd::fs::write(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
try
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-05-30 11:24:35 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
if(likely(aioctx))
|
|
|
|
return write__aio(fd, buf, opts);
|
|
|
|
#endif
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2018-05-30 11:24:35 +02:00
|
|
|
return
|
|
|
|
{
|
|
|
|
data(buf),
|
|
|
|
size_t(syscall(::pwrite, fd, data(buf), size(buf), opts.offset))
|
2018-02-06 08:28:33 +01:00
|
|
|
};
|
2018-05-30 11:24:35 +02:00
|
|
|
}
|
|
|
|
catch(const filesystem_error &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
2018-02-06 08:28:33 +01:00
|
|
|
{
|
2018-05-30 11:24:35 +02:00
|
|
|
"%s", e.what()
|
2018-02-06 08:28:33 +01:00
|
|
|
};
|
2017-10-02 11:58:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/fd.h
|
|
|
|
//
|
|
|
|
// TODO: x-platform
|
|
|
|
//
|
|
|
|
|
|
|
|
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 &);
|
|
|
|
static uint posix_flags(const std::ios::open_mode &mode);
|
|
|
|
}
|
|
|
|
|
2018-05-30 11:12:19 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::size(const fd &fd)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
syscall(::fstat, fd, &stat);
|
|
|
|
return stat.st_size;
|
|
|
|
};
|
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
uint
|
|
|
|
ircd::fs::posix_flags(const std::ios::open_mode &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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::fs::path_str(const string_view &s)
|
|
|
|
{
|
|
|
|
return data(strlcpy(path_buf, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fd::opts
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::fd::opts::opts(const std::ios::open_mode &mode)
|
|
|
|
:flags
|
|
|
|
{
|
|
|
|
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-05-30 13:12:27 +02:00
|
|
|
int flags(opts.flags);
|
|
|
|
flags |= opts.direct? O_DIRECT : 0U;
|
|
|
|
flags |= opts.cloexec? O_CLOEXEC : 0U;
|
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);
|
|
|
|
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-03-08 18:27:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs.h / misc
|
|
|
|
//
|
|
|
|
|
2018-08-12 20:56:53 +02:00
|
|
|
std::string
|
|
|
|
ircd::fs::cwd()
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-08-12 20:56:53 +02:00
|
|
|
return filesystem::current_path().string();
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 20:56:53 +02:00
|
|
|
void
|
|
|
|
ircd::fs::chdir(const string_view &path)
|
2017-08-16 21:15:23 +02:00
|
|
|
try
|
|
|
|
{
|
2018-08-12 20:56:53 +02:00
|
|
|
filesystem::current_path(fs::path(path));
|
2017-08-16 21:15:23 +02:00
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2017-08-16 21:15:23 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
2018-08-13 01:42:50 +02:00
|
|
|
"`%s': %s", path, e.what()
|
2018-02-27 05:35:09 +01:00
|
|
|
};
|
2017-08-16 21:15:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 20:56:53 +02:00
|
|
|
bool
|
|
|
|
ircd::fs::mkdir(const string_view &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-08-13 01:38:50 +02:00
|
|
|
return filesystem::create_directories(fs::path(path));
|
2018-02-27 05:35:09 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw filesystem_error
|
|
|
|
{
|
2018-08-13 01:42:50 +02:00
|
|
|
"`%s': %s", path, e.what()
|
2018-02-27 05:35:09 +01:00
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
2018-02-27 05:37:19 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::remove(fs::path(path));
|
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
2018-08-13 01:42:50 +02:00
|
|
|
"`%s': %s", path, e.what()
|
2018-02-27 05:35:09 +01:00
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 05:37:19 +01:00
|
|
|
bool
|
|
|
|
ircd::fs::remove(std::nothrow_t,
|
|
|
|
const string_view &path)
|
|
|
|
{
|
2018-03-08 18:27:32 +01:00
|
|
|
boost::system::error_code ec;
|
2018-02-27 05:37:19 +01:00
|
|
|
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 filesystem_error
|
|
|
|
{
|
2018-08-13 01:42:50 +02:00
|
|
|
"`%s' -> `%s': %s", old, new_, e.what()
|
2018-02-27 05:37:19 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::rename(std::nothrow_t,
|
|
|
|
const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
{
|
2018-03-08 18:27:32 +01:00
|
|
|
boost::system::error_code ec;
|
2018-02-27 05:37:19 +01:00
|
|
|
filesystem::rename(path(old), path(new_), ec);
|
|
|
|
return !ec;
|
|
|
|
}
|
|
|
|
|
2016-08-31 10:03:44 +02:00
|
|
|
std::vector<std::string>
|
2018-02-27 05:35:09 +01:00
|
|
|
ircd::fs::ls_recursive(const string_view &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
const filesystem::recursive_directory_iterator end;
|
|
|
|
filesystem::recursive_directory_iterator it
|
|
|
|
{
|
|
|
|
fs::path(path)
|
|
|
|
};
|
2017-08-23 23:43:25 +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;
|
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
2018-02-27 05:35:09 +01:00
|
|
|
ircd::fs::ls(const string_view &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
static const filesystem::directory_iterator end;
|
|
|
|
filesystem::directory_iterator it
|
|
|
|
{
|
|
|
|
fs::path(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;
|
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
2017-12-21 01:33:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::size(const string_view &path)
|
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
return filesystem::file_size(fs::path(path));
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-27 05:35:09 +01:00
|
|
|
ircd::fs::is_reg(const string_view &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
return filesystem::is_regular_file(fs::path(path));
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-27 05:35:09 +01:00
|
|
|
ircd::fs::is_dir(const string_view &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
return filesystem::is_directory(fs::path(path));
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-27 05:35:09 +01:00
|
|
|
ircd::fs::exists(const string_view &path)
|
2016-08-31 10:03:44 +02:00
|
|
|
try
|
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
return filesystem::exists(fs::path(path));
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
2018-02-27 05:35:09 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
2016-08-31 10:03:44 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
throw filesystem_error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
2016-08-31 10:03:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 03:44:30 +02:00
|
|
|
std::string
|
2018-05-25 04:11:05 +02:00
|
|
|
ircd::fs::make_path(const vector_view<const string_view> &list)
|
2016-09-24 03:44:30 +02:00
|
|
|
{
|
2018-02-27 05:35:09 +01:00
|
|
|
filesystem::path ret;
|
2016-09-24 03:44:30 +02:00
|
|
|
for(const auto &s : list)
|
2018-02-27 05:35:09 +01:00
|
|
|
ret /= path(s);
|
2016-09-24 03:44:30 +02:00
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
|
|
|
|
2018-02-27 05:35:09 +01:00
|
|
|
filesystem::path
|
2018-05-25 04:11:05 +02:00
|
|
|
ircd::fs::path(const vector_view<const string_view> &list)
|
2018-02-27 05:35:09 +01:00
|
|
|
{
|
|
|
|
filesystem::path ret;
|
|
|
|
for(const auto &s : list)
|
|
|
|
ret /= path(s);
|
|
|
|
|
|
|
|
return ret.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
filesystem::path
|
|
|
|
ircd::fs::path(const string_view &s)
|
|
|
|
{
|
|
|
|
return path(std::string{s});
|
|
|
|
}
|
|
|
|
|
|
|
|
filesystem::path
|
|
|
|
ircd::fs::path(std::string s)
|
|
|
|
{
|
|
|
|
return filesystem::path(std::move(s));
|
|
|
|
}
|