odb.c 34 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 9
#include "odb.h"

10
#include <zlib.h>
11
#include "git2/object.h"
12
#include "git2/sys/odb_backend.h"
13
#include "fileops.h"
14
#include "hash.h"
15
#include "delta.h"
16
#include "filter.h"
17
#include "repository.h"
18
#include "blob.h"
19

20
#include "git2/odb_backend.h"
21
#include "git2/oid.h"
22
#include "git2/oidarray.h"
23

24 25
#define GIT_ALTERNATES_FILE "info/alternates"

26 27 28 29 30 31
/*
 * We work under the assumption that most objects for long-running
 * operations will be packed
 */
#define GIT_LOOSE_PRIORITY 1
#define GIT_PACKED_PRIORITY 2
32

33 34
#define GIT_ALTERNATES_MAX_DEPTH 5

35 36
bool git_odb__strict_hash_verification = true;

37 38 39 40
typedef struct
{
	git_odb_backend *backend;
	int priority;
41 42
	bool is_alternate;
	ino_t disk_inode;
43 44
} backend_internal;

45 46 47 48 49 50 51 52 53
static git_cache *odb_cache(git_odb *odb)
{
	if (odb->rc.owner != NULL) {
		git_repository *owner = odb->rc.owner;
		return &owner->objects;
	}

	return &odb->own_cache;
}
54

55
static int odb_otype_fast(git_object_t *type_p, git_odb *db, const git_oid *id);
56
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth);
57
static int error_null_oid(int error, const char *message);
58

59
static git_object_t odb_hardcoded_type(const git_oid *id)
60 61 62 63 64
{
	static git_oid empty_tree = {{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
					   0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};

	if (!git_oid_cmp(id, &empty_tree))
65
		return GIT_OBJECT_TREE;
66

67
	return GIT_OBJECT_INVALID;
68 69
}

70
static int odb_read_hardcoded(bool *found, git_rawobj *raw, const git_oid *id)
71
{
72
	git_object_t type;
73 74 75

	*found = false;

76
	if ((type = odb_hardcoded_type(id)) == GIT_OBJECT_INVALID)
77
		return 0;
78 79 80 81

	raw->type = type;
	raw->len = 0;
	raw->data = git__calloc(1, sizeof(uint8_t));
82
	GIT_ERROR_CHECK_ALLOC(raw->data);
83 84

	*found = true;
85 86 87
	return 0;
}

88 89 90 91 92
int git_odb__format_object_header(
	size_t *written,
	char *hdr,
	size_t hdr_size,
	git_off_t obj_len,
93
	git_object_t obj_type)
94
{
Vicent Marti committed
95
	const char *type_str = git_object_type2string(obj_type);
96 97 98
	int hdr_max = (hdr_size > INT_MAX-2) ? (INT_MAX-2) : (int)hdr_size;
	int len;

lhchavez committed
99
	len = p_snprintf(hdr, hdr_max, "%s %"PRId64, type_str, (int64_t)obj_len);
100 101

	if (len < 0 || len >= hdr_max) {
102
		git_error_set(GIT_ERROR_OS, "object header creation failed");
103 104 105 106 107
		return -1;
	}

	*written = (size_t)(len + 1);
	return 0;
108 109
}

110
int git_odb__hashobj(git_oid *id, git_rawobj *obj)
111 112
{
	git_buf_vec vec[2];
113
	char header[64];
114 115
	size_t hdrlen;
	int error;
116

117
	assert(id && obj);
118

119
	if (!git_object_typeisloose(obj->type)) {
120
		git_error_set(GIT_ERROR_INVALID, "invalid object type");
121
		return -1;
122
	}
Vicent Marti committed
123

124
	if (!obj->data && obj->len != 0) {
125
		git_error_set(GIT_ERROR_INVALID, "invalid object");
126
		return -1;
127
	}
128

129 130 131
	if ((error = git_odb__format_object_header(&hdrlen,
		header, sizeof(header), obj->len, obj->type)) < 0)
		return error;
132

133
	vec[0].data = header;
Vicent Marti committed
134
	vec[0].len = hdrlen;
135
	vec[1].data = obj->data;
Vicent Marti committed
136
	vec[1].len = obj->len;
137

138
	return git_hash_vec(id, vec, 2);
139 140
}

141

142
static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source)
Ramsay Jones committed
143
{
144
	git_odb_object *object = git__calloc(1, sizeof(git_odb_object));
Ramsay Jones committed
145

146 147 148 149 150 151
	if (object != NULL) {
		git_oid_cpy(&object->cached.oid, oid);
		object->cached.type = source->type;
		object->cached.size = source->len;
		object->buffer      = source->data;
	}
Ramsay Jones committed
152

Vicent Marti committed
153
	return object;
154 155
}

156
void git_odb_object__free(void *object)
157
{
Vicent Marti committed
158
	if (object != NULL) {
159
		git__free(((git_odb_object *)object)->buffer);
160
		git__free(object);
Vicent Marti committed
161 162
	}
}
163

164 165 166 167 168 169 170
const git_oid *git_odb_object_id(git_odb_object *object)
{
	return &object->cached.oid;
}

const void *git_odb_object_data(git_odb_object *object)
{
Vicent Marti committed
171
	return object->buffer;
172 173 174 175
}

size_t git_odb_object_size(git_odb_object *object)
{
Vicent Marti committed
176
	return object->cached.size;
177 178
}

179
git_object_t git_odb_object_type(git_odb_object *object)
180
{
Vicent Marti committed
181
	return object->cached.type;
182 183
}

184 185 186 187 188 189 190
int git_odb_object_dup(git_odb_object **dest, git_odb_object *source)
{
	git_cached_obj_incref(source);
	*dest = source;
	return 0;
}

191
void git_odb_object_free(git_odb_object *object)
Vicent Marti committed
192
{
193 194 195
	if (object == NULL)
		return;

196
	git_cached_obj_decref(object);
Vicent Marti committed
197
}
198

