builtin/apply: make try_create_file() return -1 on error
[gitweb.git] / builtin / apply.c
index 6b161738732a77e81cf480defe16eb3631c9a7df..3145e03940dceaffc46015e225c0ec6cd8669e9b 100644 (file)
@@ -3704,7 +3704,7 @@ static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
        return ret;
 }
 
-static void die_on_unsafe_path(struct patch *patch)
+static int check_unsafe_path(struct patch *patch)
 {
        const char *old_name = NULL;
        const char *new_name = NULL;
@@ -3716,9 +3716,10 @@ static void die_on_unsafe_path(struct patch *patch)
                new_name = patch->new_name;
 
        if (old_name && !verify_path(old_name))
-               die(_("invalid path '%s'"), old_name);
+               return error(_("invalid path '%s'"), old_name);
        if (new_name && !verify_path(new_name))
-               die(_("invalid path '%s'"), new_name);
+               return error(_("invalid path '%s'"), new_name);
+       return 0;
 }
 
 /*
@@ -3808,8 +3809,8 @@ static int check_patch(struct apply_state *state, struct patch *patch)
                }
        }
 
-       if (!state->unsafe_paths)
-               die_on_unsafe_path(patch);
+       if (!state->unsafe_paths && check_unsafe_path(patch))
+               return -128;
 
        /*
         * An attempt to read from or delete a path that is beyond a
@@ -3837,10 +3838,14 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
        prepare_symlink_changes(state, patch);
        prepare_fn_table(state, patch);
        while (patch) {
+               int res;
                if (state->apply_verbosely)
                        say_patch_name(stderr,
                                       _("Checking patch %s..."), patch);
-               err |= check_patch(state, patch);
+               res = check_patch(state, patch);
+               if (res == -128)
+                       return -128;
+               err |= res;
                patch = patch->next;
        }
        return err;
@@ -3895,11 +3900,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
 }
 
 /* Build an index that contains the just the files needed for a 3way merge */
-static void build_fake_ancestor(struct patch *list, const char *filename)
+static int build_fake_ancestor(struct patch *list, const char *filename)
 {
        struct patch *patch;
        struct index_state result = { NULL };
        static struct lock_file lock;
+       int res;
 
        /* Once we start supporting the reverse patch, it may be
         * worth showing the new sha1 prefix, but until then...
@@ -3917,31 +3923,38 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
                        if (!preimage_sha1_in_gitlink_patch(patch, sha1))
                                ; /* ok, the textual part looks sane */
                        else
-                               die("sha1 information is lacking or useless for submodule %s",
-                                   name);
+                               return error("sha1 information is lacking or "
+                                            "useless for submodule %s", name);
                } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
                        ; /* ok */
                } else if (!patch->lines_added && !patch->lines_deleted) {
                        /* mode-only change: update the current */
                        if (get_current_sha1(patch->old_name, sha1))
-                               die("mode change for %s, which is not "
-                                   "in current HEAD", name);
+                               return error("mode change for %s, which is not "
+                                            "in current HEAD", name);
                } else
-                       die("sha1 information is lacking or useless "
-                           "(%s).", name);
+                       return error("sha1 information is lacking or useless "
+                                    "(%s).", name);
 
                ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
                if (!ce)
-                       die(_("make_cache_entry failed for path '%s'"), name);
-               if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
-                       die ("Could not add %s to temporary index", name);
+                       return error(_("make_cache_entry failed for path '%s'"),
+                                    name);
+               if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
+                       free(ce);
+                       return error("Could not add %s to temporary index",
+                                    name);
+               }
        }
 
        hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
-       if (write_locked_index(&result, &lock, COMMIT_LOCK))
-               die ("Could not write temporary index to %s", filename);
-
+       res = write_locked_index(&result, &lock, COMMIT_LOCK);
        discard_index(&result);
+
+       if (res)
+               return error("Could not write temporary index to %s", filename);
+
+       return 0;
 }
 
 static void stat_patch_list(struct apply_state *state, struct patch *patch)
@@ -4072,24 +4085,25 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
        }
 }
 
-static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
+static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
 {
        if (state->update_index) {
                if (remove_file_from_cache(patch->old_name) < 0)
-                       die(_("unable to remove %s from index"), patch->old_name);
+                       return error(_("unable to remove %s from index"), patch->old_name);
        }
        if (!state->cached) {
                if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
                        remove_path(patch->old_name);
                }
        }
+       return 0;
 }
 
