Commit c30018ad059b2c8e989a8c469f19a17ab9e17d86

Stefan Sperling 2019-10-21T09:04:57

ignore empty files in refs directory; convert parse_ref_file() to getline(3)

diff --git a/lib/reference.c b/lib/reference.c
index 162f19c..b7c702a 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -165,9 +165,9 @@ parse_ref_file(struct got_reference **ref, const char *name,
 {
 	const struct got_error *err = NULL;
 	FILE *f;
-	char *line;
-	size_t len;
-	const char delim[3] = {'\0', '\0', '\0'};
+	char *line = NULL;
+	size_t linesize = 0;
+	ssize_t linelen;
 	struct got_lockfile *lf = NULL;
 
 	if (lock) {
@@ -183,13 +183,20 @@ parse_ref_file(struct got_reference **ref, const char *name,
 		return NULL;
 	}
 
-	line = fparseln(f, &len, NULL, delim, 0);
-	if (line == NULL) {
-		err = got_error(GOT_ERR_BAD_REF_DATA);
+	linelen = getline(&line, &linesize, f);
+	if (linelen == -1) {
+		if (feof(f))
+			err = NULL; /* ignore empty files (could be locks) */
+		else
+			err = got_error_from_errno2("getline", abspath);
 		if (lock)
 			got_lockfile_unlock(lf);
 		goto done;
 	}
+	while (linelen > 0 && line[linelen - 1] == '\n') {
+		line[linelen - 1] = '\0';
+		linelen--;
+	}
 
 	err = parse_ref_line(ref, name, line);
 	if (lock) {