pack-write.con commit apply: remove directory that becomes empty by renaming the last file away (9396943)
   1#include "cache.h"
   2#include "pack.h"
   3
   4void fixup_pack_header_footer(int pack_fd,
   5                         unsigned char *pack_file_sha1,
   6                         const char *pack_name,
   7                         uint32_t object_count)
   8{
   9        static const int buf_sz = 128 * 1024;
  10        SHA_CTX c;
  11        struct pack_header hdr;
  12        char *buf;
  13
  14        if (lseek(pack_fd, 0, SEEK_SET) != 0)
  15                die("Failed seeking to start: %s", strerror(errno));
  16        if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
  17                die("Unable to reread header of %s: %s", pack_name, strerror(errno));
  18        if (lseek(pack_fd, 0, SEEK_SET) != 0)
  19                die("Failed seeking to start: %s", strerror(errno));
  20        hdr.hdr_entries = htonl(object_count);
  21        write_or_die(pack_fd, &hdr, sizeof(hdr));
  22
  23        SHA1_Init(&c);
  24        SHA1_Update(&c, &hdr, sizeof(hdr));
  25
  26        buf = xmalloc(buf_sz);
  27        for (;;) {
  28                ssize_t n = xread(pack_fd, buf, buf_sz);
  29                if (!n)
  30                        break;
  31                if (n < 0)
  32                        die("Failed to checksum %s: %s", pack_name, strerror(errno));
  33                SHA1_Update(&c, buf, n);
  34        }
  35        free(buf);
  36
  37        SHA1_Final(pack_file_sha1, &c);
  38        write_or_die(pack_fd, pack_file_sha1, 20);
  39}