]> git.proxmox.com Git - libgit2.git/blob - src/odb.c
remote: create FETCH_HEAD with a refspecless remote
[libgit2.git] / src / odb.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include "common.h"
9 #include <zlib.h>
10 #include "git2/object.h"
11 #include "git2/sys/odb_backend.h"
12 #include "fileops.h"
13 #include "hash.h"
14 #include "odb.h"
15 #include "delta-apply.h"
16 #include "filter.h"
17 #include "repository.h"
18
19 #include "git2/odb_backend.h"
20 #include "git2/oid.h"
21
22 #define GIT_ALTERNATES_FILE "info/alternates"
23
24 /* TODO: is this correct? */
25 #define GIT_LOOSE_PRIORITY 2
26 #define GIT_PACKED_PRIORITY 1
27
28 #define GIT_ALTERNATES_MAX_DEPTH 5
29
30 typedef struct
31 {
32 git_odb_backend *backend;
33 int priority;
34 bool is_alternate;
35 ino_t disk_inode;
36 } backend_internal;
37
38 static git_cache *odb_cache(git_odb *odb)
39 {
40 if (odb->rc.owner != NULL) {
41 git_repository *owner = odb->rc.owner;
42 return &owner->objects;
43 }
44
45 return &odb->own_cache;
46 }
47
48 static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth);
49
50 int git_odb__format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type)
51 {
52 const char *type_str = git_object_type2string(obj_type);
53 int len = p_snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len);
54 assert(len > 0 && len <= (int)n);
55 return len+1;
56 }
57
58 int git_odb__hashobj(git_oid *id, git_rawobj *obj)
59 {
60 git_buf_vec vec[2];
61 char header[64];
62 int hdrlen;
63
64 assert(id && obj);
65
66 if (!git_object_typeisloose(obj->type))
67 return -1;
68
69 if (!obj->data && obj->len != 0)
70 return -1;
71
72 hdrlen = git_odb__format_object_header(header, sizeof(header), obj->len, obj->type);
73
74 vec[0].data = header;
75 vec[0].len = hdrlen;
76 vec[1].data = obj->data;
77 vec[1].len = obj->len;
78
79 git_hash_vec(id, vec, 2);
80
81 return 0;
82 }
83
84
85 static git_odb_object *odb_object__alloc(const git_oid *oid, git_rawobj *source)
86 {
87 git_odb_object *object = git__calloc(1, sizeof(git_odb_object));
88
89 if (object != NULL) {
90 git_oid_cpy(&object->cached.oid, oid);
91 object->cached.type = source->type;
92 object->cached.size = source->len;
93 object->buffer = source->data;
94 }
95
96 return object;
97 }
98
99 void git_odb_object__free(void *object)
100 {
101 if (object != NULL) {
102 git__free(((git_odb_object *)object)->buffer);
103 git__free(object);
104 }
105 }
106
107 const git_oid *git_odb_object_id(git_odb_object *object)
108 {
109 return &object->cached.oid;
110 }
111
112 const void *git_odb_object_data(git_odb_object *object)
113 {
114 return object->buffer;
115 }
116
117 size_t git_odb_object_size(git_odb_object *object)
118 {
119 return object->cached.size;
120 }
121
122 git_otype git_odb_object_type(git_odb_object *object)
123 {
124 return object->cached.type;
125 }
126
127 int git_odb_object_dup(git_odb_object **dest, git_odb_object *source)
128 {
129 git_cached_obj_incref(source);
130 *dest = source;
131 return 0;
132 }
133
134 void git_odb_object_free(git_odb_object *object)
135 {
136 if (object == NULL)
137 return;
138
139 git_cached_obj_decref(object);
140 }
141
142 int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type)
143 {
144 int hdr_len;
145 char hdr[64], buffer[2048];
146 git_hash_ctx ctx;
147 ssize_t read_len = 0;
148 int error = 0;
149
150 if (!git_object_typeisloose(type)) {
151 giterr_set(GITERR_INVALID, "Invalid object type for hash");
152 return -1;
153 }
154
155 if ((error = git_hash_ctx_init(&ctx)) < 0)
156 return -1;
157
158 hdr_len = git_odb__format_object_header(hdr, sizeof(hdr), size, type);
159
160 if ((error = git_hash_update(&ctx, hdr, hdr_len)) < 0)
161 goto done;
162
163 while (size > 0 && (read_len = p_read(fd, buffer, sizeof(buffer))) > 0) {
164 if ((error = git_hash_update(&ctx, buffer, read_len)) < 0)
165 goto done;
166
167 size -= read_len;
168 }
169
170 /* If p_read returned an error code, the read obviously failed.
171 * If size is not zero, the file was truncated after we originally
172 * stat'd it, so we consider this a read failure too */
173 if (read_len < 0 || size > 0) {
174 giterr_set(GITERR_OS, "Error reading file for hashing");
175 error = -1;
176
177 goto done;
178 }
179
180 error = git_hash_final(out, &ctx);
181
182 done:
183 git_hash_ctx_cleanup(&ctx);
184 return error;
185 }
186
187 int git_odb__hashfd_filtered(
188 git_oid *out, git_file fd, size_t size, git_otype type, git_filter_list *fl)
189 {
190 int error;
191 git_buf raw = GIT_BUF_INIT;
192
193 if (!fl)
194 return git_odb__hashfd(out, fd, size, type);
195
196 /* size of data is used in header, so we have to read the whole file
197 * into memory to apply filters before beginning to calculate the hash
198 */
199
200 if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
201 git_buf post = GIT_BUF_INIT;
202
203 error = git_filter_list_apply_to_data(&post, fl, &raw);
204
205 git_buf_free(&raw);
206
207 if (!error)
208 error = git_odb_hash(out, post.ptr, post.size, type);
209
210 git_buf_free(&post);
211 }
212
213 return error;
214 }
215
216 int git_odb__hashlink(git_oid *out, const char *path)
217 {
218 struct stat st;
219 git_off_t size;
220 int result;
221
222 if (git_path_lstat(path, &st) < 0)
223 return -1;
224
225 size = st.st_size;
226
227 if (!git__is_sizet(size)) {
228 giterr_set(GITERR_OS, "File size overflow for 32-bit systems");
229 return -1;
230 }
231
232 if (S_ISLNK(st.st_mode)) {
233 char *link_data;
234 ssize_t read_len;
235
236 link_data = git__malloc((size_t)(size + 1));
237 GITERR_CHECK_ALLOC(link_data);
238
239 read_len = p_readlink(path, link_data, (size_t)size);
240 link_data[size] = '\0';
241 if (read_len != (ssize_t)size) {
242 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", path);
243 git__free(link_data);
244 return -1;
245 }
246
247 result = git_odb_hash(out, link_data, (size_t)size, GIT_OBJ_BLOB);
248 git__free(link_data);
249 } else {
250 int fd = git_futils_open_ro(path);
251 if (fd < 0)
252 return -1;
253 result = git_odb__hashfd(out, fd, (size_t)size, GIT_OBJ_BLOB);
254 p_close(fd);
255 }
256
257 return result;
258 }
259
260 int git_odb_hashfile(git_oid *out, const char *path, git_otype type)
261 {
262 git_off_t size;
263 int result, fd = git_futils_open_ro(path);
264 if (fd < 0)
265 return fd;
266
267 if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
268 giterr_set(GITERR_OS, "File size overflow for 32-bit systems");
269 p_close(fd);
270 return -1;
271 }
272
273 result = git_odb__hashfd(out, fd, (size_t)size, type);
274 p_close(fd);
275 return result;
276 }
277
278 int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
279 {
280 git_rawobj raw;
281
282 assert(id);
283
284 raw.data = (void *)data;
285 raw.len = len;
286 raw.type = type;
287
288 return git_odb__hashobj(id, &raw);
289 }
290
291 /**
292 * FAKE WSTREAM
293 */
294
295 typedef struct {
296 git_odb_stream stream;
297 char *buffer;
298 size_t size, written;
299 git_otype type;
300 } fake_wstream;
301
302 static int fake_wstream__fwrite(git_odb_stream *_stream, const git_oid *oid)
303 {
304 fake_wstream *stream = (fake_wstream *)_stream;
305 return _stream->backend->write(_stream->backend, oid, stream->buffer, stream->size, stream->type);
306 }
307
308 static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len)
309 {
310 fake_wstream *stream = (fake_wstream *)_stream;
311
312 if (stream->written + len > stream->size)
313 return -1;
314
315 memcpy(stream->buffer + stream->written, data, len);
316 stream->written += len;
317 return 0;
318 }
319
320 static void fake_wstream__free(git_odb_stream *_stream)
321 {
322 fake_wstream *stream = (fake_wstream *)_stream;
323
324 git__free(stream->buffer);
325 git__free(stream);
326 }
327
328 static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, size_t size, git_otype type)
329 {
330 fake_wstream *stream;
331
332 stream = git__calloc(1, sizeof(fake_wstream));
333 GITERR_CHECK_ALLOC(stream);
334
335 stream->size = size;
336 stream->type = type;
337 stream->buffer = git__malloc(size);
338 if (stream->buffer == NULL) {
339 git__free(stream);
340 return -1;
341 }
342
343 stream->stream.backend = backend;
344 stream->stream.read = NULL; /* read only */
345 stream->stream.write = &fake_wstream__write;
346 stream->stream.finalize_write = &fake_wstream__fwrite;
347 stream->stream.free = &fake_wstream__free;
348 stream->stream.mode = GIT_STREAM_WRONLY;
349
350 *stream_p = (git_odb_stream *)stream;
351 return 0;
352 }
353
354 /***********************************************************
355 *
356 * OBJECT DATABASE PUBLIC API
357 *
358 * Public calls for the ODB functionality
359 *
360 ***********************************************************/
361
362 static int backend_sort_cmp(const void *a, const void *b)
363 {
364 const backend_internal *backend_a = (const backend_internal *)(a);
365 const backend_internal *backend_b = (const backend_internal *)(b);
366
367 if (backend_a->is_alternate == backend_b->is_alternate)
368 return (backend_b->priority - backend_a->priority);
369
370 return backend_a->is_alternate ? 1 : -1;
371 }
372
373 int git_odb_new(git_odb **out)
374 {
375 git_odb *db = git__calloc(1, sizeof(*db));
376 GITERR_CHECK_ALLOC(db);
377
378 if (git_cache_init(&db->own_cache) < 0 ||
379 git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) {
380 git__free(db);
381 return -1;
382 }
383
384 *out = db;
385 GIT_REFCOUNT_INC(db);
386 return 0;
387 }
388
389 static int add_backend_internal(
390 git_odb *odb, git_odb_backend *backend,
391 int priority, bool is_alternate, ino_t disk_inode)
392 {
393 backend_internal *internal;
394
395 assert(odb && backend);
396
397 GITERR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
398
399 /* Check if the backend is already owned by another ODB */
400 assert(!backend->odb || backend->odb == odb);
401
402 internal = git__malloc(sizeof(backend_internal));
403 GITERR_CHECK_ALLOC(internal);
404
405 internal->backend = backend;
406 internal->priority = priority;
407 internal->is_alternate = is_alternate;
408 internal->disk_inode = disk_inode;
409
410 if (git_vector_insert(&odb->backends, internal) < 0) {
411 git__free(internal);
412 return -1;
413 }
414
415 git_vector_sort(&odb->backends);
416 internal->backend->odb = odb;
417 return 0;
418 }
419
420 int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority)
421 {
422 return add_backend_internal(odb, backend, priority, false, 0);
423 }
424
425 int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
426 {
427 return add_backend_internal(odb, backend, priority, true, 0);
428 }
429
430 size_t git_odb_num_backends(git_odb *odb)
431 {
432 assert(odb);
433 return odb->backends.length;
434 }
435
436 static int git_odb__error_unsupported_in_backend(const char *action)
437 {
438 giterr_set(GITERR_ODB,
439 "Cannot %s - unsupported in the loaded odb backends", action);
440 return -1;
441 }
442
443
444 int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos)
445 {
446 backend_internal *internal;
447
448 assert(odb && odb);
449 internal = git_vector_get(&odb->backends, pos);
450
451 if (internal && internal->backend) {
452 *out = internal->backend;
453 return 0;
454 }
455
456 giterr_set(GITERR_ODB, "No ODB backend loaded at index %" PRIuZ, pos);
457 return GIT_ENOTFOUND;
458 }
459
460 static int add_default_backends(
461 git_odb *db, const char *objects_dir,
462 bool as_alternates, int alternate_depth)
463 {
464 size_t i;
465 struct stat st;
466 ino_t inode;
467 git_odb_backend *loose, *packed;
468
469 /* TODO: inodes are not really relevant on Win32, so we need to find
470 * a cross-platform workaround for this */
471 #ifdef GIT_WIN32
472 GIT_UNUSED(i);
473 GIT_UNUSED(st);
474
475 inode = 0;
476 #else
477 if (p_stat(objects_dir, &st) < 0) {
478 if (as_alternates)
479 return 0;
480
481 giterr_set(GITERR_ODB, "Failed to load object database in '%s'", objects_dir);
482 return -1;
483 }
484
485 inode = st.st_ino;
486
487 for (i = 0; i < db->backends.length; ++i) {
488 backend_internal *backend = git_vector_get(&db->backends, i);
489 if (backend->disk_inode == inode)
490 return 0;
491 }
492 #endif
493
494 /* add the loose object backend */
495 if (git_odb_backend_loose(&loose, objects_dir, -1, 0) < 0 ||
496 add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
497 return -1;
498
499 /* add the packed file backend */
500 if (git_odb_backend_pack(&packed, objects_dir) < 0 ||
501 add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates, inode) < 0)
502 return -1;
503
504 return load_alternates(db, objects_dir, alternate_depth);
505 }
506
507 static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth)
508 {
509 git_buf alternates_path = GIT_BUF_INIT;
510 git_buf alternates_buf = GIT_BUF_INIT;
511 char *buffer;
512 const char *alternate;
513 int result = 0;
514
515 /* Git reports an error, we just ignore anything deeper */
516 if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH)
517 return 0;
518
519 if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0)
520 return -1;
521
522 if (git_path_exists(alternates_path.ptr) == false) {
523 git_buf_free(&alternates_path);
524 return 0;
525 }
526
527 if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) {
528 git_buf_free(&alternates_path);
529 return -1;
530 }
531
532 buffer = (char *)alternates_buf.ptr;
533
534 /* add each alternate as a new backend; one alternate per line */
535 while ((alternate = git__strtok(&buffer, "\r\n")) != NULL) {
536 if (*alternate == '\0' || *alternate == '#')
537 continue;
538
539 /*
540 * Relative path: build based on the current `objects`
541 * folder. However, relative paths are only allowed in
542 * the current repository.
543 */
544 if (*alternate == '.' && !alternate_depth) {
545 if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0)
546 break;
547 alternate = git_buf_cstr(&alternates_path);
548 }
549
550 if ((result = add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0)
551 break;
552 }
553
554 git_buf_free(&alternates_path);
555 git_buf_free(&alternates_buf);
556
557 return result;
558 }
559
560 int git_odb_add_disk_alternate(git_odb *odb, const char *path)
561 {
562 return add_default_backends(odb, path, true, 0);
563 }
564
565 int git_odb_open(git_odb **out, const char *objects_dir)
566 {
567 git_odb *db;
568
569 assert(out && objects_dir);
570
571 *out = NULL;
572
573 if (git_odb_new(&db) < 0)
574 return -1;
575
576 if (add_default_backends(db, objects_dir, 0, 0) < 0) {
577 git_odb_free(db);
578 return -1;
579 }
580
581 *out = db;
582 return 0;
583 }
584
585 static void odb_free(git_odb *db)
586 {
587 size_t i;
588
589 for (i = 0; i < db->backends.length; ++i) {
590 backend_internal *internal = git_vector_get(&db->backends, i);
591 git_odb_backend *backend = internal->backend;
592
593 if (backend->free) backend->free(backend);
594 else git__free(backend);
595
596 git__free(internal);
597 }
598
599 git_vector_free(&db->backends);
600 git_cache_free(&db->own_cache);
601
602 git__memzero(db, sizeof(*db));
603 git__free(db);
604 }
605
606 void git_odb_free(git_odb *db)
607 {
608 if (db == NULL)
609 return;
610
611 GIT_REFCOUNT_DEC(db, odb_free);
612 }
613
614 int git_odb_exists(git_odb *db, const git_oid *id)
615 {
616 git_odb_object *object;
617 size_t i;
618 bool found = false;
619
620 assert(db && id);
621
622 if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
623 git_odb_object_free(object);
624 return (int)true;
625 }
626
627 for (i = 0; i < db->backends.length && !found; ++i) {
628 backend_internal *internal = git_vector_get(&db->backends, i);
629 git_odb_backend *b = internal->backend;
630
631 if (b->exists != NULL)
632 found = (bool)b->exists(b, id);
633 }
634
635 return (int)found;
636 }
637
638 int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)
639 {
640 int error;
641 git_odb_object *object;
642
643 error = git_odb__read_header_or_object(&object, len_p, type_p, db, id);
644
645 if (object)
646 git_odb_object_free(object);
647
648 return error;
649 }
650
651 int git_odb__read_header_or_object(
652 git_odb_object **out, size_t *len_p, git_otype *type_p,
653 git_odb *db, const git_oid *id)
654 {
655 size_t i;
656 int error = GIT_ENOTFOUND;
657 git_odb_object *object;
658
659 assert(db && id && out && len_p && type_p);
660
661 if ((object = git_cache_get_raw(odb_cache(db), id)) != NULL) {
662 *len_p = object->cached.size;
663 *type_p = object->cached.type;
664 *out = object;
665 return 0;
666 }
667
668 *out = NULL;
669
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;
673
674 if (b->read_header != NULL)
675 error = b->read_header(len_p, type_p, b, id);
676 }
677
678 if (!error || error == GIT_PASSTHROUGH)
679 return 0;
680
681 /*
682 * no backend could read only the header.
683 * try reading the whole object and freeing the contents
684 */
685 if ((error = git_odb_read(&object, db, id)) < 0)
686 return error; /* error already set - pass along */
687
688 *len_p = object->cached.size;
689 *type_p = object->cached.type;
690 *out = object;
691
692 return 0;
693 }
694
695 int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
696 {
697 size_t i, reads = 0;
698 int error;
699 git_rawobj raw;
700 git_odb_object *object;
701
702 assert(out && db && id);
703
704 *out = git_cache_get_raw(odb_cache(db), id);
705 if (*out != NULL)
706 return 0;
707
708 error = GIT_ENOTFOUND;
709
710 for (i = 0; i < db->backends.length && error < 0; ++i) {
711 backend_internal *internal = git_vector_get(&db->backends, i);
712 git_odb_backend *b = internal->backend;
713
714 if (b->read != NULL) {
715 ++reads;
716 error = b->read(&raw.data, &raw.len, &raw.type, b, id);
717 }
718 }
719
720 if (error && error != GIT_PASSTHROUGH) {
721 if (!reads)
722 return git_odb__error_notfound("no match for id", id);
723 return error;
724 }
725
726 if ((object = odb_object__alloc(id, &raw)) == NULL)
727 return -1;
728
729 *out = git_cache_store_raw(odb_cache(db), object);
730 return 0;
731 }
732
733 int git_odb_read_prefix(
734 git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len)
735 {
736 size_t i;
737 int error = GIT_ENOTFOUND;
738 git_oid found_full_oid = {{0}};
739 git_rawobj raw;
740 void *data = NULL;
741 bool found = false;
742 git_odb_object *object;
743
744 assert(out && db);
745
746 if (len < GIT_OID_MINPREFIXLEN)
747 return git_odb__error_ambiguous("prefix length too short");
748
749 if (len > GIT_OID_HEXSZ)
750 len = GIT_OID_HEXSZ;
751
752 if (len == GIT_OID_HEXSZ) {
753 *out = git_cache_get_raw(odb_cache(db), short_id);
754 if (*out != NULL)
755 return 0;
756 }
757
758 for (i = 0; i < db->backends.length; ++i) {
759 backend_internal *internal = git_vector_get(&db->backends, i);
760 git_odb_backend *b = internal->backend;
761
762 if (b->read_prefix != NULL) {
763 git_oid full_oid;
764 error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, short_id, len);
765 if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH)
766 continue;
767
768 if (error)
769 return error;
770
771 git__free(data);
772 data = raw.data;
773
774 if (found && git_oid__cmp(&full_oid, &found_full_oid)) {
775 git__free(raw.data);
776 return git_odb__error_ambiguous("multiple matches for prefix");
777 }
778
779 found_full_oid = full_oid;
780 found = true;
781 }
782 }
783
784 if (!found)
785 return git_odb__error_notfound("no match for prefix", short_id);
786
787 if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL)
788 return -1;
789
790 *out = git_cache_store_raw(odb_cache(db), object);
791 return 0;
792 }
793
794 int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
795 {
796 unsigned int i;
797 backend_internal *internal;
798
799 git_vector_foreach(&db->backends, i, internal) {
800 git_odb_backend *b = internal->backend;
801 int error = b->foreach(b, cb, payload);
802 if (error < 0)
803 return error;
804 }
805
806 return 0;
807 }
808
809 int git_odb_write(
810 git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type)
811 {
812 size_t i;
813 int error = GIT_ERROR;
814 git_odb_stream *stream;
815
816 assert(oid && db);
817
818 git_odb_hash(oid, data, len, type);
819 if (git_odb_exists(db, oid))
820 return 0;
821
822 for (i = 0; i < db->backends.length && error < 0; ++i) {
823 backend_internal *internal = git_vector_get(&db->backends, i);
824 git_odb_backend *b = internal->backend;
825
826 /* we don't write in alternates! */
827 if (internal->is_alternate)
828 continue;
829
830 if (b->write != NULL)
831 error = b->write(b, oid, data, len, type);
832 }
833
834 if (!error || error == GIT_PASSTHROUGH)
835 return 0;
836
837 /* if no backends were able to write the object directly, we try a
838 * streaming write to the backends; just write the whole object into the
839 * stream in one push
840 */
841 if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
842 return error;
843
844 stream->write(stream, data, len);
845 error = stream->finalize_write(stream, oid);
846 git_odb_stream_free(stream);
847
848 return error;
849 }
850
851 static void hash_header(git_hash_ctx *ctx, size_t size, git_otype type)
852 {
853 char header[64];
854 int hdrlen;
855
856 hdrlen = git_odb__format_object_header(header, sizeof(header), size, type);
857 git_hash_update(ctx, header, hdrlen);
858 }
859
860 int git_odb_open_wstream(
861 git_odb_stream **stream, git_odb *db, size_t size, git_otype type)
862 {
863 size_t i, writes = 0;
864 int error = GIT_ERROR;
865 git_hash_ctx *ctx;
866
867 assert(stream && db);
868
869 for (i = 0; i < db->backends.length && error < 0; ++i) {
870 backend_internal *internal = git_vector_get(&db->backends, i);
871 git_odb_backend *b = internal->backend;
872
873 /* we don't write in alternates! */
874 if (internal->is_alternate)
875 continue;
876
877 if (b->writestream != NULL) {
878 ++writes;
879 error = b->writestream(stream, b, size, type);
880 } else if (b->write != NULL) {
881 ++writes;
882 error = init_fake_wstream(stream, b, size, type);
883 }
884 }
885
886 if (error == GIT_PASSTHROUGH)
887 error = 0;
888 if (error < 0 && !writes)
889 error = git_odb__error_unsupported_in_backend("write object");
890
891 ctx = git__malloc(sizeof(git_hash_ctx));
892 GITERR_CHECK_ALLOC(ctx);
893
894
895 git_hash_ctx_init(ctx);
896 hash_header(ctx, size, type);
897 (*stream)->hash_ctx = ctx;
898
899 (*stream)->declared_size = size;
900 (*stream)->received_bytes = 0;
901
902 return error;
903 }
904
905 static int git_odb_stream__invalid_length(
906 const git_odb_stream *stream,
907 const char *action)
908 {
909 giterr_set(GITERR_ODB,
910 "Cannot %s - "
911 "Invalid length. %"PRIuZ" was expected. The "
912 "total size of the received chunks amounts to %"PRIuZ".",
913 action, stream->declared_size, stream->received_bytes);
914
915 return -1;
916 }
917
918 int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
919 {
920 git_hash_update(stream->hash_ctx, buffer, len);
921
922 stream->received_bytes += len;
923
924 if (stream->received_bytes > stream->declared_size)
925 return git_odb_stream__invalid_length(stream,
926 "stream_write()");
927
928 return stream->write(stream, buffer, len);
929 }
930
931 int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
932 {
933 if (stream->received_bytes != stream->declared_size)
934 return git_odb_stream__invalid_length(stream,
935 "stream_finalize_write()");
936
937 git_hash_final(out, stream->hash_ctx);
938
939 if (git_odb_exists(stream->backend->odb, out))
940 return 0;
941
942 return stream->finalize_write(stream, out);
943 }
944
945 int git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len)
946 {
947 return stream->read(stream, buffer, len);
948 }
949
950 void git_odb_stream_free(git_odb_stream *stream)
951 {
952 git__free(stream->hash_ctx);
953 stream->free(stream);
954 }
955
956 int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
957 {
958 size_t i, reads = 0;
959 int error = GIT_ERROR;
960
961 assert(stream && db);
962
963 for (i = 0; i < db->backends.length && error < 0; ++i) {
964 backend_internal *internal = git_vector_get(&db->backends, i);
965 git_odb_backend *b = internal->backend;
966
967 if (b->readstream != NULL) {
968 ++reads;
969 error = b->readstream(stream, b, oid);
970 }
971 }
972
973 if (error == GIT_PASSTHROUGH)
974 error = 0;
975 if (error < 0 && !reads)
976 error = git_odb__error_unsupported_in_backend("read object streamed");
977
978 return error;
979 }
980
981 int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer_progress_callback progress_cb, void *progress_payload)
982 {
983 size_t i, writes = 0;
984 int error = GIT_ERROR;
985
986 assert(out && db);
987
988 for (i = 0; i < db->backends.length && error < 0; ++i) {
989 backend_internal *internal = git_vector_get(&db->backends, i);
990 git_odb_backend *b = internal->backend;
991
992 /* we don't write in alternates! */
993 if (internal->is_alternate)
994 continue;
995
996 if (b->writepack != NULL) {
997 ++writes;
998 error = b->writepack(out, b, db, progress_cb, progress_payload);
999 }
1000 }
1001
1002 if (error == GIT_PASSTHROUGH)
1003 error = 0;
1004 if (error < 0 && !writes)
1005 error = git_odb__error_unsupported_in_backend("write pack");
1006
1007 return error;
1008 }
1009
1010 void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
1011 {
1012 GIT_UNUSED(backend);
1013 return git__malloc(len);
1014 }
1015
1016 int git_odb_refresh(struct git_odb *db)
1017 {
1018 size_t i;
1019 assert(db);
1020
1021 for (i = 0; i < db->backends.length; ++i) {
1022 backend_internal *internal = git_vector_get(&db->backends, i);
1023 git_odb_backend *b = internal->backend;
1024
1025 if (b->refresh != NULL) {
1026 int error = b->refresh(b);
1027 if (error < 0)
1028 return error;
1029 }
1030 }
1031
1032 return 0;
1033 }
1034
1035 int git_odb__error_notfound(const char *message, const git_oid *oid)
1036 {
1037 if (oid != NULL) {
1038 char oid_str[GIT_OID_HEXSZ + 1];
1039 git_oid_tostr(oid_str, sizeof(oid_str), oid);
1040 giterr_set(GITERR_ODB, "Object not found - %s (%s)", message, oid_str);
1041 } else
1042 giterr_set(GITERR_ODB, "Object not found - %s", message);
1043
1044 return GIT_ENOTFOUND;
1045 }
1046
1047 int git_odb__error_ambiguous(const char *message)
1048 {
1049 giterr_set(GITERR_ODB, "Ambiguous SHA1 prefix - %s", message);
1050 return GIT_EAMBIGUOUS;
1051 }
1052