Skip to content

Commit

Permalink
[PATCH] clean up pack index handling a bit
Browse files Browse the repository at this point in the history
Especially with the new index format to come, it is more appropriate
to encapsulate more into check_packed_git_idx() and assume less of the
index format in struct packed_git.

To that effect, the index_base is renamed to index_data with void * type
so it is not used directly but other pointers initialized with it. This
allows for a couple pointer cast removal, as well as providing a better
generic name to grep for when adding support for new index versions or
formats.

And index_data is declared const too while at it.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Nicolas Pitre authored and Junio C Hamano committed Mar 17, 2007
1 parent ac527b0 commit 4287307
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 87 deletions.
14 changes: 8 additions & 6 deletions builtin-pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ static void prepare_pack_revindex(struct pack_revindex *rix)
struct packed_git *p = rix->p;
int num_ent = num_packed_objects(p);
int i;
void *index = p->index_base + 256;
const char *index = p->index_data;

index += 4 * 256;
rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
for (i = 0; i < num_ent; i++) {
unsigned int hl = *((unsigned int *)((char *) index + 24*i));
uint32_t hl = *((uint32_t *)(index + 24 * i));
rix->revindex[i].offset = ntohl(hl);
rix->revindex[i].nr = i;
}
Expand Down Expand Up @@ -217,11 +218,11 @@ static off_t find_packed_object_size(struct packed_git *p, off_t ofs)
return entry[1].offset - ofs;
}

static unsigned char *find_packed_object_name(struct packed_git *p,
off_t ofs)
static const unsigned char *find_packed_object_name(struct packed_git *p,
off_t ofs)
{
struct revindex_entry *entry = find_packed_object(p, ofs);
return (unsigned char *)(p->index_base + 256) + 24 * entry->nr + 4;
return ((unsigned char *)p->index_data) + 4 * 256 + 24 * entry->nr + 4;
}

