midx: write header information to lockfile
authorDerrick Stolee <stolee@gmail.com>
Thu, 12 Jul 2018 19:39:22 +0000 (15:39 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Jul 2018 18:27:28 +0000 (11:27 -0700)
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 <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx.c
t/t5319-multi-pack-index.sh

diff --git a/midx.c b/midx.c
index 32468db..f85f2d3 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -1,7 +1,57 @@
 #include "cache.h"
+#include "csum-file.h"
+#include "lockfile.h"
 #include "midx.h"
 
+#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
+#define MIDX_VERSION 1
+#define MIDX_HASH_VERSION 1
+#define MIDX_HEADER_SIZE 12
+
+static char *get_midx_filename(const char *object_dir)
+{
+       return xstrfmt("%s/pack/multi-pack-index", object_dir);
+}
+
+static size_t write_midx_header(struct hashfile *f,
+                               unsigned char num_chunks,
+                               uint32_t num_packs)
+{
+       unsigned char byte_values[4];
+
+       hashwrite_be32(f, MIDX_SIGNATURE);
+       byte_values[0] = MIDX_VERSION;
+       byte_values[1] = MIDX_HASH_VERSION;
+       byte_values[2] = num_chunks;
+       byte_values[3] = 0; /* unused */
+       hashwrite(f, byte_values, sizeof(byte_values));
+       hashwrite_be32(f, num_packs);
+
+       return MIDX_HEADER_SIZE;
+}
+
 int write_midx_file(const char *object_dir)
 {
+       unsigned char num_chunks = 0;
+       char *midx_name;
+       struct hashfile *f = NULL;
+       struct lock_file lk;
+
+       midx_name = get_midx_filename(object_dir);
+       if (safe_create_leading_directories(midx_name)) {
+               UNLEAK(midx_name);
+               die_errno(_("unable to create leading directories of %s"),
+                         midx_name);
+       }
+
+       hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
+       f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
+       FREE_AND_NULL(midx_name);
+
+       write_midx_header(f, num_chunks, 0);
+
+       finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
+       commit_lock_file(&lk);
+
        return 0;
 }
index ec3ddbe..50e80f8 100755 (executable)
@@ -4,7 +4,9 @@ test_description='multi-pack-indexes'
 . ./test-lib.sh
 
 test_expect_success 'write midx with no packs' '
-       git multi-pack-index --object-dir=. write
+       test_when_finished rm -f pack/multi-pack-index &&
+       git multi-pack-index --object-dir=. write &&
+       test_path_is_file pack/multi-pack-index
 '
 
 test_done