199
int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_object_t type)
Vicent Marti committed
200
{
201
	size_t hdr_len;
202
	char hdr[64], buffer[FILEIO_BUFSIZE];
203
	git_hash_ctx ctx;
204
	ssize_t read_len = 0;
205
	int error = 0;
Vicent Marti committed
206

207
	if (!git_object_typeisloose(type)) {
208
		git_error_set(GIT_ERROR_INVALID, "invalid object type for hash");
209 210 211
		return -1;
	}

212
	if ((error = git_hash_ctx_init(&ctx)) < 0)
213
		return error;
Vicent Marti committed
214

215 216 217
	if ((error = git_odb__format_object_header(&hdr_len, hdr,
		sizeof(hdr), size, type)) < 0)
		goto done;
218

219
	if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0)
220
		goto done;
Vicent Marti committed
221

Vicent Marti committed
222
	while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) {
223
		if ((error = git_hash_update(&ctx, buffer, read_len)) < 0)
224 225
			goto done;

Vicent Marti committed
226 227 228
		size -= read_len;
	}

Vicent Marti committed
229 230 231 232
	/* If p_read returned an error code, the read obviously failed.
	 * If size is not zero, the file was truncated after we originally
	 * stat'd it, so we consider this a read failure too */
	if (read_len < 0 || size > 0) {
233
		git_error_set(GIT_ERROR_OS, "error reading file for hashing");
234 235 236
		error = -1;

		goto done;
Vicent Marti committed
237 238
	}

239
	error = git_hash_final(out, &ctx);
Vicent Marti committed
240

241
done:
242
	git_hash_ctx_cleanup(&ctx);
243
	return error;
Vicent Marti committed
244 245
}

246
int git_odb__hashfd_filtered(
247
	git_oid *out, git_file fd, size_t size, git_object_t type, git_filter_list *fl)
248 249 250 251
{
	int error;
	git_buf raw = GIT_BUF_INIT;

252
	if (!fl)
253 254 255 256 257 258
		return git_odb__hashfd(out, fd, size, type);

	/* size of data is used in header, so we have to read the whole file
	 * into memory to apply filters before beginning to calculate the hash
	 */

259
	if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
260
		git_buf post = GIT_BUF_INIT;
261

262
		error = git_filter_list_apply_to_data(&post, fl, &raw);
263

264
		git_buf_dispose(&raw);
265

266 267 268
		if (!error)
			error = git_odb_hash(out, post.ptr, post.size, type);

269
		git_buf_dispose(&post);
270
	}
271 272 273 274

	return error;
}

275 276 277
int git_odb__hashlink(git_oid *out, const char *path)
{
	struct stat st;
278
	int size;
279
	int result;
280

281
	if (git_path_lstat(path, &st) < 0)
282
		return -1;
283

284
	if (!git__is_int(st.st_size) || (int)st.st_size < 0) {
285
		git_error_set(GIT_ERROR_FILESYSTEM, "file size overflow for 32-bit systems");
286 287
		return -1;
	}
288

289
	size = (int)st.st_size;
290

291 292
	if (S_ISLNK(st.st_mode)) {
		char *link_data;
293 294
		int read_len;
		size_t alloc_size;
295

296
		GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, size, 1);
297
		link_data = git__malloc(alloc_size);
298
		GIT_ERROR_CHECK_ALLOC(link_data);
299

300
		read_len = p_readlink(path, link_data, size);
301
		link_data[size] = '\0';
302
		if (read_len != size) {
303
			git_error_set(GIT_ERROR_OS, "failed to read symlink data for '%s'", path);
304
			git__free(link_data);
305 306
			return -1;
		}
307

308
		result = git_odb_hash(out, link_data, size, GIT_OBJECT_BLOB);
309
		git__free(link_data);
310
	} else {
311 312 313
		int fd = git_futils_open_ro(path);
		if (fd < 0)
			return -1;
314
		result = git_odb__hashfd(out, fd, size, GIT_OBJECT_BLOB);
315 316 317
		p_close(fd);
	}

318
	return result;
319 320
}

321
int git_odb_hashfile(git_oid *out, const char *path, git_object_t type)
322 323
{
	git_off_t size;
324 325
	int result, fd = git_futils_open_ro(path);
	if (fd < 0)
326
		return fd;
327 328

	if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
329
		git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
330
		p_close(fd);
331
		return -1;
332 333
	}

334
	result = git_odb__hashfd(out, fd, (size_t)size, type);
335
	p_close(fd);
336
	return result;
337 338
}

339
int git_odb_hash(git_oid *id, const void *data, size_t len, git_object_t type)
Vicent Marti committed
340 341
{
	git_rawobj raw;
342

Vicent Marti committed
343
	assert(id);
344

Vicent Marti committed
345 346 347
	raw.data = (void *)data;
	raw.len = len;
	raw.type = type;
348

349
	return git_odb__hashobj(id, &raw);
350 351
}

352 353 354 355 356 357 358 359
/**
 * FAKE WSTREAM
 */

typedef struct {
	git_odb_stream stream;
	char *buffer;
	size_t size, written;
360
	git_object_t type;
361 362
} fake_wstream;

363
static int fake_wstream__fwrite(git_odb_stream *_stream, const git_oid *oid)
364 365
{
	fake_wstream *stream = (fake_wstream *)_stream;
366
	return _stream->backend->write(_stream->backend, oid, stream->buffer, stream->size, stream->type);
367 368 369 370 371 372
}

static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len)
{
	fake_wstream *stream = (fake_wstream *)_stream;

373
	assert(stream->written + len <= stream->size);
374 375 376

	memcpy(stream->buffer + stream->written, data, len);
	stream->written += len;
377
	return 0;
378 379 380 381 382 383
}

