Browse Source

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>
maint
Junio C Hamano 19 years ago
parent
commit
bb6b8e4f87
  1. 2
      cache.h
  2. 35
      sha1_file.c

2
cache.h

@ -240,6 +240,8 @@ extern int move_temp_to_file(const char *tmpfile, char *filename);


extern int has_sha1_pack(const unsigned char *sha1); extern int has_sha1_pack(const unsigned char *sha1);
extern int has_sha1_file(const unsigned char *sha1); extern int has_sha1_file(const unsigned char *sha1);
extern void *map_sha1_file(const unsigned char *sha1, unsigned long *);
extern int legacy_loose_object(unsigned char *);


extern int has_pack_file(const unsigned char *sha1); extern int has_pack_file(const unsigned char *sha1);
extern int has_pack_index(const unsigned char *sha1); extern int has_pack_index(const unsigned char *sha1);

35
sha1_file.c

@ -646,8 +646,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long siz
return memcmp(sha1, real_sha1, 20) ? -1 : 0; return memcmp(sha1, real_sha1, 20) ? -1 : 0;
} }


static void *map_sha1_file_internal(const unsigned char *sha1, void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
unsigned long *size)
{ {
struct stat st; struct stat st;
void *map; void *map;
@ -684,10 +683,26 @@ static void *map_sha1_file_internal(const unsigned char *sha1,
return map; return map;
} }


int legacy_loose_object(unsigned char *map)
{
unsigned int word;

/*
* Is it a zlib-compressed buffer? If so, the first byte
* must be 0x78 (15-bit window size, deflated), and the
* first 16-bit word is evenly divisible by 31
*/
word = (map[0] << 8) + map[1];
if (map[0] == 0x78 && !(word % 31))
return 1;
else
return 0;
}

static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz) static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
{ {
unsigned char c; unsigned char c;
unsigned int word, bits; unsigned int bits;
unsigned long size; unsigned long size;
static const char *typename[8] = { static const char *typename[8] = {
NULL, /* OBJ_EXT */ NULL, /* OBJ_EXT */
@ -703,13 +718,7 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
stream->next_out = buffer; stream->next_out = buffer;
stream->avail_out = bufsiz; stream->avail_out = bufsiz;


/* if (legacy_loose_object(map)) {
* Is it a zlib-compressed buffer? If so, the first byte
* must be 0x78 (15-bit window size, deflated), and the
* first 16-bit word is evenly divisible by 31
*/
word = (map[0] << 8) + map[1];
if (map[0] == 0x78 && !(word % 31)) {
inflateInit(stream); inflateInit(stream);
return inflate(stream, 0); return inflate(stream, 0);
} }
@ -1246,7 +1255,7 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
z_stream stream; z_stream stream;
char hdr[128]; char hdr[128];


map = map_sha1_file_internal(sha1, &mapsize); map = map_sha1_file(sha1, &mapsize);
if (!map) { if (!map) {
struct pack_entry e; struct pack_entry e;


@ -1291,7 +1300,7 @@ void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size


if (find_pack_entry(sha1, &e)) if (find_pack_entry(sha1, &e))
return read_packed_sha1(sha1, type, size); return read_packed_sha1(sha1, type, size);
map = map_sha1_file_internal(sha1, &mapsize); map = map_sha1_file(sha1, &mapsize);
if (map) { if (map) {
buf = unpack_sha1_file(map, mapsize, type, size); buf = unpack_sha1_file(map, mapsize, type, size);
munmap(map, mapsize); munmap(map, mapsize);
@ -1629,7 +1638,7 @@ int write_sha1_to_fd(int fd, const unsigned char *sha1)
{ {
int retval; int retval;
unsigned long objsize; unsigned long objsize;
void *buf = map_sha1_file_internal(sha1, &objsize); void *buf = map_sha1_file(sha1, &objsize);


if (buf) { if (buf) {
retval = write_buffer(fd, buf, objsize); retval = write_buffer(fd, buf, objsize);

Loading…
Cancel
Save