Skip to content

Commit

Permalink
Add dummy fs.io plugin, r_io_system now returns char*
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Oct 22, 2017
1 parent 4a58713 commit ca1b44e
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 98 deletions.
21 changes: 13 additions & 8 deletions libr/core/cconfig.c
Expand Up @@ -23,6 +23,15 @@ static void set_options(RConfigNode *node, ...) {
va_end (argp);
}

static bool isGdbPlugin(RCore *core) {
if (core->io && core->io->desc && core->io->desc->plugin) {
if (core->io->desc->plugin->name && !strcmp (core->io->desc->plugin->name, "gdb")) {
return true;
}
}
return false;
}

static void print_node_options(RConfigNode *node) {
RListIter *iter;
char *option;
Expand Down Expand Up @@ -1016,12 +1025,10 @@ static int cb_dbg_gdb_page_size(void *user, void *data) {
if (node->i_value < 64) { // 64 is hardcoded min packet size
return false;
}
if (core->io && core->io->desc && core->io->desc->plugin
&& core->io->desc->plugin->name
&& !strcmp (core->io->desc->plugin->name, "gdb")) {
if (isGdbPlugin (core)) {
char cmd[64];
snprintf (cmd, sizeof (cmd), "page_size %"PFMT64d, node->i_value);
r_io_system (core->io, cmd);
free (r_io_system (core->io, cmd));
}
return true;
}
Expand All @@ -1032,12 +1039,10 @@ static int cb_dbg_gdb_retries(void *user, void *data) {
if (node->i_value <= 0) {
return false;
}
if (core->io && core->io->desc && core->io->desc->plugin
&& core->io->desc->plugin->name
&& !strcmp (core->io->desc->plugin->name, "gdb")) {
if (isGdbPlugin (core)) {
char cmd[64];
snprintf (cmd, sizeof (cmd), "retries %"PFMT64d, node->i_value);
r_io_system (core->io, cmd);
free (r_io_system (core->io, cmd));
}
return true;
}
Expand Down
27 changes: 23 additions & 4 deletions libr/core/cmd.c
Expand Up @@ -470,7 +470,11 @@ static int cmd_rap(void *data, const char *input) {
core->cmdremote = input[2]? 1: 0;
r_cons_println (r_str_bool (core->cmdremote));
} else {
r_io_system (core->io, input + 1);
char *res = r_io_system (core->io, input + 1);
if (res) {
r_cons_printf ("%s\n", res);
free (res);
}
}
break;
case '$': // "=$"
Expand Down Expand Up @@ -513,7 +517,13 @@ static int cmd_rap(void *data, const char *input) {

static int cmd_rap_run(void *data, const char *input) {
RCore *core = (RCore *)data;
return r_io_system (core->io, input);
char *res = r_io_system (core->io, input);
if (res) {
int ret = atoi (res);
free (res);
return ret;
}
return false;
}

static int cmd_yank(void *data, const char *input) {
Expand Down Expand Up @@ -1830,7 +1840,12 @@ static int r_core_cmd_subst_i(RCore *core, char *cmd, char *colon) {
if (*cmd) {
r_core_cmd_pipe (core, cmd, ptr + 1);
} else {
r_io_system (core->io, ptr + 1);
char *res = r_io_system (core->io, ptr + 1);
if (res) {
r_cons_printf ("%s\n", res);
free (res);
}
free (res);
}
core->num->value = value;
return 0;
Expand Down Expand Up @@ -2968,7 +2983,11 @@ R_API int r_core_cmd(RCore *core, const char *cstr, int log) {
}
if (core->cmdremote) {
if (*cstr != '=' && *cstr != 'q' && strncmp (cstr, "!=", 2)) {
r_io_system (core->io, cstr);
char *res = r_io_system (core->io, cstr);
if (res) {
r_cons_printf ("%s\n", res);
free (res);
}
goto beach; // false
}
}
Expand Down
87 changes: 87 additions & 0 deletions libr/fs/p/fs_io.c
@@ -0,0 +1,87 @@
/* radare - LGPL - Copyright 2017 - pancake */

#include <r_fs.h>
#include <r_lib.h>
#include <sys/stat.h>

static RFSFile* fs_io_open(RFSRoot *root, const char *path) {
FILE *fd;
RFSFile *file = r_fs_file_new (root, path);
if (!file) {
return NULL;
}
file->ptr = NULL;
file->p = root->p;
fd = r_sandbox_fopen (path, "r");
if (fd) {
fseek (fd, 0, SEEK_END);
file->size = ftell (fd);
fclose (fd);
} else {
r_fs_file_free (file);
file = NULL;
}
return file;
}

static bool fs_io_read(RFSFile *file, ut64 addr, int len) {
free (file->data);
file->data = (void*)r_file_slurp_range (file->name, 0, len, NULL);
return false;
}

static void fs_io_close(RFSFile *file) {
//fclose (file->ptr);
}

static void append_file(RList *list, const char *name, int type, int time, ut64 size) {
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 RList *fs_io_dir(RFSRoot *root, const char *path, int view /*ignored*/) {
RList *list = r_list_new ();
if (!list) {
return NULL;
}
{
// snprintf (fullpath, sizeof (fullpath)-1, "%s/%s", path, de->d_name);
append_file (list, "file-a", 'f', 0, 0);
append_file (list, "file-b", 'f', 0, 12);
}
return list;
}

static int fs_io_mount(RFSRoot *root) {
root->ptr = NULL; // XXX: TODO
return true;
}

static void fs_io_umount(RFSRoot *root) {
root->ptr = NULL;
}

RFSPlugin r_fs_plugin_io = {
.name = "io",
.desc = "r_io based filesystem",
.open = fs_io_open,
.read = fs_io_read,
.close = fs_io_close,
.dir = &fs_io_dir,
.mount = fs_io_mount,
.umount = fs_io_umount,
};

#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_FS,
.data = &r_fs_plugin_io,
.version = R2_VERSION
};
#endif
9 changes: 9 additions & 0 deletions libr/fs/p/io.mk
@@ -0,0 +1,9 @@
OBJ_IO=fs_io.o

STATIC_OBJ+=${OBJ_IO}
TARGET_IO=fs_io.${EXT_SO}

ALL_TARGETS+=${TARGET_IO}

${TARGET_IO}: ${OBJ_IO}
${CC} $(call libname,fs_io) ${LDFLAGS} ${CFLAGS} -o ${TARGET_IO} ${OBJ_IO} ${EXTRA}
1 change: 1 addition & 0 deletions libr/include/r_fs.h
Expand Up @@ -126,6 +126,7 @@ R_API const char *r_fs_partition_type_get(int n);
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_ext2;
extern RFSPlugin r_fs_plugin_fat;
extern RFSPlugin r_fs_plugin_ntfs;
Expand Down
4 changes: 2 additions & 2 deletions libr/include/r_io.h
Expand Up @@ -141,7 +141,7 @@ typedef struct r_io_plugin_t {
RIOUndo undo;
bool isdbg;
// int (*is_file_opened)(RIO *io, RIODesc *fd, const char *);
int (*system)(RIO *io, RIODesc *fd, const char *);
char *(*system)(RIO *io, RIODesc *fd, const char *);
RIODesc* (*open)(RIO *io, const char *, int rw, int mode);
RList* /*RIODesc* */ (*open_many)(RIO *io, const char *, int rw, int mode);
int (*read)(RIO *io, RIODesc *fd, ut8 *buf, int count);
Expand Down Expand Up @@ -338,7 +338,7 @@ R_API bool r_io_read (RIO *io, ut8 *buf, int len);
R_API bool r_io_write (RIO *io, ut8 *buf, int len);
R_API ut64 r_io_size (RIO *io);
R_API bool r_io_is_listener (RIO *io);
R_API int r_io_system (RIO *io, const char* cmd);
R_API char *r_io_system (RIO *io, const char* cmd);
R_API bool r_io_resize (RIO *io, ut64 newsize);
R_API int r_io_extend_at (RIO *io, ut64 addr, ut64 size);
R_API bool r_io_set_write_mask (RIO *io, const ut8 *mask, int len);
Expand Down
4 changes: 2 additions & 2 deletions libr/io/io.c
Expand Up @@ -558,11 +558,11 @@ R_API bool r_io_is_listener(RIO* io) {
return false;
}

R_API int r_io_system(RIO* io, const char* cmd) {
R_API char *r_io_system(RIO* io, const char* cmd) {
if (io && io->desc && io->desc->plugin && io->desc->plugin->system) {
return io->desc->plugin->system (io, io->desc, cmd);
}
return -1;
return NULL;
}

R_API bool r_io_resize(RIO* io, ut64 newsize) {
Expand Down
8 changes: 3 additions & 5 deletions libr/io/p/io_bochs.c
@@ -1,4 +1,4 @@
// Copyright (c) 2016 - LGPL, SkUaTeR, All rights reserved.
// Copyright (c) 2016-2017 - LGPL, SkUaTeR, All rights reserved.

#include <r_io.h>
#include <r_lib.h>
Expand Down Expand Up @@ -90,7 +90,7 @@ static int __close(RIODesc *fd) {
return true;
}

static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
lprintf ("system command (%s)\n", cmd);
if (!strcmp (cmd, "help")) {
lprintf ("Usage: =!cmd args\n"
Expand All @@ -99,13 +99,11 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
lprintf ("io_system: Enviando comando bochs\n");
bochs_send_cmd (desc, &cmd[1], true);
io->cb_printf ("%s\n", desc->data);
return 1;
} else if (!strncmp (cmd, "dobreak", 7)) {
bochs_cmd_stop (desc);
io->cb_printf ("%s\n", desc->data);
return 1;
}
return true;
return NULL;
}

RIOPlugin r_io_plugin_bochs = {
Expand Down

0 comments on commit ca1b44e

Please sign in to comment.