summaryrefslogtreecommitdiff
path: root/wrapper.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2015-08-24 13:03:07 -0700
committerJunio C Hamano <gitster@pobox.com>2015-08-24 13:09:02 -0700
commit12d6ce1dba504dfc5279b8d24da3edb4865c2820 (patch)
treeb986a6e22bf290aa4f74d566169a6d54db6e4f3f /wrapper.c
parent57c867efe4e005e40cfdee8a64550d7a95bbb9a0 (diff)
downloadgit-12d6ce1dba504dfc5279b8d24da3edb4865c2820.tar.gz
write_file(): drop "fatal" parameter
All callers except three passed 1 for the "fatal" parameter to ask this function to die upon error, but to a casual reader of the code, it was not all obvious what that 1 meant. Instead, split the function into two based on a common write_file_v() that takes the flag, introduce write_file_gently() as a new way to attempt creating a file without dying on error, and make three callers to call it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/wrapper.c b/wrapper.c
index e451463431..8c8925b72a 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -621,19 +621,17 @@ char *xgetcwd(void)
return strbuf_detach(&sb, NULL);
}
-int write_file(const char *path, int fatal, const char *fmt, ...)
+static int write_file_v(const char *path, int fatal,
+ const char *fmt, va_list params)
{
struct strbuf sb = STRBUF_INIT;
- va_list params;
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
if (fatal)
die_errno(_("could not open %s for writing"), path);
return -1;
}
- va_start(params, fmt);
strbuf_vaddf(&sb, fmt, params);
- va_end(params);
if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
int err = errno;
close(fd);
@@ -652,6 +650,28 @@ int write_file(const char *path, int fatal, const char *fmt, ...)
return 0;
}
+int write_file(const char *path, const char *fmt, ...)
+{
+ int status;
+ va_list params;
+
+ va_start(params, fmt);
+ status = write_file_v(path, 1, fmt, params);
+ va_end(params);
+ return status;
+}
+
+int write_file_gently(const char *path, const char *fmt, ...)
+{
+ int status;
+ va_list params;
+
+ va_start(params, fmt);
+ status = write_file_v(path, 0, fmt, params);
+ va_end(params);
+ return status;
+}
+
void sleep_millisec(int millisec)
{
poll(NULL, 0, millisec);