@matheusbsbs / matheus-bagliano / commits / 1af1c2b63d
Fork 0

read-cache/write-cache: optionally return cache checksum SHA1.

read_cache_1() and write_cache_1() takes an extra parameter *sha1 that returns the checksum of the index file when non-NULL. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 23, 2006 at 16:52 UTC 1af1c2b63db6a413fbeb9b08cd55dcb735d7597d
2 files changed +31 -9
cache.h
+4 -1
@@ -138,8 +138,11 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path
138 #define alloc_nr(x) (((x)+16)*3/2)
139
140 /* Initialize and use the cache information */
141 +extern int read_cache_1(unsigned char *);
142 +extern int write_cache_1(int, struct cache_entry **, int, unsigned char *);
143 extern int read_cache(void);
142 -extern int write_cache(int newfd, struct cache_entry **cache, int entries);
144 +extern int write_cache(int, struct cache_entry **, int);
145 +
146 extern int cache_name_pos(const char *name, int namelen);
147 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
148 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
read-cache.c
+27 -8
@@ -496,10 +496,12 @@ int add_cache_entry(struct cache_entry *ce, int option)
496 return 0;
497 }
498
499 -static int verify_hdr(struct cache_header *hdr, unsigned long size)
499 +static int verify_hdr(struct cache_header *hdr, unsigned long size, unsigned char *sha1)
500 {
501 SHA_CTX c;
502 - unsigned char sha1[20];
502 + unsigned char sha1_buf[20];
503 + if (!sha1)
504 + sha1 = sha1_buf;
505
506 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
507 return error("bad signature");
@@ -513,7 +515,7 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
515 return 0;
516 }
517
516 -int read_cache(void)
518 +int read_cache_1(unsigned char *cache_sha1)
519 {
520 int fd, i;
521 struct stat st;
@@ -547,7 +549,7 @@ int read_cache(void)
549 die("index file mmap failed (%s)", strerror(errno));
550
551 hdr = map;
550 - if (verify_hdr(hdr, size) < 0)
552 + if (verify_hdr(hdr, size, cache_sha1) < 0)
553 goto unmap;
554
555 active_nr = ntohl(hdr->hdr_entries);
@@ -595,7 +597,7 @@ static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
597 return 0;
598 }
599
598 -static int ce_flush(SHA_CTX *context, int fd)
600 +static int ce_flush(SHA_CTX *context, int fd, unsigned char *sha1)
601 {
602 unsigned int left = write_buffer_len;
603
@@ -612,7 +614,8 @@ static int ce_flush(SHA_CTX *context, int fd)
614 }
615
616 /* Append the SHA1 signature at the end */
615 - SHA1_Final(write_buffer + left, context);
617 + SHA1_Final(sha1, context);
618 + memcpy(write_buffer + left, sha1, 20);
619 left += 20;
620 if (write(fd, write_buffer, left) != left)
621 return -1;
@@ -663,11 +666,14 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
666 }
667 }
668
666 -int write_cache(int newfd, struct cache_entry **cache, int entries)
669 +int write_cache_1(int newfd, struct cache_entry **cache, int entries,
670 + unsigned char *cache_sha1)
671 {
672 SHA_CTX c;
673 struct cache_header hdr;
674 int i, removed;
675 + int status;
676 + unsigned char sha1[20];
677
678 for (i = removed = 0; i < entries; i++)
679 if (!cache[i]->ce_mode)
@@ -691,5 +697,18 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
697 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
698 return -1;
699 }
694 - return ce_flush(&c, newfd);
700 + status = ce_flush(&c, newfd, sha1);
701 + if (cache_sha1)
702 + memcpy(cache_sha1, sha1, 20);
703 + return status;
704 +}
705 +
706 +int read_cache(void)
707 +{
708 + return read_cache_1(NULL);
709 +}
710 +
711 +int write_cache(int newfd, struct cache_entry **cache, int entries)
712 +{
713 + return write_cache_1(newfd, cache, entries, NULL);
714 }