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