Now that cache.h needs strbuf.h, remove useless includes.
[gitweb.git] / builtin-apply.c
index 25b1447901d4085bdc5b554451c5e79b72cbae12..1256716aece3d3e97f615cc6d8957da68d3e14d6 100644 (file)
@@ -181,34 +181,21 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
 
 static void *read_patch_file(int fd, unsigned long *sizep)
 {
-       unsigned long size = 0, alloc = CHUNKSIZE;
-       void *buffer = xmalloc(alloc);
+       struct strbuf buf;
 
-       for (;;) {
-               ssize_t nr = alloc - size;
-               if (nr < 1024) {
-                       alloc += CHUNKSIZE;
-                       buffer = xrealloc(buffer, alloc);
-                       nr = alloc - size;
-               }
-               nr = xread(fd, (char *) buffer + size, nr);
-               if (!nr)
-                       break;
-               if (nr < 0)
-                       die("git-apply: read returned %s", strerror(errno));
-               size += nr;
-       }
-       *sizep = size;
+       strbuf_init(&buf, 0);
+       if (strbuf_read(&buf, fd, 0) < 0)
+               die("git-apply: read returned %s", strerror(errno));
+       *sizep = buf.len;
 
        /*
         * Make sure that we have some slop in the buffer
         * so that we can do speculative "memcmp" etc, and
         * see to it that it is NUL-filled.
         */
-       if (alloc < size + SLOP)
-               buffer = xrealloc(buffer, size + SLOP);
-       memset((char *) buffer + size, 0, SLOP);
-       return buffer;
+       strbuf_grow(&buf, SLOP);
+       memset(buf.buf + buf.len, 0, SLOP);
+       return strbuf_detach(&buf);
 }
 
 static unsigned long linelen(const char *buffer, unsigned long size)
@@ -1458,8 +1445,7 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign
 {
        int fd;
        unsigned long got;
-       unsigned long nsize;
-       char *nbuf;
+       struct strbuf nbuf;
        unsigned long size = *size_p;
        char *buf = *buf_p;
 
@@ -1478,13 +1464,12 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign
                        got += ret;
                }
                close(fd);
-               nsize = got;
-               nbuf = convert_to_git(path, buf, &nsize);
-               if (nbuf) {
+               strbuf_init(&nbuf, 0);
+               if (convert_to_git(path, buf, size, &nbuf)) {
                        free(buf);
-                       *buf_p = nbuf;
-                       *alloc_p = nsize;
-                       *size_p = nsize;
+                       *buf_p = nbuf.buf;
+                       *alloc_p = nbuf.alloc;
+                       *size_p = nbuf.len;
                }
                return got != size;
        default:
@@ -1514,7 +1499,8 @@ static int find_offset(const char *buf, unsigned long size, const char *fragment
        }
 
        /* Exact line number? */
-       if (!memcmp(buf + start, fragment, fragsize))
+       if ((start + fragsize <= size) &&
+           !memcmp(buf + start, fragment, fragsize))
                return start;
 
        /*
@@ -2450,7 +2436,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;
-       char *nbuf;
+       struct strbuf nbuf;
 
        if (S_ISGITLINK(mode)) {
                struct stat st;
@@ -2469,9 +2455,11 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
        if (fd < 0)
                return -1;
 
-       nbuf = convert_to_working_tree(path, buf, &size);
-       if (nbuf)
-               buf = nbuf;
+       strbuf_init(&nbuf, 0);
+       if (convert_to_working_tree(path, buf, size, &nbuf)) {
+               size = nbuf.len;
+               buf  = nbuf.buf;
+       }
 
        while (size) {
                int written = xwrite(fd, buf, size);
@@ -2484,8 +2472,7 @@ 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 (nbuf)
-               free(nbuf);
+       strbuf_release(&nbuf);
        return 0;
 }