cgit

commit e16f1783346a090e4ea1194dcaae7f03e813f6a2

Author: Lars Hjemli <hjemli@gmail.com>

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>

 cgit.h | 1 +
 scan-tree.c | 16 ++--------------
 shared.c | 21 +++++++++++++++++++++
 ui-repolist.c | 19 ++++++++++---------


diff --git a/cgit.h b/cgit.h
index d90ccdcafff809bff7734f1949f3a84f5cbd18ee..adb8da4f49be0499eb83ed4a40794f11825be96a 100644
--- a/cgit.h
+++ b/cgit.h
@@ -283,5 +283,6 @@
 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 47f3988b3723673b0ed06b104f41822711db46cf..95dc65b3ce1fdd9a27e9e0bfccfa78e54f562f23 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -35,25 +35,13 @@
 	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 @@ 	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))




diff --git a/shared.c b/shared.c
index 911a55a9a4476482b6333bd82d9abb515787021c..4cb95739c197d28b9fa381ff192d09cba7f5c473 100644
--- a/shared.c
+++ b/shared.c
@@ -393,3 +393,24 @@ 	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 6d2f93ffc05444b4ca34c17632c5f4f05e6a347d..7c7aa9bc28a714047ad2863319eb2ad52bc5ea5b 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -18,19 +18,20 @@ #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)