{"thread":{"id":"3949","subject":"[PATCH 1/4] read-cache/write-cache: optionally return cache checksum SHA1.","startedAt":"2006-04-23T23:52:08Z","lastAt":"2006-04-23T23:52:08Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"19023","messageId":"7vodyretp3.fsf@assigned-by-dhcp.cox.net","threadId":"3949","inReplyTo":null,"subject":"[PATCH 1/4] read-cache/write-cache: optionally return cache checksum SHA1.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-04-23T23:52:08Z","receivedAt":"2006-04-23T23:52:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"read_cache_1() and write_cache_1() takes an extra parameter\n*sha1 that returns the checksum of the index file when non-NULL.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n * This is a low-impact small preparation for the following\n   three, which is an interesting optimization.\n\n cache.h      |    5 ++++-\n read-cache.c |   35 +++++++++++++++++++++++++++--------\n 2 files changed, 31 insertions(+), 9 deletions(-)\n\nc2a742f3ac14e93704c917e4d9b08881d6032281\ndiff --git a/cache.h b/cache.h\nindex 69801b0..8c9947e 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -138,8 +138,11 @@ extern const char *prefix_filename(const\n #define alloc_nr(x) (((x)+16)*3/2)\n \n /* Initialize and use the cache information */\n+extern int read_cache_1(unsigned char *);\n+extern int write_cache_1(int, struct cache_entry **, int, unsigned char *);\n extern int read_cache(void);\n-extern int write_cache(int newfd, struct cache_entry **cache, int entries);\n+extern int write_cache(int, struct cache_entry **, int);\n+\n extern int cache_name_pos(const char *name, int namelen);\n #define ADD_CACHE_OK_TO_ADD 1\t\t/* Ok to add */\n #define ADD_CACHE_OK_TO_REPLACE 2\t/* Ok to replace file/directory */\ndiff --git a/read-cache.c b/read-cache.c\nindex f97f92d..50e094e 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -496,10 +496,12 @@ int add_cache_entry(struct cache_entry *\n \treturn 0;\n }\n \n-static int verify_hdr(struct cache_header *hdr, unsigned long size)\n+static int verify_hdr(struct cache_header *hdr, unsigned long size, unsigned char *sha1)\n {\n \tSHA_CTX c;\n-\tunsigned char sha1[20];\n+\tunsigned char sha1_buf[20];\n+\tif (!sha1)\n+\t\tsha1 = sha1_buf;\n \n \tif (hdr->hdr_signature != htonl(CACHE_SIGNATURE))\n \t\treturn error(\"bad signature\");\n@@ -513,7 +515,7 @@ static int verify_hdr(struct cache_heade\n \treturn 0;\n }\n \n-int read_cache(void)\n+int read_cache_1(unsigned char *cache_sha1)\n {\n \tint fd, i;\n \tstruct stat st;\n@@ -547,7 +549,7 @@ int read_cache(void)\n \t\tdie(\"index file mmap failed (%s)\", strerror(errno));\n \n \thdr = map;\n-\tif (verify_hdr(hdr, size) < 0)\n+\tif (verify_hdr(hdr, size, cache_sha1) < 0)\n \t\tgoto unmap;\n \n \tactive_nr = ntohl(hdr->hdr_entries);\n@@ -595,7 +597,7 @@ static int ce_write(SHA_CTX *context, in\n  \treturn 0;\n }\n \n-static int ce_flush(SHA_CTX *context, int fd)\n+static int ce_flush(SHA_CTX *context, int fd, unsigned char *sha1)\n {\n \tunsigned int left = write_buffer_len;\n \n@@ -612,7 +614,8 @@ static int ce_flush(SHA_CTX *context, in\n \t}\n \n \t/* Append the SHA1 signature at the end */\n-\tSHA1_Final(write_buffer + left, context);\n+\tSHA1_Final(sha1, context);\n+\tmemcpy(write_buffer + left, sha1, 20);\n \tleft += 20;\n \tif (write(fd, write_buffer, left) != left)\n \t\treturn -1;\n@@ -663,11 +666,14 @@ static void ce_smudge_racily_clean_entry\n \t}\n }\n \n-int write_cache(int newfd, struct cache_entry **cache, int entries)\n+int write_cache_1(int newfd, struct cache_entry **cache, int entries,\n+\t\t  unsigned char *cache_sha1)\n {\n \tSHA_CTX c;\n \tstruct cache_header hdr;\n \tint i, removed;\n+\tint status;\n+\tunsigned char sha1[20];\n \n \tfor (i = removed = 0; i < entries; i++)\n \t\tif (!cache[i]->ce_mode)\n@@ -691,5 +697,18 @@ int write_cache(int newfd, struct cache_\n \t\tif (ce_write(&c, newfd, ce, ce_size(ce)) < 0)\n \t\t\treturn -1;\n \t}\n-\treturn ce_flush(&c, newfd);\n+\tstatus = ce_flush(&c, newfd, sha1);\n+\tif (cache_sha1)\n+\t\tmemcpy(cache_sha1, sha1, 20);\n+\treturn status;\n+}\n+\n+int read_cache(void)\n+{\n+\treturn read_cache_1(NULL);\n+}\n+\n+int write_cache(int newfd, struct cache_entry **cache, int entries)\n+{\n+\treturn write_cache_1(newfd, cache, entries, NULL);\n }\n-- \n1.3.0.g623a\n"}]}