ignore empty files in refs directory; convert parse_ref_file() to getline(3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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) {