odb.c 34.5 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 "futils.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
int git_odb__format_object_header(
	size_t *written,
	char *hdr,
	size_t hdr_size,
92
	git_object_size_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 324 325 326
	uint64_t size;
	int fd, error = 0;

	if ((fd = git_futils_open_ro(path)) < 0)
327
		return fd;
328

329 330 331 332
	if ((error = git_futils_filesize(&size, fd)) < 0)
		goto done;

	if (!git__is_sizet(size)) {
333
		git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
334 335
		error = -1;
		goto done;
336 337
	}

338 339 340
	error = git_odb__hashfd(out, fd, (size_t)size, type);

done:
341
	p_close(fd);
342
	return error;
343 344
}

345
int git_odb_hash(git_oid *id, const void *data, size_t len, git_object_t type)
Vicent Marti committed
346 347
{
	git_rawobj raw;
348

Vicent Marti committed
349
	assert(id);
350

Vicent Marti committed
351 352 353
	raw.data = (void *)data;
	raw.len = len;
	raw.type = type;
354

355
	return git_odb__hashobj(id, &raw);
356 357
}

358 359 360 361 362 363 364 365
/**
 * FAKE WSTREAM
 */

typedef struct {
	git_odb_stream stream;
	char *buffer;
	size_t size, written;
366
	git_object_t type;
367 368
} fake_wstream;

369
static int fake_wstream__fwrite(git_odb_stream *_stream, const git_oid *oid)
370 371
{
	fake_wstream *stream = (fake_wstream *)_stream;
372
	return _stream->backend->write(_stream->backend, oid, stream->buffer, stream->size, stream->type);
373 374 375 376 377 378
}

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

379
	assert(stream->written + len <= stream->size);
380 381 382

	memcpy(stream->buffer + stream->written, data, len);
	stream->written += len;
383
	return 0;
384 385 386 387 388 389
}

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

390 391
	git__free(stream->buffer);
	git__free(stream);
392 393
}

394
static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, git_object_size_t size, git_object_t type)
395 396
{
	fake_wstream *stream;
397
	size_t blobsize;
398

399 400
	GIT_ERROR_CHECK_BLOBSIZE(size);
	blobsize = (size_t)size;
401

402
	stream = git__calloc(1, sizeof(fake_wstream));
403
	GIT_ERROR_CHECK_ALLOC(stream);
404

405
	stream->size = blobsize;
406
	stream->type = type;
407
	stream->buffer = git__malloc(blobsize);
408
	if (stream->buffer == NULL) {
409
		git__free(stream);
410
		return -1;
411 412 413 414 415 416 417 418 419 420
	}

	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;
421
	return 0;
422
}
423

424 425 426 427 428 429 430
/***********************************************************
 *
 * OBJECT DATABASE PUBLIC API
 *
 * Public calls for the ODB functionality
 *
 ***********************************************************/
431

432
static int backend_sort_cmp(const void *a, const void *b)
433
{
434 435
	const backend_internal *backend_a = (const backend_internal *)(a);
	const backend_internal *backend_b = (const backend_internal *)(b);
436

437 438 439 440 441 442 443 444
	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);
445 446
}

447
int git_odb_new(git_odb **out)
448
{
449
	git_odb *db = git__calloc(1, sizeof(*db));
450
	GIT_ERROR_CHECK_ALLOC(db);
451

452 453 454 455 456
	if (git_cache_init(&db->own_cache) < 0) {
		git__free(db);
		return -1;
	}
	if (git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) {
457
		git_cache_dispose(&db->own_cache);
458
		git__free(db);
459
		return -1;
460
	}
461

462
	*out = db;
463
	GIT_REFCOUNT_INC(db);
464
	return 0;
465 466
}

467 468 469
static int add_backend_internal(
	git_odb *odb, git_odb_backend *backend,
	int priority, bool is_alternate, ino_t disk_inode)
