Code

Revert "builtin-archive: use RUN_SETUP"
[git.git] / builtin-apply.c
index 53935109a3e320eda6177b74be76c45ee2a6e4d2..27a182bfaa826711b2e6c4c775fbd6e83b243f7e 100644 (file)
@@ -1981,7 +1981,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
                }
        }
        else if (patch->old_name) {
-               size = st->st_size;
+               size = xsize_t(st->st_size);
                alloc = size + 8192;
                buf = xmalloc(alloc);
                if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
@@ -2355,7 +2355,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
 
 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
 {
-       int fd;
+       int fd, converted;
        char *nbuf;
        unsigned long nsize;
 
@@ -2364,17 +2364,18 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
                 * terminated.
                 */
                return symlink(buf, path);
+
+       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
+       if (fd < 0)
+               return -1;
+
        nsize = size;
        nbuf = (char *) buf;
-       if (convert_to_working_tree(path, &nbuf, &nsize)) {
-               free((char *) buf);
+       converted = convert_to_working_tree(path, &nbuf, &nsize);
+       if (converted) {
                buf = nbuf;
                size = nsize;
        }
-
-       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
-       if (fd < 0)
-               return -1;
        while (size) {
                int written = xwrite(fd, buf, size);
                if (written < 0)
@@ -2386,6 +2387,8 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
        }
        if (close(fd) < 0)
                die("closing file %s: %s", path, strerror(errno));
+       if (converted)
+               free(nbuf);
        return 0;
 }