@matheusbsbs / matheus-bagliano / commits / a69e542989
Fork 0

Refactor the pack header reading function out of receive-pack.c

Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 22, 2007 at 21:55 UTC a69e542989d0aa1576a4b6820454b9b0d2636796
3 files changed +40 -12
pack.h
+5
@@ -44,4 +44,9 @@ struct pack_header {
44 #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */
45
46 extern int verify_pack(struct packed_git *, int);
47 +
48 +#define PH_ERROR_EOF (-1)
49 +#define PH_ERROR_PACK_SIGNATURE (-2)
50 +#define PH_ERROR_PROTOCOL (-3)
51 +extern int read_pack_header(int fd, struct pack_header *);
52 #endif
receive-pack.c
+14 -12
@@ -250,20 +250,22 @@ static void read_head_info(void)
250
251 static const char *parse_pack_header(struct pack_header *hdr)
252 {
253 - char *c = (char*)hdr;
254 - ssize_t remaining = sizeof(struct pack_header);
255 - do {
256 - ssize_t r = xread(0, c, remaining);
257 - if (r <= 0)
258 - return "eof before pack header was fully read";
259 - remaining -= r;
260 - c += r;
261 - } while (remaining > 0);
262 - if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
253 + switch (read_pack_header(0, hdr)) {
254 + case PH_ERROR_EOF:
255 + return "eof before pack header was fully read";
256 +
257 + case PH_ERROR_PACK_SIGNATURE:
258 return "protocol error (pack signature mismatch detected)";
264 - if (!pack_version_ok(hdr->hdr_version))
259 +
260 + case PH_ERROR_PROTOCOL:
261 return "protocol error (pack version unsupported)";
266 - return NULL;
262 +
263 + default:
264 + return "unknown error in parse_pack_header";
265 +
266 + case 0:
267 + return NULL;
268 + }
269 }
270
271 static const char *pack_lockfile;
sha1_file.c
+21
@@ -2048,3 +2048,24 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2048 }
2049 return 0;
2050 }
2051 +
2052 +int read_pack_header(int fd, struct pack_header *header)
2053 +{
2054 + char *c = (char*)header;
2055 + ssize_t remaining = sizeof(struct pack_header);
2056 + do {
2057 + ssize_t r = xread(fd, c, remaining);
2058 + if (r <= 0)
2059 + /* "eof before pack header was fully read" */
2060 + return PH_ERROR_EOF;
2061 + remaining -= r;
2062 + c += r;
2063 + } while (remaining > 0);
2064 + if (header->hdr_signature != htonl(PACK_SIGNATURE))
2065 + /* "protocol error (pack signature mismatch detected)" */
2066 + return PH_ERROR_PACK_SIGNATURE;
2067 + if (!pack_version_ok(header->hdr_version))
2068 + /* "protocol error (pack version unsupported)" */
2069 + return PH_ERROR_PROTOCOL;
2070 + return 0;
2071 +}