refactor(shada): devirtualize writer

writer is only ever used with FileDescriptor. We already have separate
code paths for serializing shada data into memory, see
shada_encode_regs() and friends
This commit is contained in:
bfredl
2024-02-24 11:22:02 +01:00
parent 77e928fd3e
commit 7447b035ca

View File

@@ -386,26 +386,6 @@ struct sd_read_def {
///< reader structure initialization). May overflow.
};
typedef struct sd_write_def ShaDaWriteDef;
/// Function used to close files defined by ShaDaWriteDef
typedef void (*ShaDaWriteCloser)(ShaDaWriteDef *const sd_writer)
REAL_FATTR_NONNULL_ALL;
/// Function used to write ShaDa files
typedef ptrdiff_t (*ShaDaFileWriter)(ShaDaWriteDef *const sd_writer,
const void *const src,
const size_t size)
REAL_FATTR_NONNULL_ALL REAL_FATTR_WARN_UNUSED_RESULT;
/// Structure containing necessary pointers for writing ShaDa files
struct sd_write_def {
ShaDaFileWriter write; ///< Writer function.
ShaDaWriteCloser close; ///< Close function.
FileDescriptor cookie; ///< Data describing object written to.
const char *error; ///< Error message in case of error.
};
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "shada.c.generated.h"
#endif
@@ -636,21 +616,6 @@ static int read_char(ShaDaReadDef *const sd_reader)
return (int)ret;
}
/// Wrapper for writing to file descriptors
///
/// @return -1 or number of bytes written.
static ptrdiff_t write_file(ShaDaWriteDef *const sd_writer, const void *const dest,
const size_t size)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
const ptrdiff_t ret = file_write(&sd_writer->cookie, dest, size);
if (ret < 0) {
sd_writer->error = os_strerror((int)ret);
return -1;
}
return ret;
}
/// Wrapper for closing file descriptors opened for reading
static void close_sd_reader(ShaDaReadDef *const sd_reader)
FUNC_ATTR_NONNULL_ALL
@@ -659,13 +624,6 @@ static void close_sd_reader(ShaDaReadDef *const sd_reader)
xfree(sd_reader->cookie);
}
/// Wrapper for closing file descriptors opened for writing
static void close_sd_writer(ShaDaWriteDef *const sd_writer)
FUNC_ATTR_NONNULL_ALL
{
close_file(&sd_writer->cookie);
}
/// Wrapper for read that reads to IObuff and ignores bytes read
///
/// Used for skipping.
@@ -753,7 +711,7 @@ static int open_shada_file_for_reading(const char *const fname, ShaDaReadDef *sd
}
/// Wrapper for closing file descriptors
static void close_file(void *cookie)
static void close_file(FileDescriptor *cookie)
{
const int error = file_close(cookie, !!p_fs);
if (error != 0) {
@@ -762,16 +720,17 @@ static void close_file(void *cookie)
}
}
/// Msgpack callback for writing to ShaDaWriteDef*
/// Msgpack callback for writing to FileDescriptor*
static int msgpack_sd_writer_write(void *data, const char *buf, size_t len)
{
ShaDaWriteDef *const sd_writer = (ShaDaWriteDef *)data;
ptrdiff_t written_bytes = sd_writer->write(sd_writer, buf, len);
if (written_bytes == -1) {
FileDescriptor *const sd_writer = (FileDescriptor *)data;
const ptrdiff_t ret = file_write(sd_writer, buf, len);
if (ret < 0) {
semsg(_(SERR "System error while writing ShaDa file: %s"),
sd_writer->error);
os_strerror((int)ret));
return -1;
}
return 0;
}
@@ -2524,7 +2483,7 @@ static int hist_type2char(const int type)
/// @param[in] sd_reader Structure containing file reader definition. If it is
/// not NULL then contents of this file will be merged
/// with current Neovim runtime.
static ShaDaWriteResult shada_write(ShaDaWriteDef *const sd_writer, ShaDaReadDef *const sd_reader)
static ShaDaWriteResult shada_write(FileDescriptor *const sd_writer, ShaDaReadDef *const sd_reader)
FUNC_ATTR_NONNULL_ARG(1)
{
ShaDaWriteResult ret = kSDWriteSuccessful;
@@ -2998,11 +2957,7 @@ int shada_write_file(const char *const file, bool nomerge)
char *const fname = shada_filename(file);
char *tempname = NULL;
ShaDaWriteDef sd_writer = {
.write = &write_file,
.close = &close_sd_writer,
.error = NULL,
};
FileDescriptor sd_writer;
ShaDaReadDef sd_reader = { .close = NULL };
bool did_open_writer = false;
@@ -3034,7 +2989,7 @@ int shada_write_file(const char *const file, bool nomerge)
// 3: If somebody happened to delete the file after it was opened for
// reading use u=rw permissions.
shada_write_file_open: {}
error = file_open(&sd_writer.cookie, tempname, kFileCreateOnly|kFileNoSymlink, perm);
error = file_open(&sd_writer, tempname, kFileCreateOnly|kFileNoSymlink, perm);
if (error) {
if (error == UV_EEXIST || error == UV_ELOOP) {
// File already exists, try another name
@@ -3080,7 +3035,7 @@ shada_write_file_nomerge: {}
}
*tail = tail_save;
}
int error = file_open(&sd_writer.cookie, fname, kFileCreate|kFileTruncate, 0600);
int error = file_open(&sd_writer, fname, kFileCreate|kFileTruncate, 0600);
if (error) {
semsg(_(SERR "System error while opening ShaDa file %s for writing: %s"),
fname, os_strerror(error));
@@ -3104,9 +3059,7 @@ shada_write_file_nomerge: {}
verbose_leave();
}
const ShaDaWriteResult sw_ret = shada_write(&sd_writer, (nomerge
? NULL
: &sd_reader));
const ShaDaWriteResult sw_ret = shada_write(&sd_writer, (nomerge ? NULL : &sd_reader));
assert(sw_ret != kSDWriteIgnError);
if (!nomerge) {
sd_reader.close(&sd_reader);
@@ -3136,7 +3089,7 @@ shada_write_file_nomerge: {}
|| old_info.stat.st_gid != getgid()) {
const uv_uid_t old_uid = (uv_uid_t)old_info.stat.st_uid;
const uv_gid_t old_gid = (uv_gid_t)old_info.stat.st_gid;
const int fchown_ret = os_fchown(file_fd(&sd_writer.cookie),
const int fchown_ret = os_fchown(file_fd(&sd_writer),
old_uid, old_gid);
if (fchown_ret != 0) {
semsg(_(RNERR "Failed setting uid and gid for file %s: %s"),
@@ -3169,7 +3122,7 @@ shada_write_file_did_not_remove:
}
xfree(tempname);
}
sd_writer.close(&sd_writer);
close_file(&sd_writer);
xfree(fname);
return OK;