diff --git a/cache.h b/cache.h index 3f6ae242df..206d594370 100644 --- a/cache.h +++ b/cache.h @@ -1734,6 +1734,12 @@ static inline ssize_t write_str_in_full(int fd, const char *str) return write_in_full(fd, str, strlen(str)); } +/** + * Open (and truncate) the file at path, write the contents of buf to it, + * and close it. Dies if any errors are encountered. + */ +extern void write_file_buf(const char *path, const char *buf, size_t len); + extern void write_file(const char *path, const char *fmt, ...); /* pager.c */ diff --git a/wrapper.c b/wrapper.c index 7c126b87c5..b827206a42 100644 --- a/wrapper.c +++ b/wrapper.c @@ -640,22 +640,28 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...) return len; } +void write_file_buf(const char *path, const char *buf, size_t len) +{ + int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (write_in_full(fd, buf, len) != len) + die_errno(_("could not write to %s"), path); + if (close(fd)) + die_errno(_("could not close %s"), path); +} + void write_file(const char *path, const char *fmt, ...) { va_list params; struct strbuf sb = STRBUF_INIT; - int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); va_start(params, fmt); strbuf_vaddf(&sb, fmt, params); va_end(params); strbuf_complete_line(&sb); - if (write_in_full(fd, sb.buf, sb.len) != sb.len) - die_errno(_("could not write to %s"), path); + + write_file_buf(path, sb.buf, sb.len); strbuf_release(&sb); - if (close(fd)) - die_errno(_("could not close %s"), path); } void sleep_millisec(int millisec)