]> granicus.if.org Git - cgit/commitdiff
Add and use a common readfile() function
authorLars Hjemli <hjemli@gmail.com>
Tue, 18 Aug 2009 15:17:41 +0000 (17:17 +0200)
committerLars Hjemli <hjemli@gmail.com>
Tue, 18 Aug 2009 15:22:14 +0000 (17:22 +0200)
This function is used to read the full content of a textfile into a
newly allocated buffer (with zerotermination).

It replaces the earlier readfile() in scan-tree.c (which was rather
error-prone[1]), and is reused by read_agefile() in ui-repolist.c.

1: No checks for EINTR and EAGAIN, fixed-size buffer

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
cgit.h
scan-tree.c
shared.c
ui-repolist.c

diff --git a/cgit.h b/cgit.h
index d90ccdcafff809bff7734f1949f3a84f5cbd18ee..adb8da4f49be0499eb83ed4a40794f11825be96a 100644 (file)
--- a/cgit.h
+++ b/cgit.h
@@ -283,5 +283,6 @@ extern int cgit_parse_snapshots_mask(const char *str);
 extern int cgit_open_filter(struct cgit_filter *filter);
 extern int cgit_close_filter(struct cgit_filter *filter);
 
+extern int readfile(const char *path, char **buf, size_t *size);
 
 #endif /* CGIT_H */
index 47f3988b3723673b0ed06b104f41822711db46cf..95dc65b3ce1fdd9a27e9e0bfccfa78e54f562f23 100644 (file)
@@ -35,25 +35,13 @@ static int is_git_dir(const char *path)
        return 1;
 }
 
-char *readfile(const char *path)
-{
-       FILE *f;
-       static char buf[MAX_PATH];
-
-       if (!(f = fopen(path, "r")))
-               return NULL;
-       buf[0] = 0;
-       fgets(buf, MAX_PATH, f);
-       fclose(f);
-       return buf;
-}
-
 static void add_repo(const char *base, const char *path)
 {
        struct cgit_repo *repo;
        struct stat st;
        struct passwd *pwd;
        char *p;
+       size_t size;
 
        if (stat(path, &st)) {
                fprintf(stderr, "Error accessing %s: %s (%d)\n",
@@ -80,7 +68,7 @@ static void add_repo(const char *base, const char *path)
 
        p = fmt("%s/description", path);
        if (!stat(p, &st))
-               repo->desc = xstrdup(readfile(p));
+               readfile(p, &repo->desc, &size);
 
        p = fmt("%s/README.html", path);
        if (!stat(p, &st))
index 911a55a9a4476482b6333bd82d9abb515787021c..4cb95739c197d28b9fa381ff192d09cba7f5c473 100644 (file)
--- a/shared.c
+++ b/shared.c
@@ -393,3 +393,24 @@ int cgit_close_filter(struct cgit_filter *filter)
                return 0;
        die("Subprocess %s exited abnormally", filter->cmd);
 }
+
+/* Read the content of the specified file into a newly allocated buffer,
+ * zeroterminate the buffer and return 0 on success, errno otherwise.
+ */
+int readfile(const char *path, char **buf, size_t *size)
+{
+       int fd;
+       struct stat st;
+
+       fd = open(path, O_RDONLY);
+       if (fd == -1)
+               return errno;
+       if (fstat(fd, &st))
+               return errno;
+       if (!S_ISREG(st.st_mode))
+               return EISDIR;
+       *buf = xmalloc(st.st_size + 1);
+       *size = read_in_full(fd, *buf, st.st_size);
+       (*buf)[*size] = '\0';
+       return (*size == st.st_size ? 0 : errno);
+}
index 6d2f93ffc05444b4ca34c17632c5f4f05e6a347d..7c7aa9bc28a714047ad2863319eb2ad52bc5ea5b 100644 (file)
 
 time_t read_agefile(char *path)
 {
-       FILE *f;
-       static char buf[64], buf2[64];
+       time_t result;
+       size_t size;
+       char *buf;
+       static char buf2[64];
 
-       if (!(f = fopen(path, "r")))
+       if (readfile(path, &buf, &size))
                return -1;
-       buf[0] = 0;
-       if (fgets(buf, sizeof(buf), f) == NULL)
-               return -1;
-       fclose(f);
+
        if (parse_date(buf, buf2, sizeof(buf2)))
-               return strtoul(buf2, NULL, 10);
+               result = strtoul(buf2, NULL, 10);
        else
-               return 0;
+               result = 0;
+       free(buf);
+       return result;
 }
 
 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)