Ramsay Jones committed
470
{
471 472
	backend_internal *internal;

473
	assert(odb && backend);
Ramsay Jones committed
474

475
	GIT_ERROR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
476

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

480
	internal = git__malloc(sizeof(backend_internal));
481
	GIT_ERROR_CHECK_ALLOC(internal);
482 483 484 485

	internal->backend = backend;
	internal->priority = priority;
	internal->is_alternate = is_alternate;
486
	internal->disk_inode = disk_inode;
Ramsay Jones committed
487

488
	if (git_vector_insert(&odb->backends, internal) < 0) {
489
		git__free(internal);
490
		return -1;
491
	}
Ramsay Jones committed
492

493
	git_vector_sort(&odb->backends);
494
	internal->backend->odb = odb;
495
	return 0;
Ramsay Jones committed
496 497
}

498 499
int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority)
{
500
	return add_backend_internal(odb, backend, priority, false, 0);
501 502 503 504
}

int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
{
505
	return add_backend_internal(odb, backend, priority, true, 0);
506 507
}

508 509 510 511 512 513
size_t git_odb_num_backends(git_odb *odb)
{
	assert(odb);
	return odb->backends.length;
}

514 515
static int git_odb__error_unsupported_in_backend(const char *action)
{
516
	git_error_set(GIT_ERROR_ODB,
517
		"cannot %s - unsupported in the loaded odb backends", action);
518 519 520 521
	return -1;
}


522 523 524 525
int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos)
{
	backend_internal *internal;

Linquize committed
526
	assert(out && odb);
527 528 529 530 531 532 533
	internal = git_vector_get(&odb->backends, pos);

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

534
	git_error_set(GIT_ERROR_ODB, "no ODB backend loaded at index %" PRIuZ, pos);
535 536 537
	return GIT_ENOTFOUND;
}

538
int git_odb__add_default_backends(
539 540
	git_odb *db, const char *objects_dir,
	bool as_alternates, int alternate_depth)
541
{
542 543
	size_t i;
	struct stat st;
544
	ino_t inode;
545 546
	git_odb_backend *loose, *packed;

547 548
	/* TODO: inodes are not really relevant on Win32, so we need to find
	 * a cross-platform workaround for this */
549 550 551 552 553 554
#ifdef GIT_WIN32
	GIT_UNUSED(i);
	GIT_UNUSED(st);

	inode = 0;
#else
555
	if (p_stat(objects_dir, &st) < 0) {
556
		if (as_alternates)
Etienne Samson committed
557
			/* this should warn */
558 559
			return 0;

560
		git_error_set(GIT_ERROR_ODB, "failed to load object database in '%s'", objects_dir);
561 562 563
		return -1;
	}

564 565
	inode = st.st_ino;

566 567
	for (i = 0; i < db->backends.length; ++i) {
		backend_internal *backend = git_vector_get(&db->backends, i);
568
		if (backend->disk_inode == inode)
569 570
			return 0;
	}
571
#endif
572

573
	/* add the loose object backend */
574
	if (git_odb_backend_loose(&loose, objects_dir, -1, db->do_fsync, 0, 0) < 0 ||
575
		add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
576
		return -1;
577 578

	/* add the packed file backend */
579
	if (git_odb_backend_pack(&packed, objects_dir) < 0 ||
580
		add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates, inode) < 0)
581
		return -1;
582

583
	return load_alternates(db, objects_dir, alternate_depth);
584 585
}

586
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth)
587
{
588
	git_buf alternates_path = GIT_BUF_INIT;
589
	git_buf alternates_buf = GIT_BUF_INIT;
590 591
	char *buffer;
	const char *alternate;
592
	int result = 0;
593

594
	/* Git reports an error, we just ignore anything deeper */
595
	if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH)
596 597
		return 0;

598 599
	if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0)
		return -1;
600

601
	if (git_path_exists(alternates_path.ptr) == false) {
602
		git_buf_dispose(&alternates_path);
603
		return 0;
604
	}
605

606
	if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) {
607
		git_buf_dispose(&alternates_path);
608
		return -1;
609
	}
610

611
	buffer = (char *)alternates_buf.ptr;
612 613

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

618 619 620 621 622 623
		/*
		 * Relative path: build based on the current `objects`
		 * folder. However, relative paths are only allowed in
		 * the current repository.
		 */
		if (*alternate == '.' && !alternate_depth) {
624
			if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0)
625 626
				break;
			alternate = git_buf_cstr(&alternates_path);
627 628
		}

