This patch introduces a fssum option to the erofs-utils fsck tool, 
enabling checksum calculation for erofs image files.

Signed-off-by: Jiawei Wang <[email protected]>
---
 fsck/Makefile.am |  3 ++-
 fsck/main.c      | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/fsck/Makefile.am b/fsck/Makefile.am
index 5bdee4d..b7f0289 100644
--- a/fsck/Makefile.am
+++ b/fsck/Makefile.am
@@ -4,7 +4,8 @@
 AUTOMAKE_OPTIONS = foreign
 bin_PROGRAMS     = fsck.erofs
 AM_CPPFLAGS = ${libuuid_CFLAGS}
-fsck_erofs_SOURCES = main.c
+noinst_HEADERS = fssum.h
+fsck_erofs_SOURCES = main.c fssum.c
 fsck_erofs_CFLAGS = -Wall -I$(top_srcdir)/include
 fsck_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \
        ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} ${libdeflate_LIBS} \
diff --git a/fsck/main.c b/fsck/main.c
index 28f1e7e..de0a872 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -13,6 +13,7 @@
 #include "erofs/compress.h"
 #include "erofs/decompress.h"
 #include "erofs/dir.h"
+#include "fssum.h"
 #include "../lib/compressor.h"
 
 static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
@@ -31,6 +32,7 @@ struct erofsfsck_cfg {
        bool overwrite;
        bool preserve_owner;
        bool preserve_perms;
+       bool fssum;
 };
 static struct erofsfsck_cfg fsckcfg;
 
@@ -48,6 +50,7 @@ static struct option long_options[] = {
        {"no-preserve-owner", no_argument, 0, 10},
        {"no-preserve-perms", no_argument, 0, 11},
        {"offset", required_argument, 0, 12},
+       {"fssum", no_argument, 0, 13},
        {0, 0, 0, 0},
 };
 
@@ -98,6 +101,7 @@ static void usage(int argc, char **argv)
                " --extract[=X]          check if all files are well encoded, 
optionally\n"
                "                        extract to X\n"
                " --offset=#             skip # bytes at the beginning of 
IMAGE\n"
+               " --fssum                calculate the checksum of iamge\n"
                "\n"
                " -a, -A, -y             no-op, for compatibility with fsck of 
other filesystems\n"
                "\n"
@@ -225,6 +229,9 @@ static int erofsfsck_parse_options_cfg(int argc, char 
**argv)
                                return -EINVAL;
                        }
                        break;
+               case 13:
+                       fsckcfg.fssum = true;
+                       break;
                default:
                        return -EINVAL;
                }
@@ -932,6 +939,20 @@ out:
        return ret;
 }
 
+static int erofsfsck_sum_image(struct erofs_sb_info *sbi)
+{
+       struct erofs_dir_context ctx = {
+               .flags = 0,
+               .pnid = 0,
+               .dir = NULL,
+               .de_nid = sbi->root_nid,
+               .dname = "",
+               .de_namelen = 0,
+       };
+       
+       return erofs_fssum_calculate(&ctx);
+}
+
 #ifdef FUZZING
 int erofsfsck_fuzz_one(int argc, char *argv[])
 #else
@@ -953,6 +974,7 @@ int main(int argc, char *argv[])
        fsckcfg.check_decomp = false;
        fsckcfg.force = false;
        fsckcfg.overwrite = false;
+       fsckcfg.fssum = false;
        fsckcfg.preserve_owner = fsckcfg.superuser;
        fsckcfg.preserve_perms = fsckcfg.superuser;
 
@@ -1017,6 +1039,11 @@ int main(int argc, char *argv[])
                }
        }
 
+       if (fsckcfg.fssum) {
+               err = erofsfsck_sum_image(&g_sbi);
+               if (err)
+                       erofs_err("fssum calculation for image falied");
+       }
 exit_hardlink:
        if (fsckcfg.extract_path)
                erofsfsck_hardlink_exit();
-- 
2.34.1

Reply via email to