erofs-utils: lib: change function definition of erofs_blocklist_open()
authorHongzhen Luo <hongzhen@linux.alibaba.com>
Wed, 3 Jul 2024 03:03:27 +0000 (11:03 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Wed, 3 Jul 2024 07:24:06 +0000 (15:24 +0800)
Modify erofs_blocklist_open() to accept a file pointer instead of
a file path, making it suitable for external use in liberofs.

Signed-off-by: Hongzhen Luo <hongzhen@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240703030327.3280503-1-hongzhen@linux.alibaba.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
include/erofs/block_list.h
lib/block_list.c
mkfs/main.c

index 9f9975ebfee169c38140d51e20de835f4cb579c3..7db4d0c135f5eaaf9a4b5f71896b9c6e82d8416f 100644 (file)
@@ -13,8 +13,8 @@ extern "C"
 
 #include "internal.h"
 
-int erofs_blocklist_open(char *filename, bool srcmap);
-void erofs_blocklist_close(void);
+int erofs_blocklist_open(FILE *fp, bool srcmap);
+FILE *erofs_blocklist_close(void);
 
 void tarerofs_blocklist_write(erofs_blk_t blkaddr, erofs_blk_t nblocks,
                              erofs_off_t srcoff);
index f47a746fb3447b9ad0f20437cefa273cfa2c2b5d..261e9ff5f3c3ee1f08853ae12834068e04328ed9 100644 (file)
 static FILE *block_list_fp;
 bool srcmap_enabled;
 
-int erofs_blocklist_open(char *filename, bool srcmap)
+int erofs_blocklist_open(FILE *fp, bool srcmap)
 {
-       block_list_fp = fopen(filename, "w");
-
-       if (!block_list_fp)
-               return -errno;
+       if (!fp)
+               return -ENOENT;
+       block_list_fp = fp;
        srcmap_enabled = srcmap;
        return 0;
 }
 
-void erofs_blocklist_close(void)
+FILE *erofs_blocklist_close(void)
 {
-       if (!block_list_fp)
-               return;
+       FILE *fp = block_list_fp;
 
-       fclose(block_list_fp);
        block_list_fp = NULL;
+       return fp;
 }
 
 /* XXX: really need to be cleaned up */
index 321b8ace707d9146be9c5edf4a6b320334be6abb..bfd6d60da6e8ccf580af6ca6081459a75ed95d00 100644 (file)
@@ -1133,6 +1133,7 @@ int main(int argc, char **argv)
        erofs_blk_t nblocks;
        struct timeval t;
        FILE *packedfile = NULL;
+       FILE *blklst = NULL;
        u32 crc;
 
        erofs_init_configure();
@@ -1174,10 +1175,12 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if (cfg.block_list_file &&
-           erofs_blocklist_open(cfg.block_list_file, false)) {
-               erofs_err("failed to open %s", cfg.block_list_file);
-               return 1;
+       if (cfg.block_list_file) {
+               blklst = fopen(cfg.block_list_file, "w");
+               if (!blklst || erofs_blocklist_open(blklst, false)) {
+                       erofs_err("failed to open %s", cfg.block_list_file);
+                       return 1;
+               }
        }
 #endif
        erofs_show_config();
@@ -1210,8 +1213,9 @@ int main(int argc, char **argv)
                erofstar.dev = rebuild_src_count + 1;
 
                if (erofstar.mapfile) {
-                       err = erofs_blocklist_open(erofstar.mapfile, true);
-                       if (err) {
+                       blklst = fopen(erofstar.mapfile, "w");
+                       if (!blklst || erofs_blocklist_open(blklst, true)) {
+                               err = -errno;
                                erofs_err("failed to open %s", erofstar.mapfile);
                                goto exit;
                        }
@@ -1417,7 +1421,9 @@ exit:
                erofs_iput(root);
        z_erofs_compress_exit();
        z_erofs_dedupe_exit();
-       erofs_blocklist_close();
+       blklst = erofs_blocklist_close();
+       if (blklst)
+               fclose(blklst);
        erofs_dev_close(&sbi);
        erofs_cleanup_compress_hints();
        erofs_cleanup_exclude_rules();