-static void add_index_file(struct apply_state *state,
-                          const char *path,
-                          unsigned mode,
-                          void *buf,
-                          unsigned long size)
+static int add_index_file(struct apply_state *state,
+                         const char *path,
+                         unsigned mode,
+                         void *buf,
+                         unsigned long size)
 {
        struct stat st;
        struct cache_entry *ce;
@@ -4097,7 +4111,7 @@ static void add_index_file(struct apply_state *state,
        unsigned ce_size = cache_entry_size(namelen);
 
        if (!state->update_index)
-               return;
+               return 0;
 
        ce = xcalloc(1, ce_size);
        memcpy(ce->name, path, namelen);
@@ -4108,54 +4122,76 @@ static void add_index_file(struct apply_state *state,
                const char *s;
 
                if (!skip_prefix(buf, "Subproject commit ", &s) ||
-                   get_sha1_hex(s, ce->sha1))
-                       die(_("corrupt patch for submodule %s"), path);
+                   get_sha1_hex(s, ce->sha1)) {
+                       free(ce);
+                       return error(_("corrupt patch for submodule %s"), path);
+               }
        } else {
                if (!state->cached) {
-                       if (lstat(path, &st) < 0)
-                               die_errno(_("unable to stat newly created file '%s'"),
-                                         path);
+                       if (lstat(path, &st) < 0) {
+                               free(ce);
+                               return error(_("unable to stat newly "
+                                              "created file '%s': %s"),
+                                            path, strerror(errno));
+                       }
                        fill_stat_cache_info(ce, &st);
                }
-               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
-                       die(_("unable to create backing store for newly created file %s"), path);
+               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
+                       free(ce);
+                       return error(_("unable to create backing store "
+                                      "for newly created file %s"), path);
+               }
+       }
+       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
+               free(ce);
+               return error(_("unable to add cache entry for %s"), path);
        }
-       if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-               die(_("unable to add cache entry for %s"), path);
+
+       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;
 }
 
 /*
@@ -4169,15 +4205,24 @@ static void create_one_file(struct apply_state *state,
                            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;
        }
 
@@ -4196,7 +4241,10 @@ static void create_one_file(struct apply_state *state,
                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);
@@ -4210,7 +4258,7 @@ static void create_one_file(struct apply_state *state,
        die_errno(_("unable to write file '%s' mode %o"), path, mode);
 }
 
-static void add_conflicted_stages_file(struct apply_state *state,
+static int add_conflicted_stages_file(struct apply_state *state,
                                       struct patch *patch)
 {
        int stage, namelen;
@@ -4218,7 +4266,7 @@ static void add_conflicted_stages_file(struct apply_state *state,
        struct cache_entry *ce;
 
        if (!state->update_index)
-               return;
+               return 0;
        namelen = strlen(patch->new_name);
        ce_size = cache_entry_size(namelen);
        mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
@@ -4233,12 +4281,17 @@ static void add_conflicted_stages_file(struct apply_state *state,
                ce->ce_flags = create_ce_flags(stage);
                ce->ce_namelen = namelen;
                hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
-               if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-                       die(_("unable to add cache entry for %s"), patch->new_name);
+               if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
+                       free(ce);
+                       return error(_("unable to add cache entry for %s"),
+                                    patch->new_name);
+               }
        }
+
+       return 0;
 }
 
-static void create_file(struct apply_state *state, struct patch *patch)
+static int create_file(struct apply_state *state, struct patch *patch)
 {
        char *path = patch->new_name;
        unsigned mode = patch->new_mode;
@@ -4250,34 +4303,35 @@ static void create_file(struct apply_state *state, struct patch *patch)
        create_one_file(state, path, mode, buf, size);
 
        if (patch->conflicted_threeway)
-               add_conflicted_stages_file(state, patch);
+               return add_conflicted_stages_file(state, patch);
        else
-               add_index_file(state, path, mode, buf, size);
+               return add_index_file(state, path, mode, buf, size);
 }
 
 /* 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)
-                       remove_file(state, patch, 1);
-               return;
+                       return remove_file(state, patch, 1);
+               return 0;
        }
        if (patch->is_new > 0 || patch->is_copy) {
                if (phase == 1)
-                       create_file(state, patch);
-               return;
+                       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)
-               remove_file(state, patch, patch->is_rename);
+               return remove_file(state, patch, patch->is_rename);
        if (phase == 1)
-               create_file(state, patch);
+               return create_file(state, patch);
+       return 0;
 }
 
 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
@@ -4351,6 +4405,12 @@ 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;
@@ -4364,7 +4424,10 @@ static int write_out_results(struct apply_state *state, struct patch *list)
                        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;
@@ -4472,21 +4535,36 @@ static int apply_patch(struct apply_state *state,
                goto end;
        }
 
-       if ((state->check || state->apply) &&
-           check_patch_list(state, list) < 0 &&
-           !state->apply_with_reject) {
-               res = -1;
-               goto end;
+       if (state->check || state->apply) {
+               int r = check_patch_list(state, list);
+               if (r == -128) {
+                       res = -128;
+                       goto end;
+               }
+               if (r < 0 && !state->apply_with_reject) {
+                       res = -1;
+                       goto end;
+               }
        }
 
-       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)
-               build_fake_ancestor(list, state->fake_ancestor);
+       if (state->fake_ancestor &&
+           build_fake_ancestor(list, state->fake_ancestor)) {
+               res = -128;
+               goto end;
+       }
 
        if (state->diffstat)
                stat_patch_list(state, list);