0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

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

This commit is contained in:
Jason Volk 2018-05-30 02:24:35 -07:00
parent 501e82e8aa
commit ead2a567ad
4 changed files with 48 additions and 74 deletions

View file

@ -16,6 +16,7 @@ namespace ircd::fs
struct write_opts extern const write_opts_default;
// Yields ircd::ctx for write from buffer; returns view of written portion
const_buffer write(const fd &, const const_buffer &, const write_opts & = write_opts_default);
const_buffer write(const string_view &path, const const_buffer &, const write_opts & = write_opts_default);
// Yields ircd::ctx to overwrite (trunc) file from buffer; returns view of written portion
@ -31,15 +32,6 @@ struct ircd::fs::write_opts
write_opts() = default;
write_opts(const off_t &);
/// Overwrite (trunc) the file
bool overwrite {false};
/// Append to the file (offset is ignored)
bool append {false};
/// Create the file if necessary (default)
bool create {true};
/// Offset in the file to start the write from.
off_t offset {0};
@ -55,19 +47,25 @@ ircd::fs::write_opts::write_opts(const off_t &offset)
inline ircd::const_buffer
ircd::fs::overwrite(const string_view &path,
const const_buffer &buf,
const write_opts &opts_)
const write_opts &opts)
{
auto opts(opts_);
opts.overwrite = true;
return write(path, buf, opts);
const fd fd
{
path, std::ios::out | std::ios::trunc
};
return write(fd, buf, opts);
}
inline ircd::const_buffer
ircd::fs::append(const string_view &path,
const const_buffer &buf,
const write_opts &opts_)
const write_opts &opts)
{
auto opts(opts_);
opts.append = true;
return write(path, buf, opts);
const fd fd
{
path, std::ios::out | std::ios::app
};
return write(fd, buf, opts);
}

View file

@ -343,39 +343,13 @@ ircd::fs::aio::request::write::write(const int &fd,
//
ircd::const_buffer
ircd::fs::write__aio(const string_view &path,
ircd::fs::write__aio(const fd &fd,
const const_buffer &buf,
const write_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_WRONLY;
flags |= O_CLOEXEC;
flags |= opts.create? O_CREAT : 0;
flags |= opts.append? O_APPEND : 0;
flags |= opts.overwrite? O_TRUNC : 0;
const mode_t mask
{
opts.create? S_IRUSR | S_IWUSR : 0U
};
const auto fd
{
syscall(::open, pathstr, flags, mask)
};
const unwind cfd{[&fd]
{
syscall(::close, fd);
}};
aio::request::write request
{
int(fd), buf, opts
fd, buf, opts
};
const size_t bytes

View file

@ -74,7 +74,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 write__aio(const fd &, const const_buffer &, const write_opts &);
const_buffer read__aio(const fd &, const mutable_buffer &, const read_opts &);
}

View file

@ -216,11 +216,6 @@ catch(const std::exception &e)
// fs/write.h
//
namespace ircd::fs
{
const_buffer write__std(const string_view &path, const const_buffer &, const write_opts &);
}
ircd::fs::write_opts
const ircd::fs::write_opts_default
{};
@ -231,12 +226,16 @@ ircd::fs::write(const string_view &path,
const write_opts &opts)
try
{
#ifdef IRCD_USE_AIO
if(likely(aioctx))
return write__aio(path, buf, opts);
#endif
const fd fd
{
path, std::ios::out
};
return write__std(path, buf, opts);
return write(fd, buf, opts);
}
catch(const filesystem_error &)
{
throw;
}
catch(const std::exception &e)
{
@ -247,29 +246,32 @@ catch(const std::exception &e)
}
ircd::const_buffer
ircd::fs::write__std(const string_view &path,
ircd::fs::write(const fd &fd,
const const_buffer &buf,
const write_opts &opts)
try
{
const auto open_mode
#ifdef IRCD_USE_AIO
if(likely(aioctx))
return write__aio(fd, buf, opts);
#endif
return
{
opts.append?
std::ios::app:
opts.overwrite?
std::ios::trunc:
std::ios::out
data(buf),
size_t(syscall(::pwrite, fd, data(buf), size(buf), opts.offset))
};
std::ofstream file
}
catch(const filesystem_error &)
{
std::string{path}, open_mode
throw;
}
catch(const std::exception &e)
{
throw filesystem_error
{
"%s", e.what()
};
file.seekp(opts.offset, file.beg);
file.write(data(buf), size(buf));
return buf;
}
///////////////////////////////////////////////////////////////////////////////