return 0;
}
+/*
+ * Returns:
+ * -1 if an unrecoverable error happened
+ * 0 if everything went well
+ * 1 if a recoverable error happened
+ */
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
{
- int fd;
+ int fd, res;
struct strbuf nbuf = STRBUF_INIT;
if (S_ISGITLINK(mode)) {
struct stat st;
if (!lstat(path, &st) && S_ISDIR(st.st_mode))
return 0;
- return mkdir(path, 0777);
+ return !!mkdir(path, 0777);
}
if (has_symlinks && S_ISLNK(mode))
/* Although buf:size is counted string, it also is NUL
* terminated.
*/
- return symlink(buf, path);
+ return !!symlink(buf, path);
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
if (fd < 0)
- return -1;
+ return 1;
if (convert_to_working_tree(path, buf, size, &nbuf)) {
size = nbuf.len;
buf = nbuf.buf;
}
- write_or_die(fd, buf, size);
+
+ res = write_in_full(fd, buf, size) < 0;
+ if (res)
+ error_errno(_("failed to write to '%s'"), path);
strbuf_release(&nbuf);
- if (close(fd) < 0)
- die_errno(_("closing file '%s'"), path);
- return 0;
+ if (close(fd) < 0 && !res)
+ return error_errno(_("closing file '%s'"), path);
+
+ return res ? -1 : 0;
}
/*
const char *buf,
unsigned long size)
{
+ int res;
+
if (state->cached)
return;
- if (!try_create_file(path, mode, buf, size))
+
+ res = try_create_file(path, mode, buf, size);
+ if (res < 0)
+ exit(128);
+ if (!res)
return;
if (errno == ENOENT) {
if (safe_create_leading_directories(path))
return;
- if (!try_create_file(path, mode, buf, size))
+ res = try_create_file(path, mode, buf, size);
+ if (res < 0)
+ exit(128);
+ if (!res)
return;
}
for (;;) {
char newpath[PATH_MAX];
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
- if (!try_create_file(newpath, mode, buf, size)) {
+ res = try_create_file(newpath, mode, buf, size);
+ if (res < 0)
+ exit(128);
+ if (!res) {
if (!rename(newpath, path))
return;
unlink_or_warn(newpath);
}
/* phase zero is to remove, phase one is to create */
-static void write_out_one_result(struct apply_state *state,
- struct patch *patch,
- int phase)
+static int write_out_one_result(struct apply_state *state,
+ struct patch *patch,
+ int phase)
{
if (patch->is_delete > 0) {
- if (phase == 0) {
- if (remove_file(state, patch, 1))
- exit(128);
- }
- return;
+ if (phase == 0)
+ return remove_file(state, patch, 1);
+ return 0;
}
if (patch->is_new > 0 || patch->is_copy) {
- if (phase == 1) {
- if (create_file(state, patch))
- exit(128);
- }
- return;
+ if (phase == 1)
+ return create_file(state, patch);
+ return 0;
}
/*
* Rename or modification boils down to the same
* thing: remove the old, write the new
*/
- if (phase == 0) {
- if (remove_file(state, patch, patch->is_rename))
- exit(128);
- }
- if (phase == 1) {
- if (create_file(state, patch))
- exit(128);
- }
+ if (phase == 0)
+ return remove_file(state, patch, patch->is_rename);
+ if (phase == 1)
+ return create_file(state, patch);
+ return 0;
}
static int write_out_one_reject(struct apply_state *state, struct patch *patch)
return -1;
}
+/*
+ * Returns:
+ * -1 if an error happened
+ * 0 if the patch applied cleanly
+ * 1 if the patch did not apply cleanly
+ */
static int write_out_results(struct apply_state *state, struct patch *list)
{
int phase;
if (l->rejected)
errs = 1;
else {
- write_out_one_result(state, l, phase);
+ if (write_out_one_result(state, l, phase)) {
+ string_list_clear(&cpath, 0);
+ return -1;
+ }
if (phase == 1) {
if (write_out_one_reject(state, l))
errs = 1;
}
}
- if (state->apply && write_out_results(state, list)) {
- /* with --3way, we still need to write the index out */
- res = state->apply_with_reject ? -1 : 1;
- goto end;
+ if (state->apply) {
+ int write_res = write_out_results(state, list);
+ if (write_res < 0) {
+ res = -128;
+ goto end;
+ }
+ if (write_res > 0) {
+ /* with --3way, we still need to write the index out */
+ res = state->apply_with_reject ? -1 : 1;
+ goto end;
+ }
}
if (state->fake_ancestor &&