sha1_file.c: expose map_sha1_file() interface.
This exposes map_sha1_file() interface to mmap a loose object file, and legacy_loose_object() function, split from unpack_sha1_header(). They will be used in the next patch to reuse the deflated data from new-style loose object files when generating packs. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jul 17, 2006 at 15:04 UTC
bb6b8e4f877e8b59b3756960e29d1955072dab56
2 files changed
+24
-13
cache.h
+2
@@ -240,6 +240,8 @@ extern int move_temp_to_file(const char *tmpfile, char *filename);
240
241
extern int has_sha1_pack(const unsigned char *sha1);
242
extern int has_sha1_file(const unsigned char *sha1);
243
+extern void *map_sha1_file(const unsigned char *sha1, unsigned long *);
244
+extern int legacy_loose_object(unsigned char *);
245
246
extern int has_pack_file(const unsigned char *sha1);
247
extern int has_pack_index(const unsigned char *sha1);
sha1_file.c
+22
-13
@@ -646,8 +646,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long siz
646
return memcmp(sha1, real_sha1, 20) ? -1 : 0;
647
}
648
649
-static void *map_sha1_file_internal(const unsigned char *sha1,
650
- unsigned long *size)
649
+void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
650
{
651
struct stat st;
652
void *map;
@@ -684,10 +683,26 @@ static void *map_sha1_file_internal(const unsigned char *sha1,
683
return map;
684
}
685
686
+int legacy_loose_object(unsigned char *map)
687
+{
688
+ unsigned int word;
689
+
690
+ /*
691
+ * Is it a zlib-compressed buffer? If so, the first byte
692
+ * must be 0x78 (15-bit window size, deflated), and the
693
+ * first 16-bit word is evenly divisible by 31
694
+ */
695
+ word = (map[0] << 8) + map[1];
696
+ if (map[0] == 0x78 && !(word % 31))
697
+ return 1;
698
+ else
699
+ return 0;
700
+}
701
+
702
static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
703
{
704
unsigned char c;
690
- unsigned int word, bits;
705
+ unsigned int bits;
706
unsigned long size;
707
static const char *typename[8] = {
708
NULL, /* OBJ_EXT */
@@ -703,13 +718,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
718
stream->next_out = buffer;
719
stream->avail_out = bufsiz;
720
706
- /*
707
- * Is it a zlib-compressed buffer? If so, the first byte
708
- * must be 0x78 (15-bit window size, deflated), and the
709
- * first 16-bit word is evenly divisible by 31
710
- */
711
- word = (map[0] << 8) + map[1];
712
- if (map[0] == 0x78 && !(word % 31)) {
721
+ if (legacy_loose_object(map)) {
722
inflateInit(stream);
723
return inflate(stream, 0);
724
}
@@ -1246,7 +1255,7 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
1255
z_stream stream;
1256
char hdr[128];
1257
1249
- map = map_sha1_file_internal(sha1, &mapsize);
1258
+ map = map_sha1_file(sha1, &mapsize);
1259
if (!map) {
1260
struct pack_entry e;
1261
@@ -1291,7 +1300,7 @@ void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size
1300
1301
if (find_pack_entry(sha1, &e))
1302
return read_packed_sha1(sha1, type, size);
1294
- map = map_sha1_file_internal(sha1, &mapsize);
1303
+ map = map_sha1_file(sha1, &mapsize);
1304
if (map) {
1305
buf = unpack_sha1_file(map, mapsize, type, size);
1306
munmap(map, mapsize);
@@ -1629,7 +1638,7 @@ int write_sha1_to_fd(int fd, const unsigned char *sha1)
1638
{
1639
int retval;
1640
unsigned long objsize;
1632
- void *buf = map_sha1_file_internal(sha1, &objsize);
1641
+ void *buf = map_sha1_file(sha1, &objsize);
1642
1643
if (buf) {
1644
retval = write_buffer(fd, buf, objsize);