odb_pack.c 16.4 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8
 */

#include "common.h"
9

10
#include <zlib.h>
11
#include "git2/repository.h"
12 13
#include "git2/indexer.h"
#include "git2/sys/odb_backend.h"
14 15 16
#include "fileops.h"
#include "hash.h"
#include "odb.h"
17
#include "delta.h"
18
#include "sha1_lookup.h"
19
#include "mwindow.h"
20
#include "pack.h"
21

22
#include "git2/odb_backend.h"
23

24 25 26
/* re-freshen pack files no more than every 2 seconds */
#define FRESHEN_FREQUENCY 2

Vicent Marti committed
27 28 29
struct pack_backend {
	git_odb_backend parent;
	git_vector packs;
30
	struct git_pack_file *last_found;
31
	char *pack_folder;
Vicent Marti committed
32 33
};

34 35
struct pack_writepack {
	struct git_odb_writepack parent;
36
	git_indexer *indexer;
37 38
};

Vicent Marti committed
39 40 41
/**
 * The wonderful tale of a Packed Object lookup query
 * ===================================================
Vicent Marti committed
42 43 44
 *	A riveting and epic story of epicness and ASCII
 *			art, presented by yours truly,
 *				Sir Vicent of Marti
Vicent Marti committed
45 46 47 48 49 50 51 52 53 54
 *
 *
 *	Chapter 1: Once upon a time...
 *	Initialization of the Pack Backend
 *	--------------------------------------------------
 *
 *	# git_odb_backend_pack
 *	| Creates the pack backend structure, initializes the
 *	| callback pointers to our default read() and exist() methods,
 *	| and tries to preload all the known packfiles in the ODB.
Vicent Marti committed
55
 * |
Vicent Marti committed
56
 *	|-# packfile_load_all
Vicent Marti committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 *	 | Tries to find the `pack` folder, if it exists. ODBs without
 *	 | a pack folder are ignored altogether. If there's a `pack` folder
 *	 | we run a `dirent` callback through every file in the pack folder
 *	 | to find our packfiles. The packfiles are then sorted according
 *	 | to a sorting callback.
 * 	 |
 *	 |-# packfile_load__cb
 *	 | | This callback is called from `dirent` with every single file
 *	 | | inside the pack folder. We find the packs by actually locating
 *	 | | their index (ends in ".idx"). From that index, we verify that
 *	 | | the corresponding packfile exists and is valid, and if so, we
 *	| | add it to the pack list.
 *	 | |
 *	 | |-# packfile_check
 *	 |		Make sure that there's a packfile to back this index, and store
 *	 |		some very basic information regarding the packfile itself,
 *	 |		such as the full path, the size, and the modification time.
 *	 |		We don't actually open the packfile to check for internal consistency.
 *	|
 *	|-# packfile_sort__cb
 *		Sort all the preloaded packs according to some specific criteria:
 *		we prioritize the "newer" packs because it's more likely they
 *		contain the objects we are looking for, and we prioritize local
 *		packs over remote ones.
Vicent Marti committed
81 82 83 84 85 86 87
 *
 *
 *
 *	Chapter 2: To be, or not to be...
 *	A standard packed `exist` query for an OID
 *	--------------------------------------------------
 *
Vicent Marti committed
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
 * # pack_backend__exists
 * | Check if the given SHA1 oid exists in any of the packs
 * | that have been loaded for our ODB.
 * |
 * |-# pack_entry_find
 *	| Iterate through all the packs that have been preloaded
 *	| (starting by the pack where the latest object was found)
 *	| to try to find the OID in one of them.
 *	|
 *	|-# pack_entry_find1
 *		| Check the index of an individual pack to see if the SHA1
 *		| OID can be found. If we can find the offset to that SHA1
 *		| inside of the index, that means the object is contained
 *		| inside of the packfile and we can stop searching.
 *		| Before returning, we verify that the packfile behing the
 *		| index we are searching still exists on disk.
 *		|
 *		|-# pack_entry_find_offset
 *		| | Mmap the actual index file to disk if it hasn't been opened
 *		| | yet, and run a binary search through it to find the OID.
 *		| | See <http://book.git-scm.com/7_the_packfile.html> for specifics
 *		| | on the Packfile Index format and how do we find entries in it.
 *		| |
 *		| |-# pack_index_open
 *		|	| Guess the name of the index based on the full path to the
 *		|	| packfile, open it and verify its contents. Only if the index
 *		|	| has not been opened already.
 *		|	|
 *		|	|-# pack_index_check
 *		|		Mmap the index file and do a quick run through the header
 *		|		to guess the index version (right now we support v1 and v2),
 *		|		and to verify that the size of the index makes sense.
 *		|
 *		|-# packfile_open
 *			See `packfile_open` in Chapter 3
Vicent Marti committed
123 124 125 126 127 128 129 130 131
 *
 *
 *
 *	Chapter 3: The neverending story...
 *	A standard packed `lookup` query for an OID
 *	--------------------------------------------------
 *	TODO
 *
 */
