Skip to content

Commit 397faba

Browse files
committed
Fix nocache:// uri handler ##io
1 parent 9c9530e commit 397faba

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

libr/io/p/io_default.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,9 @@ static bool mmap_refresh(RIOMMapFileObj *mmo) {
7878
}
7979
if (mmo->rawio) {
8080
mmo->fd = open_file (mmo->filename, mmo->perm, mmo->mode);
81-
if (mmo->nocache) {
82-
#ifdef F_NOCACHE
83-
fcntl (mmo->fd, F_NOCACHE, 1);
84-
#endif
85-
}
86-
return mmo->fd != -1;
81+
goto done;
8782
}
88-
int fd = open_file (mmo->filename, mmo->perm, mmo->mode);
83+
const int fd = open_file (mmo->filename, mmo->perm, mmo->mode);
8984
if (fd == -1) {
9085
return false;
9186
}
@@ -96,6 +91,7 @@ static bool mmap_refresh(RIOMMapFileObj *mmo) {
9691
}
9792
mmo->rawio = true;
9893
mmo->fd = fd;
94+
done:
9995
#ifdef F_NOCACHE
10096
if (mmo->nocache) {
10197
fcntl (mmo->fd, F_NOCACHE, 1);
@@ -121,8 +117,12 @@ static RIOMMapFileObj *mmap_create(RIO *io, const char *filename, int perm, int
121117
} else if (r_str_startswith (filename, "stdio://")) {
122118
filename += strlen ("stdio://");
123119
mmo->rawio = true;
120+
} else if (r_str_startswith (filename, "nocache://")) {
121+
mmo->rawio = true;
122+
mmo->nocache = true;
123+
} else {
124+
// TODO later: mmo->rawio = true;
124125
}
125-
mmo->nocache = r_str_startswith (filename, "nocache://");
126126
if (mmo->nocache) {
127127
filename += strlen ("nocache://");
128128
}
@@ -162,6 +162,8 @@ static bool uricheck(const char *filename) {
162162
filename += strlen ("file://");
163163
} else if (r_str_startswith (filename, "stdio://")) {
164164
filename += strlen ("stdio://");
165+
} else if (r_str_startswith (filename, "nocache://")) {
166+
filename += strlen ("nocache://");
165167
} else {
166168
return false;
167169
}
@@ -176,9 +178,6 @@ static int r_io_def_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
176178
memset (buf, io->Oxff, count);
177179
return count;
178180
}
179-
if (!mmo) {
180-
return -1;
181-
}
182181
if (mmo->rawio) {
183182
if (lseek (mmo->fd, mmo->addr, SEEK_SET) < 0) {
184183
return -1;
@@ -210,7 +209,7 @@ static int mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
210209
return write (mmo->fd, buf, count);
211210
}
212211

213-
if (mmo && mmo->buf) {
212+
if (mmo->buf) {
214213
if (!(mmo->perm & R_PERM_W)) {
215214
return -1;
216215
}
@@ -222,6 +221,7 @@ static int mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
222221

223222
int len = r_file_mmap_write (mmo->filename, addr, buf, count);
224223
if (len != count) {
224+
// XXX this is wrong. what about non-fd baked mmos?
225225
// aim to hack some corner cases?
226226
if (lseek (fd->fd, addr, 0) < 0) {
227227
return -1;

libr/io/p/io_mmap.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/* radare - LGPL - Copyright 2013-2024 - pancake */
1+
/* radare - LGPL - Copyright 2013-2025 - pancake */
22

33
#include <r_userconf.h>
44
#include <r_io.h>
55
#include <r_lib.h>
66

77
typedef struct r_io_mmo_t {
8-
char * filename;
8+
char *filename;
99
int mode;
1010
int flags;
1111
int fd;
1212
int opened;
1313
ut8 modified;
1414
RBuffer *buf;
15-
RIO * io_backref;
15+
RIO *io_backref;
1616
} RIOMMapFileObj;
1717

1818
static ut64 r_io_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence) {
@@ -21,16 +21,16 @@ static ut64 r_io_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence
2121
case R_IO_SEEK_SET:
2222
seek_val = (r_buf_size (mmo->buf) < offset)? r_buf_size (mmo->buf): offset;
2323
r_buf_seek (mmo->buf, io->off = seek_val, R_BUF_SET);
24-
return seek_val;
24+
break;
2525
case R_IO_SEEK_CUR:
2626
seek_val = (r_buf_size (mmo->buf) < (offset + r_buf_tell (mmo->buf)))? r_buf_size (mmo->buf):
2727
offset + r_buf_tell (mmo->buf);
2828
r_buf_seek (mmo->buf, io->off = seek_val, R_BUF_SET);
29-
return seek_val;
29+
break;
3030
case R_IO_SEEK_END:
3131
seek_val = r_buf_size (mmo->buf);
3232
r_buf_seek (mmo->buf, io->off = seek_val, R_BUF_SET);
33-
return seek_val;
33+
break;
3434
}
3535
return seek_val;
3636
}
@@ -50,18 +50,16 @@ static bool r_io_mmap_refresh_buf(RIOMMapFileObj *mmo) {
5050
}
5151

5252
static void r_io_mmap_free(RIOMMapFileObj *mmo) {
53-
free (mmo->filename);
54-
r_buf_free (mmo->buf);
55-
memset (mmo, 0, sizeof (RIOMMapFileObj));
56-
free (mmo);
53+
if (mmo) {
54+
free (mmo->filename);
55+
r_buf_free (mmo->buf);
56+
free (mmo);
57+
}
5758
}
5859

5960
RIOMMapFileObj *r_io_mmap_create_new_file(RIO *io, const char *filename, int mode, int flags) {
6061
R_RETURN_VAL_IF_FAIL (io && filename, NULL);
6162
RIOMMapFileObj *mmo = R_NEW0 (RIOMMapFileObj);
62-
if (!mmo) {
63-
return NULL;
64-
}
6563
mmo->filename = strdup (filename);
6664
mmo->fd = r_num_rand (0xFFFF); // XXX: Use r_io_fd api
6765
mmo->mode = mode;
@@ -95,23 +93,19 @@ static int r_io_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
9593
}
9694

9795
static int r_io_mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
98-
RIOMMapFileObj *mmo;
99-
int len = count;
100-
ut64 addr;
101-
10296
if (!io || !fd || !fd->data || !buf) {
10397
return -1;
10498
}
105-
mmo = fd->data;
106-
addr = io->off;
99+
RIOMMapFileObj *mmo = fd->data;
100+
ut64 addr = io->off;
107101
if (!(mmo->flags & R_PERM_W)) {
108102
return -1;
109103
}
110104
if ((count + addr > r_buf_size (mmo->buf)) || r_buf_size (mmo->buf) == 0) {
111105
ut64 sz = count + addr;
112106
r_file_truncate (mmo->filename, sz);
113107
}
114-
len = r_file_mmap_write (mmo->filename, io->off, buf, len);
108+
int len = r_file_mmap_write (mmo->filename, io->off, buf, count);
115109
if (!r_io_mmap_refresh_buf (mmo) ) {
116110
R_LOG_ERROR ("failed to refresh the mmap backed buffer");
117111
// XXX - not sure what needs to be done here (error handling).

0 commit comments

Comments
 (0)