Skip to content

Commit 739d8a1

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ccceb7b commit 739d8a1

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

builtin/apply.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,38 +4150,48 @@ static int add_index_file(struct apply_state *state,
41504150
return 0;
41514151
}
41524152

4153+
/*
4154+
* Returns:
4155+
* -1 if an unrecoverable error happened
4156+
* 0 if everything went well
4157+
* 1 if a recoverable error happened
4158+
*/
41534159
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
41544160
{
4155-
int fd;
4161+
int fd, res;
41564162
struct strbuf nbuf = STRBUF_INIT;
41574163

41584164
if (S_ISGITLINK(mode)) {
41594165
struct stat st;
41604166
if (!lstat(path, &st) && S_ISDIR(st.st_mode))
41614167
return 0;
4162-
return mkdir(path, 0777);
4168+
return !!mkdir(path, 0777);
41634169
}
41644170

41654171
if (has_symlinks && S_ISLNK(mode))
41664172
/* Although buf:size is counted string, it also is NUL
41674173
* terminated.
41684174
*/
4169-
return symlink(buf, path);
4175+
return !!symlink(buf, path);
41704176

41714177
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
41724178
if (fd < 0)
4173-
return -1;
4179+
return 1;
41744180

41754181
if (convert_to_working_tree(path, buf, size, &nbuf)) {
41764182
size = nbuf.len;
41774183
buf = nbuf.buf;
41784184
}
4179-
write_or_die(fd, buf, size);
4185+
4186+
res = write_in_full(fd, buf, size) < 0;
4187+
if (res)
4188+
error_errno(_("failed to write to '%s'"), path);
41804189
strbuf_release(&nbuf);
41814190

4182-
if (close(fd) < 0)
4183-
die_errno(_("closing file '%s'"), path);
4184-
return 0;
4191+
if (close(fd) < 0 && !res)
4192+
return error_errno(_("closing file '%s'"), path);
4193+
4194+
return res ? -1 : 0;
41854195
}
41864196

41874197
/*
@@ -4195,15 +4205,24 @@ static void create_one_file(struct apply_state *state,
41954205
const char *buf,
41964206
unsigned long size)
41974207
{
4208+
int res;
4209+
41984210
if (state->cached)
41994211
return;
4200-
if (!try_create_file(path, mode, buf, size))
4212+
4213+
res = try_create_file(path, mode, buf, size);
4214+
if (res < 0)
4215+
exit(128);
4216+
if (!res)
42014217
return;
42024218

42034219
if (errno == ENOENT) {
42044220
if (safe_create_leading_directories(path))
42054221
return;
4206-
if (!try_create_file(path, mode, buf, size))
4222+
res = try_create_file(path, mode, buf, size);
4223+
if (res < 0)
4224+
exit(128);
4225+
if (!res)
42074226
return;
42084227
}
42094228

@@ -4222,7 +4241,10 @@ static void create_one_file(struct apply_state *state,
42224241
for (;;) {
42234242
char newpath[PATH_MAX];
42244243
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4225-
if (!try_create_file(newpath, mode, buf, size)) {
4244+
res = try_create_file(newpath, mode, buf, size);
4245+
if (res < 0)
4246+
exit(128);
4247+
if (!res) {
42264248
if (!rename(newpath, path))
42274249
return;
42284250
unlink_or_warn(newpath);

0 commit comments

Comments
 (0)