132 133 134 135


/***********************************************************
 *
Vicent Marti committed
136
 * FORWARD DECLARATIONS
137 138 139
 *
 ***********************************************************/

Vicent Marti committed
140
static int packfile_sort__cb(const void *a_, const void *b_);
141

142
static int packfile_load__cb(void *_data, git_buf *path);
143

144
static int pack_entry_find(struct git_pack_entry *e,
145
	struct pack_backend *backend, const git_oid *oid);
146

147 148
/* Can find the offset of an object given
 * a prefix of an identifier.
149
 * Sets GIT_EAMBIGUOUS if short oid is ambiguous.
150 151
 * This method assumes that len is between
 * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
152
 */
153 154 155 156
static int pack_entry_find_prefix(
	struct git_pack_entry *e,
	struct pack_backend *backend,
	const git_oid *short_oid,
157
	size_t len);
158

Vicent Marti committed
159 160 161 162 163 164 165


/***********************************************************
 *
 * PACK WINDOW MANAGEMENT
 *
 ***********************************************************/
166

Vicent Marti committed
167
static int packfile_sort__cb(const void *a_, const void *b_)
168
{
169 170
	const struct git_pack_file *a = a_;
	const struct git_pack_file *b = b_;
Vicent Marti committed
171
	int st;
172

Vicent Marti committed
173 174
	/*
	 * Local packs tend to contain objects specific to our
Vicent Marti committed
175
	 * variant of the project than remote ones. In addition,
Vicent Marti committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	 * remote ones could be on a network mounted filesystem.
	 * Favor local ones for these reasons.
	 */
	st = a->pack_local - b->pack_local;
	if (st)
		return -st;

	/*
	 * Younger packs tend to contain more recent objects,
	 * and more recent objects tend to get accessed more
	 * often.
	 */
	if (a->mtime < b->mtime)
		return 1;
	else if (a->mtime == b->mtime)
		return 0;

	return -1;
194 195
}

Vicent Marti committed
196

197
static int packfile_load__cb(void *data, git_buf *path)
198
{
199
	struct pack_backend *backend = data;
200
	struct git_pack_file *pack;
201 202
	const char *path_str = git_buf_cstr(path);
	size_t i, cmp_len = git_buf_len(path);
Vicent Marti committed
203
	int error;
204

205
	if (cmp_len <= strlen(".idx") || git__suffixcmp(path_str, ".idx") != 0)
206
		return 0; /* not an index */
207

208 209
	cmp_len -= strlen(".idx");

210
	for (i = 0; i < backend->packs.length; ++i) {
211
		struct git_pack_file *p = git_vector_get(&backend->packs, i);
212 213

		if (memcmp(p->pack_name, path_str, cmp_len) == 0)
214
			return 0;
215
	}
216

217
	error = git_mwindow_get_pack(&pack, path->ptr);
218 219 220 221

	/* ignore missing .pack file as git does */
	if (error == GIT_ENOTFOUND) {
		giterr_clear();
222
		return 0;
223 224 225 226 227
	}

	if (!error)
		error = git_vector_insert(&backend->packs, pack);

228
	return error;
Vicent Marti committed
229

230 231
}

232 233 234 235 236
static int pack_entry_find_inner(
	struct git_pack_entry *e,
	struct pack_backend *backend,
	const git_oid *oid,
	struct git_pack_file *last_found)
Vicent Marti committed
237
{
238
	size_t i;
239

240 241
	if (last_found &&
		git_pack_entry_find(e, last_found, oid, GIT_OID_HEXSZ) == 0)
242
		return 0;
243

Vicent Marti committed
244
	for (i = 0; i < backend->packs.length; ++i) {
245
		struct git_pack_file *p;
246

Vicent Marti committed
247
		p = git_vector_get(&backend->packs, i);
248
		if (p == last_found)
Vicent Marti committed
249
			continue;
250

251
		if (git_pack_entry_find(e, p, oid, GIT_OID_HEXSZ) == 0) {
Vicent Marti committed
252
			backend->last_found = p;
253
			return 0;
254
		}
Vicent Marti committed
255 256
	}

257 258 259 260 261
	return -1;
}

