Assertify the IO API a bit more

This commit is contained in:
pancake 2019-07-31 02:51:39 +02:00 committed by radare
parent 1de3f56d36
commit 0e31f4935e
3 changed files with 47 additions and 52 deletions

View File

@ -449,7 +449,7 @@ R_API bool r_core_project_open(RCore *core, const char *prjfile, bool thready) {
r_bin_file_delete_all (core->bin);
// open new file
// TODO: handle read/read-write mode
r_io_desc_init (core->io);
r_io_close_all (core->io);
if (filepath[0]) {
/* Old-style project without embedded on commands to open all files. */
if (!r_core_file_open (core, filepath, 0, UT64_MAX)) {

View File

@ -393,7 +393,6 @@ R_API void r_io_wundo_set_all(RIO *io, int set);
R_API int r_io_wundo_set(RIO *io, int n, int set);
//desc.c
R_API bool r_io_desc_init (RIO *io);
R_API RIODesc *r_io_desc_new (RIO *io, RIOPlugin *plugin, const char *uri, int flags, int mode, void *data);
R_API RIODesc *r_io_desc_open (RIO *io, const char *uri, int flags, int mode);
R_API RIODesc *r_io_desc_open_plugin (RIO *io, RIOPlugin *plugin, const char *uri, int flags, int mode);
@ -416,7 +415,10 @@ R_API int r_io_desc_get_tid (RIODesc *desc);
R_API bool r_io_desc_get_base (RIODesc *desc, ut64 *base);
R_API int r_io_desc_read_at (RIODesc *desc, ut64 addr, ut8 *buf, int len);
R_API int r_io_desc_write_at (RIODesc *desc, ut64 addr, const ut8 *buf, int len);
R_API bool r_io_desc_fini (RIO *io);
/* lifecycle */
R_IPI bool r_io_desc_init (RIO *io);
R_IPI bool r_io_desc_fini (RIO *io);
/* io/cache.c */
R_API int r_io_cache_invalidate(RIO *io, ut64 from, ut64 to);

View File

@ -1,26 +1,14 @@
/* radare2 - LGPL - Copyright 2017-2018 - condret, pancake, alvaro */
/* radare2 - LGPL - Copyright 2017-2019 - condret, pancake, alvaro */
#include <r_io.h>
#include <sdb.h>
#include <string.h>
R_API bool r_io_desc_init(RIO* io) {
if (!io || io->files) {
return false;
}
//fd is signed
io->files = r_id_storage_new (3, 0x80000000);
if (!io->files) {
return false;
}
return true;
}
//shall be used by plugins for creating descs
//XXX kill mode
R_API RIODesc* r_io_desc_new(RIO* io, RIOPlugin* plugin, const char* uri, int perm, int mode, void* data) {
ut32 fd32 = 0;
// this is because emscript is a bitch
// this is required for emscripten builds to work, but should assert
if (!io || !plugin || !uri) {
return NULL;
}
@ -57,14 +45,9 @@ R_API void r_io_desc_free(RIODesc* desc) {
}
R_API bool r_io_desc_add(RIO* io, RIODesc* desc) {
if (!desc || !io) {
return false;
}
//just for the case when plugins cannot use r_io_desc_new
if (!desc->io) {
desc->io = io;
}
r_return_val_if_fail (io && desc && desc->io, false);
if (!r_id_storage_set (io->files, desc, desc->fd)) {
// TODO: use assert here
eprintf ("You are using this API incorrectly\n");
eprintf ("fd %d was probably not generated by this RIO-instance\n", desc->fd);
r_sys_backtrace ();
@ -74,10 +57,8 @@ R_API bool r_io_desc_add(RIO* io, RIODesc* desc) {
}
R_API bool r_io_desc_del(RIO* io, int fd) { //can we pass this a riodesc and check if it belongs to the desc->io ?
RIODesc* desc;
if (!io || !io->files || !(desc = r_id_storage_get (io->files, fd))) {
return false;
}
r_return_val_if_fail (io && io->files, false);
RIODesc* desc = r_id_storage_get (io->files, fd);
r_io_desc_free (desc);
if (desc == io->desc) {
io->desc = NULL;
@ -88,14 +69,12 @@ R_API bool r_io_desc_del(RIO* io, int fd) { //can we pass this a riodesc and ch
}
R_API RIODesc* r_io_desc_get(RIO* io, int fd) {
if (!io || !io->files) {
return NULL;
}
r_return_val_if_fail (io && io->files, NULL);
return (RIODesc*) r_id_storage_get (io->files, fd);
}
R_API RIODesc *r_io_desc_open(RIO *io, const char *uri, int perm, int mode) {
r_return_val_if_fail (io && io->files && uri, NULL);
r_return_val_if_fail (io && uri, NULL);
RIOPlugin *plugin = r_io_plugin_resolve (io, uri, 0);
if (!plugin || !plugin->open) {
return NULL;
@ -114,14 +93,15 @@ R_API RIODesc *r_io_desc_open(RIO *io, const char *uri, int perm, int mode) {
if (!desc->plugin) {
desc->plugin = plugin;
}
r_io_desc_add (io, desc);
if (!r_io_desc_add (io, desc)) {
r_io_desc_free (desc);
return NULL;
}
return desc;
}
R_API RIODesc *r_io_desc_open_plugin(RIO *io, RIOPlugin *plugin, const char *uri, int perm, int mode) {
if (!io || !io->files || !uri) {
return NULL;
}
r_return_val_if_fail (io && io->files && uri, NULL);
if (!plugin || !plugin->open || !plugin->check || !plugin->check (io, uri, false)) {
return NULL;
}
@ -139,7 +119,10 @@ R_API RIODesc *r_io_desc_open_plugin(RIO *io, RIOPlugin *plugin, const char *uri
if (!desc->name) {
desc->name = strdup (uri);
}
r_io_desc_add (io, desc);
if (!r_io_desc_add (io, desc)) {
r_io_desc_free (desc);
return NULL;
}
return desc;
}
@ -162,10 +145,7 @@ R_API bool r_io_desc_close(RIODesc *desc) {
//returns length of written bytes
R_API int r_io_desc_write(RIODesc *desc, const ut8* buf, int len) {
//check pointers
if (!buf || !desc || !desc->plugin || len < 1) {
return 0;
}
r_return_val_if_fail (desc && buf && len > 0, -1);
//check pointers and pcache
if (desc->io && (desc->io->p_cache & 2)) {
return r_io_desc_cache_write (desc,
@ -331,6 +311,28 @@ R_API int r_io_desc_write_at(RIODesc *desc, ut64 addr, const ut8 *buf, int len)
return 0;
}
R_API int r_io_desc_extend(RIODesc *desc, ut64 size) {
if (desc && desc->plugin && desc->plugin->extend) {
return desc->plugin->extend (desc->io, desc, size);
}
return 0;
}
/* lifecycle */
// TODO: move into io.c : r_io_init
R_IPI bool r_io_desc_init(RIO* io) {
r_return_val_if_fail (io, false);
r_io_desc_fini (io);
// TODO: it leaks if called twice
//fd is signed
io->files = r_id_storage_new (3, 0x80000000);
if (!io->files) {
return false;
}
return true;
}
static bool desc_fini_cb(void* user, void* data, ut32 id) {
RIODesc* desc = (RIODesc*) data;
if (desc->plugin && desc->plugin->close) {
@ -340,18 +342,9 @@ static bool desc_fini_cb(void* user, void* data, ut32 id) {
return true;
}
R_API int r_io_desc_extend(RIODesc *desc, ut64 size) {
if (desc && desc->plugin && desc->plugin->extend) {
return desc->plugin->extend (desc->io, desc, size);
}
return 0;
}
//closes all descs and frees all descs and io->files
R_API bool r_io_desc_fini(RIO* io) {
if (!io || !io->files) {
return false;
}
R_IPI bool r_io_desc_fini(RIO* io) {
r_return_val_if_fail (io && io->files, NULL);
r_id_storage_foreach (io->files, desc_fini_cb, io);
r_id_storage_free (io->files);
io->files = NULL;