pack-check.con commit optimize verify-pack a bit (9909323)
   1#include "cache.h"
   2#include "pack.h"
   3#include "pack-revindex.h"
   4
   5struct idx_entry
   6{
   7        const unsigned char *sha1;
   8        off_t                offset;
   9};
  10
  11static int compare_entries(const void *e1, const void *e2)
  12{
  13        const struct idx_entry *entry1 = e1;
  14        const struct idx_entry *entry2 = e2;
  15        if (entry1->offset < entry2->offset)
  16                return -1;
  17        if (entry1->offset > entry2->offset)
  18                return 1;
  19        return 0;
  20}
  21
  22static int verify_packfile(struct packed_git *p,
  23                struct pack_window **w_curs)
  24{
  25        off_t index_size = p->index_size;
  26        const unsigned char *index_base = p->index_data;
  27        SHA_CTX ctx;
  28        unsigned char sha1[20], *pack_sig;
  29        off_t offset = 0, pack_sig_ofs = p->pack_size - 20;
  30        uint32_t nr_objects, i;
  31        int err = 0;
  32        struct idx_entry *entries;
  33
  34        /* Note that the pack header checks are actually performed by
  35         * use_pack when it first opens the pack file.  If anything
  36         * goes wrong during those checks then the call will die out
  37         * immediately.
  38         */
  39
  40        SHA1_Init(&ctx);
  41        while (offset < pack_sig_ofs) {
  42                unsigned int remaining;
  43                unsigned char *in = use_pack(p, w_curs, offset, &remaining);
  44                offset += remaining;
  45                if (offset > pack_sig_ofs)
  46                        remaining -= (unsigned int)(offset - pack_sig_ofs);
  47                SHA1_Update(&ctx, in, remaining);
  48        }
  49        SHA1_Final(sha1, &ctx);
  50        pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
  51        if (hashcmp(sha1, pack_sig))
  52                err = error("%s SHA1 checksum mismatch",
  53                            p->pack_name);
  54        if (hashcmp(index_base + index_size - 40, pack_sig))
  55                err = error("%s SHA1 does not match its inddex",
  56                            p->pack_name);
  57        unuse_pack(w_curs);
  58
  59        /* Make sure everything reachable from idx is valid.  Since we
  60         * have verified that nr_objects matches between idx and pack,
  61         * we do not do scan-streaming check on the pack file.
  62         */
  63        nr_objects = p->num_objects;
  64        entries = xmalloc(nr_objects * sizeof(*entries));
  65        /* first sort entries by pack offset, since unpacking them is more efficient that way */
  66        for (i = 0; i < nr_objects; i++) {
  67                entries[i].sha1 = nth_packed_object_sha1(p, i);
  68                if (!entries[i].sha1)
  69                        die("internal error pack-check nth-packed-object");
  70                entries[i].offset = nth_packed_object_offset(p, i);
  71        }
  72        qsort(entries, nr_objects, sizeof(*entries), compare_entries);
  73
  74        for (i = 0; i < nr_objects; i++) {
  75                void *data;
  76                enum object_type type;
  77                unsigned long size;
  78
  79                data = unpack_entry(p, entries[i].offset, &type, &size);
  80                if (!data) {
  81                        err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
  82                                    sha1_to_hex(entries[i].sha1), p->pack_name,
  83                                    (uintmax_t)entries[i].offset);
  84                        break;
  85                }
  86                if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
  87                        err = error("packed %s from %s is corrupt",
  88                                    sha1_to_hex(entries[i].sha1), p->pack_name);
  89                        free(data);
  90                        break;
  91                }
  92                free(data);
  93        }
  94        free(entries);
  95
  96        return err;
  97}
  98
  99
 100#define MAX_CHAIN 50
 101
 102static void show_pack_info(struct packed_git *p)
 103{
 104        uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1];
 105
 106        nr_objects = p->num_objects;
 107        memset(chain_histogram, 0, sizeof(chain_histogram));
 108
 109        for (i = 0; i < nr_objects; i++) {
 110                const unsigned char *sha1;
 111                unsigned char base_sha1[20];
 112                const char *type;
 113                unsigned long size;
 114                unsigned long store_size;
 115                off_t offset;
 116                unsigned int delta_chain_length;
 117
 118                sha1 = nth_packed_object_sha1(p, i);
 119                if (!sha1)
 120                        die("internal error pack-check nth-packed-object");
 121                offset = find_pack_entry_one(sha1, p);
 122                if (!offset)
 123                        die("internal error pack-check find-pack-entry-one");
 124
 125                type = packed_object_info_detail(p, offset, &size, &store_size,
 126                                                 &delta_chain_length,
 127                                                 base_sha1);
 128                printf("%s ", sha1_to_hex(sha1));
 129                if (!delta_chain_length)
 130                        printf("%-6s %lu %lu %"PRIuMAX"\n",
 131                               type, size, store_size, (uintmax_t)offset);
 132                else {
 133                        printf("%-6s %lu %lu %"PRIuMAX" %u %s\n",
 134                               type, size, store_size, (uintmax_t)offset,
 135                               delta_chain_length, sha1_to_hex(base_sha1));
 136                        if (delta_chain_length <= MAX_CHAIN)
 137                                chain_histogram[delta_chain_length]++;
 138                        else
 139                                chain_histogram[0]++;
 140                }
 141        }
 142
 143        for (i = 0; i <= MAX_CHAIN; i++) {
 144                if (!chain_histogram[i])
 145                        continue;
 146                printf("chain length = %d: %d object%s\n", i,
 147                       chain_histogram[i], chain_histogram[i] > 1 ? "s" : "");
 148        }
 149        if (chain_histogram[0])
 150                printf("chain length > %d: %d object%s\n", MAX_CHAIN,
 151                       chain_histogram[0], chain_histogram[0] > 1 ? "s" : "");
 152}
 153
 154int verify_pack(struct packed_git *p, int verbose)
 155{
 156        off_t index_size;
 157        const unsigned char *index_base;
 158        SHA_CTX ctx;
 159        unsigned char sha1[20];
 160        int err = 0;
 161        struct pack_window *w_curs = NULL;
 162
 163        if (open_pack_index(p))
 164                return error("packfile %s index not opened", p->pack_name);
 165        index_size = p->index_size;
 166        index_base = p->index_data;
 167
 168        /* Verify SHA1 sum of the index file */
 169        SHA1_Init(&ctx);
 170        SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
 171        SHA1_Final(sha1, &ctx);
 172        if (hashcmp(sha1, index_base + index_size - 20))
 173                err = error("Packfile index for %s SHA1 mismatch",
 174                            p->pack_name);
 175
 176        /* Verify pack file */
 177        err |= verify_packfile(p, &w_curs);
 178        unuse_pack(&w_curs);
 179
 180        if (verbose) {
 181                if (err)
 182                        printf("%s: bad\n", p->pack_name);
 183                else {
 184                        show_pack_info(p);
 185                        printf("%s: ok\n", p->pack_name);
 186                }
 187        }
 188
 189        return err;
 190}