Skip to content

Commit 7ac4f3a

Browse files
committed
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 <[email protected]>
1 parent ed9c322 commit 7ac4f3a

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

builtin/fsck.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static void check_connectivity(void)
337337
}
338338
}
339339

340-
static int fsck_obj(struct object *obj)
340+
static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
341341
{
342342
int err;
343343

@@ -351,7 +351,7 @@ static int fsck_obj(struct object *obj)
351351

352352
if (fsck_walk(obj, NULL, &fsck_obj_options))
353353
objerror(obj, "broken links");
354-
err = fsck_object(obj, NULL, 0, &fsck_obj_options);
354+
err = fsck_object(obj, buffer, size, &fsck_obj_options);
355355
if (err)
356356
goto out;
357357

@@ -396,7 +396,7 @@ static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
396396
}
397397
obj->flags &= ~(REACHABLE | SEEN);
398398
obj->flags |= HAS_OBJ;
399-
return fsck_obj(obj);
399+
return fsck_obj(obj, buffer, size);
400400
}
401401

402402
static int default_refs;
@@ -504,44 +504,42 @@ static void get_default_heads(void)
504504
}
505505
}
506506

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)
509508
{
510509
struct object *obj;
511-
void *contents;
512510
enum object_type type;
513511
unsigned long size;
512+
void *contents;
514513
int eaten;
515514

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+
}
518521

519522
if (!contents && type != OBJ_BLOB)
520-
die("BUG: read_loose_object streamed a non-blob");
523+
BUG("read_loose_object streamed a non-blob");
521524

522525
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-
533526
if (!obj) {
534527
errors_found |= ERROR_OBJECT;
535-
error("%s: object corrupt or missing: %s",
528+
error("%s: object could not be parsed: %s",
536529
oid_to_hex(oid), path);
530+
if (!eaten)
531+
free(contents);
537532
return 0; /* keep checking other objects */
538533
}
539534

540535
obj->flags &= ~(REACHABLE | SEEN);
541536
obj->flags |= HAS_OBJ;
542-
if (fsck_obj(obj))
537+
if (fsck_obj(obj, contents, size))
543538
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 */
545543
}
546544

547545
static int fsck_cruft(const char *basename, const char *path, void *data)

fsck.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,20 @@ static int fsck_tag(struct tag *tag, const char *data,
899899
return fsck_tag_buffer(tag, data, size, options);
900900
}
901901

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+
902908
int fsck_object(struct object *obj, void *data, unsigned long size,
903909
struct fsck_options *options)
904910
{
905911
if (!obj)
906912
return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
907913

908914
if (obj->type == OBJ_BLOB)
909-
return 0;
915+
return fsck_blob((struct blob *)obj, data, size, options);
910916
if (obj->type == OBJ_TREE)
911917
return fsck_tree((struct tree *) obj, options);
912918
if (obj->type == OBJ_COMMIT)

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ int read_loose_object(const char *path,
22092209
goto out;
22102210
}
22112211

2212-
if (*type == OBJ_BLOB) {
2212+
if (*type == OBJ_BLOB && *size > big_file_threshold) {
22132213
if (check_stream_sha1(&stream, hdr, *size, path, expected_sha1) < 0)
22142214
goto out;
22152215
} else {

0 commit comments

Comments
 (0)