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>

Jeff King committed Jan 7, 2019 at 03:37 UTC 00a7760e81e067cdfb6113e02d141d508529ffc7
3 files changed +51 -49
cache.h
+2 -2
@@ -1269,8 +1269,8 @@ extern char *xdg_cache_home(const char *filename);
1269
1270 extern int git_open_cloexec(const char *name, int flags);
1271 #define git_open(name) git_open_cloexec(name, O_RDONLY)
1272 -extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
1273 -extern int parse_sha1_header(const char *hdr, unsigned long *sizep);
1272 +extern int unpack_loose_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
1273 +extern int parse_loose_header(const char *hdr, unsigned long *sizep);
1274
1275 extern int check_object_signature(const struct object_id *oid, void *buf, unsigned long size, const char *type);
1276
sha1-file.c
+43 -41
@@ -976,9 +976,9 @@ void *map_loose_object(struct repository *r,
976 return map_loose_object_1(r, NULL, oid, size);
977 }
978
979 -static int unpack_sha1_short_header(git_zstream *stream,
980 - unsigned char *map, unsigned long mapsize,
981 - void *buffer, unsigned long bufsiz)
979 +static int unpack_loose_short_header(git_zstream *stream,
980 + unsigned char *map, unsigned long mapsize,
981 + void *buffer, unsigned long bufsiz)
982 {
983 /* Get the data stream */
984 memset(stream, 0, sizeof(*stream));
@@ -991,12 +991,12 @@ static int unpack_sha1_short_header(git_zstream *stream,
991 return git_inflate(stream, 0);
992 }
993
994 -int unpack_sha1_header(git_zstream *stream,
995 - unsigned char *map, unsigned long mapsize,
996 - void *buffer, unsigned long bufsiz)
994 +int unpack_loose_header(git_zstream *stream,
995 + unsigned char *map, unsigned long mapsize,
996 + void *buffer, unsigned long bufsiz)
997 {
998 - int status = unpack_sha1_short_header(stream, map, mapsize,
999 - buffer, bufsiz);
998 + int status = unpack_loose_short_header(stream, map, mapsize,
999 + buffer, bufsiz);
1000
1001 if (status < Z_OK)
1002 return status;
@@ -1007,13 +1007,13 @@ int unpack_sha1_header(git_zstream *stream,
1007 return 0;
1008 }
1009
1010 -static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1011 - unsigned long mapsize, void *buffer,
1012 - unsigned long bufsiz, struct strbuf *header)
1010 +static int unpack_loose_header_to_strbuf(git_zstream *stream, unsigned char *map,
1011 + unsigned long mapsize, void *buffer,
1012 + unsigned long bufsiz, struct strbuf *header)
1013 {
1014 int status;
1015
1016 - status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
1016 + status = unpack_loose_short_header(stream, map, mapsize, buffer, bufsiz);
1017 if (status < Z_OK)
1018 return -1;
1019
@@ -1043,7 +1043,9 @@ static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1043 return -1;
1044 }
1045
1046 -static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
1046 +static void *unpack_loose_rest(git_zstream *stream,
1047 + void *buffer, unsigned long size,
1048 + const struct object_id *oid)
1049 {
1050 int bytes = strlen(buffer) + 1;
1051 unsigned char *buf = xmallocz(size);
@@ -1080,10 +1082,10 @@ static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long s
1082 }
1083
1084 if (status < 0)
1083 - error(_("corrupt loose object '%s'"), sha1_to_hex(sha1));
1085 + error(_("corrupt loose object '%s'"), oid_to_hex(oid));
1086 else if (stream->avail_in)
1087 error(_("garbage at end of loose object '%s'"),
1086 - sha1_to_hex(sha1));
1088 + oid_to_hex(oid));
1089 free(buf);
1090 return NULL;
1091 }
@@ -1093,8 +1095,8 @@ static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long s
1095 * too permissive for what we want to check. So do an anal
1096 * object header parse by hand.
1097 */
1096 -static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1097 - unsigned int flags)
1098 +static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
1099 + unsigned int flags)
1100 {
1101 const char *type_buf = hdr;
1102 unsigned long size;
@@ -1154,12 +1156,12 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1156 return *hdr ? -1 : type;
1157 }
1158
1157 -int parse_sha1_header(const char *hdr, unsigned long *sizep)
1159 +int parse_loose_header(const char *hdr, unsigned long *sizep)
1160 {
1161 struct object_info oi = OBJECT_INFO_INIT;
1162
1163 oi.sizep = sizep;
1162 - return parse_sha1_header_extended(hdr, &oi, 0);
1164 + return parse_loose_header_extended(hdr, &oi, 0);
1165 }
1166
1167 static int loose_object_info(struct repository *r,
@@ -1207,24 +1209,24 @@ static int loose_object_info(struct repository *r,
1209 if (oi->disk_sizep)
1210 *oi->disk_sizep = mapsize;
1211 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
1210 - if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
1212 + if (unpack_loose_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
1213 status = error(_("unable to unpack %s header with --allow-unknown-type"),
1214 oid_to_hex(oid));
1213 - } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1215 + } else if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1216 status = error(_("unable to unpack %s header"),
1217 oid_to_hex(oid));
1218 if (status < 0)
1219 ; /* Do nothing */
1220 else if (hdrbuf.len) {
1219 - if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
1221 + if ((status = parse_loose_header_extended(hdrbuf.buf, oi, flags)) < 0)
1222 status = error(_("unable to parse %s header with --allow-unknown-type"),
1223 oid_to_hex(oid));
1222 - } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
1224 + } else if ((status = parse_loose_header_extended(hdr, oi, flags)) < 0)
1225 status = error(_("unable to parse %s header"), oid_to_hex(oid));
1226
1227 if (status >= 0 && oi->contentp) {
1226 - *oi->contentp = unpack_sha1_rest(&stream, hdr,
1227 - *oi->sizep, oid->hash);
1228 + *oi->contentp = unpack_loose_rest(&stream, hdr,
1229 + *oi->sizep, oid);
1230 if (!*oi->contentp) {
1231 git_inflate_end(&stream);
1232 status = -1;
@@ -2184,14 +2186,14 @@ void odb_clear_loose_cache(struct object_directory *odb)
2186 sizeof(odb->loose_objects_subdir_seen));
2187 }
2188
2187 -static int check_stream_sha1(git_zstream *stream,
2188 - const char *hdr,
2189 - unsigned long size,
2190 - const char *path,
2191 - const unsigned char *expected_sha1)
2189 +static int check_stream_oid(git_zstream *stream,
2190 + const char *hdr,
2191 + unsigned long size,
2192 + const char *path,
2193 + const struct object_id *expected_oid)
2194 {
2195 git_hash_ctx c;
2194 - unsigned char real_sha1[GIT_MAX_RAWSZ];
2196 + struct object_id real_oid;
2197 unsigned char buf[4096];
2198 unsigned long total_read;
2199 int status = Z_OK;
@@ -2207,7 +2209,7 @@ static int check_stream_sha1(git_zstream *stream,
2209
2210 /*
2211 * This size comparison must be "<=" to read the final zlib packets;
2210 - * see the comment in unpack_sha1_rest for details.
2212 + * see the comment in unpack_loose_rest for details.
2213 */
2214 while (total_read <= size &&
2215 (status == Z_OK ||
@@ -2223,19 +2225,19 @@ static int check_stream_sha1(git_zstream *stream,
2225 git_inflate_end(stream);
2226
2227 if (status != Z_STREAM_END) {
2226 - error(_("corrupt loose object '%s'"), sha1_to_hex(expected_sha1));
2228 + error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
2229 return -1;
2230 }
2231 if (stream->avail_in) {
2232 error(_("garbage at end of loose object '%s'"),
2231 - sha1_to_hex(expected_sha1));
2233 + oid_to_hex(expected_oid));
2234 return -1;
2235 }
2236
2235 - the_hash_algo->final_fn(real_sha1, &c);
2236 - if (!hasheq(expected_sha1, real_sha1)) {
2237 + the_hash_algo->final_fn(real_oid.hash, &c);
2238 + if (!oideq(expected_oid, &real_oid)) {
2239 error(_("sha1 mismatch for %s (expected %s)"), path,
2238 - sha1_to_hex(expected_sha1));
2240 + oid_to_hex(expected_oid));
2241 return -1;
2242 }
2243
@@ -2262,12 +2264,12 @@ int read_loose_object(const char *path,
2264 goto out;
2265 }
2266
2265 - if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
2267 + if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
2268 error(_("unable to unpack header of %s"), path);
2269 goto out;
2270 }
2271
2270 - *type = parse_sha1_header(hdr, size);
2272 + *type = parse_loose_header(hdr, size);
2273 if (*type < 0) {
2274 error(_("unable to parse header of %s"), path);
2275 git_inflate_end(&stream);
@@ -2275,10 +2277,10 @@ int read_loose_object(const char *path,
2277 }
2278
2279 if (*type == OBJ_BLOB && *size > big_file_threshold) {
2278 - if (check_stream_sha1(&stream, hdr, *size, path, expected_oid->hash) < 0)
2280 + if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
2281 goto out;
2282 } else {
2281 - *contents = unpack_sha1_rest(&stream, hdr, *size, expected_oid->hash);
2283 + *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
2284 if (!*contents) {
2285 error(_("unable to unpack contents of %s"), path);
2286 git_inflate_end(&stream);
streaming.c
+6 -6
@@ -342,12 +342,12 @@ static open_method_decl(loose)
342 oid, &st->u.loose.mapsize);
343 if (!st->u.loose.mapped)
344 return -1;
345 - if ((unpack_sha1_header(&st->z,
346 - st->u.loose.mapped,
347 - st->u.loose.mapsize,
348 - st->u.loose.hdr,
349 - sizeof(st->u.loose.hdr)) < 0) ||
350 - (parse_sha1_header(st->u.loose.hdr, &st->size) < 0)) {
345 + if ((unpack_loose_header(&st->z,
346 + st->u.loose.mapped,
347 + st->u.loose.mapsize,
348 + st->u.loose.hdr,
349 + sizeof(st->u.loose.hdr)) < 0) ||
350 + (parse_loose_header(st->u.loose.hdr, &st->size) < 0)) {
351 git_inflate_end(&st->z);
352 munmap(st->u.loose.mapped, st->u.loose.mapsize);
353 return -1;