builtin/apply: make try_create_file() return -1 on error

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", try_create_file() should return -1 in case of
error.

Unfortunately try_create_file() currently returns -1 to signal a
recoverable error. To fix that, let's make it return 1 in case of
a recoverable error and -1 in case of an unrecoverable error.

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Christian Couder 2016-09-04 22:18:18 +02:00 committed by Junio C Hamano
parent ccceb7bb13
commit 739d8a16b5

View File

@ -4150,38 +4150,48 @@ static int add_index_file(struct apply_state *state,
return 0; return 0;
} }
/*
* Returns:
* -1 if an unrecoverable error happened
* 0 if everything went well
* 1 if a recoverable error happened
*/
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
{ {
int fd; int fd, res;
struct strbuf nbuf = STRBUF_INIT; struct strbuf nbuf = STRBUF_INIT;
if (S_ISGITLINK(mode)) { if (S_ISGITLINK(mode)) {
struct stat st; struct stat st;
if (!lstat(path, &st) && S_ISDIR(st.st_mode)) if (!lstat(path, &st) && S_ISDIR(st.st_mode))
return 0; return 0;
return mkdir(path, 0777); return !!mkdir(path, 0777);
} }
if (has_symlinks && S_ISLNK(mode)) if (has_symlinks && S_ISLNK(mode))
/* Although buf:size is counted string, it also is NUL /* Although buf:size is counted string, it also is NUL
* terminated. * terminated.
*/ */
return symlink(buf, path); return !!symlink(buf, path);
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666); fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
if (fd < 0) if (fd < 0)
return -1; return 1;
if (convert_to_working_tree(path, buf, size, &nbuf)) { if (convert_to_working_tree(path, buf, size, &nbuf)) {
size = nbuf.len; size = nbuf.len;
buf = nbuf.buf; buf = nbuf.buf;
} }
write_or_die(fd, buf, size);
res = write_in_full(fd, buf, size) < 0;
if (res)
error_errno(_("failed to write to '%s'"), path);
strbuf_release(&nbuf); strbuf_release(&nbuf);
if (close(fd) < 0) if (close(fd) < 0 && !res)
die_errno(_("closing file '%s'"), path); return error_errno(_("closing file '%s'"), path);
return 0;
return res ? -1 : 0;
} }
/* /*
@ -4195,15 +4205,24 @@ static void create_one_file(struct apply_state *state,
const char *buf, const char *buf,
unsigned long size) unsigned long size)
{ {
int res;
if (state->cached) if (state->cached)
return; return;
if (!try_create_file(path, mode, buf, size))
res = try_create_file(path, mode, buf, size);
if (res < 0)
exit(128);
if (!res)
return; return;
if (errno == ENOENT) { if (errno == ENOENT) {
if (safe_create_leading_directories(path)) if (safe_create_leading_directories(path))
return; return;
if (!try_create_file(path, mode, buf, size)) res = try_create_file(path, mode, buf, size);
if (res < 0)
exit(128);
if (!res)
return; return;
} }
@ -4222,7 +4241,10 @@ static void create_one_file(struct apply_state *state,
for (;;) { for (;;) {
char newpath[PATH_MAX]; char newpath[PATH_MAX];
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr); mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
if (!try_create_file(newpath, mode, buf, size)) { res = try_create_file(newpath, mode, buf, size);
if (res < 0)
exit(128);
if (!res) {
if (!rename(newpath, path)) if (!rename(newpath, path))
return; return;
unlink_or_warn(newpath); unlink_or_warn(newpath);