reftable/system: provide thin wrapper for tempfile subsystem

We use the tempfile subsystem to write temporary tables, but given that
we're in the process of converting the reftable library to become
standalone we cannot use this subsystem directly anymore. While we could
in theory convert the code to use mkstemp(3p) instead, we'd lose access
to our infrastructure that automatically prunes tempfiles via atexit(3p)
or signal handlers.

Provide a thin wrapper for the tempfile subsystem instead. Like this,
the compatibility shim is fully self-contained in "reftable/system.c".
Downstream users of the reftable library would have to implement their
own tempfile shims by replacing "system.c" with a custom version.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
This commit is contained in:
Patrick Steinhardt
2024-10-23 11:56:07 +02:00
committed by Taylor Blau
parent 3c58a50c5e
commit 3f5e8d23cc
4 changed files with 113 additions and 35 deletions

49
reftable/system.c Normal file
View File

@@ -0,0 +1,49 @@
#include "system.h"
#include "basics.h"
#include "reftable-error.h"
#include "../tempfile.h"
int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern)
{
struct tempfile *tempfile;
tempfile = mks_tempfile(pattern);
if (!tempfile)
return REFTABLE_IO_ERROR;
out->path = tempfile->filename.buf;
out->fd = tempfile->fd;
out->priv = tempfile;
return 0;
}
int tmpfile_close(struct reftable_tmpfile *t)
{
struct tempfile *tempfile = t->priv;
int ret = close_tempfile_gently(tempfile);
t->fd = -1;
if (ret < 0)
return REFTABLE_IO_ERROR;
return 0;
}
int tmpfile_delete(struct reftable_tmpfile *t)
{
struct tempfile *tempfile = t->priv;
int ret = delete_tempfile(&tempfile);
*t = REFTABLE_TMPFILE_INIT;
if (ret < 0)
return REFTABLE_IO_ERROR;
return 0;
}
int tmpfile_rename(struct reftable_tmpfile *t, const char *path)
{
struct tempfile *tempfile = t->priv;
int ret = rename_tempfile(&tempfile, path);
*t = REFTABLE_TMPFILE_INIT;
if (ret < 0)
return REFTABLE_IO_ERROR;
return 0;
}