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>

Christian Couder committed Sep 4, 2016 at 22:18 UTC 603752a88df398cbe6cad449b9fbd49aa28dfa20
1 file changed +21 -15
builtin/apply.c
+21 -15
@@ -4198,32 +4198,36 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
4198 * We optimistically assume that the directories exist,
4199 * which is true 99% of the time anyway. If they don't,
4200 * we create them and try again.
4201 + *
4202 + * Returns:
4203 + * -1 on error
4204 + * 0 otherwise
4205 */
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)
4211 {
4212 int res;
4213
4214 if (state->cached)
4211 - return;
4215 + return 0;
4216
4217 res = try_create_file(path, mode, buf, size);
4218 if (res < 0)
4215 - exit(128);
4219 + return -1;
4220 if (!res)
4217 - return;
4221 + return 0;
4222
4223 if (errno == ENOENT) {
4224 if (safe_create_leading_directories(path))
4221 - return;
4225 + return 0;
4226 res = try_create_file(path, mode, buf, size);
4227 if (res < 0)
4224 - exit(128);
4228 + return -1;
4229 if (!res)
4226 - return;
4230 + return 0;
4231 }
4232
4233 if (errno == EEXIST || errno == EACCES) {
@@ -4243,10 +4247,10 @@ static void create_one_file(struct apply_state *state,
4247 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4248 res = try_create_file(newpath, mode, buf, size);
4249 if (res < 0)
4246 - exit(128);
4250 + return -1;
4251 if (!res) {
4252 if (!rename(newpath, path))
4249 - return;
4253 + return 0;
4254 unlink_or_warn(newpath);
4255 break;
4256 }
@@ -4255,7 +4259,8 @@ static void create_one_file(struct apply_state *state,
4259 ++nr;
4260 }
4261 }
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);
4264 }
4265
4266 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)
4305
4306 if (!mode)
4307 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;
4310
4311 if (patch->conflicted_threeway)
4312 return add_conflicted_stages_file(state, patch);