aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Lars Hjemli <hjemli@gmail.com>2009-08-19 00:17:41 (JST)
committerGravatar Lars Hjemli <hjemli@gmail.com>2009-08-19 00:22:14 (JST)
commite16f1783346a090e4ea1194dcaae7f03e813f6a2 (patch)
tree268f5ba231895ba3a63071497764c64d44733da5
parent523c133e2e5f7089a3d18ac23f2074b60991a7f0 (diff)
downloadcgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.zip
cgit-e16f1783346a090e4ea1194dcaae7f03e813f6a2.tar.gz
Add and use a common readfile() function
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>
-rw-r--r--cgit.h1
-rw-r--r--scan-tree.c16
-rw-r--r--shared.c21
-rw-r--r--ui-repolist.c19
4 files changed, 34 insertions, 23 deletions
diff --git a/cgit.h b/cgit.h
index d90ccdc..adb8da4 100644
--- a/cgit.h
+++ b/cgit.h
@@ -283,5 +283,6 @@ extern int cgit_parse_snapshots_mask(const char *str);
283extern int cgit_open_filter(struct cgit_filter *filter); 283extern int cgit_open_filter(struct cgit_filter *filter);
284extern int cgit_close_filter(struct cgit_filter *filter); 284extern int cgit_close_filter(struct cgit_filter *filter);
285 285
286extern int readfile(const char *path, char **buf, size_t *size);
286 287
287#endif /* CGIT_H */ 288#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
@@ -35,25 +35,13 @@ static int is_git_dir(const char *path)
35 return 1; 35 return 1;
36} 36}
37 37
38char *readfile(const char *path)
39{
40 FILE *f;
41 static char buf[MAX_PATH];
42
43 if (!(f = fopen(path, "r")))
44 return NULL;
45 buf[0] = 0;
46 fgets(buf, MAX_PATH, f);
47 fclose(f);
48 return buf;
49}
50
51static void add_repo(const char *base, const char *path) 38static void add_repo(const char *base, const char *path)
52{ 39{
53 struct cgit_repo *repo; 40 struct cgit_repo *repo;
54 struct stat st; 41 struct stat st;
55 struct passwd *pwd; 42 struct passwd *pwd;
56 char *p; 43 char *p;
44 size_t size;
57 45
58 if (stat(path, &st)) { 46 if (stat(path, &st)) {
59 fprintf(stderr, "Error accessing %s: %s (%d)\n", 47 fprintf(stderr, "Error accessing %s: %s (%d)\n",
@@ -80,7 +68,7 @@ static void add_repo(const char *base, const char *path)
80 68
81 p = fmt("%s/description", path); 69 p = fmt("%s/description", path);
82 if (!stat(p, &st)) 70 if (!stat(p, &st))
83 repo->desc = xstrdup(readfile(p)); 71 readfile(p, &repo->desc, &size);
84 72
85 p = fmt("%s/README.html", path); 73 p = fmt("%s/README.html", path);
86 if (!stat(p, &st)) 74 if (!stat(p, &st))
diff --git a/shared.c b/shared.c
index 911a55a..4cb9573 100644
--- a/shared.c
+++ b/shared.c
@@ -393,3 +393,24 @@ int cgit_close_filter(struct cgit_filter *filter)
393 return 0; 393 return 0;
394 die("Subprocess %s exited abnormally", filter->cmd); 394 die("Subprocess %s exited abnormally", filter->cmd);
395} 395}
396
397/* Read the content of the specified file into a newly allocated buffer,
398 * zeroterminate the buffer and return 0 on success, errno otherwise.
399 */
400int readfile(const char *path, char **buf, size_t *size)
401{
402 int fd;
403 struct stat st;
404
405 fd = open(path, O_RDONLY);
406 if (fd == -1)
407 return errno;
408 if (fstat(fd, &st))
409 return errno;
410 if (!S_ISREG(st.st_mode))
411 return EISDIR;
412 *buf = xmalloc(st.st_size + 1);
413 *size = read_in_full(fd, *buf, st.st_size);
414 (*buf)[*size] = '\0';
415 return (*size == st.st_size ? 0 : errno);
416}
diff --git a/ui-repolist.c b/ui-repolist.c
index 6d2f93f..7c7aa9b 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -18,19 +18,20 @@
18 18
19time_t read_agefile(char *path) 19time_t read_agefile(char *path)
20{ 20{
21 FILE *f; 21 time_t result;
22 static char buf[64], buf2[64]; 22 size_t size;
23 char *buf;
24 static char buf2[64];
23 25
24 if (!(f = fopen(path, "r"))) 26 if (readfile(path, &buf, &size))
25 return -1; 27 return -1;
26 buf[0] = 0; 28
27 if (fgets(buf, sizeof(buf), f) == NULL)
28 return -1;
29 fclose(f);
30 if (parse_date(buf, buf2, sizeof(buf2))) 29 if (parse_date(buf, buf2, sizeof(buf2)))
31 return strtoul(buf2, NULL, 10); 30 result = strtoul(buf2, NULL, 10);
32 else 31 else
33 return 0; 32 result = 0;
33 free(buf);
34 return result;
34} 35}
35 36
36static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) 37static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)