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: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
86b770b0bb
commit
01e49941d6
49
reftable/system.c
Normal file
49
reftable/system.c
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user