sha1-file: modernize loose header/stream functions
As with the open/map/close functions for loose objects that were recently converted, the functions for parsing the loose object stream use the name "sha1" and a bare "unsigned char *". Let's fix that so that unpack_sha1_header() becomes unpack_loose_header(), etc. These conversions are less clear-cut than the file access functions. You could argue that the they are parsing Git's canonical object format (i.e., "type size\0contents", over which we compute the hash), which is not strictly tied to loose storage. But in practice these functions are used only for loose objects, and using the term "loose_header" (instead of "object_header") distinguishes it from the object header found in packfiles (which contains the same information in a different format). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
514c5fdd03
commit
00a7760e81
4
cache.h
4
cache.h
@ -1269,8 +1269,8 @@ extern char *xdg_cache_home(const char *filename);
|
|||||||
|
|
||||||
extern int git_open_cloexec(const char *name, int flags);
|
extern int git_open_cloexec(const char *name, int flags);
|
||||||
#define git_open(name) git_open_cloexec(name, O_RDONLY)
|
#define git_open(name) git_open_cloexec(name, O_RDONLY)
|
||||||
extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
|
extern int unpack_loose_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
|
||||||
extern int parse_sha1_header(const char *hdr, unsigned long *sizep);
|
extern int parse_loose_header(const char *hdr, unsigned long *sizep);
|
||||||
|
|
||||||
extern int check_object_signature(const struct object_id *oid, void *buf, unsigned long size, const char *type);
|
extern int check_object_signature(const struct object_id *oid, void *buf, unsigned long size, const char *type);
|
||||||
|
|
||||||
|
62
sha1-file.c
62
sha1-file.c
@ -976,7 +976,7 @@ void *map_loose_object(struct repository *r,
|
|||||||
return map_loose_object_1(r, NULL, oid, size);
|
return map_loose_object_1(r, NULL, oid, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int unpack_sha1_short_header(git_zstream *stream,
|
static int unpack_loose_short_header(git_zstream *stream,
|
||||||
unsigned char *map, unsigned long mapsize,
|
unsigned char *map, unsigned long mapsize,
|
||||||
void *buffer, unsigned long bufsiz)
|
void *buffer, unsigned long bufsiz)
|
||||||
{
|
{
|
||||||
@ -991,11 +991,11 @@ static int unpack_sha1_short_header(git_zstream *stream,
|
|||||||
return git_inflate(stream, 0);
|
return git_inflate(stream, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int unpack_sha1_header(git_zstream *stream,
|
int unpack_loose_header(git_zstream *stream,
|
||||||
unsigned char *map, unsigned long mapsize,
|
unsigned char *map, unsigned long mapsize,
|
||||||
void *buffer, unsigned long bufsiz)
|
void *buffer, unsigned long bufsiz)
|
||||||
{
|
{
|
||||||
int status = unpack_sha1_short_header(stream, map, mapsize,
|
int status = unpack_loose_short_header(stream, map, mapsize,
|
||||||
buffer, bufsiz);
|
buffer, bufsiz);
|
||||||
|
|
||||||
if (status < Z_OK)
|
if (status < Z_OK)
|
||||||
@ -1007,13 +1007,13 @@ int unpack_sha1_header(git_zstream *stream,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
|
static int unpack_loose_header_to_strbuf(git_zstream *stream, unsigned char *map,
|
||||||
unsigned long mapsize, void *buffer,
|
unsigned long mapsize, void *buffer,
|
||||||
unsigned long bufsiz, struct strbuf *header)
|
unsigned long bufsiz, struct strbuf *header)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
|
status = unpack_loose_short_header(stream, map, mapsize, buffer, bufsiz);
|
||||||
if (status < Z_OK)
|
if (status < Z_OK)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -1043,7 +1043,9 @@ static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
|
static void *unpack_loose_rest(git_zstream *stream,
|
||||||
|
void *buffer, unsigned long size,
|
||||||
|
const struct object_id *oid)
|
||||||
{
|
{
|
||||||
int bytes = strlen(buffer) + 1;
|
int bytes = strlen(buffer) + 1;
|
||||||
unsigned char *buf = xmallocz(size);
|
unsigned char *buf = xmallocz(size);
|
||||||
@ -1080,10 +1082,10 @@ static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long s
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
error(_("corrupt loose object '%s'"), sha1_to_hex(sha1));
|
error(_("corrupt loose object '%s'"), oid_to_hex(oid));
|
||||||
else if (stream->avail_in)
|
else if (stream->avail_in)
|
||||||
error(_("garbage at end of loose object '%s'"),
|
error(_("garbage at end of loose object '%s'"),
|
||||||
sha1_to_hex(sha1));
|
oid_to_hex(oid));
|
||||||
free(buf);
|
free(buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -1093,7 +1095,7 @@ static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long s
|
|||||||
* too permissive for what we want to check. So do an anal
|
* too permissive for what we want to check. So do an anal
|
||||||
* object header parse by hand.
|
* object header parse by hand.
|
||||||
*/
|
*/
|
||||||
static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
|
static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
|
||||||
unsigned int flags)
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
const char *type_buf = hdr;
|
const char *type_buf = hdr;
|
||||||
@ -1154,12 +1156,12 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
|
|||||||
return *hdr ? -1 : type;
|
return *hdr ? -1 : type;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_sha1_header(const char *hdr, unsigned long *sizep)
|
int parse_loose_header(const char *hdr, unsigned long *sizep)
|
||||||
{
|
{
|
||||||
struct object_info oi = OBJECT_INFO_INIT;
|
struct object_info oi = OBJECT_INFO_INIT;
|
||||||
|
|
||||||
oi.sizep = sizep;
|
oi.sizep = sizep;
|
||||||
return parse_sha1_header_extended(hdr, &oi, 0);
|
return parse_loose_header_extended(hdr, &oi, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int loose_object_info(struct repository *r,
|
static int loose_object_info(struct repository *r,
|
||||||
@ -1207,24 +1209,24 @@ static int loose_object_info(struct repository *r,
|
|||||||
if (oi->disk_sizep)
|
if (oi->disk_sizep)
|
||||||
*oi->disk_sizep = mapsize;
|
*oi->disk_sizep = mapsize;
|
||||||
if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
|
if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
|
||||||
if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
|
if (unpack_loose_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
|
||||||
status = error(_("unable to unpack %s header with --allow-unknown-type"),
|
status = error(_("unable to unpack %s header with --allow-unknown-type"),
|
||||||
oid_to_hex(oid));
|
oid_to_hex(oid));
|
||||||
} else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
|
} else if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
|
||||||
status = error(_("unable to unpack %s header"),
|
status = error(_("unable to unpack %s header"),
|
||||||
oid_to_hex(oid));
|
oid_to_hex(oid));
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
; /* Do nothing */
|
; /* Do nothing */
|
||||||
else if (hdrbuf.len) {
|
else if (hdrbuf.len) {
|
||||||
if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
|
if ((status = parse_loose_header_extended(hdrbuf.buf, oi, flags)) < 0)
|
||||||
status = error(_("unable to parse %s header with --allow-unknown-type"),
|
status = error(_("unable to parse %s header with --allow-unknown-type"),
|
||||||
oid_to_hex(oid));
|
oid_to_hex(oid));
|
||||||
} else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
|
} else if ((status = parse_loose_header_extended(hdr, oi, flags)) < 0)
|
||||||
status = error(_("unable to parse %s header"), oid_to_hex(oid));
|
status = error(_("unable to parse %s header"), oid_to_hex(oid));
|
||||||
|
|
||||||
if (status >= 0 && oi->contentp) {
|
if (status >= 0 && oi->contentp) {
|
||||||
*oi->contentp = unpack_sha1_rest(&stream, hdr,
|
*oi->contentp = unpack_loose_rest(&stream, hdr,
|
||||||
*oi->sizep, oid->hash);
|
*oi->sizep, oid);
|
||||||
if (!*oi->contentp) {
|
if (!*oi->contentp) {
|
||||||
git_inflate_end(&stream);
|
git_inflate_end(&stream);
|
||||||
status = -1;
|
status = -1;
|
||||||
@ -2184,14 +2186,14 @@ void odb_clear_loose_cache(struct object_directory *odb)
|
|||||||
sizeof(odb->loose_objects_subdir_seen));
|
sizeof(odb->loose_objects_subdir_seen));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_stream_sha1(git_zstream *stream,
|
static int check_stream_oid(git_zstream *stream,
|
||||||
const char *hdr,
|
const char *hdr,
|
||||||
unsigned long size,
|
unsigned long size,
|
||||||
const char *path,
|
const char *path,
|
||||||
const unsigned char *expected_sha1)
|
const struct object_id *expected_oid)
|
||||||
{
|
{
|
||||||
git_hash_ctx c;
|
git_hash_ctx c;
|
||||||
unsigned char real_sha1[GIT_MAX_RAWSZ];
|
struct object_id real_oid;
|
||||||
unsigned char buf[4096];
|
unsigned char buf[4096];
|
||||||
unsigned long total_read;
|
unsigned long total_read;
|
||||||
int status = Z_OK;
|
int status = Z_OK;
|
||||||
@ -2207,7 +2209,7 @@ static int check_stream_sha1(git_zstream *stream,
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* This size comparison must be "<=" to read the final zlib packets;
|
* This size comparison must be "<=" to read the final zlib packets;
|
||||||
* see the comment in unpack_sha1_rest for details.
|
* see the comment in unpack_loose_rest for details.
|
||||||
*/
|
*/
|
||||||
while (total_read <= size &&
|
while (total_read <= size &&
|
||||||
(status == Z_OK ||
|
(status == Z_OK ||
|
||||||
@ -2223,19 +2225,19 @@ static int check_stream_sha1(git_zstream *stream,
|
|||||||
git_inflate_end(stream);
|
git_inflate_end(stream);
|
||||||
|
|
||||||
if (status != Z_STREAM_END) {
|
if (status != Z_STREAM_END) {
|
||||||
error(_("corrupt loose object '%s'"), sha1_to_hex(expected_sha1));
|
error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (stream->avail_in) {
|
if (stream->avail_in) {
|
||||||
error(_("garbage at end of loose object '%s'"),
|
error(_("garbage at end of loose object '%s'"),
|
||||||
sha1_to_hex(expected_sha1));
|
oid_to_hex(expected_oid));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
the_hash_algo->final_fn(real_sha1, &c);
|
the_hash_algo->final_fn(real_oid.hash, &c);
|
||||||
if (!hasheq(expected_sha1, real_sha1)) {
|
if (!oideq(expected_oid, &real_oid)) {
|
||||||
error(_("sha1 mismatch for %s (expected %s)"), path,
|
error(_("sha1 mismatch for %s (expected %s)"), path,
|
||||||
sha1_to_hex(expected_sha1));
|
oid_to_hex(expected_oid));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2262,12 +2264,12 @@ int read_loose_object(const char *path,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
|
if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
|
||||||
error(_("unable to unpack header of %s"), path);
|
error(_("unable to unpack header of %s"), path);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
*type = parse_sha1_header(hdr, size);
|
*type = parse_loose_header(hdr, size);
|
||||||
if (*type < 0) {
|
if (*type < 0) {
|
||||||
error(_("unable to parse header of %s"), path);
|
error(_("unable to parse header of %s"), path);
|
||||||
git_inflate_end(&stream);
|
git_inflate_end(&stream);
|
||||||
@ -2275,10 +2277,10 @@ int read_loose_object(const char *path,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*type == OBJ_BLOB && *size > big_file_threshold) {
|
if (*type == OBJ_BLOB && *size > big_file_threshold) {
|
||||||
if (check_stream_sha1(&stream, hdr, *size, path, expected_oid->hash) < 0)
|
if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
|
||||||
goto out;
|
goto out;
|
||||||
} else {
|
} else {
|
||||||
*contents = unpack_sha1_rest(&stream, hdr, *size, expected_oid->hash);
|
*contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
|
||||||
if (!*contents) {
|
if (!*contents) {
|
||||||
error(_("unable to unpack contents of %s"), path);
|
error(_("unable to unpack contents of %s"), path);
|
||||||
git_inflate_end(&stream);
|
git_inflate_end(&stream);
|
||||||
|
@ -342,12 +342,12 @@ static open_method_decl(loose)
|
|||||||
oid, &st->u.loose.mapsize);
|
oid, &st->u.loose.mapsize);
|
||||||
if (!st->u.loose.mapped)
|
if (!st->u.loose.mapped)
|
||||||
return -1;
|
return -1;
|
||||||
if ((unpack_sha1_header(&st->z,
|
if ((unpack_loose_header(&st->z,
|
||||||
st->u.loose.mapped,
|
st->u.loose.mapped,
|
||||||
st->u.loose.mapsize,
|
st->u.loose.mapsize,
|
||||||
st->u.loose.hdr,
|
st->u.loose.hdr,
|
||||||
sizeof(st->u.loose.hdr)) < 0) ||
|
sizeof(st->u.loose.hdr)) < 0) ||
|
||||||
(parse_sha1_header(st->u.loose.hdr, &st->size) < 0)) {
|
(parse_loose_header(st->u.loose.hdr, &st->size) < 0)) {
|
||||||
git_inflate_end(&st->z);
|
git_inflate_end(&st->z);
|
||||||
munmap(st->u.loose.mapped, st->u.loose.mapsize);
|
munmap(st->u.loose.mapped, st->u.loose.mapsize);
|
||||||
return -1;
|
return -1;
|
||||||
|
Reference in New Issue
Block a user