static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backend, const git_oid *oid)
{
262
	struct git_pack_file *last_found = backend->last_found;
263 264 265 266 267

	if (backend->last_found &&
		git_pack_entry_find(e, backend->last_found, oid, GIT_OID_HEXSZ) == 0)
		return 0;

268
	if (!pack_entry_find_inner(e, backend, oid, last_found))
269 270
		return 0;

271 272
	return git_odb__error_notfound(
		"failed to find pack entry", oid, GIT_OID_HEXSZ);
Vicent Marti committed
273 274
}

275 276 277 278 279
static int pack_entry_find_prefix(
	struct git_pack_entry *e,
	struct pack_backend *backend,
	const git_oid *short_oid,
	size_t len)
280 281
{
	int error;
282
	size_t i;
283 284 285
	git_oid found_full_oid = {{0}};
	bool found = false;
	struct git_pack_file *last_found = backend->last_found;
286

287 288
	if (last_found) {
		error = git_pack_entry_find(e, last_found, short_oid, len);
289 290
		if (error == GIT_EAMBIGUOUS)
			return error;
291 292 293 294
		if (!error) {
			git_oid_cpy(&found_full_oid, &e->sha1);
			found = true;
		}
295 296 297
	}

	for (i = 0; i < backend->packs.length; ++i) {
298
		struct git_pack_file *p;
299 300

		p = git_vector_get(&backend->packs, i);
301
		if (p == last_found)
302 303
			continue;

304
		error = git_pack_entry_find(e, p, short_oid, len);
305 306 307
		if (error == GIT_EAMBIGUOUS)
			return error;
		if (!error) {
308 309 310 311
			if (found && git_oid_cmp(&e->sha1, &found_full_oid))
				return git_odb__error_ambiguous("found multiple pack entries");
			git_oid_cpy(&found_full_oid, &e->sha1);
			found = true;
312 313 314 315
			backend->last_found = p;
		}
	}

316
	if (!found)
317 318
		return git_odb__error_notfound("no matching pack entry for prefix",
			short_oid, len);
319 320
	else
		return 0;
321 322
}

Vicent Marti committed
323

324 325 326 327 328 329 330
/***********************************************************
 *
 * PACKED BACKEND PUBLIC API
 *
 * Implement the git_odb_backend API calls
 *
 ***********************************************************/
331
static int pack_backend__refresh(git_odb_backend *backend_)
Vicent Marti committed
332 333 334 335
{
	int error;
	struct stat st;
	git_buf path = GIT_BUF_INIT;
336
	struct pack_backend *backend = (struct pack_backend *)backend_;
Vicent Marti committed
337

338
	if (backend->pack_folder == NULL)
Vicent Marti committed
339 340
		return 0;

341
	if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
342
		return git_odb__error_notfound("failed to refresh packfiles", NULL, 0);
Vicent Marti committed
343

344
	git_buf_sets(&path, backend->pack_folder);
Vicent Marti committed
345 346

	/* reload all packs */
347
	error = git_path_direach(&path, 0, packfile_load__cb, backend);
Vicent Marti committed
348

349
	git_buf_free(&path);
350
	git_vector_sort(&backend->packs);
Vicent Marti committed
351

352
	return error;
Vicent Marti committed
353 354
}

355
static int pack_backend__read_header(
nulltoken committed
356 357
	size_t *len_p, git_otype *type_p,
	struct git_odb_backend *backend, const git_oid *oid)
358
{
359 360
	struct git_pack_entry e;
	int error;
361

362
	assert(len_p && type_p && backend && oid);
363

364 365
	if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0)
		return error;
366

367
	return git_packfile_resolve_header(len_p, type_p, e.p, e.offset);
368 369
}

370 371 372 373
static int pack_backend__freshen(
	git_odb_backend *backend, const git_oid *oid)
{
	struct git_pack_entry e;
374
	time_t now;
375 376 377 378 379
	int error;

	if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0)
		return error;

380 381 382 383 384 385 386 387 388 389
	now = time(NULL);

	if (e.p->last_freshen > now - FRESHEN_FREQUENCY)
		return 0;

	if ((error = git_futils_touch(e.p->pack_name, &now)) < 0)
		return error;

	e.p->last_freshen = now;
	return 0;
