@matheusbsbs / matheus-bagliano / commits / 21a02980f9
Fork 0

builtin-bundle - use buffered reads for bundle header

This eliminates all use of byte-at-a-time reading of data in this function: as Junio noted, a bundle file is seekable so we can reset the file position to the first part of the pack-file using lseek after reading the header. Signed-off-by: Mark Levedahl <mdl123@verizon.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mark Levedahl committed Aug 10, 2007 at 20:39 UTC 21a02980f9025c3a338fb897796542ddef8707d1
1 file changed +13 -24
builtin-bundle.c
+13 -24
@@ -44,38 +44,21 @@ struct bundle_header {
44 struct ref_list references;
45 };
46
47 -/* this function returns the length of the string */
48 -static int read_string(int fd, char *buffer, int size)
49 -{
50 - int i;
51 - for (i = 0; i < size - 1; i++) {
52 - ssize_t count = xread(fd, buffer + i, 1);
53 - if (count < 0)
54 - return error("Read error: %s", strerror(errno));
55 - if (count == 0) {
56 - i--;
57 - break;
58 - }
59 - if (buffer[i] == '\n')
60 - break;
61 - }
62 - buffer[i + 1] = '\0';
63 - return i + 1;
64 -}
65 -
47 /* returns an fd */
48 static int read_header(const char *path, struct bundle_header *header) {
49 char buffer[1024];
69 - int fd = open(path, O_RDONLY);
50 + int fd;
51 + long fpos;
52 + FILE *ffd = fopen(path, "rb");
53
71 - if (fd < 0)
54 + if (!ffd)
55 return error("could not open '%s'", path);
73 - if (read_string(fd, buffer, sizeof(buffer)) < 0 ||
56 + if (!fgets(buffer, sizeof(buffer), ffd) ||
57 strcmp(buffer, bundle_signature)) {
75 - close(fd);
58 + fclose(ffd);
59 return error("'%s' does not look like a v2 bundle file", path);
60 }
78 - while (read_string(fd, buffer, sizeof(buffer)) > 0
61 + while (fgets(buffer, sizeof(buffer), ffd)
62 && buffer[0] != '\n') {
63 int is_prereq = buffer[0] == '-';
64 int offset = is_prereq ? 1 : 0;
@@ -97,6 +80,12 @@ static int read_header(const char *path, struct bundle_header *header) {
80 add_to_ref_list(sha1, isspace(delim) ?
81 buffer + 41 + offset : "", list);
82 }
83 + fpos = ftell(ffd);
84 + fclose(ffd);
85 + fd = open(path, O_RDONLY);
86 + if (fd < 0)
87 + return error("could not open '%s'", path);
88 + lseek(fd, fpos, SEEK_SET);
89 return fd;
90 }
91