static void fake_wstream__free(git_odb_stream *_stream)
{
	fake_wstream *stream = (fake_wstream *)_stream;

384 385
	git__free(stream->buffer);
	git__free(stream);
386 387
}

388
static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, git_off_t size, git_object_t type)
389 390
{
	fake_wstream *stream;
391
	size_t blobsize;
392

393 394
	GIT_ERROR_CHECK_BLOBSIZE(size);
	blobsize = (size_t)size;
395

396
	stream = git__calloc(1, sizeof(fake_wstream));
397
	GIT_ERROR_CHECK_ALLOC(stream);
398

399
	stream->size = blobsize;
400
	stream->type = type;
401
	stream->buffer = git__malloc(blobsize);
402
	if (stream->buffer == NULL) {
403
		git__free(stream);
404
		return -1;
405 406 407 408 409 410 411 412 413 414
	}

	stream->stream.backend = backend;
	stream->stream.read = NULL; /* read only */
	stream->stream.write = &fake_wstream__write;
	stream->stream.finalize_write = &fake_wstream__fwrite;
	stream->stream.free = &fake_wstream__free;
	stream->stream.mode = GIT_STREAM_WRONLY;

	*stream_p = (git_odb_stream *)stream;
415
	return 0;
416
}
417

418 419 420 421 422 423 424
/***********************************************************
 *
 * OBJECT DATABASE PUBLIC API
 *
 * Public calls for the ODB functionality
 *
 ***********************************************************/
425

426
static int backend_sort_cmp(const void *a, const void *b)
427
{
428 429
	const backend_internal *backend_a = (const backend_internal *)(a);
	const backend_internal *backend_b = (const backend_internal *)(b);
430

431 432 433 434 435 436 437 438
	if (backend_b->priority == backend_a->priority) {
		if (backend_a->is_alternate)
			return -1;
		if (backend_b->is_alternate)
			return 1;
		return 0;
	}
	return (backend_b->priority - backend_a->priority);
439 440
}

441
int git_odb_new(git_odb **out)
442
{
443
	git_odb *db = git__calloc(1, sizeof(*db));
444
	GIT_ERROR_CHECK_ALLOC(db);
445

446 447
	if (git_cache_init(&db->own_cache) < 0 ||
		git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) {
448
		git__free(db);
449
		return -1;
450
	}
451

452
	*out = db;
453
	GIT_REFCOUNT_INC(db);
454
	return 0;
455 456
}

457 458 459
static int add_backend_internal(
	git_odb *odb, git_odb_backend *backend,
	int priority, bool is_alternate, ino_t disk_inode)
Ramsay Jones committed
460
{
461 462
	backend_internal *internal;

463
	assert(odb && backend);
Ramsay Jones committed
464

465
	GIT_ERROR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
466

467 468
	/* Check if the backend is already owned by another ODB */
	assert(!backend->odb || backend->odb == odb);
Ramsay Jones committed
469

470
	internal = git__malloc(sizeof(backend_internal));
471
	GIT_ERROR_CHECK_ALLOC(internal);
472 473 474 475

	internal->backend = backend;
	internal->priority = priority;
	internal->is_alternate = is_alternate;
476
	internal->disk_inode = disk_inode;
Ramsay Jones committed
477

478
	if (git_vector_insert(&odb->backends, internal) < 0) {
479
		git__free(internal);
480
		return -1;
481
	}
Ramsay Jones committed
482

483
	git_vector_sort(&odb->backends);
484
	internal->backend->odb = odb;
485
	return 0;
Ramsay Jones committed
486 487
}

488 489
int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority)
{
490
	return add_backend_internal(odb, backend, priority, false, 0);
491 492 493 494
}

int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
{
495
	return add_backend_internal(odb, backend, priority, true, 0);
496 497
}

498 499 500 501 502 503
size_t git_odb_num_backends(git_odb *odb)
{
	assert(odb);
	return odb->backends.length;
}

504 505
static int git_odb__error_unsupported_in_backend(const char *action)
{
506
	git_error_set(GIT_ERROR_ODB,
507
		"cannot %s - unsupported in the loaded odb backends", action);
508 509 510 511
	return -1;
}


512 513 514 515
int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos)
{
	backend_internal *internal;

Linquize committed
516
	assert(out && odb);
517 518 519 520 521 522 523
	internal = git_vector_get(&odb->backends, pos);

	if (internal && internal->backend) {
		*out = internal->backend;
		return 0;
	}

524
	git_error_set(GIT_ERROR_ODB, "no ODB backend loaded at index %" PRIuZ, pos);
525 526 527
	return GIT_ENOTFOUND;
}

528
int git_odb__add_default_backends(
529 530
	git_odb *db, const char *objects_dir,
	bool as_alternates, int alternate_depth)
531
{
532 533
	size_t i;
	struct stat st;
534
	ino_t inode;
535 536
	git_odb_backend *loose, *packed;

537 538
	/* TODO: inodes are not really relevant on Win32, so we need to find
	 * a cross-platform workaround for this */
539 540 541 542 543 544
#ifdef GIT_WIN32
	GIT_UNUSED(i);
	GIT_UNUSED(st);

	inode = 0;
#else
545
	if (p_stat(objects_dir, &st) < 0) {
546 547 548
		if (as_alternates)
			return 0;

549
		git_error_set(GIT_ERROR_ODB, "failed to load object database in '%s'", objects_dir);
550 551 552
		return -1;
	}

553 554
	inode = st.st_ino;

555 556
	for (i = 0; i < db->backends.length; ++i) {
		backend_internal *backend = git_vector_get(&db->backends, i);
557
		if (backend->disk_inode == inode)
558 559
			return 0;
	}
560
#endif
561

562
	/* add the loose object backend */
563
	if (git_odb_backend_loose(&loose, objects_dir, -1, db->do_fsync, 0, 0) < 0 ||
564
		add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
565
		return -1;
566 567

	/* add the packed file backend */
568
	if (git_odb_backend_pack(&packed, objects_dir) < 0 ||
569
		add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates, inode) < 0)
570
		return -1;
571

572
	return load_alternates(db, objects_dir, alternate_depth);
573 574
}

