blob: ea52ad5c9d819721ff6f631d7d5b3057fabcd633 [file] [log] [blame]
Daniel Barkalow175785e2005-04-18 18:39:481#include "blob.h"
2#include "cache.h"
3#include <stdlib.h>
4
5const char *blob_type = "blob";
6
Jason McMullan5d6ccf52005-06-03 15:05:397struct blob *lookup_blob(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 18:39:488{
9 struct object *obj = lookup_object(sha1);
10 if (!obj) {
Christopher Li812666c2005-04-26 19:00:5811 struct blob *ret = xmalloc(sizeof(struct blob));
Daniel Barkalow175785e2005-04-18 18:39:4812 memset(ret, 0, sizeof(struct blob));
13 created_object(sha1, &ret->object);
14 ret->object.type = blob_type;
Daniel Barkalow175785e2005-04-18 18:39:4815 return ret;
16 }
Nicolas Pitred1af0022005-05-20 20:59:1717 if (!obj->type)
18 obj->type = blob_type;
Daniel Barkalowa510bfa2005-04-28 14:46:3319 if (obj->type != blob_type) {
Daniel Barkalow175785e2005-04-18 18:39:4820 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 14:46:3326
Nicolas Pitrebd2c39f2005-05-06 17:48:3427int 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 14:46:3333int parse_blob(struct blob *item)
34{
35 char type[20];
36 void *buffer;
37 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 17:48:3438 int ret;
39
Daniel Barkalowa510bfa2005-04-28 14:46:3340 if (item->object.parsed)
41 return 0;
Daniel Barkalowa510bfa2005-04-28 14:46:3342 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 17:48:3449 ret = parse_blob_buffer(item, buffer, size);
50 free(buffer);
51 return ret;
Daniel Barkalowa510bfa2005-04-28 14:46:3352}