Skip to content

Commit fc59e74

Browse files
derrickstoleegitster
authored andcommitted
midx: write header information to lockfile
As we begin writing the multi-pack-index format to disk, start with the basics: the 12-byte header and the 20-byte checksum footer. Start with these basics so we can add the rest of the format in small increments. As we implement the format, we will use a technique to check that our computed offsets within the multi-pack-index file match what we are actually writing. Each method that writes to the hashfile will return the number of bytes written, and we will track that those values match our expectations. Currently, write_midx_header() returns 12, but is not checked. We will check the return value in a later commit. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a340773 commit fc59e74

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

midx.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
11
#include "cache.h"
2+
#include "csum-file.h"
3+
#include "lockfile.h"
24
#include "midx.h"
35

6+
#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
7+
#define MIDX_VERSION 1
8+
#define MIDX_HASH_VERSION 1
9+
#define MIDX_HEADER_SIZE 12
10+
11+
static char *get_midx_filename(const char *object_dir)
12+
{
13+
return xstrfmt("%s/pack/multi-pack-index", object_dir);
14+
}
15+
16+
static size_t write_midx_header(struct hashfile *f,
17+
unsigned char num_chunks,
18+
uint32_t num_packs)
19+
{
20+
unsigned char byte_values[4];
21+
22+
hashwrite_be32(f, MIDX_SIGNATURE);
23+
byte_values[0] = MIDX_VERSION;
24+
byte_values[1] = MIDX_HASH_VERSION;
25+
byte_values[2] = num_chunks;
26+
byte_values[3] = 0; /* unused */
27+
hashwrite(f, byte_values, sizeof(byte_values));
28+
hashwrite_be32(f, num_packs);
29+
30+
return MIDX_HEADER_SIZE;
31+
}
32+
433
int write_midx_file(const char *object_dir)
534
{
35+
unsigned char num_chunks = 0;
36+
char *midx_name;
37+
struct hashfile *f = NULL;
38+
struct lock_file lk;
39+
40+
midx_name = get_midx_filename(object_dir);
41+
if (safe_create_leading_directories(midx_name)) {
42+
UNLEAK(midx_name);
43+
die_errno(_("unable to create leading directories of %s"),
44+
midx_name);
45+
}
46+
47+
hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
48+
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
49+
FREE_AND_NULL(midx_name);
50+
51+
write_midx_header(f, num_chunks, 0);
52+
53+
finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
54+
commit_lock_file(&lk);
55+
656
return 0;
757
}

t/t5319-multi-pack-index.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ test_description='multi-pack-indexes'
44
. ./test-lib.sh
55

66
test_expect_success 'write midx with no packs' '
7-
git multi-pack-index --object-dir=. write
7+
test_when_finished rm -f pack/multi-pack-index &&
8+
git multi-pack-index --object-dir=. write &&
9+
test_path_is_file pack/multi-pack-index
810
'
911

1012
test_done

0 commit comments

Comments
 (0)