fsck: actually fsck blob data

Because fscking a blob has always been a noop, we didn't bother passing around the blob data. In preparation for content-level checks, let's fix up a few things: 1. The fsck_object() function just returns success for any blob. Let's a noop fsck_blob(), which we can fill in with actual logic later. 2. The fsck_loose() function in builtin/fsck.c just threw away blob content after loading it. Let's hold onto it until after we've called fsck_object(). The easiest way to do this is to just drop the parse_loose_object() helper entirely. Incidentally, this also fixes a memory leak: if we successfully loaded the object data but did not parse it, we would have left the function without freeing it. 3. When fsck_loose() loads the object data, it does so with a custom read_loose_object() helper. This function streams any blobs, regardless of size, under the assumption that we're only checking the sha1. Instead, let's actually load blobs smaller than big_file_threshold, as the normal object-reading code-paths would do. This lets us fsck small files, and a NULL return is an indication that the blob was so big that it needed to be streamed, and we can pass that information along to fsck_blob(). Signed-off-by: Jeff King <peff@peff.net>

Jeff King committed May 2, 2018 at 15:44 UTC 7ac4f3a007e2567f9d2492806186aa063f9a08d6
3 files changed +28 -24
builtin/fsck.c
+20 -22
@@ -337,7 +337,7 @@ static void check_connectivity(void)
337 }
338 }
339
340 -static int fsck_obj(struct object *obj)
340 +static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
341 {
342 int err;
343
@@ -351,7 +351,7 @@ static int fsck_obj(struct object *obj)
351
352 if (fsck_walk(obj, NULL, &fsck_obj_options))
353 objerror(obj, "broken links");
354 - err = fsck_object(obj, NULL, 0, &fsck_obj_options);
354 + err = fsck_object(obj, buffer, size, &fsck_obj_options);
355 if (err)
356 goto out;
357
@@ -396,7 +396,7 @@ static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
396 }
397 obj->flags &= ~(REACHABLE | SEEN);
398 obj->flags |= HAS_OBJ;
399 - return fsck_obj(obj);
399 + return fsck_obj(obj, buffer, size);
400 }
401
402 static int default_refs;
@@ -504,44 +504,42 @@ static void get_default_heads(void)
504 }
505 }
506
507 -static struct object *parse_loose_object(const struct object_id *oid,
508 - const char *path)
507 +static int fsck_loose(const struct object_id *oid, const char *path, void *data)
508 {
509 struct object *obj;
511 - void *contents;
510 enum object_type type;
511 unsigned long size;
512 + void *contents;
513 int eaten;
514
516 - if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
517 - return NULL;
515 + if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0) {
516 + errors_found |= ERROR_OBJECT;
517 + error("%s: object corrupt or missing: %s",
518 + oid_to_hex(oid), path);
519 + return 0; /* keep checking other objects */
520 + }
521
522 if (!contents && type != OBJ_BLOB)
520 - die("BUG: read_loose_object streamed a non-blob");
523 + BUG("read_loose_object streamed a non-blob");
524
525 obj = parse_object_buffer(oid, type, size, contents, &eaten);
523 -
524 - if (!eaten)
525 - free(contents);
526 - return obj;
527 -}
528 -
529 -static int fsck_loose(const struct object_id *oid, const char *path, void *data)
530 -{
531 - struct object *obj = parse_loose_object(oid, path);
532 -
526 if (!obj) {
527 errors_found |= ERROR_OBJECT;
535 - error("%s: object corrupt or missing: %s",
528 + error("%s: object could not be parsed: %s",
529 oid_to_hex(oid), path);
530 + if (!eaten)
531 + free(contents);
532 return 0; /* keep checking other objects */
533 }
534
535 obj->flags &= ~(REACHABLE | SEEN);
536 obj->flags |= HAS_OBJ;
542 - if (fsck_obj(obj))
537 + if (fsck_obj(obj, contents, size))
538 errors_found |= ERROR_OBJECT;
544 - return 0;
539 +
540 + if (!eaten)
541 + free(contents);
542 + return 0; /* keep checking other objects, even if we saw an error */
543 }
544
545 static int fsck_cruft(const char *basename, const char *path, void *data)
fsck.c
+7 -1
@@ -899,6 +899,12 @@ static int fsck_tag(struct tag *tag, const char *data,
899 return fsck_tag_buffer(tag, data, size, options);
900 }
901
902 +static int fsck_blob(struct blob *blob, const char *buf,
903 + unsigned long size, struct fsck_options *options)
904 +{
905 + return 0;
906 +}
907 +
908 int fsck_object(struct object *obj, void *data, unsigned long size,
909 struct fsck_options *options)
910 {
@@ -906,7 +912,7 @@ int fsck_object(struct object *obj, void *data, unsigned long size,
912 return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
913
914 if (obj->type == OBJ_BLOB)
909 - return 0;
915 + return fsck_blob((struct blob *)obj, data, size, options);
916 if (obj->type == OBJ_TREE)
917 return fsck_tree((struct tree *) obj, options);
918 if (obj->type == OBJ_COMMIT)
sha1_file.c
+1 -1
@@ -2209,7 +2209,7 @@ int read_loose_object(const char *path,
2209 goto out;
2210 }
2211
2212 - if (*type == OBJ_BLOB) {
2212 + if (*type == OBJ_BLOB && *size > big_file_threshold) {
2213 if (check_stream_sha1(&stream, hdr, *size, path, expected_sha1) < 0)
2214 goto out;
2215 } else {