Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 2 | #include "blob.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | |
| 5 | const char *blob_type = "blob"; |
| 6 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 7 | struct blob *lookup_blob(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 8 | { |
| 9 | struct object *obj = lookup_object(sha1); |
| 10 | if (!obj) { |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 11 | struct blob *ret = alloc_blob_node(); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 12 | created_object(sha1, &ret->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 13 | ret->object.type = OBJ_BLOB; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 14 | return ret; |
| 15 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 16 | if (!obj->type) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 17 | obj->type = OBJ_BLOB; |
| 18 | if (obj->type != OBJ_BLOB) { |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 19 | error("Object %s is a %s, not a blob", |
| 20 | sha1_to_hex(sha1), typename(obj->type)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 21 | return NULL; |
| 22 | } |
| 23 | return (struct blob *) obj; |
| 24 | } |
Daniel Barkalow | a510bfa | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 25 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 26 | int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size) |
| 27 | { |
| 28 | item->object.parsed = 1; |
| 29 | return 0; |
| 30 | } |
| 31 | |
Daniel Barkalow | a510bfa | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 32 | int parse_blob(struct blob *item) |
| 33 | { |
| 34 | char type[20]; |
| 35 | void *buffer; |
| 36 | unsigned long size; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 37 | int ret; |
| 38 | |
Daniel Barkalow | a510bfa | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 39 | if (item->object.parsed) |
| 40 | return 0; |
Daniel Barkalow | a510bfa | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 41 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 42 | if (!buffer) |
| 43 | return error("Could not read %s", |
| 44 | sha1_to_hex(item->object.sha1)); |
| 45 | if (strcmp(type, blob_type)) |
| 46 | return error("Object %s not a blob", |
| 47 | sha1_to_hex(item->object.sha1)); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 48 | ret = parse_blob_buffer(item, buffer, size); |
| 49 | free(buffer); |
| 50 | return ret; |
Daniel Barkalow | a510bfa | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 51 | } |