Yue Hu | b780c06 | 2021-06-24 15:02:34 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * erofs-utils/lib/block_list.c |
| 4 | * |
| 5 | * Copyright (C), 2021, Coolpad Group Limited. |
| 6 | * Created by Yue Hu <huyue2@yulong.com> |
| 7 | */ |
| 8 | #ifdef WITH_ANDROID |
| 9 | #include <stdio.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include "erofs/block_list.h" |
| 12 | |
| 13 | #define pr_fmt(fmt) "EROFS block_list: " FUNC_LINE_FMT fmt "\n" |
| 14 | #include "erofs/print.h" |
| 15 | |
| 16 | static FILE *block_list_fp = NULL; |
| 17 | |
| 18 | int erofs_droid_blocklist_fopen(void) |
| 19 | { |
| 20 | if (block_list_fp) |
| 21 | return 0; |
| 22 | |
| 23 | block_list_fp = fopen(cfg.block_list_file, "w"); |
| 24 | |
| 25 | if (!block_list_fp) |
| 26 | return -1; |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | void erofs_droid_blocklist_fclose(void) |
| 31 | { |
| 32 | if (!block_list_fp) |
| 33 | return; |
| 34 | |
| 35 | fclose(block_list_fp); |
| 36 | block_list_fp = NULL; |
| 37 | } |
| 38 | |
| 39 | static void blocklist_write(const char *path, erofs_blk_t blk_start, |
| 40 | erofs_blk_t nblocks, bool has_tail) |
| 41 | { |
| 42 | const char *fspath = erofs_fspath(path); |
| 43 | |
| 44 | fprintf(block_list_fp, "/%s", cfg.mount_point); |
| 45 | |
| 46 | if (fspath[0] != '/') |
| 47 | fprintf(block_list_fp, "/"); |
| 48 | |
| 49 | if (nblocks == 1) |
| 50 | fprintf(block_list_fp, "%s %u", fspath, blk_start); |
| 51 | else |
| 52 | fprintf(block_list_fp, "%s %u-%u", fspath, blk_start, |
| 53 | blk_start + nblocks - 1); |
| 54 | |
| 55 | if (!has_tail) |
| 56 | fprintf(block_list_fp, "\n"); |
| 57 | } |
| 58 | |
| 59 | void erofs_droid_blocklist_write(struct erofs_inode *inode, |
| 60 | erofs_blk_t blk_start, erofs_blk_t nblocks) |
| 61 | { |
| 62 | if (!block_list_fp || !cfg.mount_point || !nblocks) |
| 63 | return; |
| 64 | |
| 65 | blocklist_write(inode->i_srcpath, blk_start, nblocks, |
| 66 | !!inode->idata_size); |
| 67 | } |
| 68 | |
| 69 | void erofs_droid_blocklist_write_tail_end(struct erofs_inode *inode, |
| 70 | erofs_blk_t blkaddr) |
| 71 | { |
| 72 | if (!block_list_fp || !cfg.mount_point) |
| 73 | return; |
| 74 | |
| 75 | /* XXX: a bit hacky.. may need a better approach */ |
| 76 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 77 | return; |
| 78 | |
| 79 | /* XXX: another hack, which means it has been outputed before */ |
| 80 | if (erofs_blknr(inode->i_size)) { |
| 81 | if (blkaddr == NULL_ADDR) |
| 82 | fprintf(block_list_fp, "\n"); |
| 83 | else |
| 84 | fprintf(block_list_fp, " %u\n", blkaddr); |
| 85 | return; |
| 86 | } |
| 87 | if (blkaddr != NULL_ADDR) |
| 88 | blocklist_write(inode->i_srcpath, blkaddr, 1, false); |
| 89 | } |
| 90 | #endif |