575
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth)
576
{
577
	git_buf alternates_path = GIT_BUF_INIT;
578
	git_buf alternates_buf = GIT_BUF_INIT;
579 580
	char *buffer;
	const char *alternate;
581
	int result = 0;
582

583
	/* Git reports an error, we just ignore anything deeper */
584
	if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH)
585 586
		return 0;

587 588
	if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0)
		return -1;
589

590
	if (git_path_exists(alternates_path.ptr) == false) {
591
		git_buf_dispose(&alternates_path);
592
		return 0;
593
	}
594

595
	if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) {
596
		git_buf_dispose(&alternates_path);
597
		return -1;
598
	}
599

600
	buffer = (char *)alternates_buf.ptr;
601 602

	/* add each alternate as a new backend; one alternate per line */
603 604 605 606
	while ((alternate = git__strtok(&buffer, "\r\n")) != NULL) {
		if (*alternate == '\0' || *alternate == '#')
			continue;

607 608 609 610 611 612
		/*
		 * Relative path: build based on the current `objects`
		 * folder. However, relative paths are only allowed in
		 * the current repository.
		 */
		if (*alternate == '.' && !alternate_depth) {
613
			if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0)
614 615
				break;
			alternate = git_buf_cstr(&alternates_path);
616 617
		}

618
		if ((result = git_odb__add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0)
619 620
			break;
	}
621

622 623
	git_buf_dispose(&alternates_path);
	git_buf_dispose(&alternates_buf);
624

625
	return result;
626
}
Ramsay Jones committed
627

628 629
int git_odb_add_disk_alternate(git_odb *odb, const char *path)
{
630
	return git_odb__add_default_backends(odb, path, true, 0);
631 632
}

633
int git_odb_open(git_odb **out, const char *objects_dir)
Ramsay Jones committed
634
{
635
	git_odb *db;
Ramsay Jones committed
636

637 638 639 640
	assert(out && objects_dir);

	*out = NULL;

641 642
	if (git_odb_new(&db) < 0)
		return -1;
Ramsay Jones committed
643

644
	if (git_odb__add_default_backends(db, objects_dir, 0, 0) < 0) {
645 646 647
		git_odb_free(db);
		return -1;
	}
Ramsay Jones committed
648

649
	*out = db;
650
	return 0;
651
}
Ramsay Jones committed
652

653 654 655 656 657 658 659
int git_odb__set_caps(git_odb *odb, int caps)
{
	if (caps == GIT_ODB_CAP_FROM_OWNER) {
		git_repository *repo = odb->rc.owner;
		int val;

		if (!repo) {
660
			git_error_set(GIT_ERROR_ODB, "cannot access repository to set odb caps");
661 662 663 664 665 666 667 668 669 670
			return -1;
		}

		if (!git_repository__cvar(&val, repo, GIT_CVAR_FSYNCOBJECTFILES))
			odb->do_fsync = !!val;
	}

	return 0;
}

671
static void odb_free(git_odb *db)
672
{
673
	size_t i;
674

675
	for (i = 0; i < db->backends.length; ++i) {
676 677
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *backend = internal->backend;
678

679
		backend->free(backend);
680

681
		git__free(internal);
682 683
	}

684
	git_vector_free(&db->backends);
685
	git_cache_free(&db->own_cache);
686

687
	git__memzero(db, sizeof(*db));
688
	git__free(db);
689 690
}

691 692 693 694 695 696 697 698
void git_odb_free(git_odb *db)
{
	if (db == NULL)
		return;

	GIT_REFCOUNT_DEC(db, odb_free);
}

699 700 701 702
static int odb_exists_1(
	git_odb *db,
	const git_oid *id,
	bool only_refreshed)
703
{
704
	size_t i;
705
	bool found = false;
706

707
	for (i = 0; i < db->backends.length && !found; ++i) {
708 709
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;
710

711 712 713
		if (only_refreshed && !b->refresh)
			continue;

714
		if (b->exists != NULL)
Linquize committed
715
			found = (bool)b->exists(b, id);
716 717
	}

718
	return (int)found;
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
static int odb_freshen_1(
	git_odb *db,
	const git_oid *id,
	bool only_refreshed)
{
	size_t i;
	bool found = false;

	for (i = 0; i < db->backends.length && !found; ++i) {
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

		if (only_refreshed && !b->refresh)
			continue;

		if (b->freshen != NULL)
			found = !b->freshen(b, id);
		else if (b->exists != NULL)
			found = b->exists(b, id);
	}

	return (int)found;
}

745
int git_odb__freshen(git_odb *db, const git_oid *id)
746 747 748 749 750 751 752 753 754 755 756 757 758
{
	assert(db && id);

	if (odb_freshen_1(db, id, false))
		return 1;

	if (!git_odb_refresh(db))
		return odb_freshen_1(db, id, true);

	/* Failed to refresh, hence not found */
	return 0;
}

759
int git_odb_exists(git_odb *db, const git_oid *id)
760
{
761
	git_odb_object *object;
762

763
	assert(db && id);
764

765 766 767
	if (git_oid_iszero(id))
		return 0;

768 769
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
		git_odb_object_free(object);
770
		return 1;
771 772
	}

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	if (odb_exists_1(db, id, false))
		return 1;

	if (!git_odb_refresh(db))
		return odb_exists_1(db, id, true);

	/* Failed to refresh, hence not found */
	return 0;
}

