1 #include "block_list.h"
2 #include "block_range.h"
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7
8 struct block_list {
9 FILE *f;
10 const char *mountpoint;
11
12 const char *filename;
13 struct block_range_list blocks;
14 };
15
init(const char * file,const char * mountpoint)16 static void *init(const char *file, const char *mountpoint)
17 {
18 struct block_list *params = calloc(1, sizeof(*params));
19
20 if (!params)
21 return NULL;
22 params->mountpoint = mountpoint;
23 params->f = fopen(file, "w+");
24 if (!params->f) {
25 free(params);
26 return NULL;
27 }
28 return params;
29 }
30
start_new_file(char * path,ext2_ino_t ino EXT2FS_ATTR ((unused)),struct ext2_inode * inode EXT2FS_ATTR ((unused)),void * data)31 static int start_new_file(char *path, ext2_ino_t ino EXT2FS_ATTR((unused)),
32 struct ext2_inode *inode EXT2FS_ATTR((unused)),
33 void *data)
34 {
35 struct block_list *params = data;
36
37 params->filename = LINUX_S_ISREG(inode->i_mode) ? path : NULL;
38 return 0;
39 }
40
add_block(ext2_filsys fs EXT2FS_ATTR ((unused)),blk64_t blocknr,int metadata,void * data)41 static int add_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk64_t blocknr,
42 int metadata, void *data)
43 {
44 struct block_list *params = data;
45
46 if (params->filename && !metadata)
47 add_blocks_to_range(¶ms->blocks, blocknr, blocknr);
48 return 0;
49 }
50
inline_data(void * inline_data EXT2FS_ATTR ((unused)),void * data EXT2FS_ATTR ((unused)))51 static int inline_data(void *inline_data EXT2FS_ATTR((unused)),
52 void *data EXT2FS_ATTR((unused)))
53 {
54 return 0;
55 }
56
end_new_file(void * data)57 static int end_new_file(void *data)
58 {
59 struct block_list *params = data;
60
61 if (!params->filename || !params->blocks.head)
62 return 0;
63 if (fprintf(params->f, "%s%s ", params->mountpoint,
64 params->filename) < 0
65 || write_block_ranges(params->f, params->blocks.head, " ")
66 || fwrite("\n", 1, 1, params->f) != 1)
67 return -1;
68
69 delete_block_ranges(¶ms->blocks);
70 return 0;
71 }
72
cleanup(void * data)73 static int cleanup(void *data)
74 {
75 struct block_list *params = data;
76
77 fclose(params->f);
78 free(params);
79 return 0;
80 }
81
82 struct fsmap_format block_list_format = {
83 .init = init,
84 .start_new_file = start_new_file,
85 .add_block = add_block,
86 .inline_data = inline_data,
87 .end_new_file = end_new_file,
88 .cleanup = cleanup,
89 };
90