pancake wrote:
[...]
> Another modification you should do in r_io_shift() is to work in blocks
> before mallocing huge chunks in the heap. This is:
> - use local ut8 buf[4096]; or malloc to 64k (if you can do some
> benchmarks,
> we will see which block size is more optimal (4KB, 64KB, etc..)
I tried powers of 2 from 4KB to 512KB. On three test machines (2.2GHz
P4, 512MB RAM; 2GHz Core2, 1GB; 1GHz ARMv7, ~450MB), 64k gave the best
results. 32k was slightly better on an older box (Pentium 133MHz,
80MB), but that could also have been down to the disk. So I've gone
with 64k.
I also tried moving one smaller chunk before the main loop to ensure
that all the remaining reads were block-aligned, but that had negligible
effect, so I've left that complication out.
> core->file->size should be updated after resize(), inside the r_io
> api (not in the specific plugins)
I can't access core->file from inside r_io... there's another function
in core/file.c (r_core_file_resize) that's not used anywhere (and
treats 0 as a special case), but using a core wrapper like this would
add another layer of calls and save 0 LOC.
> Can you resend the patch with those modifications?
Attached.
Also attached is a fix for a Makefile where parallel building was
breaking.
Glyn
diff -r c695dc6194fe libr/core/cmd.c
--- a/libr/core/cmd.c Sat Feb 05 02:55:50 2011 +0100
+++ b/libr/core/cmd.c Sat Feb 05 09:40:48 2011 +0000
@@ -1393,6 +1393,7 @@
" w[mode] [arg] ; multiple write operations\n"
" x [len] ; alias for 'px' (print hexadecimal)\n"
" y [len] [off] ; yank/paste bytes from/to memory\n"
+ " r[+- ][len] ; resize file\n"
" ? [expr] ; help or evaluate math expression\n"
" /[xmp/] ; search for bytes, regexps, patterns, ..\n"
" ![cmd] ; run given command as in system(3)\n"
@@ -2749,6 +2750,55 @@
return 0;
}
+static int cmd_resize(void *data, const char *input) {
+ RCore *core = (RCore *)data;
+ st64 delta=0;
+ int grow;
+ ut64 oldsize,newsize;
+
+ oldsize = core->file->size;
+
+ switch (input[0]) {
+ case ' ':
+ newsize = r_num_math(core->num, input+1);
+ break;
+ case '+':
+ case '-':
+ delta = (st64)r_num_math(NULL, input);
+ newsize = oldsize + delta;
+ break;
+ case '?':
+ default:
+ r_cons_printf (
+ "Usage: r[ size|+insert|-remove]\n"
+ " r size set filesize to size, extending or
truncating\n"
+ " r-num remove num bytes, move following data down\n"
+ " r+num insert num bytes, move following data up\n");
+ return R_TRUE;
+ }
+
+ grow = (newsize > oldsize);
+
+ if (grow) {
+ r_io_resize (core->io, newsize);
+ core->file->size = newsize;
+ }
+
+ if (delta && core->offset < newsize)
+ r_io_shift (core->io, core->offset, grow?newsize:oldsize,
delta);
+
+ if (!grow) {
+ r_io_resize (core->io, newsize);
+ core->file->size = newsize;
+ }
+
+ if (newsize < core->offset+core->blocksize ||
+ oldsize < core->offset+core->blocksize)
+ r_core_block_read (core, 0);
+
+ return R_TRUE;
+}
+
static const char *cmdhit = NULL;
static const char *searchprefix = NULL;
static int __cb_hit(RSearchKeyword *kw, void *user, ut64 addr) {
@@ -4316,6 +4366,7 @@
r_cmd_add (core->cmd, "Project", "project", &cmd_project);
r_cmd_add (core->cmd, "open", "open or map file", &cmd_open);
r_cmd_add (core->cmd, "yank", "yank bytes", &cmd_yank);
+ r_cmd_add (core->cmd, "resize", "change file size", &cmd_resize);
r_cmd_add (core->cmd, "Visual", "enter visual mode", &cmd_visual);
r_cmd_add (core->cmd, "!", "run system command", &cmd_system);
r_cmd_add (core->cmd, "=", "io pipe", &cmd_rap);
diff -r c695dc6194fe libr/core/core.c
--- a/libr/core/core.c Sat Feb 05 02:55:50 2011 +0100
+++ b/libr/core/core.c Sat Feb 05 09:40:48 2011 +0000
@@ -91,6 +91,7 @@
"y", "yy", "y?",
"wx", "ww", "wf", "w?",
"pc", "pD", "px", "pX", "po", "pm", "pr", "pt", "ps", "pz", "pr >",
"pu", "pU", "p?",
+ "r", "r+", "r-",
NULL
};
diff -r c695dc6194fe libr/include/r_io.h
--- a/libr/include/r_io.h Sat Feb 05 02:55:50 2011 +0100
+++ b/libr/include/r_io.h Sat Feb 05 09:40:48 2011 +0000
@@ -201,6 +201,7 @@
R_API ut64 r_io_size(RIO *io); //, int fd);
R_API int r_io_resize(struct r_io_t *io, ut64 newsize);
R_API int r_io_accept(RIO *io, int fd);
+R_API int r_io_shift(RIO *io, ut64 start, ut64 end, st64 move);
/* io/cache.c */
R_API void r_io_cache_commit(RIO *io);
diff -r c695dc6194fe libr/io/io.c
--- a/libr/io/io.c Sat Feb 05 02:55:50 2011 +0100
+++ b/libr/io/io.c Sat Feb 05 09:40:48 2011 +0000
@@ -368,3 +368,36 @@
}
return R_FALSE;
}
+
+/* moves bytes up (+) or down (-) within the specified range */
+R_API int r_io_shift(RIO *io, ut64 start, ut64 end, st64 move) {
+ ut64 rest, src, shiftsize;
+ ut64 chunksize=0x10000;
+ ut8 *buf;
+
+ shiftsize = r_num_abs(move);
+ if (!shiftsize || (end-start) <= shiftsize ) return R_FALSE;
+ rest = (end-start) - shiftsize;
+
+ if (!(buf = malloc (chunksize))) return R_FALSE;
+
+ if (move>0)
+ src = end-shiftsize;
+ else
+ src = start+shiftsize;
+
+ while (rest>0) {
+ if (chunksize>rest) chunksize=rest;
+ if (move>0) src -= chunksize;
+
+ r_io_read_at(io, src, buf, chunksize);
+ r_io_write_at(io, src+move, buf, chunksize);
+
+ if (move<0) src += chunksize;
+ rest -= chunksize;
+ }
+
+ free(buf);
+
+ return R_TRUE;
+}
diff -r c695dc6194fe libr/fs/p/grub/Makefile
--- a/libr/fs/p/grub/Makefile Sat Feb 05 02:55:50 2011 +0100
+++ b/libr/fs/p/grub/Makefile Sat Feb 05 10:06:23 2011 +0000
@@ -36,9 +36,9 @@
#CFLAGS+=-fnested-functions
BIN=test${EXT_EXE}
-all: ${KERNOBJS} main.o ${BIN}
+all: ${BIN}
-${BIN}:
+${BIN}: ${KERNOBJS} main.o
${CC} -o ${BIN} main.o ${CFLAGS} ${KERNOBJS}
lib: all libgrubfs.a
_______________________________________________
radare mailing list
[email protected]
http://lists.nopcode.org/listinfo.cgi/radare-nopcode.org