blob: ea52ad5c9d819721ff6f631d7d5b3057fabcd633 [file] [log] [blame]
Daniel Barkalow175785e2005-04-18 11:39:48 -07001#include "blob.h"
2#include "cache.h"
3#include <stdlib.h>
4
5const char *blob_type = "blob";
6
Jason McMullan5d6ccf52005-06-03 11:05:39 -04007struct blob *lookup_blob(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -07008{
9 struct object *obj = lookup_object(sha1);
10 if (!obj) {
Christopher Li812666c2005-04-26 12:00:58 -070011 struct blob *ret = xmalloc(sizeof(struct blob));
Daniel Barkalow175785e2005-04-18 11:39:48 -070012 memset(ret, 0, sizeof(struct blob));
13 created_object(sha1, &ret->object);
14 ret->object.type = blob_type;
Daniel Barkalow175785e2005-04-18 11:39:48 -070015 return ret;
16 }
Nicolas Pitred1af0022005-05-20 16:59:17 -040017 if (!obj->type)
18 obj->type = blob_type;
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070019 if (obj->type != blob_type) {
Daniel Barkalow175785e2005-04-18 11:39:48 -070020 error("Object %s is a %s, not a blob",
21 sha1_to_hex(sha1), obj->type);
22 return NULL;
23 }
24 return (struct blob *) obj;
25}
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070026
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040027int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
28{
29 item->object.parsed = 1;
30 return 0;
31}
32
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070033int parse_blob(struct blob *item)
34{
35 char type[20];
36 void *buffer;
37 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040038 int ret;
39
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070040 if (item->object.parsed)
41 return 0;
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070042 buffer = read_sha1_file(item->object.sha1, type, &size);
43 if (!buffer)
44 return error("Could not read %s",
45 sha1_to_hex(item->object.sha1));
46 if (strcmp(type, blob_type))
47 return error("Object %s not a blob",
48 sha1_to_hex(item->object.sha1));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040049 ret = parse_blob_buffer(item, buffer, size);
50 free(buffer);
51 return ret;
Daniel Barkalowa510bfa2005-04-28 07:46:33 -070052}