Blame src/odb.c

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include <zlib.h>
Packit ae9e2a
#include "git2/object.h"
Packit ae9e2a
#include "git2/sys/odb_backend.h"
Packit ae9e2a
#include "fileops.h"
Packit ae9e2a
#include "hash.h"
Packit ae9e2a
#include "odb.h"
Packit ae9e2a
#include "delta.h"
Packit ae9e2a
#include "filter.h"
Packit ae9e2a
#include "repository.h"
Packit ae9e2a
Packit ae9e2a
#include "git2/odb_backend.h"
Packit ae9e2a
#include "git2/oid.h"
Packit ae9e2a
#include "git2/oidarray.h"
Packit ae9e2a
Packit ae9e2a
#define GIT_ALTERNATES_FILE "info/alternates"
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
 * We work under the assumption that most objects for long-running
Packit ae9e2a
 * operations will be packed
Packit ae9e2a
 */
Packit ae9e2a
#define GIT_LOOSE_PRIORITY 1
Packit ae9e2a
#define GIT_PACKED_PRIORITY 2
Packit ae9e2a
Packit ae9e2a
#define GIT_ALTERNATES_MAX_DEPTH 5
Packit ae9e2a
Packit ae9e2a
bool git_odb__strict_hash_verification = true;
Packit ae9e2a
Packit ae9e2a
typedef struct
Packit ae9e2a
{
Packit ae9e2a
	git_odb_backend *backend;
Packit ae9e2a
	int priority;
Packit ae9e2a
	bool is_alternate;
Packit ae9e2a
	ino_t disk_inode;
Packit ae9e2a
} backend_internal;
Packit ae9e2a
Packit ae9e2a
static git_cache *odb_cache(git_odb *odb)
Packit ae9e2a
{
Packit ae9e2a
	if (odb->rc.owner != NULL) {
Packit ae9e2a
		git_repository *owner = odb->rc.owner;
Packit ae9e2a
		return &owner->objects;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return &odb->own_cache;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_otype_fast(git_otype *type_p, git_odb *db, const git_oid *id);
Packit ae9e2a
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth);
Packit ae9e2a
Packit ae9e2a
static git_otype odb_hardcoded_type(const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	static git_oid empty_tree = {{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
Packit ae9e2a
					   0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};
Packit ae9e2a
Packit ae9e2a
	if (!git_oid_cmp(id, &empty_tree))
Packit ae9e2a
		return GIT_OBJ_TREE;
Packit ae9e2a
Packit ae9e2a
	return GIT_OBJ_BAD;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_read_hardcoded(git_rawobj *raw, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	git_otype type = odb_hardcoded_type(id);
Packit ae9e2a
	if (type == GIT_OBJ_BAD)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	raw->type = type;
Packit ae9e2a
	raw->len = 0;
Packit ae9e2a
	raw->data = git__calloc(1, sizeof(uint8_t));
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__format_object_header(char *hdr, size_t n, git_off_t obj_len, git_otype obj_type)
Packit ae9e2a
{
Packit ae9e2a
	const char *type_str = git_object_type2string(obj_type);
Packit ae9e2a
	int len = p_snprintf(hdr, n, "%s %lld", type_str, (long long)obj_len);
Packit ae9e2a
	assert(len > 0 && len <= (int)n);
Packit ae9e2a
	return len+1;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__hashobj(git_oid *id, git_rawobj *obj)
Packit ae9e2a
{
Packit ae9e2a
	git_buf_vec vec[2];
Packit ae9e2a
	char header[64];
Packit ae9e2a
	int hdrlen;
Packit ae9e2a
Packit ae9e2a
	assert(id && obj);
Packit ae9e2a
Packit ae9e2a
	if (!git_object_typeisloose(obj->type))
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	if (!obj->data && obj->len != 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	hdrlen = git_odb__format_object_header(header, sizeof(header), obj->len, obj->type);
Packit ae9e2a
Packit ae9e2a
	vec[0].data = header;
Packit ae9e2a
	vec[0].len = hdrlen;
Packit ae9e2a
	vec[1].data = obj->data;
Packit ae9e2a
	vec[1].len = obj->len;
Packit ae9e2a
Packit ae9e2a
	git_hash_vec(id, vec, 2);
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source)
Packit ae9e2a
{
Packit ae9e2a
	git_odb_object *object = git__calloc(1, sizeof(git_odb_object));
Packit ae9e2a
Packit ae9e2a
	if (object != NULL) {
Packit ae9e2a
		git_oid_cpy(&object->cached.oid, oid);
Packit ae9e2a
		object->cached.type = source->type;
Packit ae9e2a
		object->cached.size = source->len;
Packit ae9e2a
		object->buffer      = source->data;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return object;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void git_odb_object__free(void *object)
Packit ae9e2a
{
Packit ae9e2a
	if (object != NULL) {
Packit ae9e2a
		git__free(((git_odb_object *)object)->buffer);
Packit ae9e2a
		git__free(object);
Packit ae9e2a
	}
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
const git_oid *git_odb_object_id(git_odb_object *object)
Packit ae9e2a
{
Packit ae9e2a
	return &object->cached.oid;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
const void *git_odb_object_data(git_odb_object *object)
Packit ae9e2a
{
Packit ae9e2a
	return object->buffer;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
size_t git_odb_object_size(git_odb_object *object)
Packit ae9e2a
{
Packit ae9e2a
	return object->cached.size;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
git_otype git_odb_object_type(git_odb_object *object)
Packit ae9e2a
{
Packit ae9e2a
	return object->cached.type;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_object_dup(git_odb_object **dest, git_odb_object *source)
Packit ae9e2a
{
Packit ae9e2a
	git_cached_obj_incref(source);
Packit ae9e2a
	*dest = source;
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void git_odb_object_free(git_odb_object *object)
Packit ae9e2a
{
Packit ae9e2a
	if (object == NULL)
Packit ae9e2a
		return;
Packit ae9e2a
Packit ae9e2a
	git_cached_obj_decref(object);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	int hdr_len;
Packit ae9e2a
	char hdr[64], buffer[FILEIO_BUFSIZE];
Packit ae9e2a
	git_hash_ctx ctx;
Packit ae9e2a
	ssize_t read_len = 0;
Packit ae9e2a
	int error = 0;
Packit ae9e2a
Packit ae9e2a
	if (!git_object_typeisloose(type)) {
Packit ae9e2a
		giterr_set(GITERR_INVALID, "invalid object type for hash");
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if ((error = git_hash_ctx_init(&ctx)) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	hdr_len = git_odb__format_object_header(hdr, sizeof(hdr), size, type);
Packit ae9e2a
Packit ae9e2a
	if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0)
Packit ae9e2a
		goto done;
Packit ae9e2a
Packit ae9e2a
	while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) {
Packit ae9e2a
		if ((error = git_hash_update(&ctx, buffer, read_len)) < 0)
Packit ae9e2a
			goto done;
Packit ae9e2a
Packit ae9e2a
		size -= read_len;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/* If p_read returned an error code, the read obviously failed.
Packit ae9e2a
	 * If size is not zero, the file was truncated after we originally
Packit ae9e2a
	 * stat'd it, so we consider this a read failure too */
Packit ae9e2a
	if (read_len < 0 || size > 0) {
Packit ae9e2a
		giterr_set(GITERR_OS, "error reading file for hashing");
Packit ae9e2a
		error = -1;
Packit ae9e2a
Packit ae9e2a
		goto done;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	error = git_hash_final(out, &ctx;;
Packit ae9e2a
Packit ae9e2a
done:
Packit ae9e2a
	git_hash_ctx_cleanup(&ctx;;
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__hashfd_filtered(
Packit ae9e2a
	git_oid *out, git_file fd, size_t size, git_otype type, git_filter_list *fl)
Packit ae9e2a
{
Packit ae9e2a
	int error;
Packit ae9e2a
	git_buf raw = GIT_BUF_INIT;
Packit ae9e2a
Packit ae9e2a
	if (!fl)
Packit ae9e2a
		return git_odb__hashfd(out, fd, size, type);
Packit ae9e2a
Packit ae9e2a
	/* size of data is used in header, so we have to read the whole file
Packit ae9e2a
	 * into memory to apply filters before beginning to calculate the hash
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
Packit ae9e2a
		git_buf post = GIT_BUF_INIT;
Packit ae9e2a
Packit ae9e2a
		error = git_filter_list_apply_to_data(&post, fl, &raw;;
Packit ae9e2a
Packit ae9e2a
		git_buf_free(&raw;;
Packit ae9e2a
Packit ae9e2a
		if (!error)
Packit ae9e2a
			error = git_odb_hash(out, post.ptr, post.size, type);
Packit ae9e2a
Packit ae9e2a
		git_buf_free(&post);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__hashlink(git_oid *out, const char *path)
Packit ae9e2a
{
Packit ae9e2a
	struct stat st;
Packit ae9e2a
	int size;
Packit ae9e2a
	int result;
Packit ae9e2a
Packit ae9e2a
	if (git_path_lstat(path, &st) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	if (!git__is_int(st.st_size) || (int)st.st_size < 0) {
Packit ae9e2a
		giterr_set(GITERR_FILESYSTEM, "file size overflow for 32-bit systems");
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	size = (int)st.st_size;
Packit ae9e2a
Packit ae9e2a
	if (S_ISLNK(st.st_mode)) {
Packit ae9e2a
		char *link_data;
Packit ae9e2a
		int read_len;
Packit ae9e2a
		size_t alloc_size;
Packit ae9e2a
Packit ae9e2a
		GITERR_CHECK_ALLOC_ADD(&alloc_size, size, 1);
Packit ae9e2a
		link_data = git__malloc(alloc_size);
Packit ae9e2a
		GITERR_CHECK_ALLOC(link_data);
Packit ae9e2a
Packit ae9e2a
		read_len = p_readlink(path, link_data, size);
Packit ae9e2a
		link_data[size] = '\0';
Packit ae9e2a
		if (read_len != size) {
Packit ae9e2a
			giterr_set(GITERR_OS, "failed to read symlink data for '%s'", path);
Packit ae9e2a
			git__free(link_data);
Packit ae9e2a
			return -1;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		result = git_odb_hash(out, link_data, size, GIT_OBJ_BLOB);
Packit ae9e2a
		git__free(link_data);
Packit ae9e2a
	} else {
Packit ae9e2a
		int fd = git_futils_open_ro(path);
Packit ae9e2a
		if (fd < 0)
Packit ae9e2a
			return -1;
Packit ae9e2a
		result = git_odb__hashfd(out, fd, size, GIT_OBJ_BLOB);
Packit ae9e2a
		p_close(fd);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return result;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_hashfile(git_oid *out, const char *path, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	git_off_t size;
Packit ae9e2a
	int result, fd = git_futils_open_ro(path);
Packit ae9e2a
	if (fd < 0)
Packit ae9e2a
		return fd;
Packit ae9e2a
Packit ae9e2a
	if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
Packit ae9e2a
		giterr_set(GITERR_OS, "file size overflow for 32-bit systems");
Packit ae9e2a
		p_close(fd);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	result = git_odb__hashfd(out, fd, (size_t)size, type);
Packit ae9e2a
	p_close(fd);
Packit ae9e2a
	return result;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	git_rawobj raw;
Packit ae9e2a
Packit ae9e2a
	assert(id);
Packit ae9e2a
Packit ae9e2a
	raw.data = (void *)data;
Packit ae9e2a
	raw.len = len;
Packit ae9e2a
	raw.type = type;
Packit ae9e2a
Packit ae9e2a
	return git_odb__hashobj(id, &raw;;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * FAKE WSTREAM
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
typedef struct {
Packit ae9e2a
	git_odb_stream stream;
Packit ae9e2a
	char *buffer;
Packit ae9e2a
	size_t size, written;
Packit ae9e2a
	git_otype type;
Packit ae9e2a
} fake_wstream;
Packit ae9e2a
Packit ae9e2a
static int fake_wstream__fwrite(git_odb_stream *_stream, const git_oid *oid)
Packit ae9e2a
{
Packit ae9e2a
	fake_wstream *stream = (fake_wstream *)_stream;
Packit ae9e2a
	return _stream->backend->write(_stream->backend, oid, stream->buffer, stream->size, stream->type);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	fake_wstream *stream = (fake_wstream *)_stream;
Packit ae9e2a
Packit ae9e2a
	if (stream->written + len > stream->size)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	memcpy(stream->buffer + stream->written, data, len);
Packit ae9e2a
	stream->written += len;
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void fake_wstream__free(git_odb_stream *_stream)
Packit ae9e2a
{
Packit ae9e2a
	fake_wstream *stream = (fake_wstream *)_stream;
Packit ae9e2a
Packit ae9e2a
	git__free(stream->buffer);
Packit ae9e2a
	git__free(stream);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, git_off_t size, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	fake_wstream *stream;
Packit ae9e2a
Packit ae9e2a
	if (!git__is_ssizet(size)) {
Packit ae9e2a
		giterr_set(GITERR_ODB, "object size too large to keep in memory");
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	stream = git__calloc(1, sizeof(fake_wstream));
Packit ae9e2a
	GITERR_CHECK_ALLOC(stream);
Packit ae9e2a
Packit ae9e2a
	stream->size = size;
Packit ae9e2a
	stream->type = type;
Packit ae9e2a
	stream->buffer = git__malloc(size);
Packit ae9e2a
	if (stream->buffer == NULL) {
Packit ae9e2a
		git__free(stream);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	stream->stream.backend = backend;
Packit ae9e2a
	stream->stream.read = NULL; /* read only */
Packit ae9e2a
	stream->stream.write = &fake_wstream__write;
Packit ae9e2a
	stream->stream.finalize_write = &fake_wstream__fwrite;
Packit ae9e2a
	stream->stream.free = &fake_wstream__free;
Packit ae9e2a
	stream->stream.mode = GIT_STREAM_WRONLY;
Packit ae9e2a
Packit ae9e2a
	*stream_p = (git_odb_stream *)stream;
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/***********************************************************
Packit ae9e2a
 *
Packit ae9e2a
 * OBJECT DATABASE PUBLIC API
Packit ae9e2a
 *
Packit ae9e2a
 * Public calls for the ODB functionality
Packit ae9e2a
 *
Packit ae9e2a
 ***********************************************************/
Packit ae9e2a
Packit ae9e2a
static int backend_sort_cmp(const void *a, const void *b)
Packit ae9e2a
{
Packit ae9e2a
	const backend_internal *backend_a = (const backend_internal *)(a);
Packit ae9e2a
	const backend_internal *backend_b = (const backend_internal *)(b);
Packit ae9e2a
Packit ae9e2a
	if (backend_b->priority == backend_a->priority) {
Packit ae9e2a
		if (backend_a->is_alternate)
Packit ae9e2a
			return -1;
Packit ae9e2a
		if (backend_b->is_alternate)
Packit ae9e2a
			return 1;
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
	return (backend_b->priority - backend_a->priority);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_new(git_odb **out)
Packit ae9e2a
{
Packit ae9e2a
	git_odb *db = git__calloc(1, sizeof(*db));
Packit ae9e2a
	GITERR_CHECK_ALLOC(db);
Packit ae9e2a
Packit ae9e2a
	if (git_cache_init(&db->own_cache) < 0 ||
Packit ae9e2a
		git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) {
Packit ae9e2a
		git__free(db);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	*out = db;
Packit ae9e2a
	GIT_REFCOUNT_INC(db);
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int add_backend_internal(
Packit ae9e2a
	git_odb *odb, git_odb_backend *backend,
Packit ae9e2a
	int priority, bool is_alternate, ino_t disk_inode)
Packit ae9e2a
{
Packit ae9e2a
	backend_internal *internal;
Packit ae9e2a
Packit ae9e2a
	assert(odb && backend);
Packit ae9e2a
Packit ae9e2a
	GITERR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
Packit ae9e2a
Packit ae9e2a
	/* Check if the backend is already owned by another ODB */
Packit ae9e2a
	assert(!backend->odb || backend->odb == odb);
Packit ae9e2a
Packit ae9e2a
	internal = git__malloc(sizeof(backend_internal));
Packit ae9e2a
	GITERR_CHECK_ALLOC(internal);
Packit ae9e2a
Packit ae9e2a
	internal->backend = backend;
Packit ae9e2a
	internal->priority = priority;
Packit ae9e2a
	internal->is_alternate = is_alternate;
Packit ae9e2a
	internal->disk_inode = disk_inode;
Packit ae9e2a
Packit ae9e2a
	if (git_vector_insert(&odb->backends, internal) < 0) {
Packit ae9e2a
		git__free(internal);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_vector_sort(&odb->backends);
Packit ae9e2a
	internal->backend->odb = odb;
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority)
Packit ae9e2a
{
Packit ae9e2a
	return add_backend_internal(odb, backend, priority, false, 0);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
Packit ae9e2a
{
Packit ae9e2a
	return add_backend_internal(odb, backend, priority, true, 0);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
size_t git_odb_num_backends(git_odb *odb)
Packit ae9e2a
{
Packit ae9e2a
	assert(odb);
Packit ae9e2a
	return odb->backends.length;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int git_odb__error_unsupported_in_backend(const char *action)
Packit ae9e2a
{
Packit ae9e2a
	giterr_set(GITERR_ODB,
Packit ae9e2a
		"cannot %s - unsupported in the loaded odb backends", action);
Packit ae9e2a
	return -1;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos)
Packit ae9e2a
{
Packit ae9e2a
	backend_internal *internal;
Packit ae9e2a
Packit ae9e2a
	assert(out && odb);
Packit ae9e2a
	internal = git_vector_get(&odb->backends, pos);
Packit ae9e2a
Packit ae9e2a
	if (internal && internal->backend) {
Packit ae9e2a
		*out = internal->backend;
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	giterr_set(GITERR_ODB, "no ODB backend loaded at index %" PRIuZ, pos);
Packit ae9e2a
	return GIT_ENOTFOUND;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__add_default_backends(
Packit ae9e2a
	git_odb *db, const char *objects_dir,
Packit ae9e2a
	bool as_alternates, int alternate_depth)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	struct stat st;
Packit ae9e2a
	ino_t inode;
Packit ae9e2a
	git_odb_backend *loose, *packed;
Packit ae9e2a
Packit ae9e2a
	/* TODO: inodes are not really relevant on Win32, so we need to find
Packit ae9e2a
	 * a cross-platform workaround for this */
Packit ae9e2a
#ifdef GIT_WIN32
Packit ae9e2a
	GIT_UNUSED(i);
Packit ae9e2a
	GIT_UNUSED(st);
Packit ae9e2a
Packit ae9e2a
	inode = 0;
Packit ae9e2a
#else
Packit ae9e2a
	if (p_stat(objects_dir, &st) < 0) {
Packit ae9e2a
		if (as_alternates)
Packit ae9e2a
			return 0;
Packit ae9e2a
Packit ae9e2a
		giterr_set(GITERR_ODB, "failed to load object database in '%s'", objects_dir);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	inode = st.st_ino;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length; ++i) {
Packit ae9e2a
		backend_internal *backend = git_vector_get(&db->backends, i);
Packit ae9e2a
		if (backend->disk_inode == inode)
Packit ae9e2a
			return 0;
Packit ae9e2a
	}
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
	/* add the loose object backend */
Packit ae9e2a
	if (git_odb_backend_loose(&loose, objects_dir, -1, db->do_fsync, 0, 0) < 0 ||
Packit ae9e2a
		add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	/* add the packed file backend */
Packit ae9e2a
	if (git_odb_backend_pack(&packed, objects_dir) < 0 ||
Packit ae9e2a
		add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates, inode) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	return load_alternates(db, objects_dir, alternate_depth);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth)
Packit ae9e2a
{
Packit ae9e2a
	git_buf alternates_path = GIT_BUF_INIT;
Packit ae9e2a
	git_buf alternates_buf = GIT_BUF_INIT;
Packit ae9e2a
	char *buffer;
Packit ae9e2a
	const char *alternate;
Packit ae9e2a
	int result = 0;
Packit ae9e2a
Packit ae9e2a
	/* Git reports an error, we just ignore anything deeper */
Packit ae9e2a
	if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH)
Packit ae9e2a
		return 0;
Packit ae9e2a
Packit ae9e2a
	if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	if (git_path_exists(alternates_path.ptr) == false) {
Packit ae9e2a
		git_buf_free(&alternates_path);
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) {
Packit ae9e2a
		git_buf_free(&alternates_path);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	buffer = (char *)alternates_buf.ptr;
Packit ae9e2a
Packit ae9e2a
	/* add each alternate as a new backend; one alternate per line */
Packit ae9e2a
	while ((alternate = git__strtok(&buffer, "\r\n")) != NULL) {
Packit ae9e2a
		if (*alternate == '\0' || *alternate == '#')
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		/*
Packit ae9e2a
		 * Relative path: build based on the current `objects`
Packit ae9e2a
		 * folder. However, relative paths are only allowed in
Packit ae9e2a
		 * the current repository.
Packit ae9e2a
		 */
Packit ae9e2a
		if (*alternate == '.' && !alternate_depth) {
Packit ae9e2a
			if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0)
Packit ae9e2a
				break;
Packit ae9e2a
			alternate = git_buf_cstr(&alternates_path);
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		if ((result = git_odb__add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0)
Packit ae9e2a
			break;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&alternates_path);
Packit ae9e2a
	git_buf_free(&alternates_buf);
Packit ae9e2a
Packit ae9e2a
	return result;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_add_disk_alternate(git_odb *odb, const char *path)
Packit ae9e2a
{
Packit ae9e2a
	return git_odb__add_default_backends(odb, path, true, 0);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_open(git_odb **out, const char *objects_dir)
Packit ae9e2a
{
Packit ae9e2a
	git_odb *db;
Packit ae9e2a
Packit ae9e2a
	assert(out && objects_dir);
Packit ae9e2a
Packit ae9e2a
	*out = NULL;
Packit ae9e2a
Packit ae9e2a
	if (git_odb_new(&db) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	if (git_odb__add_default_backends(db, objects_dir, 0, 0) < 0) {
Packit ae9e2a
		git_odb_free(db);
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	*out = db;
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__set_caps(git_odb *odb, int caps)
Packit ae9e2a
{
Packit ae9e2a
	if (caps == GIT_ODB_CAP_FROM_OWNER) {
Packit ae9e2a
		git_repository *repo = odb->rc.owner;
Packit ae9e2a
		int val;
Packit ae9e2a
Packit ae9e2a
		if (!repo) {
Packit ae9e2a
			giterr_set(GITERR_ODB, "cannot access repository to set odb caps");
Packit ae9e2a
			return -1;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		if (!git_repository__cvar(&val, repo, GIT_CVAR_FSYNCOBJECTFILES))
Packit ae9e2a
			odb->do_fsync = !!val;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void odb_free(git_odb *db)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *backend = internal->backend;
Packit ae9e2a
Packit ae9e2a
		backend->free(backend);
Packit ae9e2a
Packit ae9e2a
		git__free(internal);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_vector_free(&db->backends);
Packit ae9e2a
	git_cache_free(&db->own_cache);
Packit ae9e2a
Packit ae9e2a
	git__memzero(db, sizeof(*db));
Packit ae9e2a
	git__free(db);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void git_odb_free(git_odb *db)
Packit ae9e2a
{
Packit ae9e2a
	if (db == NULL)
Packit ae9e2a
		return;
Packit ae9e2a
Packit ae9e2a
	GIT_REFCOUNT_DEC(db, odb_free);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_exists_1(
Packit ae9e2a
	git_odb *db,
Packit ae9e2a
	const git_oid *id,
Packit ae9e2a
	bool only_refreshed)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	bool found = false;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && !found; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (only_refreshed && !b->refresh)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->exists != NULL)
Packit ae9e2a
			found = (bool)b->exists(b, id);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return (int)found;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_freshen_1(
Packit ae9e2a
	git_odb *db,
Packit ae9e2a
	const git_oid *id,
Packit ae9e2a
	bool only_refreshed)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	bool found = false;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && !found; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (only_refreshed && !b->refresh)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->freshen != NULL)
Packit ae9e2a
			found = !b->freshen(b, id);
Packit ae9e2a
		else if (b->exists != NULL)
Packit ae9e2a
			found = b->exists(b, id);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return (int)found;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__freshen(git_odb *db, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	assert(db && id);
Packit ae9e2a
Packit ae9e2a
	if (odb_freshen_1(db, id, false))
Packit ae9e2a
		return 1;
Packit ae9e2a
Packit ae9e2a
	if (!git_odb_refresh(db))
Packit ae9e2a
		return odb_freshen_1(db, id, true);
Packit ae9e2a
Packit ae9e2a
	/* Failed to refresh, hence not found */
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_exists(git_odb *db, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	git_odb_object *object;
Packit ae9e2a
Packit ae9e2a
	assert(db && id);
Packit ae9e2a
Packit ae9e2a
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
Packit ae9e2a
		git_odb_object_free(object);
Packit ae9e2a
		return 1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (odb_exists_1(db, id, false))
Packit ae9e2a
		return 1;
Packit ae9e2a
Packit ae9e2a
	if (!git_odb_refresh(db))
Packit ae9e2a
		return odb_exists_1(db, id, true);
Packit ae9e2a
Packit ae9e2a
	/* Failed to refresh, hence not found */
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_exists_prefix_1(git_oid *out, git_odb *db,
Packit ae9e2a
	const git_oid *key, size_t len, bool only_refreshed)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	int error = GIT_ENOTFOUND, num_found = 0;
Packit ae9e2a
	git_oid last_found = {{0}}, found;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (only_refreshed && !b->refresh)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (!b->exists_prefix)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		error = b->exists_prefix(&found, b, key, len);
Packit ae9e2a
		if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH)
Packit ae9e2a
			continue;
Packit ae9e2a
		if (error)
Packit ae9e2a
			return error;
Packit ae9e2a
Packit ae9e2a
		/* make sure found item doesn't introduce ambiguity */
Packit ae9e2a
		if (num_found) {
Packit ae9e2a
			if (git_oid__cmp(&last_found, &found))
Packit ae9e2a
				return git_odb__error_ambiguous("multiple matches for prefix");
Packit ae9e2a
		} else {
Packit ae9e2a
			git_oid_cpy(&last_found, &found);
Packit ae9e2a
			num_found++;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!num_found)
Packit ae9e2a
		return GIT_ENOTFOUND;
Packit ae9e2a
Packit ae9e2a
	if (out)
Packit ae9e2a
		git_oid_cpy(out, &last_found);
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_exists_prefix(
Packit ae9e2a
	git_oid *out, git_odb *db, const git_oid *short_id, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	int error;
Packit ae9e2a
	git_oid key = {{0}}; 
Packit ae9e2a
Packit ae9e2a
	assert(db && short_id);
Packit ae9e2a
Packit ae9e2a
	if (len < GIT_OID_MINPREFIXLEN)
Packit ae9e2a
		return git_odb__error_ambiguous("prefix length too short");
Packit ae9e2a
Packit ae9e2a
	if (len >= GIT_OID_HEXSZ) {
Packit ae9e2a
		if (git_odb_exists(db, short_id)) {
Packit ae9e2a
			if (out)
Packit ae9e2a
				git_oid_cpy(out, short_id);
Packit ae9e2a
			return 0;
Packit ae9e2a
		} else {
Packit ae9e2a
			return git_odb__error_notfound(
Packit ae9e2a
				"no match for id prefix", short_id, len);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_oid__cpy_prefix(&key, short_id, len);
Packit ae9e2a
Packit ae9e2a
	error = odb_exists_prefix_1(out, db, &key, len, false);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
Packit ae9e2a
		error = odb_exists_prefix_1(out, db, &key, len, true);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND)
Packit ae9e2a
		return git_odb__error_notfound("no match for id prefix", &key, len);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_expand_ids(
Packit ae9e2a
	git_odb *db,
Packit ae9e2a
	git_odb_expand_id *ids,
Packit ae9e2a
	size_t count)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
Packit ae9e2a
	assert(db && ids);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < count; i++) {
Packit ae9e2a
		git_odb_expand_id *query = &ids[i];
Packit ae9e2a
		int error = GIT_EAMBIGUOUS;
Packit ae9e2a
Packit ae9e2a
		if (!query->type)
Packit ae9e2a
			query->type = GIT_OBJ_ANY;
Packit ae9e2a
Packit ae9e2a
		/* if we have a short OID, expand it first */
Packit ae9e2a
		if (query->length >= GIT_OID_MINPREFIXLEN && query->length < GIT_OID_HEXSZ) {
Packit ae9e2a
			git_oid actual_id;
Packit ae9e2a
Packit ae9e2a
			error = odb_exists_prefix_1(&actual_id, db, &query->id, query->length, false);
Packit ae9e2a
			if (!error) {
Packit ae9e2a
				git_oid_cpy(&query->id, &actual_id);
Packit ae9e2a
				query->length = GIT_OID_HEXSZ;
Packit ae9e2a
			}
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		/*
Packit ae9e2a
		 * now we ought to have a 40-char OID, either because we've expanded it
Packit ae9e2a
		 * or because the user passed a full OID. Ensure its type is right.
Packit ae9e2a
		 */
Packit ae9e2a
		if (query->length >= GIT_OID_HEXSZ) {
Packit ae9e2a
			git_otype actual_type;
Packit ae9e2a
Packit ae9e2a
			error = odb_otype_fast(&actual_type, db, &query->id);
Packit ae9e2a
			if (!error) {
Packit ae9e2a
				if (query->type != GIT_OBJ_ANY && query->type != actual_type)
Packit ae9e2a
					error = GIT_ENOTFOUND;
Packit ae9e2a
				else
Packit ae9e2a
					query->type = actual_type;
Packit ae9e2a
			}
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		switch (error) {
Packit ae9e2a
		/* no errors, so we've successfully expanded the OID */
Packit ae9e2a
		case 0:
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		/* the object is missing or ambiguous */
Packit ae9e2a
		case GIT_ENOTFOUND:
Packit ae9e2a
		case GIT_EAMBIGUOUS:
Packit ae9e2a
			memset(&query->id, 0, sizeof(git_oid));
Packit ae9e2a
			query->length = 0;
Packit ae9e2a
			query->type = 0;
Packit ae9e2a
			break;
Packit ae9e2a
Packit ae9e2a
		/* something went very wrong with the ODB; bail hard */
Packit ae9e2a
		default:
Packit ae9e2a
			return error;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	giterr_clear();
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	int error;
Packit ae9e2a
	git_odb_object *object;
Packit ae9e2a
Packit ae9e2a
	error = git_odb__read_header_or_object(&object, len_p, type_p, db, id);
Packit ae9e2a
Packit ae9e2a
	if (object)
Packit ae9e2a
		git_odb_object_free(object);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_read_header_1(
Packit ae9e2a
	size_t *len_p, git_otype *type_p, git_odb *db,
Packit ae9e2a
	const git_oid *id, bool only_refreshed)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	git_otype ht;
Packit ae9e2a
	bool passthrough = false;
Packit ae9e2a
	int error;
Packit ae9e2a
Packit ae9e2a
	if (!only_refreshed && (ht = odb_hardcoded_type(id)) != GIT_OBJ_BAD) {
Packit ae9e2a
		*type_p = ht;
Packit ae9e2a
		*len_p = 0;
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (only_refreshed && !b->refresh)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (!b->read_header) {
Packit ae9e2a
			passthrough = true;
Packit ae9e2a
			continue;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		error = b->read_header(len_p, type_p, b, id);
Packit ae9e2a
Packit ae9e2a
		switch (error) {
Packit ae9e2a
		case GIT_PASSTHROUGH:
Packit ae9e2a
			passthrough = true;
Packit ae9e2a
			break;
Packit ae9e2a
		case GIT_ENOTFOUND:
Packit ae9e2a
			break;
Packit ae9e2a
		default:
Packit ae9e2a
			return error;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return passthrough ? GIT_PASSTHROUGH : GIT_ENOTFOUND;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__read_header_or_object(
Packit ae9e2a
	git_odb_object **out, size_t *len_p, git_otype *type_p,
Packit ae9e2a
	git_odb *db, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	int error = GIT_ENOTFOUND;
Packit ae9e2a
	git_odb_object *object;
Packit ae9e2a
Packit ae9e2a
	assert(db && id && out && len_p && type_p);
Packit ae9e2a
Packit ae9e2a
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
Packit ae9e2a
		*len_p = object->cached.size;
Packit ae9e2a
		*type_p = object->cached.type;
Packit ae9e2a
		*out = object;
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	*out = NULL;
Packit ae9e2a
	error = odb_read_header_1(len_p, type_p, db, id, false);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
Packit ae9e2a
		error = odb_read_header_1(len_p, type_p, db, id, true);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND)
Packit ae9e2a
		return git_odb__error_notfound("cannot read header for", id, GIT_OID_HEXSZ);
Packit ae9e2a
Packit ae9e2a
	/* we found the header; return early */
Packit ae9e2a
	if (!error)
Packit ae9e2a
		return 0;
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_PASSTHROUGH) {
Packit ae9e2a
		/*
Packit ae9e2a
		 * no backend has header-reading functionality
Packit ae9e2a
		 * so try using `git_odb_read` instead
Packit ae9e2a
		 */
Packit ae9e2a
		error = git_odb_read(&object, db, id);
Packit ae9e2a
		if (!error) {
Packit ae9e2a
			*len_p = object->cached.size;
Packit ae9e2a
			*type_p = object->cached.type;
Packit ae9e2a
			*out = object;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
Packit ae9e2a
		bool only_refreshed)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	git_rawobj raw;
Packit ae9e2a
	git_odb_object *object;
Packit ae9e2a
	git_oid hashed;
Packit ae9e2a
	bool found = false;
Packit ae9e2a
	int error = 0;
Packit ae9e2a
Packit ae9e2a
	if (!only_refreshed && odb_read_hardcoded(&raw, id) == 0)
Packit ae9e2a
		found = true;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && !found; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (only_refreshed && !b->refresh)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->read != NULL) {
Packit ae9e2a
			error = b->read(&raw.data, &raw.len, &raw.type, b, id);
Packit ae9e2a
			if (error == GIT_PASSTHROUGH || error == GIT_ENOTFOUND)
Packit ae9e2a
				continue;
Packit ae9e2a
Packit ae9e2a
			if (error < 0)
Packit ae9e2a
				return error;
Packit ae9e2a
Packit ae9e2a
			found = true;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!found)
Packit ae9e2a
		return GIT_ENOTFOUND;
Packit ae9e2a
Packit ae9e2a
	if (git_odb__strict_hash_verification) {
Packit ae9e2a
		if ((error = git_odb_hash(&hashed, raw.data, raw.len, raw.type)) < 0)
Packit ae9e2a
			goto out;
Packit ae9e2a
Packit ae9e2a
		if (!git_oid_equal(id, &hashed)) {
Packit ae9e2a
			error = git_odb__error_mismatch(id, &hashed);
Packit ae9e2a
			goto out;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	giterr_clear();
Packit ae9e2a
	if ((object = odb_object__alloc(id, &raw)) == NULL)
Packit ae9e2a
		goto out;
Packit ae9e2a
Packit ae9e2a
	*out = git_cache_store_raw(odb_cache(db), object);
Packit ae9e2a
Packit ae9e2a
out:
Packit ae9e2a
	if (error)
Packit ae9e2a
		git__free(raw.data);
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	int error;
Packit ae9e2a
Packit ae9e2a
	assert(out && db && id);
Packit ae9e2a
Packit ae9e2a
	*out = git_cache_get_raw(odb_cache(db), id);
Packit ae9e2a
	if (*out != NULL)
Packit ae9e2a
		return 0;
Packit ae9e2a
Packit ae9e2a
	error = odb_read_1(out, db, id, false);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
Packit ae9e2a
		error = odb_read_1(out, db, id, true);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND)
Packit ae9e2a
		return git_odb__error_notfound("no match for id", id, GIT_OID_HEXSZ);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int odb_otype_fast(git_otype *type_p, git_odb *db, const git_oid *id)
Packit ae9e2a
{
Packit ae9e2a
	git_odb_object *object;
Packit ae9e2a
	size_t _unused;
Packit ae9e2a
	int error;
Packit ae9e2a
Packit ae9e2a
	if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
Packit ae9e2a
		*type_p = object->cached.type;
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
	
Packit ae9e2a
	error = odb_read_header_1(&_unused, type_p, db, id, false);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_PASSTHROUGH) {
Packit ae9e2a
		error = odb_read_1(&object, db, id, false);
Packit ae9e2a
		if (!error)
Packit ae9e2a
			*type_p = object->cached.type;
Packit ae9e2a
		git_odb_object_free(object);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int read_prefix_1(git_odb_object **out, git_odb *db,
Packit ae9e2a
		const git_oid *key, size_t len, bool only_refreshed)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	int error = 0;
Packit ae9e2a
	git_oid found_full_oid = {{0}};
Packit ae9e2a
	git_rawobj raw = {0};
Packit ae9e2a
	void *data = NULL;
Packit ae9e2a
	bool found = false;
Packit ae9e2a
	git_odb_object *object;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (only_refreshed && !b->refresh)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->read_prefix != NULL) {
Packit ae9e2a
			git_oid full_oid;
Packit ae9e2a
			error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, key, len);
Packit ae9e2a
Packit ae9e2a
			if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) {
Packit ae9e2a
				error = 0;
Packit ae9e2a
				continue;
Packit ae9e2a
			}
Packit ae9e2a
Packit ae9e2a
			if (error)
Packit ae9e2a
				goto out;
Packit ae9e2a
Packit ae9e2a
			git__free(data);
Packit ae9e2a
			data = raw.data;
Packit ae9e2a
Packit ae9e2a
			if (found && git_oid__cmp(&full_oid, &found_full_oid)) {
Packit ae9e2a
				git_buf buf = GIT_BUF_INIT;
Packit ae9e2a
Packit ae9e2a
				git_buf_printf(&buf, "multiple matches for prefix: %s",
Packit ae9e2a
					git_oid_tostr_s(&full_oid));
Packit ae9e2a
				git_buf_printf(&buf, " %s",
Packit ae9e2a
					git_oid_tostr_s(&found_full_oid));
Packit ae9e2a
Packit ae9e2a
				error = git_odb__error_ambiguous(buf.ptr);
Packit ae9e2a
				git_buf_free(&buf;;
Packit ae9e2a
				goto out;
Packit ae9e2a
			}
Packit ae9e2a
Packit ae9e2a
			found_full_oid = full_oid;
Packit ae9e2a
			found = true;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!found)
Packit ae9e2a
		return GIT_ENOTFOUND;
Packit ae9e2a
Packit ae9e2a
	if (git_odb__strict_hash_verification) {
Packit ae9e2a
		git_oid hash;
Packit ae9e2a
Packit ae9e2a
		if ((error = git_odb_hash(&hash, raw.data, raw.len, raw.type)) < 0)
Packit ae9e2a
			goto out;
Packit ae9e2a
Packit ae9e2a
		if (!git_oid_equal(&found_full_oid, &hash)) {
Packit ae9e2a
			error = git_odb__error_mismatch(&found_full_oid, &hash);
Packit ae9e2a
			goto out;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL)
Packit ae9e2a
		goto out;
Packit ae9e2a
Packit ae9e2a
	*out = git_cache_store_raw(odb_cache(db), object);
Packit ae9e2a
Packit ae9e2a
out:
Packit ae9e2a
	if (error)
Packit ae9e2a
		git__free(raw.data);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_read_prefix(
Packit ae9e2a
	git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	git_oid key = {{0}};
Packit ae9e2a
	int error;
Packit ae9e2a
Packit ae9e2a
	assert(out && db);
Packit ae9e2a
Packit ae9e2a
	if (len < GIT_OID_MINPREFIXLEN)
Packit ae9e2a
		return git_odb__error_ambiguous("prefix length too short");
Packit ae9e2a
Packit ae9e2a
	if (len > GIT_OID_HEXSZ)
Packit ae9e2a
		len = GIT_OID_HEXSZ;
Packit ae9e2a
Packit ae9e2a
	if (len == GIT_OID_HEXSZ) {
Packit ae9e2a
		*out = git_cache_get_raw(odb_cache(db), short_id);
Packit ae9e2a
		if (*out != NULL)
Packit ae9e2a
			return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_oid__cpy_prefix(&key, short_id, len);
Packit ae9e2a
Packit ae9e2a
	error = read_prefix_1(out, db, &key, len, false);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND && !git_odb_refresh(db))
Packit ae9e2a
		error = read_prefix_1(out, db, &key, len, true);
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_ENOTFOUND)
Packit ae9e2a
		return git_odb__error_notfound("no match for prefix", &key, len);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
Packit ae9e2a
{
Packit ae9e2a
	unsigned int i;
Packit ae9e2a
	backend_internal *internal;
Packit ae9e2a
Packit ae9e2a
	git_vector_foreach(&db->backends, i, internal) {
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
		int error = b->foreach(b, cb, payload);
Packit ae9e2a
		if (error < 0)
Packit ae9e2a
			return error;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_write(
Packit ae9e2a
	git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	int error = GIT_ERROR;
Packit ae9e2a
	git_odb_stream *stream;
Packit ae9e2a
Packit ae9e2a
	assert(oid && db);
Packit ae9e2a
Packit ae9e2a
	git_odb_hash(oid, data, len, type);
Packit ae9e2a
	if (git_odb__freshen(db, oid))
Packit ae9e2a
		return 0;
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && error < 0; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		/* we don't write in alternates! */
Packit ae9e2a
		if (internal->is_alternate)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->write != NULL)
Packit ae9e2a
			error = b->write(b, oid, data, len, type);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!error || error == GIT_PASSTHROUGH)
Packit ae9e2a
		return 0;
Packit ae9e2a
Packit ae9e2a
	/* if no backends were able to write the object directly, we try a
Packit ae9e2a
	 * streaming write to the backends; just write the whole object into the
Packit ae9e2a
	 * stream in one push
Packit ae9e2a
	 */
Packit ae9e2a
	if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
Packit ae9e2a
		return error;
Packit ae9e2a
Packit ae9e2a
	stream->write(stream, data, len);
Packit ae9e2a
	error = stream->finalize_write(stream, oid);
Packit ae9e2a
	git_odb_stream_free(stream);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void hash_header(git_hash_ctx *ctx, git_off_t size, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	char header[64];
Packit ae9e2a
	int hdrlen;
Packit ae9e2a
Packit ae9e2a
	hdrlen = git_odb__format_object_header(header, sizeof(header), size, type);
Packit ae9e2a
	git_hash_update(ctx, header, hdrlen);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_open_wstream(
Packit ae9e2a
	git_odb_stream **stream, git_odb *db, git_off_t size, git_otype type)
Packit ae9e2a
{
Packit ae9e2a
	size_t i, writes = 0;
Packit ae9e2a
	int error = GIT_ERROR;
Packit ae9e2a
	git_hash_ctx *ctx = NULL;
Packit ae9e2a
Packit ae9e2a
	assert(stream && db);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && error < 0; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		/* we don't write in alternates! */
Packit ae9e2a
		if (internal->is_alternate)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->writestream != NULL) {
Packit ae9e2a
			++writes;
Packit ae9e2a
			error = b->writestream(stream, b, size, type);
Packit ae9e2a
		} else if (b->write != NULL) {
Packit ae9e2a
			++writes;
Packit ae9e2a
			error = init_fake_wstream(stream, b, size, type);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (error < 0) {
Packit ae9e2a
		if (error == GIT_PASSTHROUGH)
Packit ae9e2a
			error = 0;
Packit ae9e2a
		else if (!writes)
Packit ae9e2a
			error = git_odb__error_unsupported_in_backend("write object");
Packit ae9e2a
Packit ae9e2a
		goto done;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	ctx = git__malloc(sizeof(git_hash_ctx));
Packit ae9e2a
	GITERR_CHECK_ALLOC(ctx);
Packit ae9e2a
Packit ae9e2a
	if ((error = git_hash_ctx_init(ctx)) < 0)
Packit ae9e2a
		goto done;
Packit ae9e2a
Packit ae9e2a
	hash_header(ctx, size, type);
Packit ae9e2a
	(*stream)->hash_ctx = ctx;
Packit ae9e2a
Packit ae9e2a
	(*stream)->declared_size = size;
Packit ae9e2a
	(*stream)->received_bytes = 0;
Packit ae9e2a
Packit ae9e2a
done:
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int git_odb_stream__invalid_length(
Packit ae9e2a
	const git_odb_stream *stream,
Packit ae9e2a
	const char *action)
Packit ae9e2a
{
Packit ae9e2a
	giterr_set(GITERR_ODB,
Packit ae9e2a
		"cannot %s - "
Packit ae9e2a
		"Invalid length. %"PRIdZ" was expected. The "
Packit ae9e2a
		"total size of the received chunks amounts to %"PRIdZ".",
Packit ae9e2a
		action, stream->declared_size, stream->received_bytes);
Packit ae9e2a
Packit ae9e2a
	return -1;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	git_hash_update(stream->hash_ctx, buffer, len);
Packit ae9e2a
Packit ae9e2a
	stream->received_bytes += len;
Packit ae9e2a
Packit ae9e2a
	if (stream->received_bytes > stream->declared_size)
Packit ae9e2a
		return git_odb_stream__invalid_length(stream,
Packit ae9e2a
			"stream_write()");
Packit ae9e2a
Packit ae9e2a
	return stream->write(stream, buffer, len);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
Packit ae9e2a
{
Packit ae9e2a
	if (stream->received_bytes != stream->declared_size)
Packit ae9e2a
		return git_odb_stream__invalid_length(stream,
Packit ae9e2a
			"stream_finalize_write()");
Packit ae9e2a
Packit ae9e2a
	git_hash_final(out, stream->hash_ctx);
Packit ae9e2a
Packit ae9e2a
	if (git_odb__freshen(stream->backend->odb, out))
Packit ae9e2a
		return 0;
Packit ae9e2a
Packit ae9e2a
	return stream->finalize_write(stream, out);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	return stream->read(stream, buffer, len);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void git_odb_stream_free(git_odb_stream *stream)
Packit ae9e2a
{
Packit ae9e2a
	if (stream == NULL)
Packit ae9e2a
		return;
Packit ae9e2a
Packit ae9e2a
	git_hash_ctx_cleanup(stream->hash_ctx);
Packit ae9e2a
	git__free(stream->hash_ctx);
Packit ae9e2a
	stream->free(stream);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
Packit ae9e2a
{
Packit ae9e2a
	size_t i, reads = 0;
Packit ae9e2a
	int error = GIT_ERROR;
Packit ae9e2a
Packit ae9e2a
	assert(stream && db);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && error < 0; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (b->readstream != NULL) {
Packit ae9e2a
			++reads;
Packit ae9e2a
			error = b->readstream(stream, b, oid);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_PASSTHROUGH)
Packit ae9e2a
		error = 0;
Packit ae9e2a
	if (error < 0 && !reads)
Packit ae9e2a
		error = git_odb__error_unsupported_in_backend("read object streamed");
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer_progress_cb progress_cb, void *progress_payload)
Packit ae9e2a
{
Packit ae9e2a
	size_t i, writes = 0;
Packit ae9e2a
	int error = GIT_ERROR;
Packit ae9e2a
Packit ae9e2a
	assert(out && db);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length && error < 0; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		/* we don't write in alternates! */
Packit ae9e2a
		if (internal->is_alternate)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (b->writepack != NULL) {
Packit ae9e2a
			++writes;
Packit ae9e2a
			error = b->writepack(out, b, db, progress_cb, progress_payload);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (error == GIT_PASSTHROUGH)
Packit ae9e2a
		error = 0;
Packit ae9e2a
	if (error < 0 && !writes)
Packit ae9e2a
		error = git_odb__error_unsupported_in_backend("write pack");
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	GIT_UNUSED(backend);
Packit ae9e2a
	return git__malloc(len);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_refresh(struct git_odb *db)
Packit ae9e2a
{
Packit ae9e2a
	size_t i;
Packit ae9e2a
	assert(db);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < db->backends.length; ++i) {
Packit ae9e2a
		backend_internal *internal = git_vector_get(&db->backends, i);
Packit ae9e2a
		git_odb_backend *b = internal->backend;
Packit ae9e2a
Packit ae9e2a
		if (b->refresh != NULL) {
Packit ae9e2a
			int error = b->refresh(b);
Packit ae9e2a
			if (error < 0)
Packit ae9e2a
				return error;
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__error_mismatch(const git_oid *expected, const git_oid *actual)
Packit ae9e2a
{
Packit ae9e2a
	char expected_oid[GIT_OID_HEXSZ + 1], actual_oid[GIT_OID_HEXSZ + 1];
Packit ae9e2a
Packit ae9e2a
	git_oid_tostr(expected_oid, sizeof(expected_oid), expected);
Packit ae9e2a
	git_oid_tostr(actual_oid, sizeof(actual_oid), actual);
Packit ae9e2a
Packit ae9e2a
	giterr_set(GITERR_ODB, "object hash mismatch - expected %s but got %s",
Packit ae9e2a
		expected_oid, actual_oid);
Packit ae9e2a
Packit ae9e2a
	return GIT_EMISMATCH;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__error_notfound(
Packit ae9e2a
	const char *message, const git_oid *oid, size_t oid_len)
Packit ae9e2a
{
Packit ae9e2a
	if (oid != NULL) {
Packit ae9e2a
		char oid_str[GIT_OID_HEXSZ + 1];
Packit ae9e2a
		git_oid_tostr(oid_str, oid_len+1, oid);
Packit ae9e2a
		giterr_set(GITERR_ODB, "object not found - %s (%.*s)",
Packit ae9e2a
			message, (int) oid_len, oid_str);
Packit ae9e2a
	} else
Packit ae9e2a
		giterr_set(GITERR_ODB, "object not found - %s", message);
Packit ae9e2a
Packit ae9e2a
	return GIT_ENOTFOUND;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb__error_ambiguous(const char *message)
Packit ae9e2a
{
Packit ae9e2a
	giterr_set(GITERR_ODB, "ambiguous SHA1 prefix - %s", message);
Packit ae9e2a
	return GIT_EAMBIGUOUS;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_odb_init_backend(git_odb_backend *backend, unsigned int version)
Packit ae9e2a
{
Packit ae9e2a
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Packit ae9e2a
		backend, version, git_odb_backend, GIT_ODB_BACKEND_INIT);
Packit ae9e2a
	return 0;
Packit ae9e2a
}