path: root/builtin/fsck.c
diff options
authorJeff King <>2018-05-02 19:44:51 (GMT)
committerJeff King <>2018-05-22 03:55:12 (GMT)
commit7ac4f3a007e2567f9d2492806186aa063f9a08d6 (patch)
tree940b2dfa953d71d3838b89cdae8f9ab193fea6ea /builtin/fsck.c
parented9c3220621d634d543bc4dd998d12167dfc57d4 (diff)
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 <>
Diffstat (limited to 'builtin/fsck.c')
1 files changed, 20 insertions, 22 deletions
diff --git a/builtin/fsck.c b/builtin/fsck.c
index ef78c6c..f91d5f3 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -337,7 +337,7 @@ static void check_connectivity(void)
-static int fsck_obj(struct object *obj)
+static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
int err;
@@ -351,7 +351,7 @@ static int fsck_obj(struct object *obj)
if (fsck_walk(obj, NULL, &fsck_obj_options))
objerror(obj, "broken links");
- err = fsck_object(obj, NULL, 0, &fsck_obj_options);
+ err = fsck_object(obj, buffer, size, &fsck_obj_options);
if (err)
goto out;
@@ -396,7 +396,7 @@ static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
obj->flags &= ~(REACHABLE | SEEN);
obj->flags |= HAS_OBJ;
- return fsck_obj(obj);
+ return fsck_obj(obj, buffer, size);
static int default_refs;
@@ -504,44 +504,42 @@ static void get_default_heads(void)
-static struct object *parse_loose_object(const struct object_id *oid,
- const char *path)
+static int fsck_loose(const struct object_id *oid, const char *path, void *data)
struct object *obj;
- void *contents;
enum object_type type;
unsigned long size;
+ void *contents;
int eaten;
- if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
- return NULL;
+ if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0) {
+ errors_found |= ERROR_OBJECT;
+ error("%s: object corrupt or missing: %s",
+ oid_to_hex(oid), path);
+ return 0; /* keep checking other objects */
+ }
if (!contents && type != OBJ_BLOB)
- die("BUG: read_loose_object streamed a non-blob");
+ BUG("read_loose_object streamed a non-blob");
obj = parse_object_buffer(oid, type, size, contents, &eaten);
- if (!eaten)
- free(contents);
- return obj;
-static int fsck_loose(const struct object_id *oid, const char *path, void *data)
- struct object *obj = parse_loose_object(oid, path);
if (!obj) {
errors_found |= ERROR_OBJECT;
- error("%s: object corrupt or missing: %s",
+ error("%s: object could not be parsed: %s",
oid_to_hex(oid), path);
+ if (!eaten)
+ free(contents);
return 0; /* keep checking other objects */
obj->flags &= ~(REACHABLE | SEEN);
obj->flags |= HAS_OBJ;
- if (fsck_obj(obj))
+ if (fsck_obj(obj, contents, size))
errors_found |= ERROR_OBJECT;
- return 0;
+ if (!eaten)
+ free(contents);
+ return 0; /* keep checking other objects, even if we saw an error */
static int fsck_cruft(const char *basename, const char *path, void *data)