builtin/apply: make try_create_file() return -1 on error
[gitweb.git] / builtin / apply.c
index ad0b87510b0252191d465dca6bd371cb9584a026..3145e03940dceaffc46015e225c0ec6cd8669e9b 100644 (file)
@@ -4099,11 +4099,11 @@ static int remove_file(struct apply_state *state, struct patch *patch, int rmdir
        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;
@@ -4111,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);
@@ -4122,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;
 }
 
 /*
@@ -4183,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;
        }
 
@@ -4210,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);
@@ -4257,7 +4291,7 @@ static int add_conflicted_stages_file(struct apply_state *state,
        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;
@@ -4268,40 +4302,36 @@ static void create_file(struct apply_state *state, struct patch *patch)
                mode = S_IFREG | 0644;
        create_one_file(state, path, mode, buf, size);
 
-       if (patch->conflicted_threeway) {
-               if (add_conflicted_stages_file(state, patch))
-                       exit(128);
-       } else
-               add_index_file(state, path, mode, buf, size);
+       if (patch->conflicted_threeway)
+               return add_conflicted_stages_file(state, patch);
+       else
+               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) {
-                       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)
-                       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) {
-               if (remove_file(state, patch, patch->is_rename))
-                       exit(128);
-       }
+       if (phase == 0)
+               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)
@@ -4375,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;
@@ -4388,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;
@@ -4508,10 +4547,17 @@ static int apply_patch(struct apply_state *state,
                }
        }
 
-       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 &&