629
		if ((result = git_odb__add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0)
630 631
			break;
	}
632

633 634
	git_buf_dispose(&alternates_path);
	git_buf_dispose(&alternates_buf);
635

636
	return result;
637
}
Ramsay Jones committed
638

639 640
int git_odb_add_disk_alternate(git_odb *odb, const char *path)
{
641
	return git_odb__add_default_backends(odb, path, true, 0);
642 643
}

644
int git_odb_open(git_odb **out, const char *objects_dir)
Ramsay Jones committed
645
{
646
	git_odb *db;
Ramsay Jones committed
647

648 649 650 651
	assert(out && objects_dir);

	*out = NULL;

652 653
	if (git_odb_new(&db) < 0)
		return -1;
Ramsay Jones committed
654

655
	if (git_odb__add_default_backends(db, objects_dir, 0, 0) < 0) {
656 657 658
		git_odb_free(db);
		return -1;
	}
Ramsay Jones committed
659

660
	*out = db;
661
	return 0;
662
}
Ramsay Jones committed
663

664 665 666 667 668 669 670
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) {
671
			git_error_set(GIT_ERROR_ODB, "cannot access repository to set odb caps");
672 673 674
			return -1;
		}

675
		if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FSYNCOBJECTFILES))
676 677 678 679 680 681
			odb->do_fsync = !!val;
	}

	return 0;
}

682
static void odb_free(git_odb *db)
683
{
684
	size_t i;
685

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

690
		backend->free(backend);
691

692
		git__free(internal);
693 694
	}

695
	git_vector_free(&db->backends);
696
	git_cache_dispose(&db->own_cache);
697

698
	git__memzero(db, sizeof(*db));
699
	git__free(db);
700 701
}

702 703 704 705 706 707 708 709
void git_odb_free(git_odb *db)
{
	if (db == NULL)
		return;

	GIT_REFCOUNT_DEC(db, odb_free);
}

710 711 712 713
static int odb_exists_1(
	git_odb *db,
	const git_oid *id,
	bool only_refreshed)
714
{
715
	size_t i;
716
	bool found = false;
717

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

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

725
		if (b->exists != NULL)
Linquize committed
726
			found = (bool)b->exists(b, id);
727 728
	}

729
	return (int)found;
730 731
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
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;
}

756
int git_odb__freshen(git_odb *db, const git_oid *id)
757 758 759 760 761 762 763 764 765 766 767 768 769
{
	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;
}

770
int git_odb_exists(git_odb *db, const git_oid *id)
771
{
772
	git_odb_object *object;
773

774
	assert(db && id);
775

776
	if (git_oid_is_zero(id))
777 778
		return 0;

779 780
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
		git_odb_object_free(object);
781
		return 1;
782 783
	}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	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;
800

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

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

808 809 810
		if (!b->exists_prefix)
			continue;

811
		error = b->exists_prefix(&found, b, key, len);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
		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)
828 829
		return GIT_ENOTFOUND;

830 831 832
	if (out)
		git_oid_cpy(out, &last_found);

833
	return 0;
834 835
}

836 837 838 839
int git_odb_exists_prefix(
	git_oid *out, git_odb *db, const git_oid *short_id, size_t len)
{
	int error;
840
	git_oid key = {{0}};
841 842 843 844 845 846

	assert(db && short_id);

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

847
	if (len >= GIT_OID_HEXSZ) {
848 849 850 851 852
		if (git_odb_exists(db, short_id)) {
			if (out)
				git_oid_cpy(out, short_id);
			return 0;
		} else {
853 854
			return git_odb__error_notfound(
				"no match for id prefix", short_id, len);
855 856 857
		}
	}

858
	git_oid__cpy_prefix(&key, short_id, len);
859 860 861 862 863 864 865

	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)
866
		return git_odb__error_notfound("no match for id prefix", &key, len);
867 868 869 870

	return error;
}

871
int git_odb_expand_ids(
872
	git_odb *db,
873 874
	git_odb_expand_id *ids,
	size_t count)
