Skip to content

Commit 603752a

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 739d8a1 commit 603752a

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

builtin/apply.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,32 +4198,36 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
41984198
* We optimistically assume that the directories exist,
41994199
* which is true 99% of the time anyway. If they don't,
42004200
* we create them and try again.
4201+
*
4202+
* Returns:
4203+
* -1 on error
4204+
* 0 otherwise
42014205
*/
4202-
static void create_one_file(struct apply_state *state,
4203-
char *path,
4204-
unsigned mode,
4205-
const char *buf,
4206-
unsigned long size)
4206+
static int create_one_file(struct apply_state *state,
4207+
char *path,
4208+
unsigned mode,
4209+
const char *buf,
4210+
unsigned long size)
42074211
{
42084212
int res;
42094213

42104214
if (state->cached)
4211-
return;
4215+
return 0;
42124216

42134217
res = try_create_file(path, mode, buf, size);
42144218
if (res < 0)
4215-
exit(128);
4219+
return -1;
42164220
if (!res)
4217-
return;
4221+
return 0;
42184222

42194223
if (errno == ENOENT) {
42204224
if (safe_create_leading_directories(path))
4221-
return;
4225+
return 0;
42224226
res = try_create_file(path, mode, buf, size);
42234227
if (res < 0)
4224-
exit(128);
4228+
return -1;
42254229
if (!res)
4226-
return;
4230+
return 0;
42274231
}
42284232

42294233
if (errno == EEXIST || errno == EACCES) {
@@ -4243,10 +4247,10 @@ static void create_one_file(struct apply_state *state,
42434247
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
42444248
res = try_create_file(newpath, mode, buf, size);
42454249
if (res < 0)
4246-
exit(128);
4250+
return -1;
42474251
if (!res) {
42484252
if (!rename(newpath, path))
4249-
return;
4253+
return 0;
42504254
unlink_or_warn(newpath);
42514255
break;
42524256
}
@@ -4255,7 +4259,8 @@ static void create_one_file(struct apply_state *state,
42554259
++nr;
42564260
}
42574261
}
4258-
die_errno(_("unable to write file '%s' mode %o"), path, mode);
4262+
return error_errno(_("unable to write file '%s' mode %o"),
4263+
path, mode);
42594264
}
42604265

42614266
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)
43004305

43014306
if (!mode)
43024307
mode = S_IFREG | 0644;
4303-
create_one_file(state, path, mode, buf, size);
4308+
if (create_one_file(state, path, mode, buf, size))
4309+
return -1;
43044310

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

0 commit comments

Comments
 (0)