f85f2d334d4762119f403d8a42292486e15640aa
   1#include "cache.h"
   2#include "csum-file.h"
   3#include "lockfile.h"
   4#include "midx.h"
   5
   6#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
   7#define MIDX_VERSION 1
   8#define MIDX_HASH_VERSION 1
   9#define MIDX_HEADER_SIZE 12
  10
  11static char *get_midx_filename(const char *object_dir)
  12{
  13        return xstrfmt("%s/pack/multi-pack-index", object_dir);
  14}
  15
  16static 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
  33int write_midx_file(const char *object_dir)
  34{
  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
  56        return 0;
  57}