• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
2 /*
3  * Copyright (C), 2021, Coolpad Group Limited.
4  * Created by Yue Hu <huyue2@yulong.com>
5  */
6 #include <stdio.h>
7 #include <sys/stat.h>
8 #include "erofs/block_list.h"
9 
10 #define EROFS_MODNAME	"erofs block_list"
11 #include "erofs/print.h"
12 
13 static FILE *block_list_fp;
14 bool srcmap_enabled;
15 
erofs_blocklist_open(char * filename,bool srcmap)16 int erofs_blocklist_open(char *filename, bool srcmap)
17 {
18 	block_list_fp = fopen(filename, "w");
19 
20 	if (!block_list_fp)
21 		return -errno;
22 	srcmap_enabled = srcmap;
23 	return 0;
24 }
25 
erofs_blocklist_close(void)26 void erofs_blocklist_close(void)
27 {
28 	if (!block_list_fp)
29 		return;
30 
31 	fclose(block_list_fp);
32 	block_list_fp = NULL;
33 }
34 
35 /* XXX: really need to be cleaned up */
tarerofs_blocklist_write(erofs_blk_t blkaddr,erofs_blk_t nblocks,erofs_off_t srcoff)36 void tarerofs_blocklist_write(erofs_blk_t blkaddr, erofs_blk_t nblocks,
37 			      erofs_off_t srcoff)
38 {
39 	if (!block_list_fp || !nblocks || !srcmap_enabled)
40 		return;
41 
42 	fprintf(block_list_fp, "%08x %8x %08" PRIx64 "\n",
43 		blkaddr, nblocks, srcoff);
44 }
45 
46 #ifdef WITH_ANDROID
blocklist_write(const char * path,erofs_blk_t blk_start,erofs_blk_t nblocks,bool first_extent,bool last_extent)47 static void blocklist_write(const char *path, erofs_blk_t blk_start,
48 			    erofs_blk_t nblocks, bool first_extent,
49 			    bool last_extent)
50 {
51 	const char *fspath = erofs_fspath(path);
52 
53 	if (first_extent) {
54 		fprintf(block_list_fp, "/%s", cfg.mount_point);
55 
56 		if (fspath[0] != '/')
57 			fprintf(block_list_fp, "/");
58 
59 		fprintf(block_list_fp, "%s", fspath);
60 	}
61 
62 	if (nblocks == 1)
63 		fprintf(block_list_fp, " %u", blk_start);
64 	else
65 		fprintf(block_list_fp, " %u-%u", blk_start,
66 			blk_start + nblocks - 1);
67 
68 	if (last_extent)
69 		fprintf(block_list_fp, "\n");
70 }
71 
erofs_droid_blocklist_write_extent(struct erofs_inode * inode,erofs_blk_t blk_start,erofs_blk_t nblocks,bool first_extent,bool last_extent)72 void erofs_droid_blocklist_write_extent(struct erofs_inode *inode,
73 					erofs_blk_t blk_start,
74 					erofs_blk_t nblocks, bool first_extent,
75 					bool last_extent)
76 {
77 	if (!block_list_fp || !cfg.mount_point)
78 		return;
79 
80 	if (!nblocks) {
81 		if (last_extent)
82 			fprintf(block_list_fp, "\n");
83 		return;
84 	}
85 
86 	blocklist_write(inode->i_srcpath, blk_start, nblocks, first_extent,
87 			last_extent);
88 }
89 
erofs_droid_blocklist_write(struct erofs_inode * inode,erofs_blk_t blk_start,erofs_blk_t nblocks)90 void erofs_droid_blocklist_write(struct erofs_inode *inode,
91 				 erofs_blk_t blk_start, erofs_blk_t nblocks)
92 {
93 	if (!block_list_fp || !cfg.mount_point || !nblocks)
94 		return;
95 
96 	blocklist_write(inode->i_srcpath, blk_start, nblocks,
97 			true, !inode->idata_size);
98 }
99 
erofs_droid_blocklist_write_tail_end(struct erofs_inode * inode,erofs_blk_t blkaddr)100 void erofs_droid_blocklist_write_tail_end(struct erofs_inode *inode,
101 					  erofs_blk_t blkaddr)
102 {
103 	if (!block_list_fp || !cfg.mount_point)
104 		return;
105 
106 	/* XXX: a bit hacky.. may need a better approach */
107 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
108 		return;
109 
110 	/* XXX: another hack, which means it has been outputed before */
111 	if (erofs_blknr(inode->sbi, inode->i_size)) {
112 		if (blkaddr == NULL_ADDR)
113 			fprintf(block_list_fp, "\n");
114 		else
115 			fprintf(block_list_fp, " %u\n", blkaddr);
116 		return;
117 	}
118 	if (blkaddr != NULL_ADDR)
119 		blocklist_write(inode->i_srcpath, blkaddr, 1, true, true);
120 }
121 #endif
122