server-info: use strbuf to read old info/packs file

This old code uses fgets with a fixed-size buffer. Let's use a strbuf instead, so we don't have to wonder if "1000" is big enough, or what happens if we see a long line. This also lets us drop our custom code to trim the newline. Probably nobody actually cares about the 1000-char limit (after all, the lines generally only say "P pack-[0-9a-f]{40}.pack"), so this is mostly just about cleanup/readability. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 5, 2019 at 14:13 UTC 4ecbd6492cde3fcf003c387414b3e9983fb2ba4b
1 file changed +8 -10
server-info.c
+8 -10
@@ -131,7 +131,7 @@ static int parse_pack_def(const char *packname, int old_cnt)
131 static int read_pack_info_file(const char *infofile)
132 {
133 FILE *fp;
134 - char line[1000];
134 + struct strbuf line = STRBUF_INIT;
135 int old_cnt = 0;
136 int stale = 1;
137
@@ -139,32 +139,30 @@ static int read_pack_info_file(const char *infofile)
139 if (!fp)
140 return 1; /* nonexistent is not an error. */
141
142 - while (fgets(line, sizeof(line), fp)) {
142 + while (strbuf_getline(&line, fp) != EOF) {
143 const char *arg;
144 - int len = strlen(line);
145 - if (len && line[len-1] == '\n')
146 - line[--len] = 0;
144
148 - if (!len)
145 + if (!line.len)
146 continue;
147
151 - if (skip_prefix(line, "P ", &arg)) {
148 + if (skip_prefix(line.buf, "P ", &arg)) {
149 /* P name */
150 if (parse_pack_def(arg, old_cnt++))
151 goto out_stale;
155 - } else if (line[0] == 'D') {
152 + } else if (line.buf[0] == 'D') {
153 /* we used to emit D but that was misguided. */
154 goto out_stale;
158 - } else if (line[0] == 'T') {
155 + } else if (line.buf[0] == 'T') {
156 /* we used to emit T but nobody uses it. */
157 goto out_stale;
158 } else {
162 - error("unrecognized: %s", line);
159 + error("unrecognized: %s", line.buf);
160 }
161 }
162 stale = 0;
163
164 out_stale:
165 + strbuf_release(&line);
166 fclose(fp);
167 return stale;
168 }