From e80d859867e515d95492e61bf5dca08a1c325130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 14 Dec 2016 13:02:47 +0100 Subject: text: expose text save method to calling code There are cases where it is useful to specify how the file should be saved. --- text.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'text.c') diff --git a/text.c b/text.c index 2163deb..55957c0 100644 --- a/text.c +++ b/text.c @@ -129,11 +129,7 @@ struct TextSave { /* used to hold context between text_save_{be char *filename; /* filename to save to as given to text_save_begin */ char *tmpname; /* temporary name used for atomic rename(2) */ int fd; /* file descriptor to write data to using text_save_write */ - enum { - TEXT_SAVE_UNKNOWN, - TEXT_SAVE_ATOMIC, /* create a new file, write content, atomically rename(2) over old file */ - TEXT_SAVE_INPLACE, /* truncate file, overwrite content (any error will result in data loss) */ - } type; + enum TextSaveMethod type; /* method used to save file */ }; /* block management */ @@ -997,7 +993,7 @@ static bool text_save_commit_inplace(TextSave *ctx) { return true; } -TextSave *text_save_begin(Text *txt, const char *filename) { +TextSave *text_save_begin(Text *txt, const char *filename, enum TextSaveMethod type) { if (!filename) return NULL; TextSave *ctx = calloc(1, sizeof *ctx); @@ -1008,11 +1004,11 @@ TextSave *text_save_begin(Text *txt, const char *filename) { if (!(ctx->filename = strdup(filename))) goto err; errno = 0; - if (text_save_begin_atomic(ctx)) + if ((type == TEXT_SAVE_AUTO || type == TEXT_SAVE_ATOMIC) && text_save_begin_atomic(ctx)) return ctx; if (errno == ENOSPC) goto err; - if (text_save_begin_inplace(ctx)) + if ((type == TEXT_SAVE_AUTO || type == TEXT_SAVE_INPLACE) && text_save_begin_inplace(ctx)) return ctx; err: text_save_cancel(ctx); @@ -1073,7 +1069,7 @@ bool text_save_range(Text *txt, Filerange *range, const char *filename) { text_snapshot(txt); return true; } - TextSave *ctx = text_save_begin(txt, filename); + TextSave *ctx = text_save_begin(txt, filename, TEXT_SAVE_AUTO); if (!ctx) return false; ssize_t written = text_write_range(txt, range, ctx->fd); -- cgit v1.2.3