static int odb_exists_prefix_1(git_oid *out, git_odb *db,
	const git_oid *key, size_t len, bool only_refreshed)
{
	size_t i;
	int error = GIT_ENOTFOUND, num_found = 0;
	git_oid last_found = {{0}}, found;
789

790 791 792 793
	for (i = 0; i < db->backends.length; ++i) {
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

794 795 796
		if (only_refreshed && !b->refresh)
			continue;

797 798 799
		if (!b->exists_prefix)
			continue;

800
		error = b->exists_prefix(&found, b, key, len);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
		if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH)
			continue;
		if (error)
			return error;

		/* make sure found item doesn't introduce ambiguity */
		if (num_found) {
			if (git_oid__cmp(&last_found, &found))
				return git_odb__error_ambiguous("multiple matches for prefix");
		} else {
			git_oid_cpy(&last_found, &found);
			num_found++;
		}
	}

	if (!num_found)
817 818
		return GIT_ENOTFOUND;

819 820 821
	if (out)
		git_oid_cpy(out, &last_found);

822
	return 0;
823 824
}

825 826 827 828
int git_odb_exists_prefix(
	git_oid *out, git_odb *db, const git_oid *short_id, size_t len)
{
	int error;
829
	git_oid key = {{0}};
830 831 832 833 834 835

	assert(db && short_id);

	if (len < GIT_OID_MINPREFIXLEN)
		return git_odb__error_ambiguous("prefix length too short");

836
	if (len >= GIT_OID_HEXSZ) {
837 838 839 840 841
		if (git_odb_exists(db, short_id)) {
			if (out)
				git_oid_cpy(out, short_id);
			return 0;
		} else {
842 843
			return git_odb__error_notfound(
				"no match for id prefix", short_id, len);
844 845 846
		}
	}

847
	git_oid__cpy_prefix(&key, short_id, len);
848 849 850 851 852 853 854

	error = odb_exists_prefix_1(out, db, &key, len, false);

	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
		error = odb_exists_prefix_1(out, db, &key, len, true);

	if (error == GIT_ENOTFOUND)
855
		return git_odb__error_notfound("no match for id prefix", &key, len);
856 857 858 859

	return error;
}

860
int git_odb_expand_ids(
861
	git_odb *db,
862 863
	git_odb_expand_id *ids,
	size_t count)
864
{
865
	size_t i;
866

867
	assert(db && ids);
868

869 870
	for (i = 0; i < count; i++) {
		git_odb_expand_id *query = &ids[i];
871
		int error = GIT_EAMBIGUOUS;
872

873
		if (!query->type)
874
			query->type = GIT_OBJECT_ANY;
875 876 877 878 879 880 881 882 883 884

		/* if we have a short OID, expand it first */
		if (query->length >= GIT_OID_MINPREFIXLEN && query->length < GIT_OID_HEXSZ) {
			git_oid actual_id;

			error = odb_exists_prefix_1(&actual_id, db, &query->id, query->length, false);
			if (!error) {
				git_oid_cpy(&query->id, &actual_id);
				query->length = GIT_OID_HEXSZ;
			}
885 886
		}

887
		/*
888 889
		 * now we ought to have a 40-char OID, either because we've expanded it
		 * or because the user passed a full OID. Ensure its type is right.
890
		 */
891
		if (query->length >= GIT_OID_HEXSZ) {
892
			git_object_t actual_type;
893

894 895
			error = odb_otype_fast(&actual_type, db, &query->id);
			if (!error) {
896
				if (query->type != GIT_OBJECT_ANY && query->type != actual_type)
897 898 899 900 901
					error = GIT_ENOTFOUND;
				else
					query->type = actual_type;
			}
		}
902

903
		switch (error) {
904
		/* no errors, so we've successfully expanded the OID */
905
		case 0:
906
			continue;
907 908 909 910

		/* the object is missing or ambiguous */
		case GIT_ENOTFOUND:
		case GIT_EAMBIGUOUS:
911 912 913
			memset(&query->id, 0, sizeof(git_oid));
			query->length = 0;
			query->type = 0;
914 915 916 917 918
			break;

		/* something went very wrong with the ODB; bail hard */
		default:
			return error;
919 920 921
		}
	}

922
	git_error_clear();
923
	return 0;
924 925
}

926
int git_odb_read_header(size_t *len_p, git_object_t *type_p, git_odb *db, const git_oid *id)
927
{
928 929 930 931 932 933 934 935 936 937 938
	int error;
	git_odb_object *object;

	error = git_odb__read_header_or_object(&object, len_p, type_p, db, id);

	if (object)
		git_odb_object_free(object);

	return error;
}

939
static int odb_read_header_1(
940
	size_t *len_p, git_object_t *type_p, git_odb *db,
941 942 943
	const git_oid *id, bool only_refreshed)
{
	size_t i;
944
	git_object_t ht;
945 946
	bool passthrough = false;
	int error;
947

948
	if (!only_refreshed && (ht = odb_hardcoded_type(id)) != GIT_OBJECT_INVALID) {
949 950 951 952 953
		*type_p = ht;
		*len_p = 0;
		return 0;
	}

954
	for (i = 0; i < db->backends.length; ++i) {
955 956 957 958 959 960
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

		if (only_refreshed && !b->refresh)
			continue;

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		if (!b->read_header) {
			passthrough = true;
			continue;
		}

		error = b->read_header(len_p, type_p, b, id);

		switch (error) {
		case GIT_PASSTHROUGH:
			passthrough = true;
			break;
		case GIT_ENOTFOUND:
			break;
		default:
			return error;
		}
977 978
	}

979
	return passthrough ? GIT_PASSTHROUGH : GIT_ENOTFOUND;
980 981
}

982
int git_odb__read_header_or_object(
983
	git_odb_object **out, size_t *len_p, git_object_t *type_p,
984 985
	git_odb *db, const git_oid *id)
{
986
	int error = GIT_ENOTFOUND;
Vicent Marti committed
987
	git_odb_object *object;
988

989
	assert(db && id && out && len_p && type_p);
Vicent Marti committed
990

991 992 993 994 995
	*out = NULL;

	if (git_oid_iszero(id))
		return error_null_oid(GIT_ENOTFOUND, "cannot read object");

996
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
Vicent Marti committed
997 998
		*len_p = object->cached.size;
		*type_p = object->cached.type;
999
		*out = object;
1000
		return 0;
Vicent Marti committed
1001
	}
