]> git.proxmox.com Git - libgit2.git/blob - src/odb.c
Merge pull request #184 from nulltoken/repo-error-handling
[libgit2.git] / src / odb.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "common.h"
27 #include "git2/zlib.h"
28 #include "git2/object.h"
29 #include "fileops.h"
30 #include "hash.h"
31 #include "odb.h"
32 #include "delta-apply.h"
33
34 #include "git2/odb_backend.h"
35
36 #define GIT_ALTERNATES_FILE "info/alternates"
37
38 /* TODO: is this correct? */
39 #define GIT_LOOSE_PRIORITY 2
40 #define GIT_PACKED_PRIORITY 1
41
42 typedef struct
43 {
44 git_odb_backend *backend;
45 int priority;
46 int is_alternate;
47 } backend_internal;
48
49 static int format_object_header(char *hdr, size_t n, git_rawobj *obj)
50 {
51 const char *type_str = git_object_type2string(obj->type);
52 int len = snprintf(hdr, n, "%s %"PRIuZ, type_str, obj->len);
53
54 assert(len > 0); /* otherwise snprintf() is broken */
55 assert(((size_t) len) < n); /* otherwise the caller is broken! */
56
57 if (len < 0 || ((size_t) len) >= n)
58 return GIT_ERROR;
59 return len+1;
60 }
61
62 int git_odb__hash_obj(git_oid *id, char *hdr, size_t n, int *len, git_rawobj *obj)
63 {
64 git_buf_vec vec[2];
65 int hdrlen;
66
67 assert(id && hdr && len && obj);
68
69 if (!git_object_typeisloose(obj->type))
70 return GIT_ERROR;
71
72 if (!obj->data && obj->len != 0)
73 return GIT_ERROR;
74
75 if ((hdrlen = format_object_header(hdr, n, obj)) < 0)
76 return GIT_ERROR;
77
78 *len = hdrlen;
79
80 vec[0].data = hdr;
81 vec[0].len = hdrlen;
82 vec[1].data = obj->data;
83 vec[1].len = obj->len;
84
85 git_hash_vec(id, vec, 2);
86
87 return GIT_SUCCESS;
88 }
89
90
91 static git_odb_object *new_odb_object(const git_oid *oid, git_rawobj *source)
92 {
93 git_odb_object *object = git__malloc(sizeof(git_odb_object));
94 memset(object, 0x0, sizeof(git_odb_object));
95
96 git_oid_cpy(&object->cached.oid, oid);
97 memcpy(&object->raw, source, sizeof(git_rawobj));
98
99 return object;
100 }
101
102 static void free_odb_object(void *o)
103 {
104 git_odb_object *object = (git_odb_object *)o;
105
106 if (object != NULL) {
107 free(object->raw.data);
108 free(object);
109 }
110 }
111
112 const git_oid *git_odb_object_id(git_odb_object *object)
113 {
114 return &object->cached.oid;
115 }
116
117 const void *git_odb_object_data(git_odb_object *object)
118 {
119 return object->raw.data;
120 }
121
122 size_t git_odb_object_size(git_odb_object *object)
123 {
124 return object->raw.len;
125 }
126
127 git_otype git_odb_object_type(git_odb_object *object)
128 {
129 return object->raw.type;
130 }
131
132 void git_odb_object_close(git_odb_object *object)
133 {
134 git_cached_obj_decref((git_cached_obj *)object, &free_odb_object);
135 }
136
137 int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
138 {
139 char hdr[64];
140 int hdrlen;
141 git_rawobj raw;
142
143 assert(id);
144
145 raw.data = (void *)data;
146 raw.len = len;
147 raw.type = type;
148
149 return git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, &raw);
150 }
151
152 /**
153 * FAKE WSTREAM
154 */
155
156 typedef struct {
157 git_odb_stream stream;
158 char *buffer;
159 size_t size, written;
160 git_otype type;
161 } fake_wstream;
162
163 static int fake_wstream__fwrite(git_oid *oid, git_odb_stream *_stream)
164 {
165 fake_wstream *stream = (fake_wstream *)_stream;
166 return _stream->backend->write(oid, _stream->backend, stream->buffer, stream->size, stream->type);
167 }
168
169 static int fake_wstream__write(git_odb_stream *_stream, const char *data, size_t len)
170 {
171 fake_wstream *stream = (fake_wstream *)_stream;
172
173 if (stream->written + len >= stream->size)
174 return GIT_ENOMEM;
175
176 memcpy(stream->buffer + stream->written, data, len);
177 stream->written += len;
178 return GIT_SUCCESS;
179 }
180
181 static void fake_wstream__free(git_odb_stream *_stream)
182 {
183 fake_wstream *stream = (fake_wstream *)_stream;
184
185 free(stream->buffer);
186 free(stream);
187 }
188
189 static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, size_t size, git_otype type)
190 {
191 fake_wstream *stream;
192
193 stream = git__calloc(1, sizeof(fake_wstream));
194 if (stream == NULL)
195 return GIT_ENOMEM;
196
197 stream->size = size;
198 stream->type = type;
199 stream->buffer = git__malloc(size);
200 if (stream->buffer == NULL) {
201 free(stream);
202 return GIT_ENOMEM;
203 }
204
205 stream->stream.backend = backend;
206 stream->stream.read = NULL; /* read only */
207 stream->stream.write = &fake_wstream__write;
208 stream->stream.finalize_write = &fake_wstream__fwrite;
209 stream->stream.free = &fake_wstream__free;
210 stream->stream.mode = GIT_STREAM_WRONLY;
211
212 *stream_p = (git_odb_stream *)stream;
213 return GIT_SUCCESS;
214 }
215
216 /***********************************************************
217 *
218 * OBJECT DATABASE PUBLIC API
219 *
220 * Public calls for the ODB functionality
221 *
222 ***********************************************************/
223
224 static int backend_sort_cmp(const void *a, const void *b)
225 {
226 const backend_internal *backend_a = *(const backend_internal **)(a);
227 const backend_internal *backend_b = *(const backend_internal **)(b);
228
229 if (backend_a->is_alternate == backend_b->is_alternate)
230 return (backend_b->priority - backend_a->priority);
231
232 return backend_a->is_alternate ? 1 : -1;
233 }
234
235 int git_odb_new(git_odb **out)
236 {
237 int error;
238
239 git_odb *db = git__calloc(1, sizeof(*db));
240 if (!db)
241 return GIT_ENOMEM;
242
243 git_cache_init(&db->cache, GIT_DEFAULT_CACHE_SIZE, &free_odb_object);
244 if (error < GIT_SUCCESS)
245 return error;
246
247 if (git_vector_init(&db->backends, 4, backend_sort_cmp) < GIT_SUCCESS) {
248 free(db);
249 return GIT_ENOMEM;
250 }
251
252 *out = db;
253 return GIT_SUCCESS;
254 }
255
256 static int add_backend_internal(git_odb *odb, git_odb_backend *backend, int priority, int is_alternate)
257 {
258 backend_internal *internal;
259
260 assert(odb && backend);
261
262 if (backend->odb != NULL && backend->odb != odb)
263 return GIT_EBUSY;
264
265 internal = git__malloc(sizeof(backend_internal));
266 if (internal == NULL)
267 return GIT_ENOMEM;
268
269 internal->backend = backend;
270 internal->priority = priority;
271 internal->is_alternate = is_alternate;
272
273 if (git_vector_insert(&odb->backends, internal) < 0) {
274 free(internal);
275 return GIT_ENOMEM;
276 }
277
278 git_vector_sort(&odb->backends);
279 internal->backend->odb = odb;
280 return GIT_SUCCESS;
281 }
282
283 int git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority)
284 {
285 return add_backend_internal(odb, backend, priority, 0);
286 }
287
288 int git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority)
289 {
290 return add_backend_internal(odb, backend, priority, 1);
291 }
292
293 static int add_default_backends(git_odb *db, const char *objects_dir, int as_alternates)
294 {
295 git_odb_backend *loose, *packed;
296 int error;
297
298 /* add the loose object backend */
299 error = git_odb_backend_loose(&loose, objects_dir);
300 if (error < GIT_SUCCESS)
301 return error;
302
303 error = add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates);
304 if (error < GIT_SUCCESS)
305 return error;
306
307 /* add the packed file backend */
308 error = git_odb_backend_pack(&packed, objects_dir);
309 if (error < GIT_SUCCESS)
310 return error;
311
312 error = add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates);
313 if (error < GIT_SUCCESS)
314 return error;
315
316 return GIT_SUCCESS;
317 }
318
319 static int load_alternates(git_odb *odb, const char *objects_dir)
320 {
321 char alternates_path[GIT_PATH_MAX];
322 char alternate[GIT_PATH_MAX];
323 char *buffer;
324
325 gitfo_buf alternates_buf = GITFO_BUF_INIT;
326 int error;
327
328 git__joinpath(alternates_path, objects_dir, GIT_ALTERNATES_FILE);
329
330 if (gitfo_exists(alternates_path) < GIT_SUCCESS)
331 return GIT_SUCCESS;
332
333 if (gitfo_read_file(&alternates_buf, alternates_path) < GIT_SUCCESS)
334 return GIT_EOSERR;
335
336 buffer = (char *)alternates_buf.data;
337 error = GIT_SUCCESS;
338
339 /* add each alternate as a new backend; one alternate per line */
340 while ((error == GIT_SUCCESS) && (buffer = git__strtok(alternate, buffer, "\r\n")) != NULL)
341 error = add_default_backends(odb, alternate, 1);
342
343 gitfo_free_buf(&alternates_buf);
344 return error;
345 }
346
347 int git_odb_open(git_odb **out, const char *objects_dir)
348 {
349 git_odb *db;
350 int error;
351
352 assert(out && objects_dir);
353
354 *out = NULL;
355
356 if ((error = git_odb_new(&db)) < 0)
357 return error;
358
359 if ((error = add_default_backends(db, objects_dir, 0)) < GIT_SUCCESS)
360 goto cleanup;
361
362 if ((error = load_alternates(db, objects_dir)) < GIT_SUCCESS)
363 goto cleanup;
364
365 *out = db;
366 return GIT_SUCCESS;
367
368 cleanup:
369 git_odb_close(db);
370 return error;
371 }
372
373 void git_odb_close(git_odb *db)
374 {
375 unsigned int i;
376
377 if (db == NULL)
378 return;
379
380 for (i = 0; i < db->backends.length; ++i) {
381 backend_internal *internal = git_vector_get(&db->backends, i);
382 git_odb_backend *backend = internal->backend;
383
384 if (backend->free) backend->free(backend);
385 else free(backend);
386
387 free(internal);
388 }
389
390 git_vector_free(&db->backends);
391 git_cache_free(&db->cache);
392 free(db);
393 }
394
395 int git_odb_exists(git_odb *db, const git_oid *id)
396 {
397 git_odb_object *object;
398 unsigned int i;
399 int found = 0;
400
401 assert(db && id);
402
403 if ((object = git_cache_get(&db->cache, id)) != NULL) {
404 git_odb_object_close(object);
405 return 1;
406 }
407
408 for (i = 0; i < db->backends.length && !found; ++i) {
409 backend_internal *internal = git_vector_get(&db->backends, i);
410 git_odb_backend *b = internal->backend;
411
412 if (b->exists != NULL)
413 found = b->exists(b, id);
414 }
415
416 return found;
417 }
418
419 int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)
420 {
421 unsigned int i;
422 int error = GIT_ENOTFOUND;
423 git_odb_object *object;
424
425 assert(db && id);
426
427 if ((object = git_cache_get(&db->cache, id)) != NULL) {
428 *len_p = object->raw.len;
429 *type_p = object->raw.type;
430 git_odb_object_close(object);
431 return GIT_SUCCESS;
432 }
433
434 for (i = 0; i < db->backends.length && error < 0; ++i) {
435 backend_internal *internal = git_vector_get(&db->backends, i);
436 git_odb_backend *b = internal->backend;
437
438 if (b->read_header != NULL)
439 error = b->read_header(len_p, type_p, b, id);
440 }
441
442 /*
443 * no backend could read only the header.
444 * try reading the whole object and freeing the contents
445 */
446 if (error < 0) {
447 if ((error = git_odb_read(&object, db, id)) < GIT_SUCCESS)
448 return error;
449
450 *len_p = object->raw.len;
451 *type_p = object->raw.type;
452 git_odb_object_close(object);
453 }
454
455 return GIT_SUCCESS;
456 }
457
458 int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
459 {
460 unsigned int i;
461 int error = GIT_ENOTFOUND;
462 git_rawobj raw;
463
464 assert(out && db && id);
465
466 *out = git_cache_get(&db->cache, id);
467 if (*out != NULL)
468 return GIT_SUCCESS;
469
470 for (i = 0; i < db->backends.length && error < 0; ++i) {
471 backend_internal *internal = git_vector_get(&db->backends, i);
472 git_odb_backend *b = internal->backend;
473
474 if (b->read != NULL)
475 error = b->read(&raw.data, &raw.len, &raw.type, b, id);
476 }
477
478 if (error == GIT_SUCCESS) {
479 *out = git_cache_try_store(&db->cache, new_odb_object(id, &raw));
480 }
481
482 return error;
483 }
484
485 int git_odb_write(git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type)
486 {
487 unsigned int i;
488 int error = GIT_ERROR;
489
490 assert(oid && db);
491
492 for (i = 0; i < db->backends.length && error < 0; ++i) {
493 backend_internal *internal = git_vector_get(&db->backends, i);
494 git_odb_backend *b = internal->backend;
495
496 /* we don't write in alternates! */
497 if (internal->is_alternate)
498 continue;
499
500 if (b->write != NULL)
501 error = b->write(oid, b, data, len, type);
502 }
503
504 /* if no backends were able to write the object directly, we try a streaming
505 * write to the backends; just write the whole object into the stream in one
506 * push */
507 if (error < GIT_SUCCESS) {
508 git_odb_stream *stream;
509
510 if ((error = git_odb_open_wstream(&stream, db, len, type)) == GIT_SUCCESS) {
511 stream->write(stream, data, len);
512 error = stream->finalize_write(oid, stream);
513 stream->free(stream);
514 }
515 }
516
517 return error;
518 }
519
520 int git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_otype type)
521 {
522 unsigned int i;
523 int error = GIT_ERROR;
524
525 assert(stream && db);
526
527 for (i = 0; i < db->backends.length && error < 0; ++i) {
528 backend_internal *internal = git_vector_get(&db->backends, i);
529 git_odb_backend *b = internal->backend;
530
531 /* we don't write in alternates! */
532 if (internal->is_alternate)
533 continue;
534
535 if (b->writestream != NULL)
536 error = b->writestream(stream, b, size, type);
537 else if (b->write != NULL)
538 error = init_fake_wstream(stream, b, size, type);
539 }
540
541 return error;
542 }
543
544 int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
545 {
546 unsigned int i;
547 int error = GIT_ERROR;
548
549 assert(stream && db);
550
551 for (i = 0; i < db->backends.length && error < 0; ++i) {
552 backend_internal *internal = git_vector_get(&db->backends, i);
553 git_odb_backend *b = internal->backend;
554
555 if (b->readstream != NULL)
556 error = b->readstream(stream, b, oid);
557 }
558
559 return error;
560 }
561