[PATCH v2] erofs-utils: lib: chagne function definition of erofs_blocklist_open()

Hongzhen Luo hongzhen at linux.alibaba.com
Tue Jul 2 22:14:45 AEST 2024


Modified the definition of the function `erofs_blocklist_open` to accept
a file pointer rather than a string, for external use in liberofs.

Signed-off-by: Hongzhen Luo <hongzhen at linux.alibaba.com>
---
v2: Let the caller to close the block_list.
v1: https://lore.kernel.org/all/20240702115647.2457003-1-hongzhen@linux.alibaba.com/
---
 include/erofs/block_list.h |  4 ++--
 lib/block_list.c           | 17 +++++++----------
 mkfs/main.c                | 23 +++++++++++++++--------
 3 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/include/erofs/block_list.h b/include/erofs/block_list.h
index 9f9975e..7db4d0c 100644
--- a/include/erofs/block_list.h
+++ b/include/erofs/block_list.h
@@ -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);
diff --git a/lib/block_list.c b/lib/block_list.c
index f47a746..10c7c6a 100644
--- a/lib/block_list.c
+++ b/lib/block_list.c
@@ -13,23 +13,20 @@
 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;
-
-	fclose(block_list_fp);
+	FILE *ret = block_list_fp;
 	block_list_fp = NULL;
+	return ret;
 }
 
 /* XXX: really need to be cleaned up */
diff --git a/mkfs/main.c b/mkfs/main.c
index 63ed877..07b4f8d 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -1133,6 +1133,10 @@ int main(int argc, char **argv)
 	erofs_blk_t nblocks;
 	struct timeval t;
 	FILE *packedfile = NULL;
+	FILE  *mp_fp;
+#ifdef WITH_ANDROID	
+	FILE  *cfg_fp;
+#endif
 	u32 crc;
 
 	erofs_init_configure();
@@ -1173,11 +1177,13 @@ int main(int argc, char **argv)
 		erofs_err("failed to load fs config %s", cfg.fs_config_file);
 		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) {
+		cfg_fp = fopen(cfg.block_list_file, "w");
+		if (!cfg_fp || erofs_blocklist_open(cfg_fp, false)) {
+			erofs_err("failed to open %s", cfg.block_list_file);
+			return 1;
+		}
 	}
 #endif
 	erofs_show_config();
@@ -1210,8 +1216,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) {
+			mp_fp = fopen(erofstar.mapfile, "w");
+			if (!mp_fp || erofs_blocklist_open(mp_fp, true)) {
+				err = -errno;
 				erofs_err("failed to open %s", erofstar.mapfile);
 				goto exit;
 			}
@@ -1417,7 +1424,7 @@ exit:
 		erofs_iput(root);
 	z_erofs_compress_exit();
 	z_erofs_dedupe_exit();
-	erofs_blocklist_close();
+	fclose(erofs_blocklist_close());
 	erofs_dev_close(&sbi);
 	erofs_cleanup_compress_hints();
 	erofs_cleanup_exclude_rules();
-- 
2.39.3



More information about the Linux-erofs mailing list