875
{
876
	size_t i;
877

878
	assert(db && ids);
879

880 881
	for (i = 0; i < count; i++) {
		git_odb_expand_id *query = &ids[i];
882
		int error = GIT_EAMBIGUOUS;
883

884
		if (!query->type)
885
			query->type = GIT_OBJECT_ANY;
886 887 888 889 890 891 892 893 894 895

		/* 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;
			}
896 897
		}

898
		/*
899 900
		 * 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.
901
		 */
902
		if (query->length >= GIT_OID_HEXSZ) {
903
			git_object_t actual_type;
904

905 906
			error = odb_otype_fast(&actual_type, db, &query->id);
			if (!error) {
907
				if (query->type != GIT_OBJECT_ANY && query->type != actual_type)
908 909 910 911 912
					error = GIT_ENOTFOUND;
				else
					query->type = actual_type;
			}
		}
913

914
		switch (error) {
915
		/* no errors, so we've successfully expanded the OID */
916
		case 0:
917
			continue;
918 919 920 921

		/* the object is missing or ambiguous */
		case GIT_ENOTFOUND:
		case GIT_EAMBIGUOUS:
922 923 924
			memset(&query->id, 0, sizeof(git_oid));
			query->length = 0;
			query->type = 0;
925 926 927 928 929
			break;

		/* something went very wrong with the ODB; bail hard */
		default:
			return error;
930 931 932
		}
	}

933
	git_error_clear();
934
	return 0;
935 936
}

937
int git_odb_read_header(size_t *len_p, git_object_t *type_p, git_odb *db, const git_oid *id)
938
{
939 940 941 942 943 944 945 946 947 948 949
	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;
}

950
static int odb_read_header_1(
951
	size_t *len_p, git_object_t *type_p, git_odb *db,
952 953 954
	const git_oid *id, bool only_refreshed)
{
	size_t i;
955
	git_object_t ht;
956 957
	bool passthrough = false;
	int error;
958

959
	if (!only_refreshed && (ht = odb_hardcoded_type(id)) != GIT_OBJECT_INVALID) {
960 961 962 963 964
		*type_p = ht;
		*len_p = 0;
		return 0;
	}

965
	for (i = 0; i < db->backends.length; ++i) {
966 967 968 969 970 971
		backend_internal *internal = git_vector_get(&db->backends, i);
		git_odb_backend *b = internal->backend;

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

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
		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;
		}
988 989
	}

990
	return passthrough ? GIT_PASSTHROUGH : GIT_ENOTFOUND;
991 992
}

993
int git_odb__read_header_or_object(
994
	git_odb_object **out, size_t *len_p, git_object_t *type_p,
995 996
	git_odb *db, const git_oid *id)
{
997
	int error = GIT_ENOTFOUND;
Vicent Marti committed
998
	git_odb_object *object;
999

1000
	assert(db && id && out && len_p && type_p);
Vicent Marti committed
1001

1002 1003
	*out = NULL;

1004
	if (git_oid_is_zero(id))
1005 1006
		return error_null_oid(GIT_ENOTFOUND, "cannot read object");

1007
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
Vicent Marti committed
1008 1009
		*len_p = object->cached.size;
		*type_p = object->cached.type;
1010
		*out = object;
1011
		return 0;
Vicent Marti committed
1012
	}
1013

1014
	error = odb_read_header_1(len_p, type_p, db, id, false);
1015

1016 1017
	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
		error = odb_read_header_1(len_p, type_p, db, id, true);
1018

1019 1020
	if (error == GIT_ENOTFOUND)
		return git_odb__error_notfound("cannot read header for", id, GIT_OID_HEXSZ);
1021

1022 1023
	/* we found the header; return early */
	if (!error)
1024
		return 0;
Vicent Marti committed
1025

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	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;
		}
1037
	}
1038 1039

	return error;
1040 1041
}

1042 1043
static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
		bool only_refreshed)
1044
{
1045
	size_t i;
Vicent Marti committed
1046
	git_rawobj raw;
1047
	git_odb_object *object;
1048
	git_oid hashed;
1049
	bool found = false;
1050
	int error = 0;
1051

1052 1053 1054 1055
	if (!only_refreshed) {
		if ((error = odb_read_hardcoded(&found, &raw, id)) < 0)
			return error;
	}
Vicent Marti committed
1056

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

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

1064
		if (b->read != NULL) {
1065
			error = b->read(&raw.data, &raw.len, &raw.type, b, id);
1066 1067 1068 1069 1070 1071 1072
			if (error == GIT_PASSTHROUGH || error == GIT_ENOTFOUND)
				continue;

			if (error < 0)
				return error;

			found = true;
1073
		}
Vicent Marti committed
1074 1075
	}

