aboutsummaryrefslogtreecommitdiff
path: root/text.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-12-14 13:02:47 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-12-14 14:44:49 +0100
commite80d859867e515d95492e61bf5dca08a1c325130 (patch)
treeb970c6e7e42ab0d951c0ed1cf705dc43220efe61 /text.c
parent5b689baf18ca5e1feec1fc129d8af19f9c987c52 (diff)
downloadvis-e80d859867e515d95492e61bf5dca08a1c325130.tar.gz
vis-e80d859867e515d95492e61bf5dca08a1c325130.tar.xz
text: expose text save method to calling code
There are cases where it is useful to specify how the file should be saved.
Diffstat (limited to 'text.c')
-rw-r--r--text.c14
1 files changed, 5 insertions, 9 deletions
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);