2018-02-06 08:28:33 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_FS_WRITE_H
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
struct write_opts extern const write_opts_default;
|
|
|
|
|
2018-11-29 02:10:48 +01:00
|
|
|
// Yields ircd::ctx for write from buffers; returns bytes written
|
|
|
|
size_t write(const fd &, const const_buffers &, const write_opts & = write_opts_default);
|
|
|
|
size_t write(const string_view &path, const const_buffers &, const write_opts & = write_opts_default);
|
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
// Yields ircd::ctx for write from buffer; returns view of written portion
|
2018-05-30 11:24:35 +02:00
|
|
|
const_buffer write(const fd &, const const_buffer &, const write_opts & = write_opts_default);
|
2018-04-25 03:34:46 +02:00
|
|
|
const_buffer write(const string_view &path, const const_buffer &, const write_opts & = write_opts_default);
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2018-11-29 02:16:26 +01:00
|
|
|
// Yields ircd::ctx to append to file from buffers; returns bytes appended
|
|
|
|
size_t append(const fd &, const const_buffers &, const write_opts & = write_opts_default);
|
|
|
|
size_t append(const string_view &path, const const_buffers &, const write_opts & = write_opts_default);
|
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
// Yields ircd::ctx to append to file from buffer; returns view of written portion
|
2018-05-30 11:40:20 +02:00
|
|
|
const_buffer append(const fd &, const const_buffer &, const write_opts & = write_opts_default);
|
2018-04-25 03:34:46 +02:00
|
|
|
const_buffer append(const string_view &path, const const_buffer &, const write_opts & = write_opts_default);
|
2018-05-30 11:40:20 +02:00
|
|
|
|
2018-11-29 02:21:34 +01:00
|
|
|
// Yields ircd::ctx to overwrite (trunc) file from buffers; returns bytes written
|
|
|
|
size_t overwrite(const fd &, const const_buffers &, const write_opts & = write_opts_default);
|
|
|
|
size_t overwrite(const string_view &path, const const_buffers &, const write_opts & = write_opts_default);
|
|
|
|
|
2018-05-30 11:40:20 +02:00
|
|
|
// Yields ircd::ctx to overwrite (trunc) file from buffer; returns view of written portion
|
2018-08-24 06:23:42 +02:00
|
|
|
const_buffer overwrite(const fd &, const const_buffer & = {}, const write_opts & = write_opts_default);
|
|
|
|
const_buffer overwrite(const string_view &path, const const_buffer & = {}, const write_opts & = write_opts_default);
|
2018-08-24 06:24:19 +02:00
|
|
|
|
|
|
|
// Truncate file to explicit size
|
|
|
|
void truncate(const fd &, const size_t &, const write_opts & = write_opts_default);
|
|
|
|
void truncate(const string_view &path, const size_t &, const write_opts & = write_opts_default);
|
2018-08-24 06:24:35 +02:00
|
|
|
|
|
|
|
// Allocate
|
|
|
|
void allocate(const fd &, const size_t &size, const write_opts & = write_opts_default);
|
2018-02-06 08:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Options for a write operation
|
|
|
|
struct ircd::fs::write_opts
|
2018-12-30 05:02:22 +01:00
|
|
|
:opts
|
2018-02-06 08:28:33 +01:00
|
|
|
{
|
2018-08-24 07:35:01 +02:00
|
|
|
/// for allocate()
|
2019-04-26 07:16:13 +02:00
|
|
|
bool keep_size {true};
|
2018-12-16 03:16:09 +01:00
|
|
|
|
2018-12-18 23:01:05 +01:00
|
|
|
/// Yields the ircd::ctx until the buffers are written. This performs
|
|
|
|
/// the unix incremental write loop internally. When this option is true,
|
|
|
|
/// any return value from a function in the write() suite will not be a
|
|
|
|
/// partial value requiring another invocation of write().
|
|
|
|
bool all {true};
|
2018-12-18 23:17:38 +01:00
|
|
|
|
|
|
|
/// Whether to propagate an EINTR; otherwise we reinvoke the syscall. For a
|
|
|
|
/// write(2) family call this can only happen before any data was written;
|
|
|
|
/// an exception will be thrown. We default to true because we have faith
|
|
|
|
/// in the useful propagation of an exception for this event.
|
|
|
|
bool interruptible {true};
|
2018-12-27 23:01:05 +01:00
|
|
|
|
2019-03-14 03:33:58 +01:00
|
|
|
/// Whether to update the fd's offset on appends. This happens naturally
|
|
|
|
/// when the file is opened in append mode. If not, we get the same per-
|
|
|
|
/// write atomic seek behavior if RWF_APPEND is supported. In the latter
|
|
|
|
/// case, this option determines whether the fd's offset is affected.
|
|
|
|
bool update_offset {true};
|
|
|
|
|
|
|
|
/// Whether to RWF_SYNC or RWF_DSYNC depending on the metadata option. This
|
|
|
|
/// is a range-sync, it only covers the offset and size of the write;
|
|
|
|
/// perhaps a worthy replacement for sync_file_range(2).
|
|
|
|
bool sync {false};
|
|
|
|
|
|
|
|
/// When sync is true: if metadata is true RWF_SYNC (like fsync(2)) is used,
|
|
|
|
/// otherwise RWF_DSYNC (like fdsync(2)) is used. This is only if available,
|
|
|
|
/// Careful, if it is not available you are responsible for following the
|
|
|
|
/// write with fsync(2)/fdsync(2) yourself.
|
|
|
|
bool metadata {false};
|
|
|
|
|
2019-04-06 21:13:16 +02:00
|
|
|
write_opts(const off_t & = 0);
|
2018-02-06 08:28:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::fs::write_opts::write_opts(const off_t &offset)
|
2019-04-06 21:13:16 +02:00
|
|
|
:opts{offset, op::WRITE}
|
2018-02-06 08:28:33 +01:00
|
|
|
{}
|