1 #ifndef R2_FS_H
2 #define R2_FS_H
3 
4 #include <r_types.h>
5 #include <r_list.h>
6 #include <r_bind.h> // RCoreBind
7 #include <r_io.h> // RIOBind
8 #include <r_util.h>
9 #include <r_cons.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 R_LIB_VERSION_HEADER (r_fs);
16 
17 struct r_fs_plugin_t;
18 struct r_fs_root_t;
19 struct r_fs_t;
20 
21 typedef struct r_fs_t {
22 	RIOBind iob;
23 	RCoreBind cob;
24 	RConsBind csb;
25 	RList /*<RFSPlugin>*/ *plugins;
26 	RList /*<RFSRoot>*/ *roots;
27 	int view;
28 	void *ptr;
29 } RFS;
30 
31 typedef struct r_fs_partition_plugin_t {
32 	const char *name;
33 } RFSPartitionPlugin;
34 
35 typedef struct r_fs_file_t {
36 	char *name;
37 	char *path;
38 	ut64 off;
39 	ut32 size;
40 	ut8 *data;
41 	void *ctx;
42 	char type;
43 	ut64 time;
44 	struct r_fs_plugin_t *p;
45 	struct r_fs_root_t *root;
46 	void *ptr; // internal pointer
47 } RFSFile;
48 
49 typedef struct r_fs_root_t {
50 	char *path;
51 	ut64 delta;
52 	struct r_fs_plugin_t *p;
53 	void *ptr;
54 	// TODO: deprecate
55 	RIOBind iob;
56 	RCoreBind cob;
57 } RFSRoot;
58 
59 typedef struct r_fs_plugin_t {
60 	const char *name;
61 	const char *desc;
62 	const char *license;
63 	RFSFile* (*slurp)(RFSRoot *root, const char *path);
64 	RFSFile* (*open)(RFSRoot *root, const char *path, bool create);
65 	bool (*unlink)(RFSRoot *root, const char *path);
66 	int (*write)(RFSFile *fs, ut64 addr, const ut8 *data, int len);
67 	int (*read)(RFSFile *fs, ut64 addr, int len);
68 	void (*close)(RFSFile *fs);
69 	RList *(*dir)(RFSRoot *root, const char *path, int view);
70 	void (*init)(void);
71 	void (*fini)(void);
72 	int (*mount)(RFSRoot *root);
73 	void (*umount)(RFSRoot *root);
74 } RFSPlugin;
75 
76 typedef struct r_fs_partition_t {
77 	int number;
78 	ut64 start;
79 	ut64 length;
80 	int index;
81 	int type;
82 } RFSPartition;
83 
84 typedef struct r_fs_shell_t {
85 	char **cwd;
86 	void (*set_prompt)(const char *prompt);
87 	const char* (*readline)(void);
88 	int (*hist_add)(const char *line);
89 } RFSShell;
90 
91 #define R_FS_FILE_TYPE_MOUNTPOINT 'm'
92 #define R_FS_FILE_TYPE_DIRECTORY 'd'
93 #define R_FS_FILE_TYPE_REGULAR 'r'
94 #define R_FS_FILE_TYPE_DELETED 'x'
95 #define R_FS_FILE_TYPE_SPECIAL 's'
96 #define R_FS_FILE_TYPE_MOUNT 'm'
97 
98 typedef int (*RFSPartitionIterator)(void *disk, void *ptr, void *user);
99 typedef struct r_fs_partition_type_t {
100 	const char *name;
101 	void *ptr; // grub_msdos_partition_map
102 	RFSPartitionIterator iterate;
103 	//RFSPartitionIterator parhook;
104 } RFSPartitionType;
105 #define R_FS_PARTITIONS_LENGTH (int)(sizeof (partitions)/sizeof(RFSPartitionType)-1)
106 
107 enum {
108 	R_FS_VIEW_NORMAL = 0,
109 	R_FS_VIEW_DELETED = 1,
110 	R_FS_VIEW_SPECIAL = 2,
111 	R_FS_VIEW_ALL = 0xff,
112 };
113 
114 #ifdef R_API
115 R_API RFS *r_fs_new(void);
116 R_API void r_fs_view(RFS* fs, int view);
117 R_API void r_fs_free(RFS* fs);
118 R_API void r_fs_add(RFS *fs, RFSPlugin *p);
119 R_API void r_fs_del(RFS *fs, RFSPlugin *p);
120 R_API RFSRoot *r_fs_mount(RFS* fs, const char *fstype, const char *path, ut64 delta);
121 R_API bool r_fs_umount(RFS* fs, const char *path);
122 R_API RList *r_fs_root(RFS *fs, const char *path);
123 R_API RFSFile *r_fs_open(RFS* fs, const char *path, bool create);
124 R_API void r_fs_close(RFS* fs, RFSFile *file);
125 R_API int r_fs_read(RFS* fs, RFSFile *file, ut64 addr, int len);
126 R_API int r_fs_write(RFS* fs, RFSFile* file, ut64 addr, const ut8 *data, int len);
127 R_API RFSFile *r_fs_slurp(RFS* fs, const char *path);
128 R_API RList *r_fs_dir(RFS* fs, const char *path);
129 R_API int r_fs_dir_dump(RFS* fs, const char *path, const char *name);
130 R_API RList *r_fs_find_name(RFS* fs, const char *name, const char *glob);
131 R_API RList *r_fs_find_off(RFS* fs, const char *name, ut64 off);
132 R_API RList *r_fs_partitions(RFS* fs, const char *ptype, ut64 delta);
133 R_API char *r_fs_name(RFS *fs, ut64 offset);
134 R_API int r_fs_prompt(RFS *fs, const char *root);
135 R_API bool r_fs_check(RFS *fs, const char *p);
136 R_API int r_fs_shell_prompt(RFSShell *shell, RFS *fs, const char *root);
137 
138 /* file.c */
139 R_API RFSFile *r_fs_file_new(RFSRoot *root, const char *path);
140 R_API void r_fs_file_free(RFSFile *file);
141 R_API char* r_fs_file_copy_abs_path(RFSFile* file);
142 R_API RFSRoot *r_fs_root_new(const char *path, ut64 delta);
143 R_API void r_fs_root_free(RFSRoot *root);
144 R_API RFSPartition *r_fs_partition_new(int num, ut64 start, ut64 length);
145 R_API void r_fs_partition_free(RFSPartition *p);
146 R_API const char *r_fs_partition_type(const char *part, int type);
147 R_API const char *r_fs_partition_type_get(int n);
148 R_API int r_fs_partition_get_size(void); // WTF. wrong function name
149 
150 /* plugins */
151 extern RFSPlugin r_fs_plugin_io;
152 extern RFSPlugin r_fs_plugin_r2;
153 extern RFSPlugin r_fs_plugin_ext2;
154 extern RFSPlugin r_fs_plugin_fat;
155 extern RFSPlugin r_fs_plugin_ntfs;
156 extern RFSPlugin r_fs_plugin_hfs;
157 extern RFSPlugin r_fs_plugin_hfsplus;
158 extern RFSPlugin r_fs_plugin_reiserfs;
159 extern RFSPlugin r_fs_plugin_tar;
160 extern RFSPlugin r_fs_plugin_iso9660;
161 extern RFSPlugin r_fs_plugin_udf;
162 extern RFSPlugin r_fs_plugin_ufs;
163 extern RFSPlugin r_fs_plugin_ufs2;
164 extern RFSPlugin r_fs_plugin_sfs;
165 extern RFSPlugin r_fs_plugin_tar;
166 extern RFSPlugin r_fs_plugin_btrfs;
167 extern RFSPlugin r_fs_plugin_jfs;
168 extern RFSPlugin r_fs_plugin_afs;
169 extern RFSPlugin r_fs_plugin_affs;
170 extern RFSPlugin r_fs_plugin_cpio;
171 extern RFSPlugin r_fs_plugin_xfs;
172 extern RFSPlugin r_fs_plugin_fb;
173 extern RFSPlugin r_fs_plugin_minix;
174 extern RFSPlugin r_fs_plugin_posix;
175 #endif
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif
182