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