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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 1, 2018 at 02:18 UTC
81c58cd452c3da42abdcbec77cb59cf1352216ea
1 file changed
+25
-24
pack-write.c
+25
-24
@@ -122,7 +122,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
122
uint32_t offset = htonl(obj->offset);
123
sha1write(f, &offset, 4);
124
}
125
- sha1write(f, obj->oid.hash, 20);
125
+ sha1write(f, obj->oid.hash, the_hash_algo->rawsz);
126
if ((opts->flags & WRITE_IDX_STRICT) &&
127
(i && !oidcmp(&list[-2]->oid, &obj->oid)))
128
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
169
}
170
}
171
172
- sha1write(f, sha1, 20);
172
+ sha1write(f, sha1, the_hash_algo->rawsz);
173
sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
174
? CSUM_CLOSE : CSUM_FSYNC));
175
return index_name;
@@ -203,20 +203,20 @@ off_t write_pack_header(struct sha1file *f, uint32_t nr_entries)
203
* interested in the resulting SHA1 of pack data above partial_pack_offset.
204
*/
205
void fixup_pack_header_footer(int pack_fd,
206
- unsigned char *new_pack_sha1,
206
+ unsigned char *new_pack_hash,
207
const char *pack_name,
208
uint32_t object_count,
209
- unsigned char *partial_pack_sha1,
209
+ unsigned char *partial_pack_hash,
210
off_t partial_pack_offset)
211
{
212
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;
214
struct pack_header hdr;
215
char *buf;
216
ssize_t read_result;
217
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);
220
221
if (lseek(pack_fd, 0, SEEK_SET) != 0)
222
die_errno("Failed seeking to start of '%s'", pack_name);
@@ -228,9 +228,9 @@ void fixup_pack_header_footer(int pack_fd,
228
pack_name);
229
if (lseek(pack_fd, 0, SEEK_SET) != 0)
230
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));
232
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));
234
write_or_die(pack_fd, &hdr, sizeof(hdr));
235
partial_pack_offset -= sizeof(hdr);
236
@@ -238,28 +238,28 @@ void fixup_pack_header_footer(int pack_fd,
238
aligned_sz = buf_sz - sizeof(hdr);
239
for (;;) {
240
ssize_t m, n;
241
- m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
241
+ m = (partial_pack_hash && partial_pack_offset < aligned_sz) ?
242
partial_pack_offset : aligned_sz;
243
n = xread(pack_fd, buf, m);
244
if (!n)
245
break;
246
if (n < 0)
247
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);
249
250
aligned_sz -= n;
251
if (!aligned_sz)
252
aligned_sz = buf_sz;
253
254
- if (!partial_pack_sha1)
254
+ if (!partial_pack_hash)
255
continue;
256
257
- git_SHA1_Update(&old_sha1_ctx, buf, n);
257
+ the_hash_algo->update_fn(&old_hash_ctx, buf, n);
258
partial_pack_offset -= n;
259
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)
263
die("Unexpected checksum for %s "
264
"(disk corruption?)", pack_name);
265
/*
@@ -267,23 +267,24 @@ void fixup_pack_header_footer(int pack_fd,
267
* pack, which also means making partial_pack_offset
268
* big enough not to matter anymore.
269
*/
270
- git_SHA1_Init(&old_sha1_ctx);
270
+ the_hash_algo->init_fn(&old_hash_ctx);
271
partial_pack_offset = ~partial_pack_offset;
272
partial_pack_offset -= MSB(partial_pack_offset, 1);
273
}
274
}
275
free(buf);
276
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);
281
fsync_or_die(pack_fd, pack_name);
282
}
283
284
char *index_pack_lockfile(int ip_out)
285
{
286
- char packname[46];
286
+ char packname[GIT_MAX_HEXSZ + 6];
287
+ const int len = the_hash_algo->hexsz + 6;
288
289
/*
290
* The first thing we expect from index-pack's output
@@ -292,9 +293,9 @@ char *index_pack_lockfile(int ip_out)
293
* case, we need it to remove the corresponding .keep file
294
* later on. If we don't get that then tough luck with it.
295
*/
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') {
297
const char *name;
297
- packname[45] = 0;
298
+ packname[len-1] = 0;
299
if (skip_prefix(packname, "keep\t", &name))
300
return xstrfmt("%s/pack/pack-%s.keep",
301
get_object_directory(), name);