Skip to content

Commit 81c58cd

Browse files
bk2204gitster
authored andcommitted
pack-write: switch various SHA-1 values to abstract forms
Convert various uses of hardcoded 20- and 40-based numbers to use the_hash_algo, along with direct calls to SHA-1. Adjust the names of variables to refer to "hash" instead of "sha1". Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ccc12e0 commit 81c58cd

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

pack-write.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
122122
uint32_t offset = htonl(obj->offset);
123123
sha1write(f, &offset, 4);
124124
}
125-
sha1write(f, obj->oid.hash, 20);
125+
sha1write(f, obj->oid.hash, the_hash_algo->rawsz);
126126
if ((opts->flags & WRITE_IDX_STRICT) &&
127127
(i && !oidcmp(&list[-2]->oid, &obj->oid)))
128128
die("The same object %s appears twice in the pack",
@@ -169,7 +169,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
169169
}
170170
}
171171

172-
sha1write(f, sha1, 20);
172+
sha1write(f, sha1, the_hash_algo->rawsz);
173173
sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
174174
? CSUM_CLOSE : CSUM_FSYNC));
175175
return index_name;
@@ -203,20 +203,20 @@ off_t write_pack_header(struct sha1file *f, uint32_t nr_entries)
203203
* interested in the resulting SHA1 of pack data above partial_pack_offset.
204204
*/
205205
void fixup_pack_header_footer(int pack_fd,
206-
unsigned char *new_pack_sha1,
206+
unsigned char *new_pack_hash,
207207
const char *pack_name,
208208
uint32_t object_count,
209-
unsigned char *partial_pack_sha1,
209+
unsigned char *partial_pack_hash,
210210
off_t partial_pack_offset)
211211
{
212212
int aligned_sz, buf_sz = 8 * 1024;
213-
git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
213+
git_hash_ctx old_hash_ctx, new_hash_ctx;
214214
struct pack_header hdr;
215215
char *buf;
216216
ssize_t read_result;
217217

218-
git_SHA1_Init(&old_sha1_ctx);
219-
git_SHA1_Init(&new_sha1_ctx);
218+
the_hash_algo->init_fn(&old_hash_ctx);
219+
the_hash_algo->init_fn(&new_hash_ctx);
220220

221221
if (lseek(pack_fd, 0, SEEK_SET) != 0)
222222
die_errno("Failed seeking to start of '%s'", pack_name);
@@ -228,62 +228,63 @@ void fixup_pack_header_footer(int pack_fd,
228228
pack_name);
229229
if (lseek(pack_fd, 0, SEEK_SET) != 0)
230230
die_errno("Failed seeking to start of '%s'", pack_name);
231-
git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
231+
the_hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr));
232232
hdr.hdr_entries = htonl(object_count);
233-
git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
233+
the_hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr));
234234
write_or_die(pack_fd, &hdr, sizeof(hdr));
235235
partial_pack_offset -= sizeof(hdr);
236236

237237
buf = xmalloc(buf_sz);
238238
aligned_sz = buf_sz - sizeof(hdr);
239239
for (;;) {
240240
ssize_t m, n;
241-
m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
241+
m = (partial_pack_hash && partial_pack_offset < aligned_sz) ?
242242
partial_pack_offset : aligned_sz;
243243
n = xread(pack_fd, buf, m);
244244
if (!n)
245245
break;
246246
if (n < 0)
247247
die_errno("Failed to checksum '%s'", pack_name);
248-
git_SHA1_Update(&new_sha1_ctx, buf, n);
248+
the_hash_algo->update_fn(&new_hash_ctx, buf, n);
249249

250250
aligned_sz -= n;
251251
if (!aligned_sz)
252252
aligned_sz = buf_sz;
253253

254-
if (!partial_pack_sha1)
254+
if (!partial_pack_hash)
255255
continue;
256256

257-
git_SHA1_Update(&old_sha1_ctx, buf, n);
257+
the_hash_algo->update_fn(&old_hash_ctx, buf, n);
258258
partial_pack_offset -= n;
259259
if (partial_pack_offset == 0) {
260-
unsigned char sha1[20];
261-
git_SHA1_Final(sha1, &old_sha1_ctx);
262-
if (hashcmp(sha1, partial_pack_sha1) != 0)
260+
unsigned char hash[GIT_MAX_RAWSZ];
261+
the_hash_algo->final_fn(hash, &old_hash_ctx);
262+
if (hashcmp(hash, partial_pack_hash) != 0)
263263
die("Unexpected checksum for %s "
264264
"(disk corruption?)", pack_name);
265265
/*
266266
* Now let's compute the SHA1 of the remainder of the
267267
* pack, which also means making partial_pack_offset
268268
* big enough not to matter anymore.
269269
*/
270-
git_SHA1_Init(&old_sha1_ctx);
270+
the_hash_algo->init_fn(&old_hash_ctx);
271271
partial_pack_offset = ~partial_pack_offset;
272272
partial_pack_offset -= MSB(partial_pack_offset, 1);
273273
}
274274
}
275275
free(buf);
276276

277-
if (partial_pack_sha1)
278-
git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
279-
git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
280-
write_or_die(pack_fd, new_pack_sha1, 20);
277+
if (partial_pack_hash)
278+
the_hash_algo->final_fn(partial_pack_hash, &old_hash_ctx);
279+
the_hash_algo->final_fn(new_pack_hash, &new_hash_ctx);
280+
write_or_die(pack_fd, new_pack_hash, the_hash_algo->rawsz);
281281
fsync_or_die(pack_fd, pack_name);
282282
}
283283

284284
char *index_pack_lockfile(int ip_out)
285285
{
286-
char packname[46];
286+
char packname[GIT_MAX_HEXSZ + 6];
287+
const int len = the_hash_algo->hexsz + 6;
287288

288289
/*
289290
* The first thing we expect from index-pack's output
@@ -292,9 +293,9 @@ char *index_pack_lockfile(int ip_out)
292293
* case, we need it to remove the corresponding .keep file
293294
* later on. If we don't get that then tough luck with it.
294295
*/
295-
if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n') {
296+
if (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\n') {
296297
const char *name;
297-
packname[45] = 0;
298+
packname[len-1] = 0;
298299
if (skip_prefix(packname, "keep\t", &name))
299300
return xstrfmt("%s/pack/pack-%s.keep",
300301
get_object_directory(), name);

0 commit comments

Comments
 (0)