Skip to content

Commit

Permalink
Adjust map boundaries on RIODesc resize ##io
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Nov 18, 2023
1 parent e62e844 commit 1e0ee5c
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions libr/io/io_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,49 @@ R_API ut64 r_io_desc_size(RIODesc* desc) {
return ret;
}

typedef struct desc_map_resize_t {
RIO *io;
RQueue *del;
ut64 size;
int fd;
} DescMapResize;

static bool _resize_affected_maps (void *user, void *data, ut32 id) {
DescMapResize *dmr = (DescMapResize *)user;
RIOMap *map = (RIOMap *)data;
if (map->fd == dmr->fd) {
if (map->delta >= dmr->size) {
r_queue_enqueue (dmr->del, map);
return true;
}
if ((map->delta + r_io_map_size (map)) > dmr->size) {
r_io_map_resize (dmr->io, id, dmr->size - map->delta);
}
}
return true;
}

R_API bool r_io_desc_resize(RIODesc *desc, ut64 newsize) {
if (desc && desc->plugin && desc->plugin->resize) {
bool ret = desc->plugin->resize (desc->io, desc, newsize);
if (desc && desc->plugin && desc->plugin->resize && desc->plugin->seek) {
const ut64 osize = r_io_desc_size (desc);
if (!desc->plugin->resize (desc->io, desc, newsize)) {
return false;
}
if (osize < newsize) {
return true;
}
if (desc->io && desc->io->p_cache) {
r_io_desc_cache_cleanup (desc);
}
return ret;
DescMapResize dmr = {desc->io, r_queue_new (1), newsize, desc->fd};
if (desc->io->maps) {
r_id_storage_foreach (desc->io->maps, _resize_affected_maps, &dmr);
while (!r_queue_is_empty (dmr.del)) {
r_io_map_del (desc->io, ((RIOMap *)r_queue_dequeue (dmr.del))->id);
}
}
r_queue_free (dmr.del);
return true;
}
return false;
}
Expand Down

0 comments on commit 1e0ee5c

Please sign in to comment.