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>
This commit is contained in:
Junio C Hamano
2015-08-24 13:03:07 -07:00
parent 57c867efe4
commit 12d6ce1dba
10 changed files with 40 additions and 19 deletions

View File

@ -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);