-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | scan-tree.c | 16 | ||||
-rw-r--r-- | shared.c | 21 | ||||
-rw-r--r-- | ui-repolist.c | 19 |
4 files changed, 34 insertions, 23 deletions
@@ -285,2 +285,3 @@ extern int cgit_close_filter(struct cgit_filter *filter); +extern int readfile(const char *path, char **buf, size_t *size); diff --git a/scan-tree.c b/scan-tree.c index 47f3988..95dc65b 100644 --- a/scan-tree.c +++ b/scan-tree.c @@ -37,15 +37,2 @@ static int is_git_dir(const char *path) -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) @@ -56,2 +43,3 @@ static void add_repo(const char *base, const char *path) char *p; + size_t size; @@ -82,3 +70,3 @@ static void add_repo(const char *base, const char *path) if (!stat(p, &st)) - repo->desc = xstrdup(readfile(p)); + readfile(p, &repo->desc, &size); @@ -395 +395,22 @@ int cgit_close_filter(struct cgit_filter *filter) } + +/* 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); +} diff --git a/ui-repolist.c b/ui-repolist.c index 6d2f93f..7c7aa9b 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -20,15 +20,16 @@ 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; } |