@matheusbsbs / matheus-bagliano / commits / d66b37bb19
Fork 0

Add pretend_sha1_file() interface.

The new interface allows an application to temporarily hash a small number of objects and pretend that they are available in the object store without actually writing them. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 4, 2007 at 21:42 UTC d66b37bb19ff7a347ba4ccbcdc11b00772518b57
2 files changed +58
cache.h
+1
@@ -257,6 +257,7 @@ extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, uns
257 extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
258 extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
259 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
260 +extern int pretend_sha1_file(void *, unsigned long, const char *, unsigned char *);
261
262 extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
263
sha1_file.c
+57
@@ -1505,10 +1505,67 @@ static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned lo
1505 return unpack_entry(e.p, e.offset, type, size);
1506 }
1507
1508 +/*
1509 + * This is meant to hold a *small* number of objects that you would
1510 + * want read_sha1_file() to be able to return, but yet you do not want
1511 + * to write them into the object store (e.g. a browse-only
1512 + * application).
1513 + */
1514 +static struct cached_object {
1515 + unsigned char sha1[20];
1516 + const char *type;
1517 + void *buf;
1518 + unsigned long size;
1519 +} *cached_objects;
1520 +static int cached_object_nr, cached_object_alloc;
1521 +
1522 +static struct cached_object *find_cached_object(const unsigned char *sha1)
1523 +{
1524 + int i;
1525 + struct cached_object *co = cached_objects;
1526 +
1527 + for (i = 0; i < cached_object_nr; i++, co++) {
1528 + if (!hashcmp(co->sha1, sha1))
1529 + return co;
1530 + }
1531 + return NULL;
1532 +}
1533 +
1534 +int pretend_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1)
1535 +{
1536 + struct cached_object *co;
1537 +
1538 + hash_sha1_file(buf, len, type, sha1);
1539 + if (has_sha1_file(sha1) || find_cached_object(sha1))
1540 + return 0;
1541 + if (cached_object_alloc <= cached_object_nr) {
1542 + cached_object_alloc = alloc_nr(cached_object_alloc);
1543 + cached_objects = xrealloc(cached_objects,
1544 + sizeof(*cached_objects) *
1545 + cached_object_alloc);
1546 + }
1547 + co = &cached_objects[cached_object_nr++];
1548 + co->size = len;
1549 + co->type = strdup(type);
1550 + hashcpy(co->sha1, sha1);
1551 + return 0;
1552 +}
1553 +
1554 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1555 {
1556 unsigned long mapsize;
1557 void *map, *buf;
1558 + struct cached_object *co;
1559 +
1560 + co = find_cached_object(sha1);
1561 + if (co) {
1562 + buf = xmalloc(co->size + 1);
1563 + memcpy(buf, co->buf, co->size);
1564 + ((char*)buf)[co->size] = 0;
1565 + strcpy(type, co->type);
1566 + *size = co->size;
1567 + return buf;
1568 + }
1569
1570 buf = read_packed_sha1(sha1, type, size);
1571 if (buf)