* We optimistically assume that the directories exist,
* which is true 99% of the time anyway. If they don't,
* we create them and try again.
+ *
+ * Returns:
+ * -1 on error
+ * 0 otherwise
*/
-static void create_one_file(struct apply_state *state,
- char *path,
- unsigned mode,
- const char *buf,
- unsigned long size)
+static int create_one_file(struct apply_state *state,
+ char *path,
+ unsigned mode,
+ const char *buf,
+ unsigned long size)
{
int res;
if (state->cached)
- return;
+ return 0;
res = try_create_file(path, mode, buf, size);
if (res < 0)
- exit(128);
+ return -1;
if (!res)
- return;
+ return 0;
if (errno == ENOENT) {
if (safe_create_leading_directories(path))
- return;
+ return 0;
res = try_create_file(path, mode, buf, size);
if (res < 0)
- exit(128);
+ return -1;
if (!res)
- return;
+ return 0;
}
if (errno == EEXIST || errno == EACCES) {
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
res = try_create_file(newpath, mode, buf, size);
if (res < 0)
- exit(128);
+ return -1;
if (!res) {
if (!rename(newpath, path))
- return;
+ return 0;
unlink_or_warn(newpath);
break;
}
++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,
if (!mode)
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)
return add_conflicted_stages_file(state, patch);