Subject: [PATCH/RFC/GSoC 15/17] wrapper: implement append_file()

From: Paul Tan <[email-masked]>
Date: Sat, 12 Mar 2016 18:46:35 +0800

Signed-off-by: Paul Tan <[email-masked]>
---
 cache.h   |  1 +
 wrapper.c | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/cache.h b/cache.h
index 55d443e..aa5e97c 100644
--- a/cache.h
+++ b/cache.h
@@ -1700,6 +1700,7 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
 
 extern int write_file(const char *path, const char *fmt, ...);
 extern int write_file_gently(const char *path, const char *fmt, ...);
+extern void append_file(const char *path, const char *fmt, ...);
 
 /* pager.c */
 extern void setup_pager(void);
diff --git a/wrapper.c b/wrapper.c
index 9afc1a0..cd77e94 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -709,6 +709,29 @@ int write_file_gently(const char *path, const char *fmt, ...)
 	return status;
 }
 
+void append_file(const char *path, const char *fmt, ...)
+{
+	struct strbuf sb = STRBUF_INIT;
+	int fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0666);
+	va_list params;
+	if (fd < 0)
+		die_errno(_("could not open %s for appending"), path);
+	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) {
+		int err = errno;
+		close(fd);
+		strbuf_release(&sb);
+		errno = err;
+		die_errno(_("could not write to %s"), path);
+	}
+	strbuf_release(&sb);
+	if (close(fd))
+		die_errno(_("could not close %s"), path);
+}
+
 void sleep_millisec(int millisec)
 {
 	poll(NULL, 0, millisec);
-- 
2.7.0