-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
@@ -280,8 +280,9 @@ extern const char *cgit_repobasename(const char *reponame); 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 */ diff --git a/scan-tree.c b/scan-tree.c index 47f3988..95dc65b 100644 --- a/scan-tree.c +++ b/scan-tree.c @@ -32,31 +32,19 @@ static int is_git_dir(const char *path) if (!S_ISREG(st.st_mode)) return 0; 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", path, strerror(errno), errno); return; } @@ -77,13 +65,13 @@ static void add_repo(const char *base, const char *path) repo->name = repo->url; repo->path = xstrdup(path); repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : ""); 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)) repo->readme = "README.html"; } @@ -390,6 +390,27 @@ int cgit_close_filter(struct cgit_filter *filter) return 0; waitpid(filter->pid, &filter->exitstatus, 0); if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus)) 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); +} diff --git a/ui-repolist.c b/ui-repolist.c index 6d2f93f..7c7aa9b 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -15,25 +15,26 @@ #include "cgit.h" #include "html.h" #include "ui-shared.h" 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) { char *path; struct stat s; |