390 391
}

392
static int pack_backend__read(
nulltoken committed
393 394
	void **buffer_p, size_t *len_p, git_otype *type_p,
	git_odb_backend *backend, const git_oid *oid)
395
{
396
	struct git_pack_entry e;
397
	git_rawobj raw = {NULL};
Vicent Marti committed
398
	int error;
399

400 401 402
	if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < 0 ||
		(error = git_packfile_unpack(&raw, e.p, &e.offset)) < 0)
		return error;
Vicent Marti committed
403 404 405 406 407

	*buffer_p = raw.data;
	*len_p = raw.len;
	*type_p = raw.type;

408
	return 0;
409 410
}

411
static int pack_backend__read_prefix(
Vicent Marti committed
412 413 414 415 416 417
	git_oid *out_oid,
	void **buffer_p,
	size_t *len_p,
	git_otype *type_p,
	git_odb_backend *backend,
	const git_oid *short_oid,
418
	size_t len)
419
{
420 421
	int error = 0;

422
	if (len < GIT_OID_MINPREFIXLEN)
423
		error = git_odb__error_ambiguous("prefix length too short");
424

425
	else if (len >= GIT_OID_HEXSZ) {
426
		/* We can fall back to regular read method */
427 428
		error = pack_backend__read(buffer_p, len_p, type_p, backend, short_oid);
		if (!error)
429 430
			git_oid_cpy(out_oid, short_oid);
	} else {
431
		struct git_pack_entry e;
432
		git_rawobj raw = {NULL};
433

434 435 436 437 438 439 440 441 442
		if ((error = pack_entry_find_prefix(
				&e, (struct pack_backend *)backend, short_oid, len)) == 0 &&
			(error = git_packfile_unpack(&raw, e.p, &e.offset)) == 0)
		{
			*buffer_p = raw.data;
			*len_p = raw.len;
			*type_p = raw.type;
			git_oid_cpy(out_oid, &e.sha1);
		}
443
	}
444

445
	return error;
446 447
}

448
static int pack_backend__exists(git_odb_backend *backend, const git_oid *oid)
449
{
450
	struct git_pack_entry e;
451
	return pack_entry_find(&e, (struct pack_backend *)backend, oid) == 0;
452 453
}

454 455 456 457 458 459 460 461 462 463 464 465
static int pack_backend__exists_prefix(
	git_oid *out, git_odb_backend *backend, const git_oid *short_id, size_t len)
{
	int error;
	struct pack_backend *pb = (struct pack_backend *)backend;
	struct git_pack_entry e = {0};

	error = pack_entry_find_prefix(&e, pb, short_id, len);
	git_oid_cpy(out, &e.sha1);
	return error;
}

466
static int pack_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data)
467
{
468
	int error;
469 470 471 472 473 474 475 476
	struct git_pack_file *p;
	struct pack_backend *backend;
	unsigned int i;

	assert(_backend && cb);
	backend = (struct pack_backend *)_backend;

	/* Make sure we know about the packfiles */
Vicent Marti committed
477
	if ((error = pack_backend__refresh(_backend)) < 0)
478
		return error;
479 480

	git_vector_foreach(&backend->packs, i, p) {
481
		if ((error = git_pack_foreach_entry(p, cb, data)) < 0)
482
			return error;
483
	}
484

485 486 487
	return 0;
}

488
static int pack_backend__writepack_append(struct git_odb_writepack *_writepack, const void *data, size_t size, git_transfer_progress *stats)
489 490 491 492 493
{
	struct pack_writepack *writepack = (struct pack_writepack *)_writepack;

	assert(writepack);

494
	return git_indexer_append(writepack->indexer, data, size, stats);
495 496 497 498 499 500 501 502
}

static int pack_backend__writepack_commit(struct git_odb_writepack *_writepack, git_transfer_progress *stats)
{
	struct pack_writepack *writepack = (struct pack_writepack *)_writepack;

	assert(writepack);

503
	return git_indexer_commit(writepack->indexer, stats);
504 505 506 507 508 509 510 511
}

static void pack_backend__writepack_free(struct git_odb_writepack *_writepack)
{
	struct pack_writepack *writepack = (struct pack_writepack *)_writepack;

	assert(writepack);

512
	git_indexer_free(writepack->indexer);
513 514 515 516 517
	git__free(writepack);
}