1076 1077
	if (!found)
		return GIT_ENOTFOUND;
1078

1079 1080 1081
	if (git_odb__strict_hash_verification) {
		if ((error = git_odb_hash(&hashed, raw.data, raw.len, raw.type)) < 0)
			goto out;
1082

1083 1084 1085 1086
		if (!git_oid_equal(id, &hashed)) {
			error = git_odb__error_mismatch(id, &hashed);
			goto out;
		}
1087 1088
	}

1089
	git_error_clear();
1090 1091
	if ((object = odb_object__alloc(id, &raw)) == NULL) {
		error = -1;
1092
		goto out;
1093
	}
1094 1095

	*out = git_cache_store_raw(odb_cache(db), object);
1096 1097 1098 1099 1100

out:
	if (error)
		git__free(raw.data);
	return error;
1101 1102
}

1103 1104 1105 1106 1107 1108
int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
{
	int error;

	assert(out && db && id);

1109
	if (git_oid_is_zero(id))
1110 1111
		return error_null_oid(GIT_ENOTFOUND, "cannot read object");

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	*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)
1122
		return git_odb__error_notfound("no match for id", id, GIT_OID_HEXSZ);
1123 1124 1125 1126

	return error;
}

1127
static int odb_otype_fast(git_object_t *type_p, git_odb *db, const git_oid *id)
1128 1129 1130 1131 1132
{
	git_odb_object *object;
	size_t _unused;
	int error;

1133
	if (git_oid_is_zero(id))
1134 1135
		return error_null_oid(GIT_ENOTFOUND, "cannot get object type");

1136 1137
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
		*type_p = object->cached.type;
1138
		git_odb_object_free(object);
1139 1140
		return 0;
	}
1141

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	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;
}

1154 1155
static int read_prefix_1(git_odb_object **out, git_odb *db,
		const git_oid *key, size_t len, bool only_refreshed)
1156
{
1157
	size_t i;
1158
	int error = 0;
1159
	git_oid found_full_oid = {{0}};
1160
	git_rawobj raw = {0};
1161
	void *data = NULL;
1162
	bool found = false;
1163
	git_odb_object *object;
1164

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

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

1172
		if (b->read_prefix != NULL) {
1173
			git_oid full_oid;
1174
			error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, key, len);
1175 1176 1177

			if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) {
				error = 0;
1178
				continue;
1179
			}
1180 1181

			if (error)
1182
				goto out;
1183

1184 1185
			git__free(data);
			data = raw.data;
Vicent Marti committed
1186

1187
			if (found && git_oid__cmp(&full_oid, &found_full_oid)) {
1188 1189 1190 1191 1192 1193 1194 1195
				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);
1196
				git_buf_dispose(&buf);
1197
				goto out;
1198
			}
Vicent Marti committed
1199

1200 1201
			found_full_oid = full_oid;
			found = true;
1202 1203 1204
		}
	}

1205
	if (!found)
1206
		return GIT_ENOTFOUND;
1207

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	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;
		}
	}

1220 1221
	if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL) {
		error = -1;
1222
		goto out;
1223
	}
1224 1225

	*out = git_cache_store_raw(odb_cache(db), object);
1226 1227 1228 1229 1230 1231

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

	return error;
1232 1233
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
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;
	}

1254
	git_oid__cpy_prefix(&key, short_id, len);
1255 1256 1257 1258 1259 1260 1261

	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)
1262
		return git_odb__error_notfound("no match for prefix", &key, len);
1263 1264 1265 1266

	return error;
}

Ben Straub committed
1267
int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
1268 1269 1270
{
	unsigned int i;
	backend_internal *internal;
1271

1272 1273
	git_vector_foreach(&db->backends, i, internal) {
		git_odb_backend *b = internal->backend;
Ben Straub committed
1274
		int error = b->foreach(b, cb, payload);
1275
		if (error != 0)
1276
			return error;
1277 1278 1279 1280 1281
	}

	return 0;
}

1282
int git_odb_write(
1283
	git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type)
1284
{
1285
	size_t i;
1286
	int error = GIT_ERROR;
Vicent Marti committed
1287
	git_odb_stream *stream;
1288 1289 1290

	assert(oid && db);

1291
	git_odb_hash(oid, data, len, type);
1292

1293
	if (git_oid_is_zero(oid))
1294 1295
		return error_null_oid(GIT_EINVALID, "cannot write object");

1296
	if (git_odb__freshen(db, oid))
1297 1298
		return 0;

1299 1300 1301 1302 1303 1304 1305 1306 1307
	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)
1308
			error = b->write(b, oid, data, len, type);
1309 1310
	}

1311
	if (!error || error == GIT_PASSTHROUGH)
1312
		return 0;
Vicent Marti committed
1313

1314 1315 1316 1317
	/* 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
	 */
1318 1319
	if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
		return error;
1320

1321 1322
	stream->write(stream, data, len);
	error = stream->finalize_write(stream, oid);
1323
	git_odb_stream_free(stream);
1324 1325

	return error;
1326 1327
}

1328
static int hash_header(git_hash_ctx *ctx, git_object_size_t size, git_object_t type)
1329 1330
{
	char header[64];
1331 1332
	size_t hdrlen;
	int error;
1333

1334 1335 1336 1337 1338
	 if ((error = git_odb__format_object_header(&hdrlen,
		header, sizeof(header), size, type)) < 0)
		return error;

	return git_hash_update(ctx, header, hdrlen);
1339 1340
}

1341
int git_odb_open_wstream(
1342
	git_odb_stream **stream, git_odb *db, git_object_size_t size, git_object_t type)
1343
{
1344
	size_t i, writes = 0;
1345
	int error = GIT_ERROR;
1346
	git_hash_ctx *ctx = NULL;
1347

Vicent Marti committed
1348
	assert(stream && db);
1349

1350
	for (i = 0; i < db->backends.length && error < 0; ++i) {
1351 1352 1353 1354 1355 1356
		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;
1357

1358 1359
		if (b->writestream != NULL) {
			++writes;
Vicent Marti committed
1360
			error = b->writestream(stream, b, size, type);
1361 1362
		} else if (b->write != NULL) {
			++writes;
1363
			error = init_fake_wstream(stream, b, size, type);
1364
		}
Vicent Marti committed
1365 1366
	}

1367 1368 1369 1370 1371 1372 1373 1374
	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
1375

1376
	ctx = git__malloc(sizeof(git_hash_ctx));
1377
	GIT_ERROR_CHECK_ALLOC(ctx);
1378

1379 1380
	if ((error = git_hash_ctx_init(ctx)) < 0 ||
		(error = hash_header(ctx, size, type)) < 0)
1381
		goto done;
1382 1383

	(*stream)->hash_ctx = ctx;
1384 1385 1386
	(*stream)->declared_size = size;
	(*stream)->received_bytes = 0;

1387
done:
1388 1389
	if (error)
		git__free(ctx);
1390
	return error;
Vicent Marti committed
1391 1392
}

1393 1394 1395 1396
static int git_odb_stream__invalid_length(
	const git_odb_stream *stream,
	const char *action)
{
1397
	git_error_set(GIT_ERROR_ODB,
1398
		"cannot %s - "
1399 1400
		"Invalid length. %"PRId64" was expected. The "
		"total size of the received chunks amounts to %"PRId64".",
1401
		action, stream->declared_size, stream->received_bytes);
1402 1403 1404 1405

	return -1;
}

1406 1407
int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
{
1408
	git_hash_update(stream->hash_ctx, buffer, len);
1409 1410 1411 1412 1413 1414 1415

	stream->received_bytes += len;

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

1416 1417 1418 1419 1420
	return stream->write(stream, buffer, len);
}

int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
{
1421 1422 1423 1424
	if (stream->received_bytes != stream->declared_size)
		return git_odb_stream__invalid_length(stream,
			"stream_finalize_write()");

1425
	git_hash_final(out, stream->hash_ctx);
1426

1427
	if (git_odb__freshen(stream->backend->odb, out))
1428 1429
		return 0;

1430
	return stream->finalize_write(stream, out);
1431 1432 1433 1434 1435 1436 1437 1438 1439
}

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)
{
1440 1441 1442
	if (stream == NULL)
		return;

1443
	git_hash_ctx_cleanup(stream->hash_ctx);
1444
	git__free(stream->hash_ctx);
1445 1446 1447
	stream->free(stream);
}

1448 1449 1450
int git_odb_open_rstream(
	git_odb_stream **stream,
	size_t *len,
1451
	git_object_t *type,
1452 1453
	git_odb *db,
	const git_oid *oid)
Vicent Marti committed
1454
{
1455
	size_t i, reads = 0;
Vicent Marti committed
1456 1457 1458 1459 1460 1461 1462 1463
	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;

1464 1465
		if (b->readstream != NULL) {
			++reads;
1466
			error = b->readstream(stream, len, type, b, oid);
1467
		}
1468 1469
	}

1470
	if (error == GIT_PASSTHROUGH)
1471
		error = 0;
1472 1473
	if (error < 0 && !reads)
		error = git_odb__error_unsupported_in_backend("read object streamed");
Vicent Marti committed
1474

1475 1476 1477
	return error;
}

1478
int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_indexer_progress_cb progress_cb, void *progress_payload)
1479
{
1480
	size_t i, writes = 0;
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
	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;

1493 1494
		if (b->writepack != NULL) {
			++writes;
1495
			error = b->writepack(out, b, db, progress_cb, progress_payload);
1496
		}
1497 1498 1499 1500
	}

	if (error == GIT_PASSTHROUGH)
		error = 0;
1501 1502
	if (error < 0 && !writes)
		error = git_odb__error_unsupported_in_backend("write pack");
1503 1504 1505 1506

	return error;
}

1507
void *git_odb_backend_data_alloc(git_odb_backend *backend, size_t len)
1508
{
1509
	GIT_UNUSED(backend);
1510 1511 1512
	return git__malloc(len);
}

1513
#ifndef GIT_DEPRECATE_HARD
1514 1515 1516 1517
void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
{
	return git_odb_backend_data_alloc(backend, len);
}
1518
#endif
1519

1520 1521 1522 1523 1524 1525
void git_odb_backend_data_free(git_odb_backend *backend, void *data)
{
	GIT_UNUSED(backend);
	git__free(data);
}

Vicent Marti committed
1526 1527
int git_odb_refresh(struct git_odb *db)
{
1528
	size_t i;
Vicent Marti committed
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
	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;
}

1545 1546 1547 1548 1549 1550 1551
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);

1552
	git_error_set(GIT_ERROR_ODB, "object hash mismatch - expected %s but got %s",
1553 1554 1555 1556 1557
		expected_oid, actual_oid);

	return GIT_EMISMATCH;
}

1558 1559
int git_odb__error_notfound(
	const char *message, const git_oid *oid, size_t oid_len)
1560
{
Russell Belfer committed
1561 1562
	if (oid != NULL) {
		char oid_str[GIT_OID_HEXSZ + 1];
1563
		git_oid_tostr(oid_str, oid_len+1, oid);
1564
		git_error_set(GIT_ERROR_ODB, "object not found - %s (%.*s)",
1565
			message, (int) oid_len, oid_str);
Russell Belfer committed
1566
	} else
1567
		git_error_set(GIT_ERROR_ODB, "object not found - %s", message);
Russell Belfer committed
1568

1569
	return GIT_ENOTFOUND;
1570 1571
}

1572 1573
static int error_null_oid(int error, const char *message)
{
1574
	git_error_set(GIT_ERROR_ODB, "odb: %s: null OID cannot exist", message);
1575 1576 1577
	return error;
}

1578 1579
int git_odb__error_ambiguous(const char *message)
{
1580
	git_error_set(GIT_ERROR_ODB, "ambiguous SHA1 prefix - %s", message);
1581
	return GIT_EAMBIGUOUS;
1582 1583
}

1584
int git_odb_init_backend(git_odb_backend *backend, unsigned int version)
1585
{
1586 1587 1588
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		backend, version, git_odb_backend, GIT_ODB_BACKEND_INIT);
	return 0;
1589
}