Add a pack index 'virtual function' to search by file offset In addition to searching the index by oid, we need to search by '.pack' file offset, particularly when processing OBJ_OFS_DELTA objects. Since the v1 and v2 file formats differ in the layout of the object records, we provide two implementations of the search function and initialise the (virtual) function pointer appropriately. Note that, as part of the creation of the 'offset index', we also add a check that the offset data in the index is within the bounds of the '.pack' file. Having sorted the file offsets, while creating the index, we only need to check the smallest and largest values. The offset index consists of the im_off_idx array, which contains the index entry numbers sorted into file offset order, and the im_off_next mapping array. The im_off_next array maps an index entry number to the 'next' index entry in file offset order. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
diff --git a/src/odb.c b/src/odb.c
index 87a63e9..8030223 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -40,6 +40,10 @@ struct git_pack {
uint32_t *,
struct git_pack *,
const git_oid *);
+ int (*idx_search_offset)(
+ uint32_t *,
+ struct git_pack *,
+ off_t);
/** The .idx file, mapped into memory. */
git_file idx_fd;
@@ -49,10 +53,18 @@ struct git_pack {
uint32_t *im_crc;
uint32_t *im_offset32;
uint32_t *im_offset64;
+ uint32_t *im_off_idx;
+ uint32_t *im_off_next;
/** Number of objects in this pack. */
uint32_t obj_cnt;
+ /** The size of the .pack file. */
+ off_t pack_size;
+
+ /** The mtime of the .pack file. */
+ time_t pack_mtime;
+
/** Number of git_packlist we appear in. */
unsigned int refcnt;
@@ -663,6 +675,51 @@ static int pack_openidx_map(git_pack *p)
return GIT_SUCCESS;
}
+typedef struct {
+ off_t offset;
+ uint32_t n;
+} offset_idx_info;
+
+static int cmp_offset_idx_info(const void *lhs, const void *rhs)
+{
+ const offset_idx_info *a = lhs;
+ const offset_idx_info *b = rhs;
+ return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
+}
+
+static int make_offset_index(git_pack *p, offset_idx_info *data)
+{
+ off_t min_off = 3 * 4, max_off = p->pack_size - GIT_OID_RAWSZ;
+ uint32_t *idx, *next;
+ uint32_t j;
+
+ qsort(data, p->obj_cnt, sizeof(*data), cmp_offset_idx_info);
+
+ if (data[0].offset < min_off || data[p->obj_cnt].offset > max_off)
+ return GIT_ERROR;
+
+ if ((idx = git__malloc(sizeof(*idx) * (p->obj_cnt+1))) == NULL)
+ return GIT_ERROR;
+ if ((next = git__malloc(sizeof(*next) * p->obj_cnt)) == NULL) {
+ free(idx);
+ return GIT_ERROR;
+ }
+
+ for (j = 0; j < p->obj_cnt+1; j++)
+ idx[j] = data[j].n;
+
+ for (j = 0; j < p->obj_cnt; j++) {
+ assert(idx[j] < p->obj_cnt);
+ assert(idx[j+1] < p->obj_cnt+1);
+
+ next[idx[j]] = idx[j+1];
+ }
+
+ p->im_off_idx = idx;
+ p->im_off_next = next;
+ return GIT_SUCCESS;
+}
+
static int idxv1_search(uint32_t *out, git_pack *p, const git_oid *id)
{
unsigned char *data = p->im_oid;
@@ -684,12 +741,37 @@ static int idxv1_search(uint32_t *out, git_pack *p, const git_oid *id)
return GIT_ENOTFOUND;
}
+static int idxv1_search_offset(uint32_t *out, git_pack *p, off_t offset)
+{
+ if (offset > 0 && offset < (p->pack_size - GIT_OID_RAWSZ)) {
+ uint32_t lo = 0, hi = p->obj_cnt+1;
+ unsigned char *data = p->im_oid;
+ uint32_t *idx = p->im_off_idx;
+ do {
+ uint32_t mid = (lo + hi) >> 1;
+ uint32_t n = idx[mid];
+ uint32_t pos = n * (GIT_OID_RAWSZ + 4);
+ off_t here = decode32(data + pos);
+ if (offset < here)
+ hi = mid;
+ else if (offset == here) {
+ *out = n;
+ return GIT_SUCCESS;
+ } else
+ lo = mid + 1;
+ } while (lo < hi);
+ }
+ return GIT_ENOTFOUND;
+}
+
static int pack_openidx_v1(git_pack *p)
{
uint32_t *src_fanout = p->idx_map.data;
uint32_t *im_fanout;
+ offset_idx_info *info;
size_t expsz;
- int j;
+ uint32_t j;
+
if ((im_fanout = git__malloc(sizeof(*im_fanout) * 256)) == NULL)
return GIT_ERROR;
@@ -711,8 +793,30 @@ static int pack_openidx_v1(git_pack *p)
}
p->idx_search = idxv1_search;
+ p->idx_search_offset = idxv1_search_offset;
p->im_fanout = im_fanout;
p->im_oid = (unsigned char *)(src_fanout + 256);
+
+ if ((info = git__malloc(sizeof(*info) * (p->obj_cnt+1))) == NULL) {
+ free(im_fanout);
+ return GIT_ERROR;
+ }
+
+ for (j = 0; j < p->obj_cnt; j++) {
+ uint32_t pos = j * (GIT_OID_RAWSZ + 4);
+ info[j].offset = decode32(p->im_oid + pos);
+ info[j].n = j;
+ }
+ info[p->obj_cnt].offset = p->pack_size - GIT_OID_RAWSZ;
+ info[p->obj_cnt].n = p->obj_cnt;
+
+ if (make_offset_index(p, info)) {
+ free(im_fanout);
+ free(info);
+ return GIT_ERROR;
+ }
+ free(info);
+
return GIT_SUCCESS;
}
@@ -737,11 +841,40 @@ static int idxv2_search(uint32_t *out, git_pack *p, const git_oid *id)
return GIT_ENOTFOUND;
}
+static int idxv2_search_offset(uint32_t *out, git_pack *p, off_t offset)
+{
+ if (offset > 0 && offset < (p->pack_size - GIT_OID_RAWSZ)) {
+ uint32_t lo = 0, hi = p->obj_cnt+1;
+ uint32_t *idx = p->im_off_idx;
+ do {
+ uint32_t mid = (lo + hi) >> 1;
+ uint32_t n = idx[mid];
+ uint32_t o32 = decode32(p->im_offset32 + n);
+ off_t here = o32;
+
+ if (o32 & 0x80000000) {
+ uint32_t o64_idx = (o32 & ~0x80000000);
+ here = decode64(p->im_offset64 + 2*o64_idx);
+ }
+
+ if (offset < here)
+ hi = mid;
+ else if (offset == here) {
+ *out = n;
+ return GIT_SUCCESS;
+ } else
+ lo = mid + 1;
+ } while (lo < hi);
+ }
+ return GIT_ENOTFOUND;
+}
+
static int pack_openidx_v2(git_pack *p)
{
unsigned char *data = p->idx_map.data;
uint32_t *src_fanout = (uint32_t *)(data + 8);
uint32_t *im_fanout;
+ offset_idx_info *info;
size_t sz, o64_sz, o64_len;
uint32_t j;
@@ -766,25 +899,67 @@ static int pack_openidx_v2(git_pack *p)
}
p->idx_search = idxv2_search;
+ p->idx_search_offset = idxv2_search_offset;
p->im_fanout = im_fanout;
p->im_oid = (unsigned char *)(src_fanout + 256);
p->im_crc = (uint32_t *)(p->im_oid + 20 * p->obj_cnt);
p->im_offset32 = p->im_crc + p->obj_cnt;
p->im_offset64 = p->im_offset32 + p->obj_cnt;
+ if ((info = git__malloc(sizeof(*info) * (p->obj_cnt+1))) == NULL) {
+ free(im_fanout);
+ return GIT_ERROR;
+ }
+
/* check 64-bit offset table index values are within bounds */
o64_sz = p->idx_map.len - sz;
o64_len = o64_sz / 8;
for (j = 0; j < p->obj_cnt; j++) {
uint32_t o32 = decode32(p->im_offset32 + j);
+ off_t offset = o32;
if (o32 & 0x80000000) {
uint32_t o64_idx = (o32 & ~0x80000000);
if (o64_idx >= o64_len) {
free(im_fanout);
+ free(info);
return GIT_ERROR;
}
+ offset = decode64(p->im_offset64 + 2*o64_idx);
}
+ info[j].offset = offset;
+ info[j].n = j;
+ }
+ info[p->obj_cnt].offset = p->pack_size - GIT_OID_RAWSZ;
+ info[p->obj_cnt].n = p->obj_cnt;
+
+ if (make_offset_index(p, info)) {
+ free(im_fanout);
+ free(info);
+ return GIT_ERROR;
}
+ free(info);
+
+ return GIT_SUCCESS;
+}
+
+static int pack_stat(git_pack *p)
+{
+ char pb[GIT_PATH_MAX];
+ struct stat sb;
+
+ if (git__fmt(pb, sizeof(pb), "%s/pack/%s.pack",
+ p->db->objects_dir,
+ p->pack_name) < 0)
+ return GIT_ERROR;
+
+ if (stat(pb, &sb) || !S_ISREG(sb.st_mode))
+ return GIT_ERROR;
+
+ if (sb.st_size < (3 * 4 + GIT_OID_RAWSZ))
+ return GIT_ERROR;
+
+ p->pack_size = sb.st_size;
+ p->pack_mtime = sb.st_mtime;
return GIT_SUCCESS;
}
@@ -792,40 +967,51 @@ static int pack_openidx_v2(git_pack *p)
static int pack_openidx(git_pack *p)
{
gitlck_lock(&p->lock);
- if (p->invalid)
- goto unlock_fail;
+
+ if (p->invalid) {
+ gitlck_unlock(&p->lock);
+ return GIT_ERROR;
+ }
+
if (++p->idxcnt == 1 && !p->idx_search) {
+ int status, version;
uint32_t *data;
- if (pack_openidx_map(p))
- goto invalid_fail;
+ if (pack_stat(p) || pack_openidx_map(p)) {
+ p->invalid = 1;
+ p->idxcnt--;
+ gitlck_unlock(&p->lock);
+ return GIT_ERROR;
+ }
data = p->idx_map.data;
+ status = GIT_SUCCESS;
+ version = 1;
- if (decode32(&data[0]) == PACK_TOC) {
- switch (decode32(&data[1])) {
- case 2:
- if (pack_openidx_v2(p))
- goto unmap_fail;
- break;
- default:
- goto unmap_fail;
- }
- } else if (pack_openidx_v1(p))
- goto unmap_fail;
- }
- gitlck_unlock(&p->lock);
- return GIT_SUCCESS;
+ if (decode32(&data[0]) == PACK_TOC)
+ version = decode32(&data[1]);
-unmap_fail:
- gitfo_free_map(&p->idx_map);
+ switch (version) {
+ case 1:
+ status = pack_openidx_v1(p);
+ break;
+ case 2:
+ status = pack_openidx_v2(p);
+ break;
+ default:
+ status = GIT_ERROR;
+ }
-invalid_fail:
- p->invalid = 1;
- p->idxcnt--;
+ if (status != GIT_SUCCESS) {
+ gitfo_free_map(&p->idx_map);
+ p->invalid = 1;
+ p->idxcnt--;
+ gitlck_unlock(&p->lock);
+ return status;
+ }
+ }
-unlock_fail:
gitlck_unlock(&p->lock);
- return GIT_ERROR;
+ return GIT_SUCCESS;
}
static void pack_decidx(git_pack *p)