Initial implementation of the r2 filesystem

This commit is contained in:
pancake 2017-10-23 03:48:48 +02:00
parent 3379258f87
commit 3f64b7a1dc
7 changed files with 171 additions and 2 deletions

View File

@ -198,7 +198,7 @@ R_API void r_config_list(RConfig *cfg, const char *str, int rad) {
*dot = 0;
}
if (!strcmp (str, space)) {
cfg->cb_printf ("%s\n", node->name);
cfg->cb_printf ("%s\n", dot + 1);
}
free (space);
}

View File

@ -1794,6 +1794,7 @@ R_API bool r_core_init(RCore *core) {
r_io_bind (core->io, &(core->print->iob));
r_io_bind (core->io, &(core->anal->iob));
r_io_bind (core->io, &(core->fs->iob));
r_core_bind (core, &(core->fs->cob));
r_io_bind (core->io, &(core->bin->iob));
r_flag_bind (core->flags, &(core->anal->flb));
r_anal_bind (core->anal, &(core->parser->analb));

View File

@ -149,6 +149,7 @@ R_API RFSRoot* r_fs_mount(RFS* fs, const char* fstype, const char* path, ut64 de
root->p = p;
//memcpy (&root->iob, &fs->iob, sizeof (root->iob));
root->iob = fs->iob;
root->cob = fs->cob;
if (!p->mount (root)) {
eprintf ("r_fs_mount: Cannot mount partition\n");
free (str);

153
libr/fs/p/fs_r2.c Normal file
View File

@ -0,0 +1,153 @@
/* radare - LGPL - Copyright 2017 - pancake */
#include <r_fs.h>
#include <r_lib.h>
#include <sys/stat.h>
typedef RList *(*DirHandler)(RFSRoot *root, const char *path);
typedef RFSFile *(*CatHandler)(RFSRoot *root, const char *path);
typedef struct {
const char *path;
DirHandler dir;
CatHandler cat;
} Routes;
static RFSFile *__cfg_cat(RFSRoot *root, const char *path);
static RList *__root(RFSRoot *root, const char *path);
static RList *__cfg(RFSRoot *root, const char *path);
static Routes routes[] = {
{"/cfg", &__cfg, &__cfg_cat},
{"/flags", NULL},
{"/", &__root},
{NULL, NULL}
};
static RFSFile* fs_r2_open(RFSRoot *root, const char *path) {
int i;
for (i = 0; routes[i].path; i++) {
if (routes[i].cat && !strncmp (path, routes[i].path, strlen (routes[i].path))) {
return routes[i].cat (root, path);
}
}
return NULL;
}
static bool fs_r2_read(RFSFile *file, ut64 addr, int len) {
// eprintf ("TODO: fs.r2.read\n");
return NULL;
}
static void fs_r2_close(RFSFile *file) {
// eprintf ("TODO: fs.r2.close\n");
//fclose (file->ptr);
}
static void append_file(RList *list, const char *name, int type, int time, ut64 size) {
if (!list || !name || !*name) {
return;
}
RFSFile *fsf = r_fs_file_new (NULL, name);
if (!fsf) {
return;
}
fsf->type = type;
fsf->time = time;
fsf->size = size;
r_list_append (list, fsf);
}
static RFSFile *__cfg_cat(RFSRoot *root, const char *path) {
char *a = strdup (path + 5);
r_str_replace_char (a, '/', '.');
char *res = root->cob.cmdstrf (root->cob.core, "e %s", a);
// root->iob.io->cb_printf ("%s\n", res);
eprintf ("%s", res);
RFSFile *file = r_fs_file_new (root, path);
file->ptr = NULL;
file->p = root->p;
file->size = strlen (res);
free (res);
return file;
}
static RList *__cfg(RFSRoot *root, const char *path) {
const char *prefix = NULL;
if (!strncmp (path, "/cfg/", 5)) {
prefix = path + 5;
}
char *cmd = prefix
? r_str_newf ("es %s", prefix)
: strdup ("es");
char *res = root->cob.cmdstr (root->cob.core, cmd);
free (cmd);
if (res) {
RList *list = r_list_new ();
if (!list) {
free (res);
return NULL;
}
int i, count = 0;
int *lines = r_str_split_lines (res, &count);
for (i = 0; i < count; i++) {
char *line = res + lines[i];
append_file (list, line, prefix? 'f': 'd', 0, 0);
}
free (res);
return list;
}
return NULL;
}
static RList *__root(RFSRoot *root, const char *path) {
RList *list = r_list_newf (NULL);
if (!list) {
return NULL;
}
int i;
for (i = 0; routes[i].path; i++) {
append_file (list, routes[i].path + 1, 'd', 0, 0);
}
return list;
}
static RList *fs_r2_dir(RFSRoot *root, const char *path, int view /*ignored*/) {
int i;
for (i = 0; routes[i].path; i++) {
if (routes[i].dir && !strncmp (path, routes[i].path, strlen (routes[i].path))) {
return routes[i].dir (root, path);
}
}
return NULL;
}
static int fs_r2_mount(RFSRoot *root) {
root->ptr = NULL;
return true;
}
static void fs_r2_umount(RFSRoot *root) {
root->ptr = NULL;
}
RFSPlugin r_fs_plugin_r2 = {
.name = "r2",
.desc = "r2-based filesystem",
.open = fs_r2_open,
.read = fs_r2_read,
.close = fs_r2_close,
.dir = &fs_r2_dir,
.mount = fs_r2_mount,
.umount = fs_r2_umount,
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_FS,
.data = &r_fs_plugin_r2,
.versr2n = R2_VERSION
};
#endif

9
libr/fs/p/r2.mk Normal file
View File

@ -0,0 +1,9 @@
OBJ_R2=fs_r2.o
STATIC_OBJ+=${OBJ_R2}
TARGET_R2=fs_r2.${EXT_SO}
ALL_TARGETS+=${TARGET_R2}
${TARGET_R2}: ${OBJ_R2}
${CC} $(call libname,fs_r2) ${LDFLAGS} ${CFLAGS} -o ${TARGET_R2} ${OBJ_R2} ${EXTRA}

View File

@ -3,7 +3,8 @@
#include <r_types.h>
#include <r_list.h>
#include <r_io.h>
#include <r_bind.h> // RCoreBind
#include <r_io.h> // RIOBind
#ifdef __cplusplus
extern "C" {
@ -17,6 +18,7 @@ struct r_fs_t;
typedef struct r_fs_t {
RIOBind iob;
RCoreBind cob;
RList /*<RFSPlugin>*/ *plugins;
RList /*<RFSRoot>*/ *roots;
int view;
@ -47,6 +49,7 @@ typedef struct r_fs_root_t {
struct r_fs_plugin_t *p;
void *ptr;
RIOBind iob;
RCoreBind cob;
} RFSRoot;
typedef struct r_fs_plugin_t {
@ -128,6 +131,7 @@ R_API int r_fs_partition_get_size(void); // WTF. wrong function name
/* plugins */
extern RFSPlugin r_fs_plugin_io;
extern RFSPlugin r_fs_plugin_r2;
extern RFSPlugin r_fs_plugin_ext2;
extern RFSPlugin r_fs_plugin_fat;
extern RFSPlugin r_fs_plugin_ntfs;

View File

@ -180,6 +180,7 @@ debug.null
egg.exec
egg.xor
fs.io
fs.r2
fs.ext2
fs.fat
fs.fb