strbuf_read_file enhancement, and use it.
[gitweb.git] / builtin-apply.c
index 740623e6c09cbf85bd8d54aac560dc2c5015078b..fec96a8d9d24cafe492a8dbe40bdb470244ea9f5 100644 (file)
@@ -1425,8 +1425,6 @@ static void show_stats(struct patch *patch)
 
 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
 {
-       int fd;
-
        switch (st->st_mode & S_IFMT) {
        case S_IFLNK:
                strbuf_grow(buf, st->st_size);
@@ -1435,14 +1433,8 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
                strbuf_setlen(buf, st->st_size);
                return 0;
        case S_IFREG:
-               fd = open(path, O_RDONLY);
-               if (fd < 0)
-                       return error("unable to open %s", path);
-               if (strbuf_read(buf, fd, st->st_size) < 0) {
-                       close(fd);
-                       return -1;
-               }
-               close(fd);
+               if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
+                       return error("unable to open or read %s", path);
                convert_to_git(path, buf->buf, buf->len, buf);
                return 0;
        default: