Add git_odb_read_header method to the ODB As requested, git_odb_read_header looks up an object on the ODB, but loads only the header information (type & size) without loading any of the actual file contents in memory. It is significantly faster than doing a git_odb_read if you only need an object's information and not its contents. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
diff --git a/src/git/odb.h b/src/git/odb.h
index a132346..b481242 100644
--- a/src/git/odb.h
+++ b/src/git/odb.h
@@ -69,6 +69,25 @@ typedef struct {
GIT_EXTERN(int) git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id);
/**
+ * Read the header of an object from the database, without
+ * reading its full contents.
+ *
+ * Only the 'type' and 'len' fields of the git_rawobj structure
+ * are filled. The 'data' pointer will always be NULL.
+ *
+ * The raw object pointed by 'out' doesn't need to be manually
+ * closed with git_obj_close().
+ *
+ * @param out object descriptor to populate upon reading.
+ * @param db database to search for the object in.
+ * @param id identity of the object to read.
+ * @return
+ * - GIT_SUCCESS if the object was read;
+ * - GIT_ENOTFOUND if the object is not in the database.
+ */
+GIT_EXTERN(int) git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id);
+
+/**
* Read an object from the database using only pack files.
*
* If GIT_ENOTFOUND then out->data is set to NULL.
diff --git a/src/odb.c b/src/odb.c
index 8fbcebd..ff258b5 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -33,6 +33,10 @@
#define GIT_PACK_NAME_MAX (5 + 40 + 1)
+#define OBJ_LOCATION_NOTFOUND GIT_ENOTFOUND
+#define OBJ_LOCATION_INPACK 1
+#define OBJ_LOCATION_LOOSE 2
+
typedef struct {
uint32_t n;
unsigned char *oid;
@@ -40,6 +44,7 @@ typedef struct {
off_t size;
} index_entry;
+
struct git_pack {
git_odb *db;
git_lck lock;
@@ -148,6 +153,21 @@ static struct {
{ "REF_DELTA", 0 } /* 7 = GIT_OBJ_REF_DELTA */
};
+typedef union obj_location {
+ char loose_path[GIT_PATH_MAX];
+ struct {
+ git_pack *ptr;
+ uint32_t n;
+ } pack;
+} obj_location;
+
+
+/***********************************************************
+ *
+ * MISCELANEOUS HELPER FUNCTIONS
+ *
+ ***********************************************************/
+
GIT_INLINE(uint32_t) decode32(void *b)
{
return ntohl(*((uint32_t *)b));
@@ -340,6 +360,17 @@ static size_t get_object_header(obj_hdr *hdr, unsigned char *data)
return used;
}
+
+
+
+
+
+/***********************************************************
+ *
+ * ZLIB RELATED FUNCTIONS
+ *
+ ***********************************************************/
+
static void init_stream(z_stream *s, void *out, size_t len)
{
memset(s, 0, sizeof(*s));
@@ -624,58 +655,19 @@ static int deflate_obj(gitfo_buf *buf, char *hdr, int hdrlen, git_rawobj *obj, i
return GIT_SUCCESS;
}
-static int write_obj(gitfo_buf *buf, git_oid *id, git_odb *db)
-{
- char file[GIT_PATH_MAX];
- char temp[GIT_PATH_MAX];
- git_file fd;
- if (object_file_name(file, sizeof(file), db->objects_dir, id))
- return GIT_ERROR;
- if (make_temp_file(&fd, temp, sizeof(temp), file) < 0)
- return GIT_ERROR;
- if (gitfo_write(fd, buf->data, buf->len) < 0) {
- gitfo_close(fd);
- gitfo_unlink(temp);
- return GIT_ERROR;
- }
- if (db->fsync_object_files)
- gitfo_fsync(fd);
- gitfo_close(fd);
- gitfo_chmod(temp, 0444);
- if (gitfo_move_file(temp, file) < 0) {
- gitfo_unlink(temp);
- return GIT_ERROR;
- }
-
- return GIT_SUCCESS;
-}
-static int open_alternates(git_odb *db)
-{
- unsigned n = 0;
-
- gitlck_lock(&db->lock);
- if (db->alternates) {
- gitlck_unlock(&db->lock);
- return 1;
- }
-
- db->alternates = git__malloc(sizeof(*db->alternates) * (n + 1));
- if (!db->alternates) {
- gitlck_unlock(&db->lock);
- return -1;
- }
-
- db->alternates[n] = NULL;
- db->n_alternates = n;
- gitlck_unlock(&db->lock);
- return 0;
-}
+/***********************************************************
+ *
+ * PACKFILE INDEX FUNCTIONS
+ *
+ * Get index formation for packfile indexes v1 and v2
+ *
+ ***********************************************************/
static int pack_openidx_map(git_pack *p)
{
@@ -1019,6 +1011,19 @@ static int pack_openidx_v2(git_pack *p)
return GIT_SUCCESS;
}
+
+
+
+
+
+/***********************************************************
+ *
+ * PACKFILE FUNCTIONS
+ *
+ * Locate, open and access the contents of a packfile
+ *
+ ***********************************************************/
+
static int pack_stat(git_pack *p)
{
char pb[GIT_PATH_MAX];
@@ -1365,127 +1370,20 @@ static int search_packs(git_pack **p, uint32_t *n, git_odb *db, const git_oid *i
return GIT_ENOTFOUND;
}
-static int exists_packed(git_odb *db, const git_oid *id)
-{
- return !search_packs(NULL, NULL, db, id);
-}
-
-static int exists_loose(git_odb *db, const git_oid *id)
-{
- char file[GIT_PATH_MAX];
-
- if (object_file_name(file, sizeof(file), db->objects_dir, id))
- return 0;
-
- if (gitfo_exists(file) < 0)
- return 0;
- return 1;
-}
-int git_odb_exists(git_odb *db, const git_oid *id)
-{
- /* TODO: extend to search alternate db's */
- if (exists_packed(db, id))
- return 1;
- return exists_loose(db, id);
-}
-int git_odb_open(git_odb **out, const char *objects_dir)
-{
- git_odb *db = git__calloc(1, sizeof(*db));
- if (!db)
- return GIT_ERROR;
- db->objects_dir = git__strdup(objects_dir);
- if (!db->objects_dir) {
- free(db);
- return GIT_ERROR;
- }
-
- gitlck_init(&db->lock);
- db->object_zlib_level = Z_BEST_SPEED;
- db->fsync_object_files = 0;
- *out = db;
- return GIT_SUCCESS;
-}
-void git_odb_close(git_odb *db)
-{
- git_packlist *pl;
-
- if (!db)
- return;
-
- gitlck_lock(&db->lock);
-
- pl = db->packlist;
- db->packlist = NULL;
-
- if (db->alternates) {
- git_odb **alt;
- for (alt = db->alternates; *alt; alt++)
- git_odb_close(*alt);
- free(db->alternates);
- }
-
- free(db->objects_dir);
-
- gitlck_unlock(&db->lock);
- if (pl)
- packlist_dec(db, pl);
- gitlck_free(&db->lock);
- free(db);
-}
-
-int git_odb_read(
- git_rawobj *out,
- git_odb *db,
- const git_oid *id)
-{
-attempt:
-
- assert(out && db);
-
- if (!git_odb__read_packed(out, db, id))
- return GIT_SUCCESS;
- if (!git_odb__read_loose(out, db, id))
- return GIT_SUCCESS;
- if (!open_alternates(db))
- goto attempt;
-
- out->data = NULL;
- return GIT_ENOTFOUND;
-}
-
-int git_odb__read_loose(git_rawobj *out, git_odb *db, const git_oid *id)
-{
- char file[GIT_PATH_MAX];
- gitfo_buf obj = GITFO_BUF_INIT;
-
- assert(out && db && id);
-
- out->data = NULL;
- out->len = 0;
- out->type = GIT_OBJ_BAD;
-
- if (object_file_name(file, sizeof(file), db->objects_dir, id))
- return GIT_ENOTFOUND; /* TODO: error handling */
-
- if (gitfo_read_file(&obj, file))
- return GIT_ENOTFOUND; /* TODO: error handling */
-
- if (inflate_disk_obj(out, &obj)) {
- gitfo_free_buf(&obj);
- return GIT_ENOTFOUND; /* TODO: error handling */
- }
-
- gitfo_free_buf(&obj);
-
- return GIT_SUCCESS;
-}
+/***********************************************************
+ *
+ * PACKFILE READING FUNCTIONS
+ *
+ * Read the contents of a packfile
+ *
+ ***********************************************************/
static int unpack_object(git_rawobj *out, git_pack *p, index_entry *e);
@@ -1611,7 +1509,6 @@ static int unpack_object(git_rawobj *out, git_pack *p, index_entry *e)
if (!p->idx_search(&n, p, &base_id) &&
!p->idx_get(&entry, p, n)) {
- /* FIXME: deflated_size - 20 ? */
res = unpack_object_delta(out, p, &entry,
buffer + GIT_OID_RAWSZ, deflated_size, inflated_size);
}
@@ -1626,52 +1523,374 @@ static int unpack_object(git_rawobj *out, git_pack *p, index_entry *e)
return GIT_SUCCESS;
}
-static int read_packed(git_rawobj *out, git_pack *p, const git_oid *id)
+
+
+
+
+/***********************************************************
+ *
+ * ODB OBJECT READING & WRITING
+ *
+ * Backend for the public API; read headers and full objects
+ * from the ODB. Write raw data to the ODB.
+ *
+ ***********************************************************/
+
+static int open_alternates(git_odb *db)
+{
+ unsigned n = 0;
+
+ gitlck_lock(&db->lock);
+ if (db->alternates) {
+ gitlck_unlock(&db->lock);
+ return 1;
+ }
+
+ /*
+ * FIXME: broken, makes no sense.
+ * n is always 0, the alternates array is always
+ * empty!
+ */
+
+ db->alternates = git__malloc(sizeof(*db->alternates) * (n + 1));
+ if (!db->alternates) {
+ gitlck_unlock(&db->lock);
+ return -1;
+ }
+
+ db->alternates[n] = NULL;
+ db->n_alternates = n;
+ gitlck_unlock(&db->lock);
+ return 0;
+}
+
+
+static int locate_object(obj_location *location, git_odb *db, const git_oid *id)
+{
+ memset(location, 0x0, sizeof(obj_location));
+
+ do {
+ if (object_file_name(location->loose_path, GIT_PATH_MAX, db->objects_dir, id) == GIT_SUCCESS &&
+ gitfo_exists(location->loose_path) == 0)
+ return OBJ_LOCATION_LOOSE;
+
+ if (search_packs(&location->pack.ptr, &location->pack.n, db, id) == GIT_SUCCESS)
+ return OBJ_LOCATION_INPACK;
+
+ } while (open_alternates(db) == GIT_SUCCESS);
+
+ return GIT_ENOTFOUND;
+}
+
+static int read_packed(git_rawobj *out, const obj_location *loc)
{
- uint32_t n;
index_entry e;
int res;
- assert(out && p && id);
+ assert(out && loc);
- if (pack_openidx(p))
+ if (pack_openidx(loc->pack.ptr))
return GIT_ERROR;
- res = p->idx_search(&n, p, id);
- if (!res)
- res = p->idx_get(&e, p, n);
+ res = loc->pack.ptr->idx_get(&e, loc->pack.ptr, loc->pack.n);
if (!res)
- res = unpack_object(out, p, &e);
+ res = unpack_object(out, loc->pack.ptr, &e);
- pack_decidx(p);
+ pack_decidx(loc->pack.ptr);
return res;
}
-int git_odb__read_packed(git_rawobj *out, git_odb *db, const git_oid *id)
+static int read_header_packed(git_rawobj *out, const obj_location *loc)
{
- git_packlist *pl = packlist_get(db);
- size_t j;
+ git_pack *pack;
+ index_entry e;
+ int error = GIT_SUCCESS, shift;
+ uint8_t *buffer, byte;
+
+ assert(out && loc);
+
+ pack = loc->pack.ptr;
+
+ if (pack_openidx(pack))
+ return GIT_ERROR;
+
+ if (pack->idx_get(&e, pack, loc->pack.n) < 0 ||
+ open_pack(pack) < 0) {
+ error = GIT_ENOTFOUND;
+ goto cleanup;
+ }
+
+ buffer = (uint8_t *)pack->pack_map.data + e.offset;
+
+ byte = *buffer++ & 0xFF;
+ out->type = (byte >> 4) & 0x7;
+ out->len = byte & 0xF;
+ shift = 4;
- assert(out && db && id);
+ while (byte & 0x80) {
+ byte = *buffer++ & 0xFF;
+ out->len += (byte & 0x7F) << shift;
+ shift += 7;
+ }
+
+ /*
+ * FIXME: if the object is not packed as a whole,
+ * we need to do a full load and apply the deltas before
+ * being able to read the header.
+ *
+ * I don't think there are any workarounds for this.'
+ */
+
+ if (out->type == GIT_OBJ_OFS_DELTA || out->type == GIT_OBJ_REF_DELTA) {
+ error = unpack_object(out, pack, &e);
+ git_obj_close(out);
+ }
+
+cleanup:
+ pack_decidx(loc->pack.ptr);
+ return error;
+}
+
+static int read_loose(git_rawobj *out, git_odb *db, const obj_location *loc)
+{
+ int error;
+ gitfo_buf obj = GITFO_BUF_INIT;
+
+ assert(out && db && loc);
out->data = NULL;
out->len = 0;
out->type = GIT_OBJ_BAD;
- if (!pl)
+ if (gitfo_read_file(&obj, loc->loose_path) < 0)
return GIT_ENOTFOUND;
- for (j = 0; j < pl->n_packs; j++) {
- if (!read_packed(out, pl->packs[j], id)) {
- packlist_dec(db, pl);
- return GIT_SUCCESS;
+ error = inflate_disk_obj(out, &obj);
+ gitfo_free_buf(&obj);
+
+ return error;
+}
+
+static int read_header_loose(git_rawobj *out, git_odb *db, const obj_location *loc)
+{
+ int error = GIT_SUCCESS, z_return = Z_ERRNO, read_bytes;
+ git_file fd;
+ z_stream zs;
+ obj_hdr header_obj;
+ unsigned char raw_buffer[16], inflated_buffer[64];
+
+ assert(out && db && loc);
+
+ out->data = NULL;
+
+ if ((fd = gitfo_open(loc->loose_path, O_RDONLY)) < 0)
+ return GIT_ENOTFOUND;
+
+ init_stream(&zs, inflated_buffer, sizeof(inflated_buffer));
+
+ if (inflateInit(&zs) < Z_OK) {
+ error = GIT_ERROR;
+ goto cleanup;
+ }
+
+ do {
+ if ((read_bytes = read(fd, raw_buffer, sizeof(raw_buffer))) > 0) {
+ set_stream_input(&zs, raw_buffer, read_bytes);
+ z_return = inflate(&zs, 0);
}
+ } while (z_return == Z_OK);
+
+ if ((z_return != Z_STREAM_END && z_return != Z_BUF_ERROR)
+ || get_object_header(&header_obj, inflated_buffer) == 0
+ || git_obj__loose_object_type(header_obj.type) == 0) {
+ error = GIT_EOBJCORRUPTED;
+ goto cleanup;
}
- packlist_dec(db, pl);
- return GIT_ENOTFOUND;
+ out->len = header_obj.size;
+ out->type = header_obj.type;
+
+cleanup:
+ finish_inflate(&zs);
+ gitfo_close(fd);
+ return error;
+}
+
+static int write_obj(gitfo_buf *buf, git_oid *id, git_odb *db)
+{
+ char file[GIT_PATH_MAX];
+ char temp[GIT_PATH_MAX];
+ git_file fd;
+
+ if (object_file_name(file, sizeof(file), db->objects_dir, id))
+ return GIT_ERROR;
+
+ if (make_temp_file(&fd, temp, sizeof(temp), file) < 0)
+ return GIT_ERROR;
+
+ if (gitfo_write(fd, buf->data, buf->len) < 0) {
+ gitfo_close(fd);
+ gitfo_unlink(temp);
+ return GIT_ERROR;
+ }
+
+ if (db->fsync_object_files)
+ gitfo_fsync(fd);
+ gitfo_close(fd);
+ gitfo_chmod(temp, 0444);
+
+ if (gitfo_move_file(temp, file) < 0) {
+ gitfo_unlink(temp);
+ return GIT_ERROR;
+ }
+
+ return GIT_SUCCESS;
+}
+
+
+
+
+
+
+/***********************************************************
+ *
+ * OBJECT DATABASE PUBLIC API
+ *
+ * Public calls for the ODB functionality
+ *
+ ***********************************************************/
+
+int git_odb_open(git_odb **out, const char *objects_dir)
+{
+ git_odb *db = git__calloc(1, sizeof(*db));
+ if (!db)
+ return GIT_ERROR;
+
+ db->objects_dir = git__strdup(objects_dir);
+ if (!db->objects_dir) {
+ free(db);
+ return GIT_ERROR;
+ }
+
+ gitlck_init(&db->lock);
+
+ db->object_zlib_level = Z_BEST_SPEED;
+ db->fsync_object_files = 0;
+
+ *out = db;
+ return GIT_SUCCESS;
+}
+
+void git_odb_close(git_odb *db)
+{
+ git_packlist *pl;
+
+ if (!db)
+ return;
+
+ gitlck_lock(&db->lock);
+
+ pl = db->packlist;
+ db->packlist = NULL;
+
+ if (db->alternates) {
+ git_odb **alt;
+ for (alt = db->alternates; *alt; alt++)
+ git_odb_close(*alt);
+ free(db->alternates);
+ }
+
+ free(db->objects_dir);
+
+ gitlck_unlock(&db->lock);
+ if (pl)
+ packlist_dec(db, pl);
+ gitlck_free(&db->lock);
+ free(db);
+}
+
+int git_odb__read_packed(git_rawobj *out, git_odb *db, const git_oid *id)
+{
+ obj_location loc;
+
+ if (locate_object(&loc, db, id) != OBJ_LOCATION_INPACK)
+ return GIT_ENOTFOUND;
+
+ return read_packed(out, &loc);
+}
+
+int git_odb__read_loose(git_rawobj *out, git_odb *db, const git_oid *id)
+{
+ obj_location loc;
+
+ if (locate_object(&loc, db, id) != OBJ_LOCATION_LOOSE)
+ return GIT_ENOTFOUND;
+
+ return read_loose(out, db, &loc);
+}
+
+int git_odb_exists(git_odb *db, const git_oid *id)
+{
+ obj_location loc;
+ assert(db && id);
+ return locate_object(&loc, db, id) == OBJ_LOCATION_NOTFOUND ? 0 : 1;
+}
+
+int git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id)
+{
+ obj_location loc;
+ int found, error = 0;
+
+ assert(out && db);
+
+ found = locate_object(&loc, db, id);
+
+ switch (found) {
+ case OBJ_LOCATION_LOOSE:
+ error = read_header_loose(out, db, &loc);
+ break;
+
+ case OBJ_LOCATION_INPACK:
+ error = read_header_packed(out, &loc);
+ break;
+
+ case OBJ_LOCATION_NOTFOUND:
+ error = GIT_ENOTFOUND;
+ break;
+ }
+
+ return error;
+}
+
+int git_odb_read(
+ git_rawobj *out,
+ git_odb *db,
+ const git_oid *id)
+{
+ obj_location loc;
+ int found, error = 0;
+
+ assert(out && db);
+
+ found = locate_object(&loc, db, id);
+
+ switch (found) {
+ case OBJ_LOCATION_LOOSE:
+ error = read_loose(out, db, &loc);
+ break;
+
+ case OBJ_LOCATION_INPACK:
+ error = read_packed(out, &loc);
+ break;
+
+ case OBJ_LOCATION_NOTFOUND:
+ error = GIT_ENOTFOUND;
+ break;
+ }
+
+ return error;
}
int git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj)