odb.c 25.1 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
#include <zlib.h>
10
#include "git2/object.h"
11
#include "git2/sys/odb_backend.h"
12
#include "fileops.h"
13
#include "hash.h"
14
#include "odb.h"
Vicent Marti committed
15
#include "delta-apply.h"
16
#include "filter.h"
17
#include "repository.h"
18

19
#include "git2/odb_backend.h"
20
#include "git2/oid.h"
21

22 23
#define GIT_ALTERNATES_FILE "info/alternates"

24 25 26 27
/* TODO: is this correct? */
#define GIT_LOOSE_PRIORITY 2
#define GIT_PACKED_PRIORITY 1

28 29
#define GIT_ALTERNATES_MAX_DEPTH 5

30 31 32 33
typedef struct
{
	git_odb_backend *backend;
	int priority;
34 35
	bool is_alternate;
	ino_t disk_inode;
36 37
} backend_internal;

38 39 40 41 42 43 44 45 46
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;
}
47

48 49
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth);

50
int git_odb__format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type)
51
{
Vicent Marti committed
52
	const char *type_str = git_object_type2string(obj_type);
53
	int len = p_snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len);
54
	assert(len > 0 && len <= (int)n);
55 56 57
	return len+1;
}

58
int git_odb__hashobj(git_oid *id, git_rawobj *obj)
59 60
{
	git_buf_vec vec[2];
61 62
	char header[64];
	int hdrlen;
63

64
	assert(id && obj);
65

66
	if (!git_object_typeisloose(obj->type))
67
		return -1;
Vicent Marti committed
68

69
	if (!obj->data && obj->len != 0)
70
		return -1;
71

72
	hdrlen = git_odb__format_object_header(header, sizeof(header), obj->len, obj->type);
73

74
	vec[0].data = header;
Vicent Marti committed
75
	vec[0].len = hdrlen;
76
	vec[1].data = obj->data;
Vicent Marti committed
77
	vec[1].len = obj->len;
78 79 80

	git_hash_vec(id, vec, 2);

81
	return 0;
82 83
}

84

85
static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source)
Ramsay Jones committed
86
{
87
	git_odb_object *object = git__calloc(1, sizeof(git_odb_object));
Ramsay Jones committed
88

89 90 91 92 93 94
	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
95

Vicent Marti committed
96
	return object;
97 98
}

99
void git_odb_object__free(void *object)
100
{
Vicent Marti committed
101
	if (object != NULL) {
102
		git__free(((git_odb_object *)object)->buffer);
103
		git__free(object);
Vicent Marti committed
104 105
	}
}
106

107 108 109 110 111 112 113
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
114
	return object->buffer;
115 116 117 118
}

size_t git_odb_object_size(git_odb_object *object)
{
Vicent Marti committed
119
	return object->cached.size;
120 121 122 123
}

git_otype git_odb_object_type(git_odb_object *object)
{
Vicent Marti committed
124
	return object->cached.type;
125 126
}

127 128 129 130 131 132 133
int git_odb_object_dup(git_odb_object **dest, git_odb_object *source)
{
	git_cached_obj_incref(source);
	*dest = source;
	return 0;
}

134
void git_odb_object_free(git_odb_object *object)
Vicent Marti committed
135
{
136 137 138
	if (object == NULL)
		return;

139
	git_cached_obj_decref(object);
Vicent Marti committed
140
}
141

142
int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type)
Vicent Marti committed
143
{
144
	int hdr_len;
Vicent Marti committed
145
	char hdr[64], buffer[2048];
146
	git_hash_ctx ctx;
147
	ssize_t read_len = 0;
148
	int error = 0;
Vicent Marti committed
149

150 151 152 153 154
	if (!git_object_typeisloose(type)) {
		giterr_set(GITERR_INVALID, "Invalid object type for hash");
		return -1;
	}

155 156
	if ((error = git_hash_ctx_init(&ctx)) < 0)
		return -1;
Vicent Marti committed
157

158
	hdr_len = git_odb__format_object_header(hdr, sizeof(hdr), size, type);
159

160
	if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0)
161
		goto done;
Vicent Marti committed
162

Vicent Marti committed
163
	while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) {
164
		if ((error = git_hash_update(&ctx, buffer, read_len)) < 0)
165 166
			goto done;

Vicent Marti committed
167 168 169
		size -= read_len;
	}

Vicent Marti committed
170 171 172 173 174
	/* 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) {
		giterr_set(GITERR_OS, "Error reading file for hashing");
175 176 177
		error = -1;

		goto done;
Vicent Marti committed
178 179
	}

180
	error = git_hash_final(out, &ctx);
Vicent Marti committed
181

182
done:
183
	git_hash_ctx_cleanup(&ctx);
184
	return error;
Vicent Marti committed
185 186
}

187
int git_odb__hashfd_filtered(
188
	git_oid *out, git_file fd, size_t size, git_otype type, git_filter_list *fl)
189 190 191 192
{
	int error;
	git_buf raw = GIT_BUF_INIT;

193
	if (!fl)
194 195 196 197 198 199
		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
	 */

200
	if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
201
		git_buf post = GIT_BUF_INIT;
202

203
		error = git_filter_list_apply_to_data(&post, fl, &raw);
204

205
		git_buf_free(&raw);
206

207 208 209
		if (!error)
			error = git_odb_hash(out, post.ptr, post.size, type);

210
		git_buf_free(&post);
211
	}
212 213 214 215

	return error;
}

216 217 218 219
int git_odb__hashlink(git_oid *out, const char *path)
{
	struct stat st;
	git_off_t size;
220
	int result;
221

222
	if (git_path_lstat(path, &st) < 0)
223
		return -1;
224 225 226

	size = st.st_size;

227 228 229 230
	if (!git__is_sizet(size)) {
		giterr_set(GITERR_OS, "File size overflow for 32-bit systems");
		return -1;
	}
231 232 233 234 235

	if (S_ISLNK(st.st_mode)) {
		char *link_data;
		ssize_t read_len;

236
		link_data = git__malloc((size_t)(size + 1));
237
		GITERR_CHECK_ALLOC(link_data);
238

239 240
		read_len = p_readlink(path, link_data, (size_t)size);
		link_data[size] = '\0';
241 242
		if (read_len != (ssize_t)size) {
			giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", path);
243
			git__free(link_data);
244 245
			return -1;
		}
246

247
		result = git_odb_hash(out, link_data, (size_t)size, GIT_OBJ_BLOB);
248
		git__free(link_data);
249
	} else {
250 251 252 253
		int fd = git_futils_open_ro(path);
		if (fd < 0)
			return -1;
		result = git_odb__hashfd(out, fd, (size_t)size, GIT_OBJ_BLOB);
254 255 256
		p_close(fd);
	}

257
	return result;
258 259
}

260 261 262
int git_odb_hashfile(git_oid *out, const char *path, git_otype type)
{
	git_off_t size;
263 264
	int result, fd = git_futils_open_ro(path);
	if (fd < 0)
265
		return fd;
266 267

	if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
268
		giterr_set(GITERR_OS, "File size overflow for 32-bit systems");
269
		p_close(fd);
270
		return -1;
271 272
	}

273
	result = git_odb__hashfd(out, fd, (size_t)size, type);
274
	p_close(fd);
275
	return result;
276 277
}

Vicent Marti committed
278 279 280
int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
{
	git_rawobj raw;
281

Vicent Marti committed
282
	assert(id);
283

Vicent Marti committed
284 285 286
	raw.data = (void *)data;
	raw.len = len;
	raw.type = type;
287

288
	return git_odb__hashobj(id, &raw);
289 290
}

291 292 293 294 295 296 297 298 299 300 301
/**
 * FAKE WSTREAM
 */

typedef struct {
	git_odb_stream stream;
	char *buffer;
	size_t size, written;
	git_otype type;
} fake_wstream;

302
static int fake_wstream__fwrite(git_odb_stream *_stream, const git_oid *oid)
303 304
{
	fake_wstream *stream = (fake_wstream *)_stream;
305
	return _stream->backend->write(_stream->backend, oid, stream->buffer, stream->size, stream->type);
306 307 308 309 310 311
}

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

312
	if (stream->written + len > stream->size)
313
		return -1;
314 315 316

	memcpy(stream->buffer + stream->written, data, len);
	stream->written += len;
317
	return 0;
318 319 320 321 322 323
}

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

324 325
	git__free(stream->buffer);
	git__free(stream);
326 327 328 329 330 331 332
}

static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, size_t size, git_otype type)
{
	fake_wstream *stream;

	stream = git__calloc(1, sizeof(fake_wstream));
333
	GITERR_CHECK_ALLOC(stream);
334 335 336 337 338

	stream->size = size;
	stream->type = type;
	stream->buffer = git__malloc(size);
	if (stream->buffer == NULL) {
339
		git__free(stream);
340
		return -1;
341 342 343 344 345 346 347 348 349 350
	}

	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;
351
	return 0;
352
}
353

354 355 356 357 358 359 360
/***********************************************************
 *
 * OBJECT DATABASE PUBLIC API
 *
 * Public calls for the ODB functionality
 *
 ***********************************************************/
361

362
static int backend_sort_cmp(const void *a, const void *b)
363
{
364 365
	const backend_internal *backend_a = (const backend_internal *)(a);
	const backend_internal *backend_b = (const backend_internal *)(b);
366 367 368

	if (backend_a->is_alternate == backend_b->is_alternate)
		return (backend_b->priority - backend_a->priority);
369

370
	return backend_a->is_alternate ? 1 : -1;
371 372
}

373
int git_odb_new(git_odb **out)
374
{
375
	git_odb *db = git__calloc(1, sizeof(*db));
376
	GITERR_CHECK_ALLOC(db);
377

378 379
	if (git_cache_init(&db->own_cache) < 0 ||
		git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) {
380
		git__free(db);
381
		return -1;
382
	}
383

384
	*out = db;
385
	GIT_REFCOUNT_INC(db);
386
	return 0;
387 388
}

389 390 391
static int add_backend_internal(
	git_odb *odb, git_odb_backend *backend,
	int priority, bool is_alternate, ino_t disk_inode)
Ramsay Jones committed
392
{
393 394
	backend_internal *internal;

395
	assert(odb && backend);
Ramsay Jones committed
396

Ben Straub committed
397
	GITERR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
398

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

402
	internal = git__malloc(sizeof(backend_internal));
403
	GITERR_CHECK_ALLOC(internal);
404 405 406 407

	internal->backend = backend;
	internal->priority = priority;
	internal->is_alternate = is_alternate;
408
	internal->disk_inode = disk_inode;
Ramsay Jones committed
409

410
	if (git_vector_insert(&odb->backends, internal) < 0) {
411
		git__free(internal);
412
		return -1;
413
	}
Ramsay Jones committed
414

415
	git_vector_sort(&odb->backends);
416
	internal->backend->odb = odb;
417
	return 0;
Ramsay Jones committed
418 419
}

420 421
int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority)
{
422
	return add_backend_internal(odb, backend, priority, false, 0);
423 424 425 426
}

int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
{
427
	return add_backend_internal(odb, backend, priority, true, 0);
428 429
}

430 431 432 433 434 435
size_t git_odb_num_backends(git_odb *odb)
{
	assert(odb);
	return odb->backends.length;
}

436 437 438 439 440 441 442 443
static int git_odb__error_unsupported_in_backend(const char *action)
{
	giterr_set(GITERR_ODB,
		"Cannot %s - unsupported in the loaded odb backends", action);
	return -1;
}


444 445 446 447
int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos)
{
	backend_internal *internal;

Linquize committed
448
	assert(out && odb);
449 450 451 452 453 454 455
	internal = git_vector_get(&odb->backends, pos);

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

456
	giterr_set(GITERR_ODB, "No ODB backend loaded at index %" PRIuZ, pos);
457 458 459
	return GIT_ENOTFOUND;
}

460 461 462
static int add_default_backends(
	git_odb *db, const char *objects_dir,
	bool as_alternates, int alternate_depth)
463
{
464 465
	size_t i;
	struct stat st;
466
	ino_t inode;
467 468
	git_odb_backend *loose, *packed;

469 470
	/* TODO: inodes are not really relevant on Win32, so we need to find
	 * a cross-platform workaround for this */
471 472 473 474 475 476
#ifdef GIT_WIN32
	GIT_UNUSED(i);
	GIT_UNUSED(st);

	inode = 0;
#else
477
	if (p_stat(objects_dir, &st) < 0) {
478 479 480
		if (as_alternates)
			return 0;

481 482 483 484
		giterr_set(GITERR_ODB, "Failed to load object database in '%s'", objects_dir);
		return -1;
	}

485 486
	inode = st.st_ino;

487 488
	for (i = 0; i < db->backends.length; ++i) {
		backend_internal *backend = git_vector_get(&db->backends, i);
489
		if (backend->disk_inode == inode)
490 491
			return 0;
	}
492
#endif
493

494
	/* add the loose object backend */
495
	if (git_odb_backend_loose(&loose, objects_dir, -1, 0, 0, 0) < 0 ||
496
		add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
497
		return -1;
498 499

	/* add the packed file backend */
500
	if (git_odb_backend_pack(&packed, objects_dir) < 0 ||
501
		add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates, inode) < 0)
502
		return -1;
503

504
	return load_alternates(db, objects_dir, alternate_depth);
505 506
}

507
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth)
508
{
509
	git_buf alternates_path = GIT_BUF_INIT;
510
	git_buf alternates_buf = GIT_BUF_INIT;
511 512
	char *buffer;
	const char *alternate;
513
	int result = 0;
514

515
	/* Git reports an error, we just ignore anything deeper */
516
	if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH)
517 518
		return 0;

519 520
	if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0)
		return -1;
521

522
	if (git_path_exists(alternates_path.ptr) == false) {
523
		git_buf_free(&alternates_path);
524
		return 0;
525
	}
526

527
	if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) {
528
		git_buf_free(&alternates_path);
529
		return -1;
530
	}
531

532
	buffer = (char *)alternates_buf.ptr;
533 534

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

539 540 541 542 543 544
		/*
		 * Relative path: build based on the current `objects`
		 * folder. However, relative paths are only allowed in
		 * the current repository.
		 */
		if (*alternate == '.' && !alternate_depth) {
545
			if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0)
546 547
				break;
			alternate = git_buf_cstr(&alternates_path);
548 549
		}

550
		if ((result = add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0)
551 552
			break;
	}
553

554
	git_buf_free(&alternates_path);
555 556
	git_buf_free(&alternates_buf);

557
	return result;
558
}
Ramsay Jones committed
559

560 561
int git_odb_add_disk_alternate(git_odb *odb, const char *path)
{
562
	return add_default_backends(odb, path, true, 0);
563 564
}

565
int git_odb_open(git_odb **out, const char *objects_dir)
Ramsay Jones committed
566
{
567
	git_odb *db;
Ramsay Jones committed
568

569 570 571 572
	assert(out && objects_dir);

	*out = NULL;

573 574
	if (git_odb_new(&db) < 0)
		return -1;
Ramsay Jones committed
575

576
	if (add_default_backends(db, objects_dir, 0, 0) < 0) {
577 578 579
		git_odb_free(db);
		return -1;
	}
Ramsay Jones committed
580

581
	*out = db;
582
	return 0;
583
}
Ramsay Jones committed
584

585
static void odb_free(git_odb *db)
586
{
587
	size_t i;
588

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

593
		if (backend->free) backend->free(backend);
594
		else git__free(backend);
595

596
		git__free(internal);
597 598
	}

599
	git_vector_free(&db->backends);
600
	git_cache_free(&db->own_cache);
601

602
	git__memzero(db, sizeof(*db));
603
	git__free(db);
604 605
}

606 607 608 609 610 611 612 613
void git_odb_free(git_odb *db)
{
	if (db == NULL)
		return;

	GIT_REFCOUNT_DEC(db, odb_free);
}

614
int git_odb_exists(git_odb *db, const git_oid *id)
615
{
Vicent Marti committed
616
	git_odb_object *object;
617
	size_t i;
618
	bool found = false;
619

620
	assert(db && id);
621

622
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
623
		git_odb_object_free(object);
624
		return (int)true;
Vicent Marti committed
625 626
	}

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

631
		if (b->exists != NULL)
Linquize committed
632
			found = (bool)b->exists(b, id);
633 634
	}

635
	return (int)found;
636 637
}

638 639 640 641 642
int git_odb_exists_prefix(
	git_oid *out, git_odb *db, const git_oid *short_id, size_t len)
{
	int error = GIT_ENOTFOUND, num_found = 0;
	size_t i;
643
	git_oid key = {{0}}, last_found = {{0}}, found;
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

	assert(db && short_id);

	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) {
		if (git_odb_exists(db, short_id)) {
			if (out)
				git_oid_cpy(out, short_id);
			return 0;
		} else {
			return git_odb__error_notfound("no match for id prefix", short_id);
		}
	}

662 663 664 665 666
	/* just copy valid part of short_id */
	memcpy(&key.id, short_id->id, (len + 1) / 2);
	if (len & 1)
		key.id[len / 2] &= 0xF0;

667 668 669 670 671 672 673
	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->exists_prefix)
			continue;

674
		error = b->exists_prefix(&found, b, &key, len);
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		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)
691
		return git_odb__error_notfound("no match for id prefix", &key);
692 693 694
	if (out)
		git_oid_cpy(out, &last_found);

695
	return 0;
696 697
}

Vicent Marti committed
698
int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)
699
{
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
	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;
}

int git_odb__read_header_or_object(
	git_odb_object **out, size_t *len_p, git_otype *type_p,
	git_odb *db, const git_oid *id)
{
715
	size_t i;
716
	int error = GIT_ENOTFOUND;
Vicent Marti committed
717
	git_odb_object *object;
718

719
	assert(db && id && out && len_p && type_p);
Vicent Marti committed
720

721
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
Vicent Marti committed
722 723
		*len_p = object->cached.size;
		*type_p = object->cached.type;
724
		*out = object;
725
		return 0;
Vicent Marti committed
726
	}
727

728 729
	*out = NULL;

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

734
		if (b->read_header != NULL)
Vicent Marti committed
735
			error = b->read_header(len_p, type_p, b, id);
736 737
	}

738
	if (!error || error == GIT_PASSTHROUGH)
739
		return 0;
Vicent Marti committed
740

741 742 743 744
	/*
	 * no backend could read only the header.
	 * try reading the whole object and freeing the contents
	 */
745 746
	if ((error = git_odb_read(&object, db, id)) < 0)
		return error; /* error already set - pass along */
747

Vicent Marti committed
748 749
	*len_p = object->cached.size;
	*type_p = object->cached.type;
750 751
	*out = object;

752
	return 0;
753 754
}

Vicent Marti committed
755
int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
756
{
757
	size_t i, reads = 0;
Vicent Marti committed
758
	int error;
Vicent Marti committed
759
	git_rawobj raw;
760
	git_odb_object *object;
761

762
	assert(out && db && id);
763

764
	*out = git_cache_get_raw(odb_cache(db), id);
Vicent Marti committed
765
	if (*out != NULL)
766
		return 0;
Vicent Marti committed
767

Vicent Marti committed
768
	error = GIT_ENOTFOUND;
Vicent Marti committed
769

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

774
		if (b->read != NULL) {
775
			++reads;
Vicent Marti committed
776
			error = b->read(&raw.data, &raw.len, &raw.type, b, id);
777
		}
Vicent Marti committed
778 779
	}

780 781 782
	if (error && error != GIT_PASSTHROUGH) {
		if (!reads)
			return git_odb__error_notfound("no match for id", id);
783
		return error;
784
	}
785

786
	giterr_clear();
787 788 789 790
	if ((object = odb_object__alloc(id, &raw)) == NULL)
		return -1;

	*out = git_cache_store_raw(odb_cache(db), object);
791
	return 0;
792 793
}

794
int git_odb_read_prefix(
795
	git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len)
796
{
797
	size_t i;
798
	int error = GIT_ENOTFOUND;
799
	git_oid key = {{0}}, found_full_oid = {{0}};
800
	git_rawobj raw;
801
	void *data = NULL;
802
	bool found = false;
803
	git_odb_object *object;
804

Vicent Marti committed
805
	assert(out && db);
806

807
	if (len < GIT_OID_MINPREFIXLEN)
808
		return git_odb__error_ambiguous("prefix length too short");
809 810 811 812
	if (len > GIT_OID_HEXSZ)
		len = GIT_OID_HEXSZ;

	if (len == GIT_OID_HEXSZ) {
813
		*out = git_cache_get_raw(odb_cache(db), short_id);
Vicent Marti committed
814
		if (*out != NULL)
815
			return 0;
816 817
	}

818 819 820 821 822
	/* just copy valid part of short_id */
	memcpy(&key.id, short_id->id, (len + 1) / 2);
	if (len & 1)
		key.id[len / 2] &= 0xF0;

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

827
		if (b->read_prefix != NULL) {
828
			git_oid full_oid;
829
			error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, &key, len);
830
			if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH)
831 832 833
				continue;

			if (error)
834
				return error;
835

836 837
			git__free(data);
			data = raw.data;
Vicent Marti committed
838

839 840
			if (found && git_oid__cmp(&full_oid, &found_full_oid)) {
				git__free(raw.data);
841
				return git_odb__error_ambiguous("multiple matches for prefix");
842
			}
Vicent Marti committed
843

844 845
			found_full_oid = full_oid;
			found = true;
846 847 848
		}
	}

849
	if (!found)
850
		return git_odb__error_notfound("no match for prefix", &key);
851

852 853 854 855
	if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL)
		return -1;

	*out = git_cache_store_raw(odb_cache(db), object);
856
	return 0;
857 858
}

Ben Straub committed
859
int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
860 861 862
{
	unsigned int i;
	backend_internal *internal;
863

864 865
	git_vector_foreach(&db->backends, i, internal) {
		git_odb_backend *b = internal->backend;
Ben Straub committed
866
		int error = b->foreach(b, cb, payload);
867 868
		if (error < 0)
			return error;
869 870 871 872 873
	}

	return 0;
}

874 875
int git_odb_write(
	git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type)
876
{
877
	size_t i;
878
	int error = GIT_ERROR;
Vicent Marti committed
879
	git_odb_stream *stream;
880 881 882

	assert(oid && db);

883 884 885 886
	git_odb_hash(oid, data, len, type);
	if (git_odb_exists(db, oid))
		return 0;

887 888 889 890 891 892 893 894 895
	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)
896
			error = b->write(b, oid, data, len, type);
897 898
	}

899
	if (!error || error == GIT_PASSTHROUGH)
900
		return 0;
Vicent Marti committed
901

902 903 904 905
	/* 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
	 */
906 907
	if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
		return error;
908

909 910
	stream->write(stream, data, len);
	error = stream->finalize_write(stream, oid);
911
	git_odb_stream_free(stream);
912 913

	return error;
914 915
}

916 917 918 919 920 921 922 923 924
static void hash_header(git_hash_ctx *ctx, size_t size, git_otype type)
{
	char header[64];
	int hdrlen;

	hdrlen = git_odb__format_object_header(header, sizeof(header), size, type);
	git_hash_update(ctx, header, hdrlen);
}

925 926
int git_odb_open_wstream(
	git_odb_stream **stream, git_odb *db, size_t size, git_otype type)
927
{
928
	size_t i, writes = 0;
929
	int error = GIT_ERROR;
930
	git_hash_ctx *ctx = NULL;
931

Vicent Marti committed
932
	assert(stream && db);
933

934
	for (i = 0; i < db->backends.length && error < 0; ++i) {
935 936 937 938 939 940
		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;
941

942 943
		if (b->writestream != NULL) {
			++writes;
Vicent Marti committed
944
			error = b->writestream(stream, b, size, type);
945 946
		} else if (b->write != NULL) {
			++writes;
947
			error = init_fake_wstream(stream, b, size, type);
948
		}
Vicent Marti committed
949 950
	}

951 952 953 954 955 956 957 958
	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
959

960 961 962
	ctx = git__malloc(sizeof(git_hash_ctx));
	GITERR_CHECK_ALLOC(ctx);

963 964
	if ((error = git_hash_ctx_init(ctx)) < 0)
		goto done;
965 966 967 968

	hash_header(ctx, size, type);
	(*stream)->hash_ctx = ctx;

969 970 971
	(*stream)->declared_size = size;
	(*stream)->received_bytes = 0;

972
done:
973
	return error;
Vicent Marti committed
974 975
}

976 977 978 979 980 981 982 983 984 985 986 987 988
static int git_odb_stream__invalid_length(
	const git_odb_stream *stream,
	const char *action)
{
	giterr_set(GITERR_ODB,
		"Cannot %s - "
		"Invalid length. %"PRIuZ" was expected. The "
		"total size of the received chunks amounts to %"PRIuZ".",
		action, stream->declared_size, stream->received_bytes);		

	return -1;
}

989 990
int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
{
991
	git_hash_update(stream->hash_ctx, buffer, len);
992 993 994 995 996 997 998

	stream->received_bytes += len;

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

999 1000 1001 1002 1003
	return stream->write(stream, buffer, len);
}

int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
{
1004 1005 1006 1007
	if (stream->received_bytes != stream->declared_size)
		return git_odb_stream__invalid_length(stream,
			"stream_finalize_write()");

1008
	git_hash_final(out, stream->hash_ctx);
1009 1010 1011 1012

	if (git_odb_exists(stream->backend->odb, out))
		return 0;

1013
	return stream->finalize_write(stream, out);
1014 1015 1016 1017 1018 1019 1020 1021 1022
}

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)
{
1023 1024 1025
	if (stream == NULL)
		return;

1026
	git__free(stream->hash_ctx);
1027 1028 1029
	stream->free(stream);
}

1030
int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
Vicent Marti committed
1031
{
1032
	size_t i, reads = 0;
Vicent Marti committed
1033 1034 1035 1036 1037 1038 1039 1040
	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;

1041 1042
		if (b->readstream != NULL) {
			++reads;
Vicent Marti committed
1043
			error = b->readstream(stream, b, oid);
1044
		}
1045 1046
	}

1047
	if (error == GIT_PASSTHROUGH)
1048
		error = 0;
1049 1050
	if (error < 0 && !reads)
		error = git_odb__error_unsupported_in_backend("read object streamed");
Vicent Marti committed
1051

1052 1053 1054
	return error;
}

1055
int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer_progress_cb progress_cb, void *progress_payload)
1056
{
1057
	size_t i, writes = 0;
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	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;

1070 1071
		if (b->writepack != NULL) {
			++writes;
1072
			error = b->writepack(out, b, db, progress_cb, progress_payload);
1073
		}
1074 1075 1076 1077
	}

	if (error == GIT_PASSTHROUGH)
		error = 0;
1078 1079
	if (error < 0 && !writes)
		error = git_odb__error_unsupported_in_backend("write pack");
1080 1081 1082 1083

	return error;
}

Vicent Marti committed
1084
void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
1085
{
1086
	GIT_UNUSED(backend);
1087 1088 1089
	return git__malloc(len);
}

Vicent Marti committed
1090 1091
int git_odb_refresh(struct git_odb *db)
{
1092
	size_t i;
Vicent Marti committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	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;
}

Russell Belfer committed
1109
int git_odb__error_notfound(const char *message, const git_oid *oid)
1110
{
Russell Belfer committed
1111 1112 1113 1114 1115 1116 1117
	if (oid != NULL) {
		char oid_str[GIT_OID_HEXSZ + 1];
		git_oid_tostr(oid_str, sizeof(oid_str), oid);
		giterr_set(GITERR_ODB, "Object not found - %s (%s)", message, oid_str);
	} else
		giterr_set(GITERR_ODB, "Object not found - %s", message);

1118
	return GIT_ENOTFOUND;
1119 1120 1121 1122 1123
}

int git_odb__error_ambiguous(const char *message)
{
	giterr_set(GITERR_ODB, "Ambiguous SHA1 prefix - %s", message);
1124
	return GIT_EAMBIGUOUS;
1125 1126
}

1127
int git_odb_init_backend(git_odb_backend *backend, unsigned int version)
1128
{
1129 1130 1131
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		backend, version, git_odb_backend, GIT_ODB_BACKEND_INIT);
	return 0;
1132
}