blob: bd7d078e1ae5fe4ce0a16fda62a2c1743237941b [file] [log] [blame]
Daniel Barkalow175785e2005-04-18 11:39:48 -07001#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08002#include "blob.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07003
4const char *blob_type = "blob";
5
Jason McMullan5d6ccf52005-06-03 11:05:39 -04006struct blob *lookup_blob(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -07007{
8 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -07009 if (!obj)
10 return create_object(sha1, OBJ_BLOB, alloc_blob_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040011 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070012 obj->type = OBJ_BLOB;
13 if (obj->type != OBJ_BLOB) {
Linus Torvalds885a86a2006-06-14 16:45:13 -070014 error("Object %s is a %s, not a blob",
15 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow175785e2005-04-18 11:39:48 -070016 return NULL;
17 }
18 return (struct blob *) obj;
19}
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070020
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040021int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
22{
23 item->object.parsed = 1;
24 return 0;
25}
26
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070027int parse_blob(struct blob *item)
28{
Nicolas Pitre21666f12007-02-26 14:55:59 -050029 enum object_type type;
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070030 void *buffer;
31 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040032 int ret;
33
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070034 if (item->object.parsed)
35 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -050036 buffer = read_sha1_file(item->object.sha1, &type, &size);
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070037 if (!buffer)
38 return error("Could not read %s",
39 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -050040 if (type != OBJ_BLOB)
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070041 return error("Object %s not a blob",
42 sha1_to_hex(item->object.sha1));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040043 ret = parse_blob_buffer(item, buffer, size);
44 free(buffer);
45 return ret;
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070046}