Skip to content

Commit aab6135

Browse files
bk2204gitster
authored andcommitted
read-cache: abstract away uses of SHA-1
Convert various uses of direct calls to SHA-1 and 20- and 40-based constants to use the_hash_algo instead. Don't yet convert the on-disk data structures, which will be handled in a future commit. Adjust some comments so as not to refer explicitly to SHA-1. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 81c58cd commit aab6135

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

read-cache.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,8 +1545,8 @@ int verify_ce_order;
15451545

15461546
static int verify_hdr(struct cache_header *hdr, unsigned long size)
15471547
{
1548-
git_SHA_CTX c;
1549-
unsigned char sha1[20];
1548+
git_hash_ctx c;
1549+
unsigned char hash[GIT_MAX_RAWSZ];
15501550
int hdr_version;
15511551

15521552
if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
@@ -1558,10 +1558,10 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
15581558
if (!verify_index_checksum)
15591559
return 0;
15601560

1561-
git_SHA1_Init(&c);
1562-
git_SHA1_Update(&c, hdr, size - 20);
1563-
git_SHA1_Final(sha1, &c);
1564-
if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
1561+
the_hash_algo->init_fn(&c);
1562+
the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
1563+
the_hash_algo->final_fn(hash, &c);
1564+
if (hashcmp(hash, (unsigned char *)hdr + size - the_hash_algo->rawsz))
15651565
return error("bad index file sha1 signature");
15661566
return 0;
15671567
}
@@ -1791,7 +1791,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
17911791
die_errno("cannot stat the open index");
17921792

17931793
mmap_size = xsize_t(st.st_size);
1794-
if (mmap_size < sizeof(struct cache_header) + 20)
1794+
if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
17951795
die("index file smaller than expected");
17961796

17971797
mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
@@ -1803,7 +1803,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
18031803
if (verify_hdr(hdr, mmap_size) < 0)
18041804
goto unmap;
18051805

1806-
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
1806+
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz);
18071807
istate->version = ntohl(hdr->hdr_version);
18081808
istate->cache_nr = ntohl(hdr->hdr_entries);
18091809
istate->cache_alloc = alloc_nr(istate->cache_nr);
@@ -1831,7 +1831,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
18311831
istate->timestamp.sec = st.st_mtime;
18321832
istate->timestamp.nsec = ST_MTIME_NSEC(st);
18331833

1834-
while (src_offset <= mmap_size - 20 - 8) {
1834+
while (src_offset <= mmap_size - the_hash_algo->rawsz - 8) {
18351835
/* After an array of active_nr index entries,
18361836
* there can be arbitrary number of extended
18371837
* sections, each of which is prefixed with
@@ -1957,19 +1957,19 @@ int unmerged_index(const struct index_state *istate)
19571957
static unsigned char write_buffer[WRITE_BUFFER_SIZE];
19581958
static unsigned long write_buffer_len;
19591959

1960-
static int ce_write_flush(git_SHA_CTX *context, int fd)
1960+
static int ce_write_flush(git_hash_ctx *context, int fd)
19611961
{
19621962
unsigned int buffered = write_buffer_len;
19631963
if (buffered) {
1964-
git_SHA1_Update(context, write_buffer, buffered);
1964+
the_hash_algo->update_fn(context, write_buffer, buffered);
19651965
if (write_in_full(fd, write_buffer, buffered) < 0)
19661966
return -1;
19671967
write_buffer_len = 0;
19681968
}
19691969
return 0;
19701970
}
19711971

1972-
static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len)
1972+
static int ce_write(git_hash_ctx *context, int fd, void *data, unsigned int len)
19731973
{
19741974
while (len) {
19751975
unsigned int buffered = write_buffer_len;
@@ -1991,7 +1991,7 @@ static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len)
19911991
return 0;
19921992
}
19931993

1994-
static int write_index_ext_header(git_SHA_CTX *context, int fd,
1994+
static int write_index_ext_header(git_hash_ctx *context, int fd,
19951995
unsigned int ext, unsigned int sz)
19961996
{
19971997
ext = htonl(ext);
@@ -2000,26 +2000,26 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd,
20002000
(ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
20012001
}
20022002

2003-
static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
2003+
static int ce_flush(git_hash_ctx *context, int fd, unsigned char *hash)
20042004
{
20052005
unsigned int left = write_buffer_len;
20062006

20072007
if (left) {
20082008
write_buffer_len = 0;
2009-
git_SHA1_Update(context, write_buffer, left);
2009+
the_hash_algo->update_fn(context, write_buffer, left);
20102010
}
20112011

2012-
/* Flush first if not enough space for SHA1 signature */
2013-
if (left + 20 > WRITE_BUFFER_SIZE) {
2012+
/* Flush first if not enough space for hash signature */
2013+
if (left + the_hash_algo->rawsz > WRITE_BUFFER_SIZE) {
20142014
if (write_in_full(fd, write_buffer, left) < 0)
20152015
return -1;
20162016
left = 0;
20172017
}
20182018

2019-
/* Append the SHA1 signature at the end */
2020-
git_SHA1_Final(write_buffer + left, context);
2021-
hashcpy(sha1, write_buffer + left);
2022-
left += 20;
2019+
/* Append the hash signature at the end */
2020+
the_hash_algo->final_fn(write_buffer + left, context);
2021+
hashcpy(hash, write_buffer + left);
2022+
left += the_hash_algo->rawsz;
20232023
return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
20242024
}
20252025

@@ -2100,7 +2100,7 @@ static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
21002100
}
21012101
}
21022102

2103-
static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
2103+
static int ce_write_entry(git_hash_ctx *c, int fd, struct cache_entry *ce,
21042104
struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
21052105
{
21062106
int size;
@@ -2167,7 +2167,7 @@ static int verify_index_from(const struct index_state *istate, const char *path)
21672167
int fd;
21682168
ssize_t n;
21692169
struct stat st;
2170-
unsigned char sha1[20];
2170+
unsigned char hash[GIT_MAX_RAWSZ];
21712171

21722172
if (!istate->initialized)
21732173
return 0;
@@ -2179,14 +2179,14 @@ static int verify_index_from(const struct index_state *istate, const char *path)
21792179
if (fstat(fd, &st))
21802180
goto out;
21812181

2182-
if (st.st_size < sizeof(struct cache_header) + 20)
2182+
if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
21832183
goto out;
21842184

2185-
n = pread_in_full(fd, sha1, 20, st.st_size - 20);
2186-
if (n != 20)
2185+
n = pread_in_full(fd, hash, the_hash_algo->rawsz, st.st_size - the_hash_algo->rawsz);
2186+
if (n != the_hash_algo->rawsz)
21872187
goto out;
21882188

2189-
if (hashcmp(istate->sha1, sha1))
2189+
if (hashcmp(istate->sha1, hash))
21902190
goto out;
21912191

21922192
close(fd);
@@ -2235,7 +2235,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
22352235
int strip_extensions)
22362236
{
22372237
int newfd = tempfile->fd;
2238-
git_SHA_CTX c;
2238+
git_hash_ctx c;
22392239
struct cache_header hdr;
22402240
int i, err = 0, removed, extended, hdr_version;
22412241
struct cache_entry **cache = istate->cache;
@@ -2273,7 +2273,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
22732273
hdr.hdr_version = htonl(hdr_version);
22742274
hdr.hdr_entries = htonl(entries - removed);
22752275

2276-
git_SHA1_Init(&c);
2276+
the_hash_algo->init_fn(&c);
22772277
if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
22782278
return -1;
22792279

0 commit comments

Comments
 (0)