]>
git.proxmox.com Git - libgit2.git/blob - src/odb.c
2 * Copyright (C) 2009-2012 the libgit2 contributors
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
10 #include "git2/object.h"
14 #include "delta-apply.h"
16 #include "git2/odb_backend.h"
19 #define GIT_ALTERNATES_FILE "info/alternates"
21 /* TODO: is this correct? */
22 #define GIT_LOOSE_PRIORITY 2
23 #define GIT_PACKED_PRIORITY 1
27 git_odb_backend
*backend
;
32 static int format_object_header(char *hdr
, size_t n
, size_t obj_len
, git_otype obj_type
)
34 const char *type_str
= git_object_type2string(obj_type
);
35 int len
= p_snprintf(hdr
, n
, "%s %"PRIuZ
, type_str
, obj_len
);
36 assert(len
> 0 && len
<= (int)n
);
40 int git_odb__hashobj(git_oid
*id
, git_rawobj
*obj
)
48 if (!git_object_typeisloose(obj
->type
))
50 if (!obj
->data
&& obj
->len
!= 0)
53 hdrlen
= format_object_header(header
, sizeof(header
), obj
->len
, obj
->type
);
57 vec
[1].data
= obj
->data
;
58 vec
[1].len
= obj
->len
;
60 git_hash_vec(id
, vec
, 2);
66 static git_odb_object
*new_odb_object(const git_oid
*oid
, git_rawobj
*source
)
68 git_odb_object
*object
= git__malloc(sizeof(git_odb_object
));
69 memset(object
, 0x0, sizeof(git_odb_object
));
71 git_oid_cpy(&object
->cached
.oid
, oid
);
72 memcpy(&object
->raw
, source
, sizeof(git_rawobj
));
77 static void free_odb_object(void *o
)
79 git_odb_object
*object
= (git_odb_object
*)o
;
82 git__free(object
->raw
.data
);
87 const git_oid
*git_odb_object_id(git_odb_object
*object
)
89 return &object
->cached
.oid
;
92 const void *git_odb_object_data(git_odb_object
*object
)
94 return object
->raw
.data
;
97 size_t git_odb_object_size(git_odb_object
*object
)
99 return object
->raw
.len
;
102 git_otype
git_odb_object_type(git_odb_object
*object
)
104 return object
->raw
.type
;
107 void git_odb_object_free(git_odb_object
*object
)
109 git_cached_obj_decref((git_cached_obj
*)object
, &free_odb_object
);
112 int git_odb__hashfd(git_oid
*out
, git_file fd
, size_t size
, git_otype type
)
115 char hdr
[64], buffer
[2048];
118 hdr_len
= format_object_header(hdr
, sizeof(hdr
), size
, type
);
120 ctx
= git_hash_new_ctx();
122 git_hash_update(ctx
, hdr
, hdr_len
);
125 ssize_t read_len
= read(fd
, buffer
, sizeof(buffer
));
128 git_hash_free_ctx(ctx
);
129 giterr_set(GITERR_OS
, "Error reading file");
133 git_hash_update(ctx
, buffer
, read_len
);
137 git_hash_final(out
, ctx
);
138 git_hash_free_ctx(ctx
);
143 int git_odb__hashlink(git_oid
*out
, const char *path
)
149 if (git_path_lstat(path
, &st
) < 0)
154 if (!git__is_sizet(size
)) {
155 giterr_set(GITERR_OS
, "File size overflow for 32-bit systems");
159 if (S_ISLNK(st
.st_mode
)) {
163 link_data
= git__malloc((size_t)size
);
164 GITERR_CHECK_ALLOC(link_data
);
166 read_len
= p_readlink(path
, link_data
, (size_t)(size
+ 1));
167 if (read_len
!= (ssize_t
)size
) {
168 giterr_set(GITERR_OS
, "Failed to read symlink data for '%s'", path
);
172 result
= git_odb_hash(out
, link_data
, (size_t)size
, GIT_OBJ_BLOB
);
173 git__free(link_data
);
175 int fd
= git_futils_open_ro(path
);
178 result
= git_odb__hashfd(out
, fd
, (size_t)size
, GIT_OBJ_BLOB
);
185 int git_odb_hashfile(git_oid
*out
, const char *path
, git_otype type
)
188 int result
, fd
= git_futils_open_ro(path
);
192 if ((size
= git_futils_filesize(fd
)) < 0 || !git__is_sizet(size
)) {
193 giterr_set(GITERR_OS
, "File size overflow for 32-bit systems");
198 result
= git_odb__hashfd(out
, fd
, (size_t)size
, type
);
203 int git_odb_hash(git_oid
*id
, const void *data
, size_t len
, git_otype type
)
209 raw
.data
= (void *)data
;
213 return git_odb__hashobj(id
, &raw
);
221 git_odb_stream stream
;
223 size_t size
, written
;
227 static int fake_wstream__fwrite(git_oid
*oid
, git_odb_stream
*_stream
)
229 fake_wstream
*stream
= (fake_wstream
*)_stream
;
230 return _stream
->backend
->write(oid
, _stream
->backend
, stream
->buffer
, stream
->size
, stream
->type
);
233 static int fake_wstream__write(git_odb_stream
*_stream
, const char *data
, size_t len
)
235 fake_wstream
*stream
= (fake_wstream
*)_stream
;
237 if (stream
->written
+ len
> stream
->size
)
240 memcpy(stream
->buffer
+ stream
->written
, data
, len
);
241 stream
->written
+= len
;
245 static void fake_wstream__free(git_odb_stream
*_stream
)
247 fake_wstream
*stream
= (fake_wstream
*)_stream
;
249 git__free(stream
->buffer
);
253 static int init_fake_wstream(git_odb_stream
**stream_p
, git_odb_backend
*backend
, size_t size
, git_otype type
)
255 fake_wstream
*stream
;
257 stream
= git__calloc(1, sizeof(fake_wstream
));
258 GITERR_CHECK_ALLOC(stream
);
262 stream
->buffer
= git__malloc(size
);
263 if (stream
->buffer
== NULL
) {
268 stream
->stream
.backend
= backend
;
269 stream
->stream
.read
= NULL
; /* read only */
270 stream
->stream
.write
= &fake_wstream__write
;
271 stream
->stream
.finalize_write
= &fake_wstream__fwrite
;
272 stream
->stream
.free
= &fake_wstream__free
;
273 stream
->stream
.mode
= GIT_STREAM_WRONLY
;
275 *stream_p
= (git_odb_stream
*)stream
;
279 /***********************************************************
281 * OBJECT DATABASE PUBLIC API
283 * Public calls for the ODB functionality
285 ***********************************************************/
287 static int backend_sort_cmp(const void *a
, const void *b
)
289 const backend_internal
*backend_a
= (const backend_internal
*)(a
);
290 const backend_internal
*backend_b
= (const backend_internal
*)(b
);
292 if (backend_a
->is_alternate
== backend_b
->is_alternate
)
293 return (backend_b
->priority
- backend_a
->priority
);
295 return backend_a
->is_alternate
? 1 : -1;
298 int git_odb_new(git_odb
**out
)
300 git_odb
*db
= git__calloc(1, sizeof(*db
));
301 GITERR_CHECK_ALLOC(db
);
303 if (git_cache_init(&db
->cache
, GIT_DEFAULT_CACHE_SIZE
, &free_odb_object
) < 0 ||
304 git_vector_init(&db
->backends
, 4, backend_sort_cmp
) < 0)
311 GIT_REFCOUNT_INC(db
);
315 static int add_backend_internal(git_odb
*odb
, git_odb_backend
*backend
, int priority
, int is_alternate
)
317 backend_internal
*internal
;
319 assert(odb
&& backend
);
321 /* Check if the backend is already owned by another ODB */
322 assert(!backend
->odb
|| backend
->odb
== odb
);
324 internal
= git__malloc(sizeof(backend_internal
));
325 GITERR_CHECK_ALLOC(internal
);
327 internal
->backend
= backend
;
328 internal
->priority
= priority
;
329 internal
->is_alternate
= is_alternate
;
331 if (git_vector_insert(&odb
->backends
, internal
) < 0) {
336 git_vector_sort(&odb
->backends
);
337 internal
->backend
->odb
= odb
;
341 int git_odb_add_backend(git_odb
*odb
, git_odb_backend
*backend
, int priority
)
343 return add_backend_internal(odb
, backend
, priority
, 0);
346 int git_odb_add_alternate(git_odb
*odb
, git_odb_backend
*backend
, int priority
)
348 return add_backend_internal(odb
, backend
, priority
, 1);
351 static int add_default_backends(git_odb
*db
, const char *objects_dir
, int as_alternates
)
353 git_odb_backend
*loose
, *packed
;
355 /* add the loose object backend */
356 if (git_odb_backend_loose(&loose
, objects_dir
, -1, 0) < 0 ||
357 add_backend_internal(db
, loose
, GIT_LOOSE_PRIORITY
, as_alternates
) < 0)
360 /* add the packed file backend */
361 if (git_odb_backend_pack(&packed
, objects_dir
) < 0 ||
362 add_backend_internal(db
, packed
, GIT_PACKED_PRIORITY
, as_alternates
) < 0)
368 static int load_alternates(git_odb
*odb
, const char *objects_dir
)
370 git_buf alternates_path
= GIT_BUF_INIT
;
371 git_buf alternates_buf
= GIT_BUF_INIT
;
373 const char *alternate
;
376 if (git_buf_joinpath(&alternates_path
, objects_dir
, GIT_ALTERNATES_FILE
) < 0)
379 if (git_path_exists(alternates_path
.ptr
) == false) {
380 git_buf_free(&alternates_path
);
384 if (git_futils_readbuffer(&alternates_buf
, alternates_path
.ptr
) < 0) {
385 git_buf_free(&alternates_path
);
389 buffer
= (char *)alternates_buf
.ptr
;
391 /* add each alternate as a new backend; one alternate per line */
392 while ((alternate
= git__strtok(&buffer
, "\r\n")) != NULL
) {
393 if (*alternate
== '\0' || *alternate
== '#')
396 /* relative path: build based on the current `objects` folder */
397 if (*alternate
== '.') {
398 if ((result
= git_buf_joinpath(&alternates_path
, objects_dir
, alternate
)) < 0)
400 alternate
= git_buf_cstr(&alternates_path
);
403 if ((result
= add_default_backends(odb
, alternate
, 1)) < 0)
407 git_buf_free(&alternates_path
);
408 git_buf_free(&alternates_buf
);
413 int git_odb_open(git_odb
**out
, const char *objects_dir
)
417 assert(out
&& objects_dir
);
421 if (git_odb_new(&db
) < 0)
424 if (add_default_backends(db
, objects_dir
, 0) < 0 ||
425 load_alternates(db
, objects_dir
) < 0)
435 static void odb_free(git_odb
*db
)
439 for (i
= 0; i
< db
->backends
.length
; ++i
) {
440 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
441 git_odb_backend
*backend
= internal
->backend
;
443 if (backend
->free
) backend
->free(backend
);
444 else git__free(backend
);
449 git_vector_free(&db
->backends
);
450 git_cache_free(&db
->cache
);
454 void git_odb_free(git_odb
*db
)
459 GIT_REFCOUNT_DEC(db
, odb_free
);
462 int git_odb_exists(git_odb
*db
, const git_oid
*id
)
464 git_odb_object
*object
;
470 if ((object
= git_cache_get(&db
->cache
, id
)) != NULL
) {
471 git_odb_object_free(object
);
475 for (i
= 0; i
< db
->backends
.length
&& !found
; ++i
) {
476 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
477 git_odb_backend
*b
= internal
->backend
;
479 if (b
->exists
!= NULL
)
480 found
= b
->exists(b
, id
);
486 int git_odb_read_header(size_t *len_p
, git_otype
*type_p
, git_odb
*db
, const git_oid
*id
)
489 int error
= GIT_ENOTFOUND
;
490 git_odb_object
*object
;
494 if ((object
= git_cache_get(&db
->cache
, id
)) != NULL
) {
495 *len_p
= object
->raw
.len
;
496 *type_p
= object
->raw
.type
;
497 git_odb_object_free(object
);
501 for (i
= 0; i
< db
->backends
.length
&& error
< 0; ++i
) {
502 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
503 git_odb_backend
*b
= internal
->backend
;
505 if (b
->read_header
!= NULL
)
506 error
= b
->read_header(len_p
, type_p
, b
, id
);
509 if (!error
|| error
== GIT_PASSTHROUGH
)
513 * no backend could read only the header.
514 * try reading the whole object and freeing the contents
516 if ((error
= git_odb_read(&object
, db
, id
)) < 0)
517 return error
; /* error already set - pass along */
519 *len_p
= object
->raw
.len
;
520 *type_p
= object
->raw
.type
;
521 git_odb_object_free(object
);
525 int git_odb_read(git_odb_object
**out
, git_odb
*db
, const git_oid
*id
)
528 int error
= GIT_ENOTFOUND
;
531 assert(out
&& db
&& id
);
533 *out
= git_cache_get(&db
->cache
, id
);
537 for (i
= 0; i
< db
->backends
.length
&& error
< 0; ++i
) {
538 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
539 git_odb_backend
*b
= internal
->backend
;
542 error
= b
->read(&raw
.data
, &raw
.len
, &raw
.type
, b
, id
);
545 /* TODO: If no backends are configured, this returns GIT_ENOTFOUND but
546 * will never have called giterr_set().
549 if (error
&& error
!= GIT_PASSTHROUGH
)
552 *out
= git_cache_try_store(&db
->cache
, new_odb_object(id
, &raw
));
556 int git_odb_read_prefix(
557 git_odb_object
**out
, git_odb
*db
, const git_oid
*short_id
, size_t len
)
560 int error
= GIT_ENOTFOUND
;
561 git_oid found_full_oid
= {{0}};
568 if (len
< GIT_OID_MINPREFIXLEN
)
569 return git_odb__error_ambiguous("prefix length too short");
571 if (len
> GIT_OID_HEXSZ
)
574 if (len
== GIT_OID_HEXSZ
) {
575 *out
= git_cache_get(&db
->cache
, short_id
);
580 for (i
= 0; i
< db
->backends
.length
; ++i
) {
581 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
582 git_odb_backend
*b
= internal
->backend
;
584 if (b
->read
!= NULL
) {
586 error
= b
->read_prefix(&full_oid
, &raw
.data
, &raw
.len
, &raw
.type
, b
, short_id
, len
);
587 if (error
== GIT_ENOTFOUND
|| error
== GIT_PASSTHROUGH
)
595 if (found
&& git_oid_cmp(&full_oid
, &found_full_oid
))
596 return git_odb__error_ambiguous("multiple matches for prefix");
597 found_full_oid
= full_oid
;
603 return git_odb__error_notfound("no match for prefix", short_id
);
605 *out
= git_cache_try_store(&db
->cache
, new_odb_object(&found_full_oid
, &raw
));
609 int git_odb_foreach(git_odb
*db
, int (*cb
)(git_oid
*oid
, void *data
), void *data
)
612 backend_internal
*internal
;
614 git_vector_foreach(&db
->backends
, i
, internal
) {
615 git_odb_backend
*b
= internal
->backend
;
616 int error
= b
->foreach(b
, cb
, data
);
625 git_oid
*oid
, git_odb
*db
, const void *data
, size_t len
, git_otype type
)
628 int error
= GIT_ERROR
;
629 git_odb_stream
*stream
;
633 for (i
= 0; i
< db
->backends
.length
&& error
< 0; ++i
) {
634 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
635 git_odb_backend
*b
= internal
->backend
;
637 /* we don't write in alternates! */
638 if (internal
->is_alternate
)
641 if (b
->write
!= NULL
)
642 error
= b
->write(oid
, b
, data
, len
, type
);
645 if (!error
|| error
== GIT_PASSTHROUGH
)
648 /* if no backends were able to write the object directly, we try a streaming
649 * write to the backends; just write the whole object into the stream in one
652 if ((error
= git_odb_open_wstream(&stream
, db
, len
, type
)) != 0)
655 stream
->write(stream
, data
, len
);
656 error
= stream
->finalize_write(oid
, stream
);
657 stream
->free(stream
);
662 int git_odb_open_wstream(
663 git_odb_stream
**stream
, git_odb
*db
, size_t size
, git_otype type
)
666 int error
= GIT_ERROR
;
668 assert(stream
&& db
);
670 for (i
= 0; i
< db
->backends
.length
&& error
< 0; ++i
) {
671 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
672 git_odb_backend
*b
= internal
->backend
;
674 /* we don't write in alternates! */
675 if (internal
->is_alternate
)
678 if (b
->writestream
!= NULL
)
679 error
= b
->writestream(stream
, b
, size
, type
);
680 else if (b
->write
!= NULL
)
681 error
= init_fake_wstream(stream
, b
, size
, type
);
684 if (error
== GIT_PASSTHROUGH
)
690 int git_odb_open_rstream(git_odb_stream
**stream
, git_odb
*db
, const git_oid
*oid
)
693 int error
= GIT_ERROR
;
695 assert(stream
&& db
);
697 for (i
= 0; i
< db
->backends
.length
&& error
< 0; ++i
) {
698 backend_internal
*internal
= git_vector_get(&db
->backends
, i
);
699 git_odb_backend
*b
= internal
->backend
;
701 if (b
->readstream
!= NULL
)
702 error
= b
->readstream(stream
, b
, oid
);
705 if (error
== GIT_PASSTHROUGH
)
711 void * git_odb_backend_malloc(git_odb_backend
*backend
, size_t len
)
714 return git__malloc(len
);
717 int git_odb__error_notfound(const char *message
, const git_oid
*oid
)
720 char oid_str
[GIT_OID_HEXSZ
+ 1];
721 git_oid_tostr(oid_str
, sizeof(oid_str
), oid
);
722 giterr_set(GITERR_ODB
, "Object not found - %s (%s)", message
, oid_str
);
724 giterr_set(GITERR_ODB
, "Object not found - %s", message
);
726 return GIT_ENOTFOUND
;
729 int git_odb__error_ambiguous(const char *message
)
731 giterr_set(GITERR_ODB
, "Ambiguous SHA1 prefix - %s", message
);
732 return GIT_EAMBIGUOUS
;