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(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,unsigned int zeroedlen)34 void tarerofs_blocklist_write(erofs_blk_t blkaddr, erofs_blk_t nblocks,
35 erofs_off_t srcoff, unsigned int zeroedlen)
36 {
37 if (!block_list_fp || !nblocks || !srcmap_enabled)
38 return;
39
40 if (zeroedlen)
41 fprintf(block_list_fp, "%08x %8x %08" PRIx64 " %08u\n",
42 blkaddr, nblocks, srcoff, zeroedlen);
43 else
44 fprintf(block_list_fp, "%08x %8x %08" PRIx64 "\n",
45 blkaddr, nblocks, srcoff);
46 }
47
48 #ifdef WITH_ANDROID
blocklist_write(const char * path,erofs_blk_t blk_start,erofs_blk_t nblocks,bool first_extent,bool last_extent)49 static void blocklist_write(const char *path, erofs_blk_t blk_start,
50 erofs_blk_t nblocks, bool first_extent,
51 bool last_extent)
52 {
53 const char *fspath = erofs_fspath(path);
54
55 if (first_extent) {
56 fprintf(block_list_fp, "/%s", cfg.mount_point);
57
58 if (fspath[0] != '/')
59 fprintf(block_list_fp, "/");
60
61 fprintf(block_list_fp, "%s", fspath);
62 }
63
64 if (nblocks == 1)
65 fprintf(block_list_fp, " %u", blk_start);
66 else
67 fprintf(block_list_fp, " %u-%u", blk_start,
68 blk_start + nblocks - 1);
69
70 if (last_extent)
71 fprintf(block_list_fp, "\n");
72 }
73
erofs_droid_blocklist_write_extent(struct erofs_inode * inode,erofs_blk_t blk_start,erofs_blk_t nblocks,bool first_extent,bool last_extent)74 void erofs_droid_blocklist_write_extent(struct erofs_inode *inode,
75 erofs_blk_t blk_start,
76 erofs_blk_t nblocks, bool first_extent,
77 bool last_extent)
78 {
79 if (!block_list_fp || !cfg.mount_point)
80 return;
81
82 if (!nblocks) {
83 if (last_extent)
84 fprintf(block_list_fp, "\n");
85 return;
86 }
87
88 blocklist_write(inode->i_srcpath, blk_start, nblocks, first_extent,
89 last_extent);
90 }
91
erofs_droid_blocklist_write(struct erofs_inode * inode,erofs_blk_t blk_start,erofs_blk_t nblocks)92 void erofs_droid_blocklist_write(struct erofs_inode *inode,
93 erofs_blk_t blk_start, erofs_blk_t nblocks)
94 {
95 if (!block_list_fp || !cfg.mount_point || !nblocks)
96 return;
97
98 blocklist_write(inode->i_srcpath, blk_start, nblocks,
99 true, !inode->idata_size);
100 }
101
erofs_droid_blocklist_write_tail_end(struct erofs_inode * inode,erofs_blk_t blkaddr)102 void erofs_droid_blocklist_write_tail_end(struct erofs_inode *inode,
103 erofs_blk_t blkaddr)
104 {
105 if (!block_list_fp || !cfg.mount_point)
106 return;
107
108 /* XXX: a bit hacky.. may need a better approach */
109 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
110 return;
111
112 /* XXX: another hack, which means it has been outputed before */
113 if (erofs_blknr(inode->sbi, inode->i_size)) {
114 if (blkaddr == NULL_ADDR)
115 fprintf(block_list_fp, "\n");
116 else
117 fprintf(block_list_fp, " %u\n", blkaddr);
118 return;
119 }
120 if (blkaddr != NULL_ADDR)
121 blocklist_write(inode->i_srcpath, blkaddr, 1, true, true);
122 }
123 #endif
124