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

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

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

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Christian Couder 2016-09-04 22:18:19 +02:00 committed by Junio C Hamano
parent 739d8a16b5
commit 603752a88d
1 changed files with 21 additions and 15 deletions

View File

@ -4198,8 +4198,12 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
* We optimistically assume that the directories exist, * We optimistically assume that the directories exist,
* which is true 99% of the time anyway. If they don't, * which is true 99% of the time anyway. If they don't,
* we create them and try again. * we create them and try again.
*
* Returns:
* -1 on error
* 0 otherwise
*/ */
static void create_one_file(struct apply_state *state, static int create_one_file(struct apply_state *state,
char *path, char *path,
unsigned mode, unsigned mode,
const char *buf, const char *buf,
@ -4208,22 +4212,22 @@ static void create_one_file(struct apply_state *state,
int res; int res;


if (state->cached) if (state->cached)
return; return 0;


res = try_create_file(path, mode, buf, size); res = try_create_file(path, mode, buf, size);
if (res < 0) if (res < 0)
exit(128); return -1;
if (!res) if (!res)
return; return 0;


if (errno == ENOENT) { if (errno == ENOENT) {
if (safe_create_leading_directories(path)) if (safe_create_leading_directories(path))
return; return 0;
res = try_create_file(path, mode, buf, size); res = try_create_file(path, mode, buf, size);
if (res < 0) if (res < 0)
exit(128); return -1;
if (!res) if (!res)
return; return 0;
} }


if (errno == EEXIST || errno == EACCES) { if (errno == EEXIST || errno == EACCES) {
@ -4243,10 +4247,10 @@ static void create_one_file(struct apply_state *state,
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr); mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
res = try_create_file(newpath, mode, buf, size); res = try_create_file(newpath, mode, buf, size);
if (res < 0) if (res < 0)
exit(128); return -1;
if (!res) { if (!res) {
if (!rename(newpath, path)) if (!rename(newpath, path))
return; return 0;
unlink_or_warn(newpath); unlink_or_warn(newpath);
break; break;
} }
@ -4255,7 +4259,8 @@ static void create_one_file(struct apply_state *state,
++nr; ++nr;
} }
} }
die_errno(_("unable to write file '%s' mode %o"), path, mode); return error_errno(_("unable to write file '%s' mode %o"),
path, mode);
} }


static int add_conflicted_stages_file(struct apply_state *state, static int add_conflicted_stages_file(struct apply_state *state,
@ -4300,7 +4305,8 @@ static int create_file(struct apply_state *state, struct patch *patch)


if (!mode) if (!mode)
mode = S_IFREG | 0644; mode = S_IFREG | 0644;
create_one_file(state, path, mode, buf, size); if (create_one_file(state, path, mode, buf, size))
return -1;


if (patch->conflicted_threeway) if (patch->conflicted_threeway)
return add_conflicted_stages_file(state, patch); return add_conflicted_stages_file(state, patch);