🌐 AI搜索 & 代理 主页
blob: d1af2e62f14965fab491a3d9dde712f02b718adc [file] [log] [blame]
Daniel Barkalow175785e2005-04-18 18:39:481#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 09:33:542#include "blob.h"
Daniel Barkalow175785e2005-04-18 18:39:483#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) {
Linus Torvalds855419f2006-06-19 17:44:1511 struct blob *ret = alloc_blob_node();
Daniel Barkalow175785e2005-04-18 18:39:4812 created_object(sha1, &ret->object);
Linus Torvalds19746322006-07-12 03:45:3113 ret->object.type = OBJ_BLOB;
Daniel Barkalow175785e2005-04-18 18:39:4814 return ret;
15 }
Nicolas Pitred1af0022005-05-20 20:59:1716 if (!obj->type)
Linus Torvalds19746322006-07-12 03:45:3117 obj->type = OBJ_BLOB;
18 if (obj->type != OBJ_BLOB) {
Linus Torvalds885a86a2006-06-14 23:45:1319 error("Object %s is a %s, not a blob",
20 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow175785e2005-04-18 18:39:4821 return NULL;
22 }
23 return (struct blob *) obj;
24}
Daniel Barkalowa510bfa2005-04-28 14:46:3325
Nicolas Pitrebd2c39f2005-05-06 17:48:3426int parse_blob_buffer(struct blob *item, void *buffer, unsigned long size)
27{
28 item->object.parsed = 1;
29 return 0;
30}
31
Daniel Barkalowa510bfa2005-04-28 14:46:3332int parse_blob(struct blob *item)
33{
34 char type[20];
35 void *buffer;
36 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 17:48:3437 int ret;
38
Daniel Barkalowa510bfa2005-04-28 14:46:3339 if (item->object.parsed)
40 return 0;
Daniel Barkalowa510bfa2005-04-28 14:46:3341 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 Pitrebd2c39f2005-05-06 17:48:3448 ret = parse_blob_buffer(item, buffer, size);
49 free(buffer);
50 return ret;
Daniel Barkalowa510bfa2005-04-28 14:46:3351}