1002

1003
	error = odb_read_header_1(len_p, type_p, db, id, false);
1004

1005 1006
	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
		error = odb_read_header_1(len_p, type_p, db, id, true);
1007

1008 1009
	if (error == GIT_ENOTFOUND)
		return git_odb__error_notfound("cannot read header for", id, GIT_OID_HEXSZ);
1010

1011 1012
	/* we found the header; return early */
	if (!error)
1013
		return 0;
Vicent Marti committed
1014

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	if (error == GIT_PASSTHROUGH) {
		/*
		 * no backend has header-reading functionality
		 * so try using `git_odb_read` instead
		 */
		error = git_odb_read(&object, db, id);
		if (!error) {
			*len_p = object->cached.size;
			*type_p = object->cached.type;
			*out = object;
		}
1026
	}
1027 1028

	return error;
1029 1030
}

1031 1032
static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
		bool only_refreshed)
1033
{
1034
	size_t i;
Vicent Marti committed
1035
	git_rawobj raw;
1036
	git_odb_object *object;
1037
	git_oid hashed;
1038
	bool found = false;
1039
	int error = 0;
1040

1041 1042 1043 1044
	if (!only_refreshed) {
		if ((error = odb_read_hardcoded(&found, &raw, id)) < 0)
			return error;
	}
Vicent Marti committed
1045

1046
	for (i = 0; i < db->backends.length && !found; ++i) {
1047 1048
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;
1049

1050 1051 1052
		if (only_refreshed && !b->refresh)
			continue;

1053
		if (b->read != NULL) {
1054
			error = b->read(&raw.data, &raw.len, &raw.type, b, id);
1055 1056 1057 1058 1059 1060 1061
			if (error == GIT_PASSTHROUGH || error == GIT_ENOTFOUND)
				continue;

			if (error < 0)
				return error;

			found = true;
1062
		}
Vicent Marti committed
1063 1064
	}

1065 1066
	if (!found)
		return GIT_ENOTFOUND;
1067

1068 1069 1070
	if (git_odb__strict_hash_verification) {
		if ((error = git_odb_hash(&hashed, raw.data, raw.len, raw.type)) < 0)
			goto out;
1071

1072 1073 1074 1075
		if (!git_oid_equal(id, &hashed)) {
			error = git_odb__error_mismatch(id, &hashed);
			goto out;
		}
1076 1077
	}

1078
	git_error_clear();
1079 1080
	if ((object = odb_object__alloc(id, &raw)) == NULL) {
		error = -1;
1081
		goto out;
1082
	}
1083 1084

	*out = git_cache_store_raw(odb_cache(db), object);
1085 1086 1087 1088 1089

out:
	if (error)
		git__free(raw.data);
	return error;
1090 1091
}

1092 1093 1094 1095 1096 1097
int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
{
	int error;

	assert(out && db && id);

1098 1099 1100
	if (git_oid_iszero(id))
		return error_null_oid(GIT_ENOTFOUND, "cannot read object");

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
	*out = git_cache_get_raw(odb_cache(db), id);
	if (*out != NULL)
		return 0;

	error = odb_read_1(out, db, id, false);

	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
		error = odb_read_1(out, db, id, true);

	if (error == GIT_ENOTFOUND)
1111
		return git_odb__error_notfound("no match for id", id, GIT_OID_HEXSZ);
1112 1113 1114 1115

	return error;
}

1116
static int odb_otype_fast(git_object_t *type_p, git_odb *db, const git_oid *id)
1117 1118 1119 1120 1121
{
	git_odb_object *object;
	size_t _unused;
	int error;

1122 1123 1124
	if (git_oid_iszero(id))
		return error_null_oid(GIT_ENOTFOUND, "cannot get object type");

1125 1126 1127 1128
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
		*type_p = object->cached.type;
		return 0;
	}
1129

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	error = odb_read_header_1(&_unused, type_p, db, id, false);

	if (error == GIT_PASSTHROUGH) {
		error = odb_read_1(&object, db, id, false);
		if (!error)
			*type_p = object->cached.type;
		git_odb_object_free(object);
	}

	return error;
}

1142 1143
static int read_prefix_1(git_odb_object **out, git_odb *db,
		const git_oid *key, size_t len, bool only_refreshed)
1144
{
1145
	size_t i;
1146
	int error = 0;
1147
	git_oid found_full_oid = {{0}};
1148
	git_rawobj raw = {0};
1149
	void *data = NULL;
1150
	bool found = false;
1151
	git_odb_object *object;
1152

1153
	for (i = 0; i < db->backends.length; ++i) {
1154 1155 1156
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

1157 1158 1159
		if (only_refreshed && !b->refresh)
			continue;

1160
		if (b->read_prefix != NULL) {
1161
			git_oid full_oid;
1162
			error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, key, len);
1163 1164 1165

			if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) {
				error = 0;
1166
				continue;
1167
			}
1168 1169

			if (error)
1170
				goto out;
1171

1172 1173
			git__free(data);
			data = raw.data;
Vicent Marti committed
1174

1175
			if (found && git_oid__cmp(&full_oid, &found_full_oid)) {
1176 1177 1178 1179 1180 1181 1182 1183
				git_buf buf = GIT_BUF_INIT;

				git_buf_printf(&buf, "multiple matches for prefix: %s",
					git_oid_tostr_s(&full_oid));
				git_buf_printf(&buf, " %s",
					git_oid_tostr_s(&found_full_oid));

				error = git_odb__error_ambiguous(buf.ptr);
1184
				git_buf_dispose(&buf);
1185
				goto out;
1186
			}
Vicent Marti committed
1187

1188 1189
			found_full_oid = full_oid;
			found = true;
1190 1191 1192
		}
	}

