]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Implement read_file() for reading an arbitrary file into memory
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 26 Aug 2010 19:09:02 +0000 (21:09 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 26 Aug 2010 20:03:33 +0000 (22:03 +0200)
ccache.h
util.c

index 449acda321f078ab5aecc278e75d193e5354f804..6f7430f0ab3df892b07224a620f66c70df34179e 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -149,6 +149,7 @@ int x_munmap(void *addr, size_t length);
 int x_rename(const char *oldpath, const char *newpath);
 char *x_readlink(const char *path);
 char *read_text_file(const char *path);
+int read_file(const char *path, size_t size_hint, char **data, size_t *size);
 
 /* ------------------------------------------------------------------------- */
 /* stats.c */
diff --git a/util.c b/util.c
index 62107be3738812f85ce5cc41187c2ebecd186455..31d49772394db992292db45a8cee349d889b7b55 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1158,33 +1158,55 @@ x_readlink(const char *path)
 }
 #endif
 
-/* Return the content of a text file, or NULL on error. Caller frees. */
-char *
-read_text_file(const char *path)
+/*
+ * Reads the content of a file. Size hint 0 means no hint. Returns 0 on
+ * failure, otherwise 1.
+ */
+int
+read_file(const char *path, size_t size_hint, char **data, size_t *size)
 {
        int fd, ret;
-       size_t pos = 0, allocated = 1024;
-       char *result;
+       size_t pos = 0, allocated = (size_hint == 0) ? 16384 : size_hint;
 
        fd = open(path, O_RDONLY);
        if (fd == -1) {
-               return NULL;
+               return 0;
        }
-       result = x_malloc(allocated);
+       *data = x_malloc(allocated);
        ret = 0;
        do {
                pos += ret;
                if (pos > allocated / 2) {
                        allocated *= 2;
-                       result = realloc(result, allocated);
+                       *data = realloc(*data, allocated);
                }
-       } while ((ret = read(fd, result + pos, allocated - pos - 1)) > 0);
+       } while ((ret = read(fd, *data + pos, allocated - pos)) > 0);
        close(fd);
        if (ret == -1) {
-               free(result);
-               return NULL;
+               cc_log("Failed reading %s", path);
+               free(*data);
+               return 0;
        }
-       result[pos] = '\0';
 
-       return result;
+       *size = pos;
+       return 1;
+}
+
+/*
+ * Return the content (with NUL termination) of a text file, or NULL on error.
+ * Caller frees.
+ */
+char *
+read_text_file(const char *path)
+{
+       size_t size;
+       char *data;
+
+       if (read_file(path, 0, &data, &size)) {
+               data = x_realloc(data, size + 1);
+               data[size] = '\0';
+               return data;
+       } else {
+               return NULL;
+       }
 }