csum-file: refactor finalize_hashfile() method
If we want to use a hashfile on the temporary file for a lockfile, then we need finalize_hashfile() to fully write the trailing hash but also keep the file descriptor open. Do this by adding a new CSUM_HASH_IN_STREAM flag along with a functional change that checks this flag before writing the checksum to the stream. This differs from previous behavior since it would be written if either CSUM_CLOSE or CSUM_FSYNC is provided. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Apr 2, 2018 at 16:34 UTC
cfe83216e404223ce8c5f6ef79c4ba9a27ff872e
6 files changed
+14
-12
builtin/pack-objects.c
+2
-2
@@ -837,9 +837,9 @@ static void write_pack_file(void)
837
* If so, rewrite it like in fast-import
838
*/
839
if (pack_to_stdout) {
840
- finalize_hashfile(f, oid.hash, CSUM_CLOSE);
840
+ finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE);
841
} else if (nr_written == nr_remaining) {
842
- finalize_hashfile(f, oid.hash, CSUM_FSYNC);
842
+ finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
843
} else {
844
int fd = finalize_hashfile(f, oid.hash, 0);
845
fixup_pack_header_footer(fd, oid.hash, pack_tmp_name,
bulk-checkin.c
+1
-1
@@ -35,7 +35,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
35
unlink(state->pack_tmp_name);
36
goto clear_exit;
37
} else if (state->nr_written == 1) {
38
- finalize_hashfile(state->f, oid.hash, CSUM_FSYNC);
38
+ finalize_hashfile(state->f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
39
} else {
40
int fd = finalize_hashfile(state->f, oid.hash, 0);
41
fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name,
csum-file.c
+4
-4
@@ -61,11 +61,11 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int fl
61
the_hash_algo->final_fn(f->buffer, &f->ctx);
62
if (result)
63
hashcpy(result, f->buffer);
64
- if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
65
- /* write checksum and close fd */
64
+ if (flags & CSUM_HASH_IN_STREAM)
65
flush(f, f->buffer, the_hash_algo->rawsz);
67
- if (flags & CSUM_FSYNC)
68
- fsync_or_die(f->fd, f->name);
66
+ if (flags & CSUM_FSYNC)
67
+ fsync_or_die(f->fd, f->name);
68
+ if (flags & CSUM_CLOSE) {
69
if (close(f->fd))
70
die_errno("%s: sha1 file error on close", f->name);
71
fd = 0;
csum-file.h
+3
-2
@@ -27,8 +27,9 @@ extern void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *)
27
extern int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
28
29
/* finalize_hashfile flags */
30
-#define CSUM_CLOSE 1
31
-#define CSUM_FSYNC 2
30
+#define CSUM_CLOSE 1
31
+#define CSUM_FSYNC 2
32
+#define CSUM_HASH_IN_STREAM 4
33
34
extern struct hashfile *hashfd(int fd, const char *name);
35
extern struct hashfile *hashfd_check(const char *name);
pack-bitmap-write.c
+1
-1
@@ -535,7 +535,7 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
535
if (options & BITMAP_OPT_HASH_CACHE)
536
write_hash_cache(f, index, index_nr);
537
538
- finalize_hashfile(f, NULL, CSUM_FSYNC);
538
+ finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
539
540
if (adjust_shared_perm(tmp_file.buf))
541
die_errno("unable to make temporary bitmap file readable");
pack-write.c
+3
-2
@@ -170,8 +170,9 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
170
}
171
172
hashwrite(f, sha1, the_hash_algo->rawsz);
173
- finalize_hashfile(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
174
- ? CSUM_CLOSE : CSUM_FSYNC));
173
+ finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_CLOSE |
174
+ ((opts->flags & WRITE_IDX_VERIFY)
175
+ ? 0 : CSUM_FSYNC));
176
return index_name;
177
}
178