1193
	if (!found)
1194
		return GIT_ENOTFOUND;
1195

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	if (git_odb__strict_hash_verification) {
		git_oid hash;

		if ((error = git_odb_hash(&hash, raw.data, raw.len, raw.type)) < 0)
			goto out;

		if (!git_oid_equal(&found_full_oid, &hash)) {
			error = git_odb__error_mismatch(&found_full_oid, &hash);
			goto out;
		}
	}

1208 1209
	if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL) {
		error = -1;
1210
		goto out;
1211
	}
1212 1213

	*out = git_cache_store_raw(odb_cache(db), object);
1214 1215 1216 1217 1218 1219

out:
	if (error)
		git__free(raw.data);

	return error;
1220 1221
}

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
int git_odb_read_prefix(
	git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len)
{
	git_oid key = {{0}};
	int error;

	assert(out && db);

	if (len < GIT_OID_MINPREFIXLEN)
		return git_odb__error_ambiguous("prefix length too short");

	if (len > GIT_OID_HEXSZ)
		len = GIT_OID_HEXSZ;

	if (len == GIT_OID_HEXSZ) {
		*out = git_cache_get_raw(odb_cache(db), short_id);
		if (*out != NULL)
			return 0;
	}

1242
	git_oid__cpy_prefix(&key, short_id, len);
1243 1244 1245 1246 1247 1248 1249

	error = read_prefix_1(out, db, &key, len, false);

	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
		error = read_prefix_1(out, db, &key, len, true);

	if (error == GIT_ENOTFOUND)
1250
		return git_odb__error_notfound("no match for prefix", &key, len);
1251 1252 1253 1254

	return error;
}

Ben Straub committed
1255
int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
1256 1257 1258
{
	unsigned int i;
	backend_internal *internal;
1259

1260 1261
	git_vector_foreach(&db->backends, i, internal) {
		git_odb_backend *b = internal->backend;
Ben Straub committed
1262
		int error = b->foreach(b, cb, payload);
1263
		if (error != 0)
1264
			return error;
1265 1266 1267 1268 1269
	}

	return 0;
}

1270
int git_odb_write(
1271
	git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type)
1272
{
1273
	size_t i;
1274
	int error = GIT_ERROR;
Vicent Marti committed
1275
	git_odb_stream *stream;
1276 1277 1278

	assert(oid && db);

1279
	git_odb_hash(oid, data, len, type);
1280 1281 1282 1283

	if (git_oid_iszero(oid))
		return error_null_oid(GIT_EINVALID, "cannot write object");

1284
	if (git_odb__freshen(db, oid))
1285 1286
		return 0;

1287 1288 1289 1290 1291 1292 1293 1294 1295
	for (i = 0; i < db->backends.length && error < 0; ++i) {
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

		/* we don't write in alternates! */
		if (internal->is_alternate)
			continue;

		if (b->write != NULL)
1296
			error = b->write(b, oid, data, len, type);
1297 1298
	}

1299
	if (!error || error == GIT_PASSTHROUGH)
1300
		return 0;
Vicent Marti committed
1301

1302 1303 1304 1305
	/* if no backends were able to write the object directly, we try a
	 * streaming write to the backends; just write the whole object into the
	 * stream in one push
	 */
1306 1307
	if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
		return error;
1308

1309 1310
	stream->write(stream, data, len);
	error = stream->finalize_write(stream, oid);
1311
	git_odb_stream_free(stream);
1312 1313

	return error;
1314 1315
}

1316
static int hash_header(git_hash_ctx *ctx, git_off_t size, git_object_t type)
1317 1318
{
	char header[64];
1319 1320
	size_t hdrlen;
	int error;
1321

1322 1323 1324 1325 1326
	 if ((error = git_odb__format_object_header(&hdrlen,
		header, sizeof(header), size, type)) < 0)
		return error;

	return git_hash_update(ctx, header, hdrlen);
1327 1328
}

1329
int git_odb_open_wstream(
1330
	git_odb_stream **stream, git_odb *db, git_off_t size, git_object_t type)
1331
{
1332
	size_t i, writes = 0;
1333
	int error = GIT_ERROR;
1334
	git_hash_ctx *ctx = NULL;
1335

Vicent Marti committed
1336
	assert(stream && db);
1337

1338
	for (i = 0; i < db->backends.length && error < 0; ++i) {
1339 1340 1341 1342 1343 1344
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

		/* we don't write in alternates! */
		if (internal->is_alternate)
			continue;
1345

1346 1347
		if (b->writestream != NULL) {
			++writes;
Vicent Marti committed
1348
			error = b->writestream(stream, b, size, type);
1349 1350
		} else if (b->write != NULL) {
			++writes;
1351
			error = init_fake_wstream(stream, b, size, type);
1352
		}
Vicent Marti committed
1353 1354
	}

1355 1356 1357 1358 1359 1360 1361 1362
	if (error < 0) {
		if (error == GIT_PASSTHROUGH)
			error = 0;
		else if (!writes)
			error = git_odb__error_unsupported_in_backend("write object");

		goto done;
	}
Vicent Marti committed
1363

1364
	ctx = git__malloc(sizeof(git_hash_ctx));
1365
	GIT_ERROR_CHECK_ALLOC(ctx);
1366

1367 1368
	if ((error = git_hash_ctx_init(ctx)) < 0 ||
		(error = hash_header(ctx, size, type)) < 0)
1369
		goto done;
1370 1371

	(*stream)->hash_ctx = ctx;
1372 1373 1374
	(*stream)->declared_size = size;
	(*stream)->received_bytes = 0;

1375
done:
1376 1377
	if (error)
		git__free(ctx);
1378
	return error;
Vicent Marti committed
1379 1380
}