static int pack_backend__writepack(struct git_odb_writepack **out,
	git_odb_backend *_backend,
518
        git_odb *odb,
519
	git_transfer_progress_cb progress_cb,
520 521 522 523 524 525 526 527 528 529 530 531 532 533
	void *progress_payload)
{
	struct pack_backend *backend;
	struct pack_writepack *writepack;

	assert(out && _backend);

	*out = NULL;

	backend = (struct pack_backend *)_backend;

	writepack = git__calloc(1, sizeof(struct pack_writepack));
	GITERR_CHECK_ALLOC(writepack);

534
	if (git_indexer_new(&writepack->indexer,
535
		backend->pack_folder, 0, odb, progress_cb, progress_payload) < 0) {
536 537 538 539 540
		git__free(writepack);
		return -1;
	}

	writepack->parent.backend = _backend;
541
	writepack->parent.append = pack_backend__writepack_append;
542 543 544 545 546 547 548 549
	writepack->parent.commit = pack_backend__writepack_commit;
	writepack->parent.free = pack_backend__writepack_free;

	*out = (git_odb_writepack *)writepack;

	return 0;
}

550
static void pack_backend__free(git_odb_backend *_backend)
551
{
Vicent Marti committed
552
	struct pack_backend *backend;
553
	size_t i;
554 555 556

	assert(_backend);

Vicent Marti committed
557
	backend = (struct pack_backend *)_backend;
558

Vicent Marti committed
559
	for (i = 0; i < backend->packs.length; ++i) {
560
		struct git_pack_file *p = git_vector_get(&backend->packs, i);
561
		git_mwindow_put_pack(p);
Vicent Marti committed
562
	}
563

Vicent Marti committed
564
	git_vector_free(&backend->packs);
565 566
	git__free(backend->pack_folder);
	git__free(backend);
567 568
}

569
static int pack_backend__alloc(struct pack_backend **out, size_t initial_size)
570
{
571 572
	struct pack_backend *backend = git__calloc(1, sizeof(struct pack_backend));
	GITERR_CHECK_ALLOC(backend);
573

574 575
	if (git_vector_init(&backend->packs, initial_size, packfile_sort__cb) < 0) {
		git__free(backend);
576
		return -1;
577
	}
578

579
	backend->parent.version = GIT_ODB_BACKEND_VERSION;
580 581 582

	backend->parent.read = &pack_backend__read;
	backend->parent.read_prefix = &pack_backend__read_prefix;
583
	backend->parent.read_header = &pack_backend__read_header;
584
	backend->parent.exists = &pack_backend__exists;
585
	backend->parent.exists_prefix = &pack_backend__exists_prefix;
Vicent Marti committed
586
	backend->parent.refresh = &pack_backend__refresh;
587
	backend->parent.foreach = &pack_backend__foreach;
588
	backend->parent.writepack = &pack_backend__writepack;
589
	backend->parent.freshen = &pack_backend__freshen;
590 591
	backend->parent.free = &pack_backend__free;

592
	*out = backend;
593 594 595
	return 0;
}

596
int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx)
597
{
598
	struct pack_backend *backend = NULL;
599
	struct git_pack_file *packfile = NULL;
600

601 602
	if (pack_backend__alloc(&backend, 1) < 0)
		return -1;
603

604
	if (git_mwindow_get_pack(&packfile, idx) < 0 ||
605
		git_vector_insert(&backend->packs, packfile) < 0)
606
	{
607
		pack_backend__free((git_odb_backend *)backend);
608 609
		return -1;
	}
610

611 612 613 614 615 616 617 618 619 620 621 622
	*backend_out = (git_odb_backend *)backend;
	return 0;
}

int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
{
	int error = 0;
	struct pack_backend *backend = NULL;
	git_buf path = GIT_BUF_INIT;

	if (pack_backend__alloc(&backend, 8) < 0)
		return -1;
Vicent Marti committed
623

624 625 626
	if (!(error = git_buf_joinpath(&path, objects_dir, "pack")) &&
		git_path_isdir(git_buf_cstr(&path)))
	{
627
		backend->pack_folder = git_buf_detach(&path);
Vicent Marti committed
628
		error = pack_backend__refresh((git_odb_backend *)backend);
Vicent Marti committed
629
	}
630

631 632 633 634
	if (error < 0) {
		pack_backend__free((git_odb_backend *)backend);
		backend = NULL;
	}
635 636

	*backend_out = (git_odb_backend *)backend;
637 638 639

	git_buf_free(&path);

640
	return error;
641
}