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