static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
Expand Down Expand Up @@ -996,7 +997,8 @@ static void check_object(struct object_entry *entry)
* delta.
*/
if (!no_reuse_delta) {
unsigned char c, *base_name;
unsigned char c;
const unsigned char *base_name;
off_t ofs;
unsigned long used_0;
/* there is at least 20 bytes left in the pack */
Expand Down
9 changes: 5 additions & 4 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ struct pack_window {
extern struct packed_git {
struct packed_git *next;
struct pack_window *windows;
uint32_t *index_base;
time_t mtime;
const void *index_data;
off_t index_size;
off_t pack_size;
time_t mtime;
int index_version;
int pack_fd;
int pack_local;
unsigned char sha1[20];
Expand Down Expand Up @@ -412,7 +413,7 @@ extern int server_supports(const char *feature);

extern struct packed_git *parse_pack_index(unsigned char *sha1);
extern struct packed_git *parse_pack_index_file(const unsigned char *sha1,
char *idx_path);
const char *idx_path);

extern void prepare_packed_git(void);
extern void reprepare_packed_git(void);
Expand All @@ -424,7 +425,7 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
extern void pack_report(void);
extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
extern void unuse_pack(struct pack_window **);
extern struct packed_git *add_packed_git(char *, int, int);
extern struct packed_git *add_packed_git(const char *, int, int);
extern uint32_t num_packed_objects(const struct packed_git *p);
extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);
extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
Expand Down
8 changes: 4 additions & 4 deletions pack-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ static int verify_packfile(struct packed_git *p,
struct pack_window **w_curs)
{
off_t index_size = p->index_size;
void *index_base = p->index_base;
const unsigned char *index_base = p->index_data;
SHA_CTX ctx;
unsigned char sha1[20];
off_t offset = 0, pack_sig = p->pack_size - 20;
Expand All @@ -31,7 +31,7 @@ static int verify_packfile(struct packed_git *p,
if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
return error("Packfile %s SHA1 mismatch with itself",
p->pack_name);
if (hashcmp(sha1, (unsigned char *)index_base + index_size - 40))
if (hashcmp(sha1, index_base + index_size - 40))
return error("Packfile %s SHA1 mismatch with idx",
p->pack_name);
unuse_pack(w_curs);
Expand Down Expand Up @@ -127,7 +127,7 @@ static void show_pack_info(struct packed_git *p)
int verify_pack(struct packed_git *p, int verbose)
{
off_t index_size = p->index_size;
void *index_base = p->index_base;
const unsigned char *index_base = p->index_data;
SHA_CTX ctx;
unsigned char sha1[20];
int ret;
Expand All @@ -137,7 +137,7 @@ int verify_pack(struct packed_git *p, int verbose)
SHA1_Init(&ctx);
SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
SHA1_Final(sha1, &ctx);
if (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))
if (hashcmp(sha1, index_base + index_size - 20))
ret = error("Packfile index for %s SHA1 mismatch",
p->pack_name);

Expand Down
30 changes: 16 additions & 14 deletions pack-redundant.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static int load_all_packs, verbose, alt_odb;

struct llist_item {
struct llist_item *next;
unsigned char *sha1;
const unsigned char *sha1;
};
static struct llist {
struct llist_item *front;
Expand Down Expand Up @@ -104,9 +104,9 @@ static struct llist * llist_copy(struct llist *list)
return ret;
}

static inline struct llist_item * llist_insert(struct llist *list,
struct llist_item *after,
unsigned char *sha1)
static inline struct llist_item *llist_insert(struct llist *list,
struct llist_item *after,
const unsigned char *sha1)
{
struct llist_item *new = llist_item_get();
new->sha1 = sha1;
Expand All @@ -128,12 +128,14 @@ static inline struct llist_item * llist_insert(struct llist *list,
return new;
}

static inline struct llist_item *llist_insert_back(struct llist *list, unsigned char *sha1)
static inline struct llist_item *llist_insert_back(struct llist *list,
const unsigned char *sha1)
{
return llist_insert(list, list->back, sha1);
}

static inline struct llist_item *llist_insert_sorted_unique(struct llist *list, unsigned char *sha1, struct llist_item *hint)
static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
const unsigned char *sha1, struct llist_item *hint)
{
struct llist_item *prev = NULL, *l;

Expand Down Expand Up @@ -246,12 +248,12 @@ static struct pack_list * pack_list_difference(const struct pack_list *A,
static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
{
int p1_off, p2_off;
unsigned char *p1_base, *p2_base;
const unsigned char *p1_base, *p2_base;
struct llist_item *p1_hint = NULL, *p2_hint = NULL;

p1_off = p2_off = 256 * 4 + 4;
p1_base = (unsigned char *) p1->pack->index_base;
p2_base = (unsigned char *) p2->pack->index_base;
p1_base = p1->pack->index_data;
p2_base = p2->pack->index_data;

while (p1_off <= p1->pack->index_size - 3 * 20 &&
p2_off <= p2->pack->index_size - 3 * 20)
Expand Down Expand Up @@ -351,11 +353,11 @@ static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
{
size_t ret = 0;
int p1_off, p2_off;
unsigned char *p1_base, *p2_base;
const unsigned char *p1_base, *p2_base;

p1_off = p2_off = 256 * 4 + 4;
p1_base = (unsigned char *)p1->index_base;
p2_base = (unsigned char *)p2->index_base;
p1_base = p1->index_data;
p2_base = p2->index_data;

while (p1_off <= p1->index_size - 3 * 20 &&
p2_off <= p2->index_size - 3 * 20)
Expand Down Expand Up @@ -534,7 +536,7 @@ static struct pack_list * add_pack(struct packed_git *p)
{
struct pack_list l;
size_t off;
unsigned char *base;
const unsigned char *base;

if (!p->pack_local && !(alt_odb || verbose))
return NULL;
Expand All @@ -543,7 +545,7 @@ static struct pack_list * add_pack(struct packed_git *p)
llist_init(&l.all_objects);

off = 256 * 4 + 4;
base = (unsigned char *)p->index_base;
base = p->index_data;
while (off <= p->index_size - 3 * 20) {
llist_insert_back(l.all_objects, base + off);
off += 24;
Expand Down
30 changes: 15 additions & 15 deletions pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,15 @@ struct pack_header {
};

/*
* Packed object index header
*
* struct pack_idx_header {
* uint32_t idx_signature;
* uint32_t idx_version;
* };
*
* Note: this header isn't active yet. In future versions of git
* we may change the index file format. At that time we would start
* the first four bytes of the new index format with this signature,
* as all older git binaries would find this value illegal and abort
* reading the file.
* The first four bytes of index formats later than version 1 should
* start with this signature, as all older git binaries would find this
* value illegal and abort reading the file.
*
* This is the case because the number of objects in a packfile
* cannot exceed 1,431,660,000 as every object would need at least
* 3 bytes of data and the overall packfile cannot exceed 4 GiB due
* to the 32 bit offsets used by the index. Clearly the signature
* exceeds this maximum.
* 3 bytes of data and the overall packfile cannot exceed 4 GiB with
* version 1 of the index file due to the offsets limited to 32 bits.
* Clearly the signature exceeds this maximum.
*
* Very old git binaries will also compare the first 4 bytes to the
* next 4 bytes in the index and abort with a "non-monotonic index"
Expand All @@ -43,6 +34,15 @@ struct pack_header {
*/
#define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */

/*
* Packed object index header
*/
struct pack_idx_header {
uint32_t idx_signature;
uint32_t idx_version;
};


extern int verify_pack(struct packed_git *, int);

#define PH_ERROR_EOF (-1)
Expand Down
Loading

0 comments on commit 4287307

Please sign in to comment.