@matheusbsbs / matheus-bagliano / commits / 43057304c0
Fork 0

many cleanups to sha1_file.c

Those cleanups are mainly to set the table for the support of deltas with base objects referenced by offsets instead of sha1. This means that many pack lookup functions are converted to take a pack/offset tuple instead of a sha1. This eliminates many struct pack_entry usages since this structure carried redundent information in many cases, and it increased stack footprint needlessly for a couple recursively called functions that used to declare a local copy of it for every recursion loop. In the process, packed_object_info_detail() has been reorganized as well so to look much saner and more amenable to deltas with offset support. Finally the appropriate adjustments have been made to functions that depend on the above changes. But there is no functionality changes yet simply some code refactoring at this point. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Sep 21, 2006 at 00:05 UTC 43057304c0bbaf7bc7511daaf81df08c7909a90b
4 files changed +104 -114
builtin-pack-objects.c
+4 -4
@@ -597,15 +597,15 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
597
598 if (!exclude) {
599 for (p = packed_git; p; p = p->next) {
600 - struct pack_entry e;
601 - if (find_pack_entry_one(sha1, &e, p)) {
600 + unsigned long offset = find_pack_entry_one(sha1, p);
601 + if (offset) {
602 if (incremental)
603 return 0;
604 if (local && !p->pack_local)
605 return 0;
606 if (!found_pack) {
607 - found_offset = e.offset;
608 - found_pack = e.p;
607 + found_offset = offset;
608 + found_pack = p;
609 }
610 }
611 }
cache.h
+3 -3
@@ -390,10 +390,10 @@ extern void unuse_packed_git(struct packed_git *);
390 extern struct packed_git *add_packed_git(char *, int, int);
391 extern int num_packed_objects(const struct packed_git *p);
392 extern int nth_packed_object_sha1(const struct packed_git *, int, unsigned char*);
393 -extern int find_pack_entry_one(const unsigned char *, struct pack_entry *, struct packed_git *);
394 -extern void *unpack_entry_gently(struct pack_entry *, char *, unsigned long *);
393 +extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);
394 +extern void *unpack_entry_gently(struct packed_git *, unsigned long, char *, unsigned long *);
395 extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
396 -extern void packed_object_info_detail(struct pack_entry *, char *, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
396 +extern void packed_object_info_detail(struct packed_git *, unsigned long, char *, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
397
398 /* Dumb servers support */
399 extern int update_server_info(int);
pack-check.c
+10 -9
@@ -42,16 +42,16 @@ static int verify_packfile(struct packed_git *p)
42 */
43 for (i = err = 0; i < nr_objects; i++) {
44 unsigned char sha1[20];
45 - struct pack_entry e;
45 void *data;
46 char type[20];
48 - unsigned long size;
47 + unsigned long size, offset;
48
49 if (nth_packed_object_sha1(p, i, sha1))
50 die("internal error pack-check nth-packed-object");
52 - if (!find_pack_entry_one(sha1, &e, p))
51 + offset = find_pack_entry_one(sha1, p);
52 + if (!offset)
53 die("internal error pack-check find-pack-entry-one");
54 - data = unpack_entry_gently(&e, type, &size);
54 + data = unpack_entry_gently(p, offset, type, &size);
55 if (!data) {
56 err = error("cannot unpack %s from %s",
57 sha1_to_hex(sha1), p->pack_name);
@@ -84,25 +84,26 @@ static void show_pack_info(struct packed_git *p)
84
85 for (i = 0; i < nr_objects; i++) {
86 unsigned char sha1[20], base_sha1[20];
87 - struct pack_entry e;
87 char type[20];
88 unsigned long size;
89 unsigned long store_size;
90 + unsigned long offset;
91 unsigned int delta_chain_length;
92
93 if (nth_packed_object_sha1(p, i, sha1))
94 die("internal error pack-check nth-packed-object");
95 - if (!find_pack_entry_one(sha1, &e, p))
95 + offset = find_pack_entry_one(sha1, p);
96 + if (!offset)
97 die("internal error pack-check find-pack-entry-one");
98
98 - packed_object_info_detail(&e, type, &size, &store_size,
99 + packed_object_info_detail(p, offset, type, &size, &store_size,
100 &delta_chain_length,
101 base_sha1);
102 printf("%s ", sha1_to_hex(sha1));
103 if (!delta_chain_length)
103 - printf("%-6s %lu %u\n", type, size, e.offset);
104 + printf("%-6s %lu %lu\n", type, size, offset);
105 else {
105 - printf("%-6s %lu %u %u %s\n", type, size, e.offset,
106 + printf("%-6s %lu %lu %u %s\n", type, size, offset,
107 delta_chain_length, sha1_to_hex(base_sha1));
108 if (delta_chain_length < MAX_CHAIN)
109 chain_histogram[delta_chain_length]++;
sha1_file.c
+87 -98
@@ -884,33 +884,32 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
884 }
885
886 /* forward declaration for a mutually recursive function */
887 -static int packed_object_info(struct pack_entry *entry,
887 +static int packed_object_info(struct packed_git *p, unsigned long offset,
888 char *type, unsigned long *sizep);
889
890 -static int packed_delta_info(unsigned char *base_sha1,
891 - unsigned long delta_size,
892 - unsigned long left,
890 +static int packed_delta_info(struct packed_git *p,
891 + unsigned long offset,
892 char *type,
894 - unsigned long *sizep,
895 - struct packed_git *p)
893 + unsigned long *sizep)
894 {
897 - struct pack_entry base_ent;
895 + unsigned long base_offset;
896 + unsigned char *base_sha1 = (unsigned char *) p->pack_base + offset;
897
899 - if (left < 20)
898 + if (p->pack_size < offset + 20)
899 die("truncated pack file");
901 -
900 /* The base entry _must_ be in the same pack */
903 - if (!find_pack_entry_one(base_sha1, &base_ent, p))
901 + base_offset = find_pack_entry_one(base_sha1, p);
902 + if (!base_offset)
903 die("failed to find delta-pack base object %s",
904 sha1_to_hex(base_sha1));
905 + offset += 20;
906
907 /* We choose to only get the type of the base object and
908 * ignore potentially corrupt pack file that expects the delta
909 * based on a base with a wrong size. This saves tons of
910 * inflate() calls.
911 */
912 -
913 - if (packed_object_info(&base_ent, type, NULL))
912 + if (packed_object_info(p, base_offset, type, NULL))
913 die("cannot get info for delta-pack base");
914
915 if (sizep) {
@@ -922,8 +921,8 @@ static int packed_delta_info(unsigned char *base_sha1,
921
922 memset(&stream, 0, sizeof(stream));
923
925 - data = stream.next_in = base_sha1 + 20;
926 - stream.avail_in = left - 20;
924 + stream.next_in = (unsigned char *) p->pack_base + offset;
925 + stream.avail_in = p->pack_size - offset;
926 stream.next_out = delta_head;
927 stream.avail_out = sizeof(delta_head);
928
@@ -985,75 +984,60 @@ int check_reuse_pack_delta(struct packed_git *p, unsigned long offset,
984 return status;
985 }
986
988 -void packed_object_info_detail(struct pack_entry *e,
987 +void packed_object_info_detail(struct packed_git *p,
988 + unsigned long offset,
989 char *type,
990 unsigned long *size,
991 unsigned long *store_size,
992 unsigned int *delta_chain_length,
993 unsigned char *base_sha1)
994 {
995 - struct packed_git *p = e->p;
996 - unsigned long offset;
997 - unsigned char *pack;
995 + unsigned long val;
996 + unsigned char *next_sha1;
997 enum object_type kind;
998
1000 - offset = unpack_object_header(p, e->offset, &kind, size);
1001 - pack = (unsigned char *) p->pack_base + offset;
1002 - if (kind != OBJ_DELTA)
1003 - *delta_chain_length = 0;
1004 - else {
1005 - unsigned int chain_length = 0;
1006 - if (p->pack_size <= offset + 20)
1007 - die("pack file %s records an incomplete delta base",
1008 - p->pack_name);
1009 - hashcpy(base_sha1, pack);
1010 - do {
1011 - struct pack_entry base_ent;
1012 - unsigned long junk;
1013 -
1014 - find_pack_entry_one(pack, &base_ent, p);
1015 - offset = unpack_object_header(p, base_ent.offset,
1016 - &kind, &junk);
1017 - pack = (unsigned char *) p->pack_base + offset;
1018 - chain_length++;
1019 - } while (kind == OBJ_DELTA);
1020 - *delta_chain_length = chain_length;
1021 - }
1022 - switch (kind) {
1023 - case OBJ_COMMIT:
1024 - case OBJ_TREE:
1025 - case OBJ_BLOB:
1026 - case OBJ_TAG:
1027 - strcpy(type, type_names[kind]);
1028 - break;
1029 - default:
1030 - die("corrupted pack file %s containing object of kind %d",
1031 - p->pack_name, kind);
999 + *delta_chain_length = 0;
1000 + offset = unpack_object_header(p, offset, &kind, size);
1001 +
1002 + for (;;) {
1003 + switch (kind) {
1004 + default:
1005 + die("corrupted pack file %s containing object of kind %d",
1006 + p->pack_name, kind);
1007 + case OBJ_COMMIT:
1008 + case OBJ_TREE:
1009 + case OBJ_BLOB:
1010 + case OBJ_TAG:
1011 + strcpy(type, type_names[kind]);
1012 + *store_size = 0; /* notyet */
1013 + return;
1014 + case OBJ_DELTA:
1015 + if (p->pack_size <= offset + 20)
1016 + die("pack file %s records an incomplete delta base",
1017 + p->pack_name);
1018 + next_sha1 = (unsigned char *) p->pack_base + offset;
1019 + if (*delta_chain_length == 0)
1020 + hashcpy(base_sha1, next_sha1);
1021 + offset = find_pack_entry_one(next_sha1, p);
1022 + break;
1023 + }
1024 + offset = unpack_object_header(p, offset, &kind, &val);
1025 + (*delta_chain_length)++;
1026 }
1033 - *store_size = 0; /* notyet */
1027 }
1028
1036 -static int packed_object_info(struct pack_entry *entry,
1029 +static int packed_object_info(struct packed_git *p, unsigned long offset,
1030 char *type, unsigned long *sizep)
1031 {
1039 - struct packed_git *p = entry->p;
1040 - unsigned long offset, size, left;
1041 - unsigned char *pack;
1032 + unsigned long size;
1033 enum object_type kind;
1043 - int retval;
1034
1045 - if (use_packed_git(p))
1046 - die("cannot map packed file");
1035 + offset = unpack_object_header(p, offset, &kind, &size);
1036
1048 - offset = unpack_object_header(p, entry->offset, &kind, &size);
1049 - pack = (unsigned char *) p->pack_base + offset;
1050 - left = p->pack_size - offset;
1037 + if (kind == OBJ_DELTA)
1038 + return packed_delta_info(p, offset, type, sizep);
1039
1040 switch (kind) {
1053 - case OBJ_DELTA:
1054 - retval = packed_delta_info(pack, size, left, type, sizep, p);
1055 - unuse_packed_git(p);
1056 - return retval;
1041 case OBJ_COMMIT:
1042 case OBJ_TREE:
1043 case OBJ_BLOB:
@@ -1066,7 +1050,6 @@ static int packed_object_info(struct pack_entry *entry,
1050 }
1051 if (sizep)
1052 *sizep = size;
1069 - unuse_packed_git(p);
1053 return 0;
1054 }
1055
@@ -1103,25 +1086,26 @@ static void *unpack_delta_entry(struct packed_git *p,
1086 char *type,
1087 unsigned long *sizep)
1088 {
1106 - struct pack_entry base_ent;
1089 void *delta_data, *result, *base;
1108 - unsigned long result_size, base_size;
1109 - unsigned char* base_sha1;
1090 + unsigned long result_size, base_size, base_offset;
1091 + unsigned char *base_sha1;
1092
1111 - if ((offset + 20) >= p->pack_size)
1093 + if (p->pack_size < offset + 20)
1094 die("truncated pack file");
1113 -
1095 /* The base entry _must_ be in the same pack */
1096 base_sha1 = (unsigned char*)p->pack_base + offset;
1116 - if (!find_pack_entry_one(base_sha1, &base_ent, p))
1097 + base_offset = find_pack_entry_one(base_sha1, p);
1098 + if (!base_offset)
1099 die("failed to find delta-pack base object %s",
1100 sha1_to_hex(base_sha1));
1119 - base = unpack_entry_gently(&base_ent, type, &base_size);
1101 + offset += 20;
1102 +
1103 + base = unpack_entry_gently(p, base_offset, type, &base_size);
1104 if (!base)
1121 - die("failed to read delta-pack base object %s",
1122 - sha1_to_hex(base_sha1));
1105 + die("failed to read delta base object at %lu from %s",
1106 + base_offset, p->pack_name);
1107
1124 - delta_data = unpack_compressed_entry(p, offset + 20, delta_size);
1108 + delta_data = unpack_compressed_entry(p, offset, delta_size);
1109 result = patch_delta(base, base_size,
1110 delta_data, delta_size,
1111 &result_size);
@@ -1141,7 +1125,7 @@ static void *unpack_entry(struct pack_entry *entry,
1125
1126 if (use_packed_git(p))
1127 die("cannot map packed file");
1144 - retval = unpack_entry_gently(entry, type, sizep);
1128 + retval = unpack_entry_gently(p, entry->offset, type, sizep);
1129 unuse_packed_git(p);
1130 if (!retval)
1131 die("corrupted pack file %s", p->pack_name);
@@ -1149,14 +1133,13 @@ static void *unpack_entry(struct pack_entry *entry,
1133 }
1134
1135 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1152 -void *unpack_entry_gently(struct pack_entry *entry,
1136 +void *unpack_entry_gently(struct packed_git *p, unsigned long offset,
1137 char *type, unsigned long *sizep)
1138 {
1155 - struct packed_git *p = entry->p;
1156 - unsigned long offset, size;
1139 + unsigned long size;
1140 enum object_type kind;
1141
1159 - offset = unpack_object_header(p, entry->offset, &kind, &size);
1142 + offset = unpack_object_header(p, offset, &kind, &size);
1143 switch (kind) {
1144 case OBJ_DELTA:
1145 return unpack_delta_entry(p, offset, size, type, sizep);
@@ -1188,8 +1171,8 @@ int nth_packed_object_sha1(const struct packed_git *p, int n,
1171 return 0;
1172 }
1173
1191 -int find_pack_entry_one(const unsigned char *sha1,
1192 - struct pack_entry *e, struct packed_git *p)
1174 +unsigned long find_pack_entry_one(const unsigned char *sha1,
1175 + struct packed_git *p)
1176 {
1177 unsigned int *level1_ofs = p->index_base;
1178 int hi = ntohl(level1_ofs[*sha1]);
@@ -1199,12 +1182,8 @@ int find_pack_entry_one(const unsigned char *sha1,
1182 do {
1183 int mi = (lo + hi) / 2;
1184 int cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);
1202 - if (!cmp) {
1203 - e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
1204 - hashcpy(e->sha1, sha1);
1205 - e->p = p;
1206 - return 1;
1207 - }
1185 + if (!cmp)
1186 + return ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
1187 if (cmp > 0)
1188 hi = mi;
1189 else
@@ -1216,6 +1195,8 @@ int find_pack_entry_one(const unsigned char *sha1,
1195 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed)
1196 {
1197 struct packed_git *p;
1198 + unsigned long offset;
1199 +
1200 prepare_packed_git();
1201
1202 for (p = packed_git; p; p = p->next) {
@@ -1227,8 +1208,13 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, cons
1208 if (*ig)
1209 continue;
1210 }
1230 - if (find_pack_entry_one(sha1, e, p))
1211 + offset = find_pack_entry_one(sha1, p);
1212 + if (offset) {
1213 + e->offset = offset;
1214 + e->p = p;
1215 + hashcpy(e->sha1, sha1);
1216 return 1;
1217 + }
1218 }
1219 return 0;
1220 }
@@ -1237,10 +1223,9 @@ struct packed_git *find_sha1_pack(const unsigned char *sha1,
1223 struct packed_git *packs)
1224 {
1225 struct packed_git *p;
1240 - struct pack_entry e;
1226
1227 for (p = packs; p; p = p->next) {
1243 - if (find_pack_entry_one(sha1, &e, p))
1228 + if (find_pack_entry_one(sha1, p))
1229 return p;
1230 }
1231 return NULL;
@@ -1259,12 +1244,16 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
1244 if (!map) {
1245 struct pack_entry e;
1246
1262 - if (find_pack_entry(sha1, &e, NULL))
1263 - return packed_object_info(&e, type, sizep);
1264 - reprepare_packed_git();
1265 - if (find_pack_entry(sha1, &e, NULL))
1266 - return packed_object_info(&e, type, sizep);
1267 - return error("unable to find %s", sha1_to_hex(sha1));
1247 + if (!find_pack_entry(sha1, &e, NULL)) {
1248 + reprepare_packed_git();
1249 + if (!find_pack_entry(sha1, &e, NULL))
1250 + return error("unable to find %s", sha1_to_hex(sha1));
1251 + }
1252 + if (use_packed_git(e.p))
1253 + die("cannot map packed file");
1254 + status = packed_object_info(e.p, e.offset, type, sizep);
1255 + unuse_packed_git(e.p);
1256 + return status;
1257 }
1258 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1259 status = error("unable to unpack %s header",