470
return 1;
471
}
472
473
-void unuse_packed_git(struct packed_git *p)
473
+void unuse_pack(struct pack_window **w_cursor)
474
{
475
- if (p->windows)
476
- p->windows->inuse_cnt--;
475
+ struct pack_window *w = *w_cursor;
476
+ if (w) {
477
+ w->inuse_cnt--;
478
+ *w_cursor = NULL;
479
+ }
480
}
481
482
static void open_packed_git(struct packed_git *p)
521
die("packfile %s does not match index", p->pack_name);
522
}
523
521
-int use_packed_git(struct packed_git *p)
524
+unsigned char* use_pack(struct packed_git *p,
525
+ struct pack_window **w_cursor,
526
+ unsigned long offset,
527
+ unsigned int *left)
528
{
529
+ struct pack_window *win = p->windows;
530
+
531
if (p->pack_fd == -1)
532
open_packed_git(p);
525
- if (!p->windows) {
526
- struct pack_window *win;
527
-
533
+ if (!win) {
534
pack_mapped += p->pack_size;
535
while (packed_git_limit < pack_mapped && unuse_one_packed_git())
536
; /* nothing */
541
die("packfile %s cannot be mapped.", p->pack_name);
542
p->windows = win;
543
}
538
- p->windows->last_used = pack_used_ctr++;
539
- p->windows->inuse_cnt++;
540
- return 0;
544
+ if (win != *w_cursor) {
545
+ win->last_used = pack_used_ctr++;
546
+ win->inuse_cnt++;
547
+ *w_cursor = win;
548
+ }
549
+ if (left)
550
+ *left = win->len - offset;
551
+ return win->base + offset;
552
}
553
554
struct packed_git *add_packed_git(char *path, int path_len, int local)
894
}
895
896
static unsigned long get_delta_base(struct packed_git *p,
897
+ struct pack_window **w_curs,
898
unsigned long offset,
899
enum object_type kind,
900
unsigned long delta_obj_offset,
901
unsigned long *base_obj_offset)
902
{
891
- unsigned char *base_info = p->windows->base + offset;
903
+ unsigned char *base_info = use_pack(p, w_curs, offset, NULL);
904
unsigned long base_offset;
905
906
/* there must be at least 20 bytes left regardless of delta type */
940
char *type, unsigned long *sizep);
941
942
static int packed_delta_info(struct packed_git *p,
943
+ struct pack_window **w_curs,
944
unsigned long offset,
945
enum object_type kind,
946
unsigned long obj_offset,
949
{
950
unsigned long base_offset;
951
939
- offset = get_delta_base(p, offset, kind, obj_offset, &base_offset);
952
+ offset = get_delta_base(p, w_curs, offset, kind,
953
+ obj_offset, &base_offset);
954
955
/* We choose to only get the type of the base object and
956
* ignore potentially corrupt pack file that expects the delta
969
970
memset(&stream, 0, sizeof(stream));
971
958
- stream.next_in = p->windows->base + offset;
959
- stream.avail_in = p->pack_size - offset;
972
+ stream.next_in = use_pack(p, w_curs, offset, &stream.avail_in);
973
stream.next_out = delta_head;
974
stream.avail_out = sizeof(delta_head);
975
995
return 0;
996
}
997
985
-static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
986
- enum object_type *type, unsigned long *sizep)
998
+static unsigned long unpack_object_header(struct packed_git *p,
999
+ struct pack_window **w_curs,
1000
+ unsigned long offset,
1001
+ enum object_type *type,
1002
+ unsigned long *sizep)
1003
{
1004
+ unsigned char *base;
1005
+ unsigned int left;
1006
unsigned long used;
1007
990
- if (p->pack_size <= offset)
991
- die("object offset outside of pack file");
992
-
993
- used = unpack_object_header_gently(p->windows->base + offset,
994
- p->pack_size - offset, type, sizep);
1008
+ base = use_pack(p, w_curs, offset, &left);
1009
+ used = unpack_object_header_gently(base, left, type, sizep);
1010
if (!used)
1011
die("object offset outside of pack file");
1012
1021
unsigned int *delta_chain_length,
1022
unsigned char *base_sha1)
1023
{
1024
+ struct pack_window *w_curs = NULL;
1025
unsigned long obj_offset, val;
1026
unsigned char *next_sha1;
1027
enum object_type kind;
1028
1029
*delta_chain_length = 0;
1030
obj_offset = offset;
1015
- offset = unpack_object_header(p, offset, &kind, size);
1031
+ offset = unpack_object_header(p, &w_curs, offset, &kind, size);
1032
1033
for (;;) {
1034
switch (kind) {
1041
case OBJ_TAG:
1042
strcpy(type, type_names[kind]);
1043
*store_size = 0; /* notyet */
1044
+ unuse_pack(&w_curs);
1045
return;
1046
case OBJ_OFS_DELTA:
1030
- get_delta_base(p, offset, kind, obj_offset, &offset);
1047
+ get_delta_base(p, &w_curs, offset, kind,
1048
+ obj_offset, &offset);
1049
if (*delta_chain_length == 0) {
1050
/* TODO: find base_sha1 as pointed by offset */
1051
}
1052
break;
1053
case OBJ_REF_DELTA:
1036
- if (p->pack_size <= offset + 20)
1037
- die("pack file %s records an incomplete delta base",
1038
- p->pack_name);
1039
- next_sha1 = p->windows->base + offset;
1054
+ next_sha1 = use_pack(p, &w_curs, offset, NULL);
1055
if (*delta_chain_length == 0)
1056
hashcpy(base_sha1, next_sha1);
1057
offset = find_pack_entry_one(next_sha1, p);
1058
break;
1059
}
1060
obj_offset = offset;
1046
- offset = unpack_object_header(p, offset, &kind, &val);
1061
+ offset = unpack_object_header(p, &w_curs, offset, &kind, &val);
1062
(*delta_chain_length)++;
1063
}
1064
}
1066
static int packed_object_info(struct packed_git *p, unsigned long offset,
1067
char *type, unsigned long *sizep)
1068
{
1069
+ struct pack_window *w_curs = NULL;
1070
unsigned long size, obj_offset = offset;
1071
enum object_type kind;
1072
+ int r;
1073
1057
- offset = unpack_object_header(p, offset, &kind, &size);
1074
+ offset = unpack_object_header(p, &w_curs, offset, &kind, &size);
1075
1076
switch (kind) {
1077
case OBJ_OFS_DELTA:
1078
case OBJ_REF_DELTA:
1062
- return packed_delta_info(p, offset, kind, obj_offset, type, sizep);
1079
+ r = packed_delta_info(p, &w_curs, offset, kind,
1080
+ obj_offset, type, sizep);
1081
+ unuse_pack(&w_curs);
1082
+ return r;
1083
case OBJ_COMMIT:
1084
case OBJ_TREE:
1085
case OBJ_BLOB:
1086
case OBJ_TAG:
1087
strcpy(type, type_names[kind]);
1088
+ unuse_pack(&w_curs);
1089
break;
1090
default:
1091
die("pack %s contains unknown object type %d",
1097
}
1098
1099
static void *unpack_compressed_entry(struct packed_git *p,
1100
+ struct pack_window **w_curs,
1101
unsigned long offset,
1102
unsigned long size)
1103
{
1108
buffer = xmalloc(size + 1);
1109
buffer[size] = 0;
1110
memset(&stream, 0, sizeof(stream));
1089
- stream.next_in = p->windows->base + offset;
1090
- stream.avail_in = p->pack_size - offset;
1111
+ stream.next_in = use_pack(p, w_curs, offset, &stream.avail_in);
1112
stream.next_out = buffer;
1113
stream.avail_out = size;
1114
1124
}
1125
1126
static void *unpack_delta_entry(struct packed_git *p,
1127
+ struct pack_window **w_curs,
1128
unsigned long offset,
1129
unsigned long delta_size,
1130
enum object_type kind,
1135
void *delta_data, *result, *base;
1136
unsigned long result_size, base_size, base_offset;
1137
1116
- offset = get_delta_base(p, offset, kind, obj_offset, &base_offset);
1138
+ offset = get_delta_base(p, w_curs, offset, kind,
1139
+ obj_offset, &base_offset);
1140
base = unpack_entry(p, base_offset, type, &base_size);
1141
if (!base)
1142
die("failed to read delta base object at %lu from %s",
1143
base_offset, p->pack_name);
1144
1122
- delta_data = unpack_compressed_entry(p, offset, delta_size);
1145
+ delta_data = unpack_compressed_entry(p, w_curs, offset, delta_size);
1146
result = patch_delta(base, base_size,
1147
delta_data, delta_size,
1148
&result_size);
1157
void *unpack_entry(struct packed_git *p, unsigned long offset,
1158
char *type, unsigned long *sizep)
1159
{
1160
+ struct pack_window *w_curs = NULL;
1161
unsigned long size, obj_offset = offset;
1162
enum object_type kind;
1163
void *retval;
1164
1141
- if (use_packed_git(p))
1142
- die("cannot map packed file");
1143
- offset = unpack_object_header(p, offset, &kind, &size);
1165
+ offset = unpack_object_header(p, &w_curs, offset, &kind, &size);
1166
switch (kind) {
1167
case OBJ_OFS_DELTA:
1168
case OBJ_REF_DELTA:
1147
- retval = unpack_delta_entry(p, offset, size, kind, obj_offset, type, sizep);
1169
+ retval = unpack_delta_entry(p, &w_curs, offset, size,
1170
+ kind, obj_offset, type, sizep);
1171
break;
1172
case OBJ_COMMIT:
1173
case OBJ_TREE:
1175
case OBJ_TAG:
1176
strcpy(type, type_names[kind]);
1177
*sizep = size;
1155
- retval = unpack_compressed_entry(p, offset, size);
1178
+ retval = unpack_compressed_entry(p, &w_curs, offset, size);
1179
break;
1180
default:
1181
die("unknown object type %i in %s", kind, p->pack_name);
1182
}
1160
- unuse_packed_git(p);
1183
+ unuse_pack(&w_curs);
1184
return retval;
1185
}
1186
1307
1308
int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1309
{
1287
- int status;
1310
struct pack_entry e;
1311
1312
if (!find_pack_entry(sha1, &e, NULL)) {
1314
if (!find_pack_entry(sha1, &e, NULL))
1315
return sha1_loose_object_info(sha1, type, sizep);
1316
}
1295
- if (use_packed_git(e.p))
1296
- die("cannot map packed file");
1297
- status = packed_object_info(e.p, e.offset, type, sizep);
1298
- unuse_packed_git(e.p);
1299
- return status;
1317
+ return packed_object_info(e.p, e.offset, type, sizep);
1318
}
1319
1320
static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)