0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-26 00:32:35 +01:00

ircd::fs: Refactor fs::read stack for fs::fd.

This commit is contained in:
Jason Volk 2018-05-30 02:13:06 -07:00
parent a9e73d898c
commit 501e82e8aa
4 changed files with 56 additions and 97 deletions

View file

@ -16,9 +16,11 @@ namespace ircd::fs
struct read_opts extern const read_opts_default;
// Yields ircd::ctx for read into buffer; returns view of read portion.
const_buffer read(const fd &, const mutable_buffer &, const read_opts & = read_opts_default);
const_buffer read(const string_view &path, const mutable_buffer &, const read_opts & = read_opts_default);
// Yields ircd::ctx for read into allocated string; returns that string
std::string read(const fd &, const read_opts & = read_opts_default);
std::string read(const string_view &path, const read_opts & = read_opts_default);
}

View file

@ -293,68 +293,14 @@ ircd::fs::aio::request::read::read(const int &fd,
// ircd::fs interface
//
std::string
ircd::fs::read__aio(const string_view &path,
const read_opts &opts)
{
// Path to open(2) must be null terminated;
static thread_local char pathstr[2048];
strlcpy(pathstr, path, sizeof(pathstr));
uint flags{0};
flags |= O_RDONLY;
flags |= O_CLOEXEC;
const auto fd
{
syscall(::open, pathstr, flags, 0)
};
const unwind cfd{[&fd]
{
syscall(::close, fd);
}};
struct stat stat;
syscall(::fstat, fd, &stat);
return string(stat.st_size, [&fd, &opts]
(const mutable_buffer &buf)
{
aio::request::read request
{
int(fd), buf, opts
};
return request();
});
}
ircd::const_buffer
ircd::fs::read__aio(const string_view &path,
ircd::fs::read__aio(const fd &fd,
const mutable_buffer &buf,
const read_opts &opts)
{
// Path to open(2) must be null terminated;
static thread_local char pathstr[2048];
strlcpy(pathstr, path, sizeof(pathstr));
uint flags{0};
flags |= O_RDONLY;
flags |= O_CLOEXEC;
const auto fd
{
syscall(::open, pathstr, flags, 0)
};
const unwind cfd{[&fd]
{
syscall(::close, fd);
}};
aio::request::read request
{
int(fd), buf, opts
fd, buf, opts
};
const size_t bytes

View file

@ -75,8 +75,7 @@ struct ircd::fs::aio::request
namespace ircd::fs
{
const_buffer write__aio(const string_view &path, const const_buffer &, const write_opts &);
const_buffer read__aio(const string_view &path, const mutable_buffer &, const read_opts &);
std::string read__aio(const string_view &path, const read_opts &);
const_buffer read__aio(const fd &, const mutable_buffer &, const read_opts &);
}
/// Read request control block

View file

@ -114,12 +114,6 @@ ircd::fs::stdin::readline(const mutable_buffer &buf)
// fs/read.h
//
namespace ircd::fs
{
const_buffer read__std(const string_view &path, const mutable_buffer &, const read_opts &);
std::string read__std(const string_view &path, const read_opts &);
}
ircd::fs::read_opts
const ircd::fs::read_opts_default
{};
@ -133,12 +127,16 @@ ircd::fs::read(const string_view &path,
const read_opts &opts)
try
{
#ifdef IRCD_USE_AIO
if(likely(aioctx))
return read__aio(path, opts);
#endif
const fd fd
{
path
};
return read__std(path, opts);
return read(fd, opts);
}
catch(const filesystem_error &)
{
throw;
}
catch(const std::exception &e)
{
@ -148,18 +146,33 @@ catch(const std::exception &e)
};
}
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);
});
}
ircd::const_buffer
ircd::fs::read(const string_view &path,
const mutable_buffer &buf,
const read_opts &opts)
try
{
#ifdef IRCD_USE_AIO
if(likely(aioctx))
return read__aio(path, buf, opts);
#endif
const fd fd
{
path
};
return read__std(path, buf, opts);
return read(fd, buf, opts);
}
catch(const filesystem_error &)
{
throw;
}
catch(const std::exception &e)
{
@ -169,33 +182,32 @@ catch(const std::exception &e)
};
}
//
// std read
//
std::string
ircd::fs::read__std(const string_view &path,
const read_opts &opts)
{
std::ifstream file{std::string{path}};
std::noskipws(file);
std::istream_iterator<char> b{file};
std::istream_iterator<char> e{};
return std::string{b, e};
}
ircd::const_buffer
ircd::fs::read__std(const string_view &path,
const mutable_buffer &buf,
const read_opts &opts)
ircd::fs::read(const fd &fd,
const mutable_buffer &buf,
const read_opts &opts)
try
{
std::ifstream file{std::string{path}};
file.exceptions(file.failbit | file.badbit);
file.seekg(opts.offset, file.beg);
file.read(data(buf), size(buf));
#ifdef IRCD_USE_AIO
if(likely(aioctx))
return read__aio(fd, buf, opts);
#endif
return
{
data(buf), size_t(file.gcount())
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()
};
}