1381 1382 1383 1384
static int git_odb_stream__invalid_length(
	const git_odb_stream *stream,
	const char *action)
{
1385
	git_error_set(GIT_ERROR_ODB,
1386
		"cannot %s - "
1387 1388
		"Invalid length. %"PRId64" was expected. The "
		"total size of the received chunks amounts to %"PRId64".",
1389
		action, stream->declared_size, stream->received_bytes);
1390 1391 1392 1393

	return -1;
}

1394 1395
int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
{
1396
	git_hash_update(stream->hash_ctx, buffer, len);
1397 1398 1399 1400 1401 1402 1403

	stream->received_bytes += len;

	if (stream->received_bytes > stream->declared_size)
		return git_odb_stream__invalid_length(stream,
			"stream_write()");

1404 1405 1406 1407 1408
	return stream->write(stream, buffer, len);
}

int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
{
1409 1410 1411 1412
	if (stream->received_bytes != stream->declared_size)
		return git_odb_stream__invalid_length(stream,
			"stream_finalize_write()");

1413
	git_hash_final(out, stream->hash_ctx);
1414

1415
	if (git_odb__freshen(stream->backend->odb, out))
1416 1417
		return 0;

1418
	return stream->finalize_write(stream, out);
1419 1420 1421 1422 1423 1424 1425 1426 1427
}

int git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len)
{
	return stream->read(stream, buffer, len);
}

void git_odb_stream_free(git_odb_stream *stream)
{
1428 1429 1430
	if (stream == NULL)
		return;

1431
	git_hash_ctx_cleanup(stream->hash_ctx);
1432
	git__free(stream->hash_ctx);
1433 1434 1435
	stream->free(stream);
}

1436 1437 1438
int git_odb_open_rstream(
	git_odb_stream **stream,
	size_t *len,
1439
	git_object_t *type,
1440 1441
	git_odb *db,
	const git_oid *oid)
Vicent Marti committed
1442
{
1443
	size_t i, reads = 0;
Vicent Marti committed
1444 1445 1446 1447 1448 1449 1450 1451
	int error = GIT_ERROR;

	assert(stream && db);

	for (i = 0; i < db->backends.length && error < 0; ++i) {
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

1452 1453
		if (b->readstream != NULL) {
			++reads;
1454
			error = b->readstream(stream, len, type, b, oid);
1455
		}
1456 1457
	}

1458
	if (error == GIT_PASSTHROUGH)
1459
		error = 0;
1460 1461
	if (error < 0 && !reads)
		error = git_odb__error_unsupported_in_backend("read object streamed");
Vicent Marti committed
1462

1463 1464 1465
	return error;
}

1466
int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer_progress_cb progress_cb, void *progress_payload)
1467
{
1468
	size_t i, writes = 0;
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
	int error = GIT_ERROR;

	assert(out && db);

	for (i = 0; i < db->backends.length && error < 0; ++i) {
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

		/* we don't write in alternates! */
		if (internal->is_alternate)
			continue;

1481 1482
		if (b->writepack != NULL) {
			++writes;
1483
			error = b->writepack(out, b, db, progress_cb, progress_payload);
1484
		}
1485 1486 1487 1488
	}

	if (error == GIT_PASSTHROUGH)
		error = 0;
1489 1490
	if (error < 0 && !writes)
		error = git_odb__error_unsupported_in_backend("write pack");
1491 1492 1493 1494

	return error;
}

Vicent Marti committed
1495
void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
1496
{
1497
	GIT_UNUSED(backend);
1498 1499 1500
	return git__malloc(len);
}

Vicent Marti committed
1501 1502
int git_odb_refresh(struct git_odb *db)
{
1503
	size_t i;
Vicent Marti committed
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
	assert(db);

	for (i = 0; i < db->backends.length; ++i) {
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

		if (b->refresh != NULL) {
			int error = b->refresh(b);
			if (error < 0)
				return error;
		}
	}

	return 0;
}

1520 1521 1522 1523 1524 1525 1526
int git_odb__error_mismatch(const git_oid *expected, const git_oid *actual)
{
	char expected_oid[GIT_OID_HEXSZ + 1], actual_oid[GIT_OID_HEXSZ + 1];

	git_oid_tostr(expected_oid, sizeof(expected_oid), expected);
	git_oid_tostr(actual_oid, sizeof(actual_oid), actual);

1527
	git_error_set(GIT_ERROR_ODB, "object hash mismatch - expected %s but got %s",
1528 1529 1530 1531 1532
		expected_oid, actual_oid);

	return GIT_EMISMATCH;
}

1533 1534
int git_odb__error_notfound(
	const char *message, const git_oid *oid, size_t oid_len)
1535
{
Russell Belfer committed
1536 1537
	if (oid != NULL) {
		char oid_str[GIT_OID_HEXSZ + 1];
1538
		git_oid_tostr(oid_str, oid_len+1, oid);
1539
		git_error_set(GIT_ERROR_ODB, "object not found - %s (%.*s)",
1540
			message, (int) oid_len, oid_str);
Russell Belfer committed
1541
	} else
1542
		git_error_set(GIT_ERROR_ODB, "object not found - %s", message);
Russell Belfer committed
1543

1544
	return GIT_ENOTFOUND;
1545 1546
}

1547 1548
static int error_null_oid(int error, const char *message)
{
1549
	git_error_set(GIT_ERROR_ODB, "odb: %s: null OID cannot exist", message);
1550 1551 1552
	return error;
}

1553 1554
int git_odb__error_ambiguous(const char *message)
{
1555
	git_error_set(GIT_ERROR_ODB, "ambiguous SHA1 prefix - %s", message);
1556
	return GIT_EAMBIGUOUS;
1557 1558
}

1559
int git_odb_init_backend(git_odb_backend *backend, unsigned int version)
1560
{
1561 1562 1563
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		backend, version, git_odb_backend, GIT_ODB_BACKEND_INIT);
	return 0;
1564
}