Refactor the pack header reading function out of receive-pack.c
authorJunio C Hamano <junkio@cox.net>
Tue, 23 Jan 2007 05:55:18 +0000 (21:55 -0800)
committerJunio C Hamano <junkio@cox.net>
Thu, 25 Jan 2007 02:08:02 +0000 (18:08 -0800)
Signed-off-by: Junio C Hamano <junkio@cox.net>
pack.h
receive-pack.c
sha1_file.c

diff --git a/pack.h b/pack.h
index 821706fbcd6e6989af8f3dcb37eec32e7a519f71..deb427edbe43710dd2b76a3af0dbd87750344103 100644 (file)
--- a/pack.h
+++ b/pack.h
@@ -44,4 +44,9 @@ struct pack_header {
 #define PACK_IDX_SIGNATURE 0xff744f63  /* "\377tOc" */
 
 extern int verify_pack(struct packed_git *, int);
+
+#define PH_ERROR_EOF           (-1)
+#define PH_ERROR_PACK_SIGNATURE        (-2)
+#define PH_ERROR_PROTOCOL      (-3)
+extern int read_pack_header(int fd, struct pack_header *);
 #endif
index 6333f00c6eed5038d8da52dcedc48b80a1c73d46..b3a45526923772acc1cd0efea1523d0ace03487a 100644 (file)
@@ -250,20 +250,22 @@ static void read_head_info(void)
 
 static const char *parse_pack_header(struct pack_header *hdr)
 {
-       char *c = (char*)hdr;
-       ssize_t remaining = sizeof(struct pack_header);
-       do {
-               ssize_t r = xread(0, c, remaining);
-               if (r <= 0)
-                       return "eof before pack header was fully read";
-               remaining -= r;
-               c += r;
-       } while (remaining > 0);
-       if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
+       switch (read_pack_header(0, hdr)) {
+       case PH_ERROR_EOF:
+               return "eof before pack header was fully read";
+
+       case PH_ERROR_PACK_SIGNATURE:
                return "protocol error (pack signature mismatch detected)";
-       if (!pack_version_ok(hdr->hdr_version))
+
+       case PH_ERROR_PROTOCOL:
                return "protocol error (pack version unsupported)";
-       return NULL;
+
+       default:
+               return "unknown error in parse_pack_header";
+
+       case 0:
+               return NULL;
+       }
 }
 
 static const char *pack_lockfile;
index 43ff4023808cc8b5a5f54c75ff77e80776a581aa..498665e50c1a5053e29a80c37ad95b800bb0f86d 100644 (file)
@@ -2048,3 +2048,24 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
        }
        return 0;
 }
+
+int read_pack_header(int fd, struct pack_header *header)
+{
+       char *c = (char*)header;
+       ssize_t remaining = sizeof(struct pack_header);
+       do {
+               ssize_t r = xread(fd, c, remaining);
+               if (r <= 0)
+                       /* "eof before pack header was fully read" */
+                       return PH_ERROR_EOF;
+               remaining -= r;
+               c += r;
+       } while (remaining > 0);
+       if (header->hdr_signature != htonl(PACK_SIGNATURE))
+               /* "protocol error (pack signature mismatch detected)" */
+               return PH_ERROR_PACK_SIGNATURE;
+       if (!pack_version_ok(header->hdr_version))
+               /* "protocol error (pack version unsupported)" */
+               return PH_ERROR_PROTOCOL;
+       return 0;
+}