@matheusbsbs / matheus-bagliano / commits / 4287307833
Fork 0

[PATCH] clean up pack index handling a bit

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>

Nicolas Pitre committed Mar 16, 2007 at 16:42 UTC 4287307833a7c67b09973fc1023311e473f830b2
6 files changed +90 -87
builtin-pack-objects.c
+8 -6
@@ -166,11 +166,12 @@ static void prepare_pack_revindex(struct pack_revindex *rix)
166 struct packed_git *p = rix->p;
167 int num_ent = num_packed_objects(p);
168 int i;
169 - void *index = p->index_base + 256;
169 + const char *index = p->index_data;
170
171 + index += 4 * 256;
172 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
173 for (i = 0; i < num_ent; i++) {
173 - unsigned int hl = *((unsigned int *)((char *) index + 24*i));
174 + uint32_t hl = *((uint32_t *)(index + 24 * i));
175 rix->revindex[i].offset = ntohl(hl);
176 rix->revindex[i].nr = i;
177 }
@@ -217,11 +218,11 @@ static off_t find_packed_object_size(struct packed_git *p, off_t ofs)
218 return entry[1].offset - ofs;
219 }
220
220 -static unsigned char *find_packed_object_name(struct packed_git *p,
221 - off_t ofs)
221 +static const unsigned char *find_packed_object_name(struct packed_git *p,
222 + off_t ofs)
223 {
224 struct revindex_entry *entry = find_packed_object(p, ofs);
224 - return (unsigned char *)(p->index_base + 256) + 24 * entry->nr + 4;
225 + return ((unsigned char *)p->index_data) + 4 * 256 + 24 * entry->nr + 4;
226 }
227
228 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
@@ -996,7 +997,8 @@ static void check_object(struct object_entry *entry)
997 * delta.
998 */
999 if (!no_reuse_delta) {
999 - unsigned char c, *base_name;
1000 + unsigned char c;
1001 + const unsigned char *base_name;
1002 off_t ofs;
1003 unsigned long used_0;
1004 /* there is at least 20 bytes left in the pack */
cache.h
+5 -4
@@ -371,10 +371,11 @@ struct pack_window {
371 extern struct packed_git {
372 struct packed_git *next;
373 struct pack_window *windows;
374 - uint32_t *index_base;
375 - time_t mtime;
374 + const void *index_data;
375 off_t index_size;
376 off_t pack_size;
377 + time_t mtime;
378 + int index_version;
379 int pack_fd;
380 int pack_local;
381 unsigned char sha1[20];
@@ -412,7 +413,7 @@ extern int server_supports(const char *feature);
413
414 extern struct packed_git *parse_pack_index(unsigned char *sha1);
415 extern struct packed_git *parse_pack_index_file(const unsigned char *sha1,
415 - char *idx_path);
416 + const char *idx_path);
417
418 extern void prepare_packed_git(void);
419 extern void reprepare_packed_git(void);
@@ -424,7 +425,7 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
425 extern void pack_report(void);
426 extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
427 extern void unuse_pack(struct pack_window **);
427 -extern struct packed_git *add_packed_git(char *, int, int);
428 +extern struct packed_git *add_packed_git(const char *, int, int);
429 extern uint32_t num_packed_objects(const struct packed_git *p);
430 extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);
431 extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
pack-check.c
+4 -4
@@ -5,7 +5,7 @@ static int verify_packfile(struct packed_git *p,
5 struct pack_window **w_curs)
6 {
7 off_t index_size = p->index_size;
8 - void *index_base = p->index_base;
8 + const unsigned char *index_base = p->index_data;
9 SHA_CTX ctx;
10 unsigned char sha1[20];
11 off_t offset = 0, pack_sig = p->pack_size - 20;
@@ -31,7 +31,7 @@ static int verify_packfile(struct packed_git *p,
31 if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
32 return error("Packfile %s SHA1 mismatch with itself",
33 p->pack_name);
34 - if (hashcmp(sha1, (unsigned char *)index_base + index_size - 40))
34 + if (hashcmp(sha1, index_base + index_size - 40))
35 return error("Packfile %s SHA1 mismatch with idx",
36 p->pack_name);
37 unuse_pack(w_curs);
@@ -127,7 +127,7 @@ static void show_pack_info(struct packed_git *p)
127 int verify_pack(struct packed_git *p, int verbose)
128 {
129 off_t index_size = p->index_size;
130 - void *index_base = p->index_base;
130 + const unsigned char *index_base = p->index_data;
131 SHA_CTX ctx;
132 unsigned char sha1[20];
133 int ret;
@@ -137,7 +137,7 @@ int verify_pack(struct packed_git *p, int verbose)
137 SHA1_Init(&ctx);
138 SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
139 SHA1_Final(sha1, &ctx);
140 - if (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))
140 + if (hashcmp(sha1, index_base + index_size - 20))
141 ret = error("Packfile index for %s SHA1 mismatch",
142 p->pack_name);
143
pack-redundant.c
+16 -14
@@ -17,7 +17,7 @@ static int load_all_packs, verbose, alt_odb;
17
18 struct llist_item {
19 struct llist_item *next;
20 - unsigned char *sha1;
20 + const unsigned char *sha1;
21 };
22 static struct llist {
23 struct llist_item *front;
@@ -104,9 +104,9 @@ static struct llist * llist_copy(struct llist *list)
104 return ret;
105 }
106
107 -static inline struct llist_item * llist_insert(struct llist *list,
108 - struct llist_item *after,
109 - unsigned char *sha1)
107 +static inline struct llist_item *llist_insert(struct llist *list,
108 + struct llist_item *after,
109 + const unsigned char *sha1)
110 {
111 struct llist_item *new = llist_item_get();
112 new->sha1 = sha1;
@@ -128,12 +128,14 @@ static inline struct llist_item * llist_insert(struct llist *list,
128 return new;
129 }
130
131 -static inline struct llist_item *llist_insert_back(struct llist *list, unsigned char *sha1)
131 +static inline struct llist_item *llist_insert_back(struct llist *list,
132 + const unsigned char *sha1)
133 {
134 return llist_insert(list, list->back, sha1);
135 }
136
136 -static inline struct llist_item *llist_insert_sorted_unique(struct llist *list, unsigned char *sha1, struct llist_item *hint)
137 +static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
138 + const unsigned char *sha1, struct llist_item *hint)
139 {
140 struct llist_item *prev = NULL, *l;
141
@@ -246,12 +248,12 @@ static struct pack_list * pack_list_difference(const struct pack_list *A,
248 static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
249 {
250 int p1_off, p2_off;
249 - unsigned char *p1_base, *p2_base;
251 + const unsigned char *p1_base, *p2_base;
252 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
253
254 p1_off = p2_off = 256 * 4 + 4;
253 - p1_base = (unsigned char *) p1->pack->index_base;
254 - p2_base = (unsigned char *) p2->pack->index_base;
255 + p1_base = p1->pack->index_data;
256 + p2_base = p2->pack->index_data;
257
258 while (p1_off <= p1->pack->index_size - 3 * 20 &&
259 p2_off <= p2->pack->index_size - 3 * 20)
@@ -351,11 +353,11 @@ static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
353 {
354 size_t ret = 0;
355 int p1_off, p2_off;
354 - unsigned char *p1_base, *p2_base;
356 + const unsigned char *p1_base, *p2_base;
357
358 p1_off = p2_off = 256 * 4 + 4;
357 - p1_base = (unsigned char *)p1->index_base;
358 - p2_base = (unsigned char *)p2->index_base;
359 + p1_base = p1->index_data;
360 + p2_base = p2->index_data;
361
362 while (p1_off <= p1->index_size - 3 * 20 &&
363 p2_off <= p2->index_size - 3 * 20)
@@ -534,7 +536,7 @@ static struct pack_list * add_pack(struct packed_git *p)
536 {
537 struct pack_list l;
538 size_t off;
537 - unsigned char *base;
539 + const unsigned char *base;
540
541 if (!p->pack_local && !(alt_odb || verbose))
542 return NULL;
@@ -543,7 +545,7 @@ static struct pack_list * add_pack(struct packed_git *p)
545 llist_init(&l.all_objects);
546
547 off = 256 * 4 + 4;
546 - base = (unsigned char *)p->index_base;
548 + base = p->index_data;
549 while (off <= p->index_size - 3 * 20) {
550 llist_insert_back(l.all_objects, base + off);
551 off += 24;
pack.h
+15 -15
@@ -16,24 +16,15 @@ struct pack_header {
16 };
17
18 /*
19 - * Packed object index header
20 - *
21 - * struct pack_idx_header {
22 - * uint32_t idx_signature;
23 - * uint32_t idx_version;
24 - * };
25 - *
26 - * Note: this header isn't active yet. In future versions of git
27 - * we may change the index file format. At that time we would start
28 - * the first four bytes of the new index format with this signature,
29 - * as all older git binaries would find this value illegal and abort
30 - * reading the file.
19 + * The first four bytes of index formats later than version 1 should
20 + * start with this signature, as all older git binaries would find this
21 + * value illegal and abort reading the file.
22 *
23 * This is the case because the number of objects in a packfile
24 * cannot exceed 1,431,660,000 as every object would need at least
34 - * 3 bytes of data and the overall packfile cannot exceed 4 GiB due
35 - * to the 32 bit offsets used by the index. Clearly the signature
36 - * exceeds this maximum.
25 + * 3 bytes of data and the overall packfile cannot exceed 4 GiB with
26 + * version 1 of the index file due to the offsets limited to 32 bits.
27 + * Clearly the signature exceeds this maximum.
28 *
29 * Very old git binaries will also compare the first 4 bytes to the
30 * next 4 bytes in the index and abort with a "non-monotonic index"
@@ -43,6 +34,15 @@ struct pack_header {
34 */
35 #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */
36
37 +/*
38 + * Packed object index header
39 + */
40 +struct pack_idx_header {
41 + uint32_t idx_signature;
42 + uint32_t idx_version;
43 +};
44 +
45 +
46 extern int verify_pack(struct packed_git *, int);
47
48 #define PH_ERROR_EOF (-1)
sha1_file.c
+42 -44
@@ -432,16 +432,15 @@ void pack_report()
432 pack_mapped, peak_pack_mapped);
433 }
434
435 -static int check_packed_git_idx(const char *path,
436 - unsigned long *idx_size_,
437 - void **idx_map_)
435 +static int check_packed_git_idx(const char *path, struct packed_git *p)
436 {
437 void *idx_map;
440 - uint32_t *index;
438 + struct pack_idx_header *hdr;
439 size_t idx_size;
442 - uint32_t nr, i;
440 + uint32_t nr, i, *index;
441 int fd = open(path, O_RDONLY);
442 struct stat st;
443 +
444 if (fd < 0)
445 return -1;
446 if (fstat(fd, &st)) {
@@ -456,15 +455,12 @@ static int check_packed_git_idx(const char *path,
455 idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
456 close(fd);
457
459 - index = idx_map;
460 - *idx_map_ = idx_map;
461 - *idx_size_ = idx_size;
462 -
458 /* a future index format would start with this, as older git
459 * binaries would fail the non-monotonic index check below.
460 * give a nicer warning to the user if we can.
461 */
467 - if (index[0] == htonl(PACK_IDX_SIGNATURE)) {
462 + hdr = idx_map;
463 + if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
464 munmap(idx_map, idx_size);
465 return error("index file %s is a newer version"
466 " and is not supported by this binary"
@@ -473,6 +469,7 @@ static int check_packed_git_idx(const char *path,
469 }
470
471 nr = 0;
472 + index = idx_map;
473 for (i = 0; i < 256; i++) {
474 uint32_t n = ntohl(index[i]);
475 if (n < nr) {
@@ -494,6 +491,9 @@ static int check_packed_git_idx(const char *path,
491 return error("wrong index file size in %s", path);
492 }
493
494 + p->index_version = 1;
495 + p->index_data = idx_map;
496 + p->index_size = idx_size;
497 return 0;
498 }
499
@@ -614,7 +614,7 @@ static int open_packed_git_1(struct packed_git *p)
614 return error("end of packfile %s is unavailable", p->pack_name);
615 if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
616 return error("packfile %s signature is unavailable", p->pack_name);
617 - idx_sha1 = ((unsigned char *)p->index_base) + p->index_size - 40;
617 + idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40;
618 if (hashcmp(sha1, idx_sha1))
619 return error("packfile %s does not match index", p->pack_name);
620 return 0;
@@ -710,38 +710,37 @@ unsigned char* use_pack(struct packed_git *p,
710 return win->base + offset;
711 }
712
713 -struct packed_git *add_packed_git(char *path, int path_len, int local)
713 +struct packed_git *add_packed_git(const char *path, int path_len, int local)
714 {
715 struct stat st;
716 - struct packed_git *p;
717 - unsigned long idx_size;
718 - void *idx_map;
719 - unsigned char sha1[20];
716 + struct packed_git *p = xmalloc(sizeof(*p) + path_len + 2);
717
721 - if (check_packed_git_idx(path, &idx_size, &idx_map))
718 + /*
719 + * Make sure a corresponding .pack file exists and that
720 + * the index looks sane.
721 + */
722 + path_len -= strlen(".idx");
723 + if (path_len < 1)
724 return NULL;
723 -
724 - /* do we have a corresponding .pack file? */
725 - strcpy(path + path_len - 4, ".pack");
726 - if (stat(path, &st) || !S_ISREG(st.st_mode)) {
727 - munmap(idx_map, idx_size);
725 + memcpy(p->pack_name, path, path_len);
726 + strcpy(p->pack_name + path_len, ".pack");
727 + if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode) ||
728 + check_packed_git_idx(path, p)) {
729 + free(p);
730 return NULL;
731 }
732 +
733 /* ok, it looks sane as far as we can check without
734 * actually mapping the pack file.
735 */
733 - p = xmalloc(sizeof(*p) + path_len + 2);
734 - strcpy(p->pack_name, path);
735 - p->index_size = idx_size;
736 p->pack_size = st.st_size;
737 - p->index_base = idx_map;
737 p->next = NULL;
738 p->windows = NULL;
739 p->pack_fd = -1;
740 p->pack_local = local;
741 p->mtime = st.st_mtime;
743 - if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
744 - hashcpy(p->sha1, sha1);
742 + if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
743 + hashclr(p->sha1);
744 return p;
745 }
746
@@ -751,23 +750,19 @@ struct packed_git *parse_pack_index(unsigned char *sha1)
750 return parse_pack_index_file(sha1, path);
751 }
752
754 -struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
753 +struct packed_git *parse_pack_index_file(const unsigned char *sha1,
754 + const char *idx_path)
755 {
756 - struct packed_git *p;
757 - unsigned long idx_size;
758 - void *idx_map;
759 - char *path;
756 + const char *path = sha1_pack_name(sha1);
757 + struct packed_git *p = xmalloc(sizeof(*p) + strlen(path) + 2);
758
761 - if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
759 + if (check_packed_git_idx(idx_path, p)) {
760 + free(p);
761 return NULL;
762 + }
763
764 - path = sha1_pack_name(sha1);
765 -
766 - p = xmalloc(sizeof(*p) + strlen(path) + 2);
764 strcpy(p->pack_name, path);
768 - p->index_size = idx_size;
765 p->pack_size = 0;
770 - p->index_base = idx_map;
766 p->next = NULL;
767 p->windows = NULL;
768 p->pack_fd = -1;
@@ -1423,24 +1418,27 @@ uint32_t num_packed_objects(const struct packed_git *p)
1418 int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,
1419 unsigned char* sha1)
1420 {
1426 - void *index = p->index_base + 256;
1421 + const unsigned char *index = p->index_data;
1422 + index += 4 * 256;
1423 if (num_packed_objects(p) <= n)
1424 return -1;
1429 - hashcpy(sha1, (unsigned char *) index + (24 * n) + 4);
1425 + hashcpy(sha1, index + 24 * n + 4);
1426 return 0;
1427 }
1428
1429 off_t find_pack_entry_one(const unsigned char *sha1,
1430 struct packed_git *p)
1431 {
1436 - uint32_t *level1_ofs = p->index_base;
1432 + const uint32_t *level1_ofs = p->index_data;
1433 int hi = ntohl(level1_ofs[*sha1]);
1434 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1439 - void *index = p->index_base + 256;
1435 + const unsigned char *index = p->index_data;
1436 +
1437 + index += 4 * 256;
1438
1439 do {
1440 int mi = (lo + hi) / 2;
1443 - int cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);
1441 + int cmp = hashcmp(index + 24 * mi + 4, sha1);
1442 if (!cmp)
1443 return ntohl(*((uint32_t *)((char *)index + (24 * mi))));
1444 if (cmp > 0)