Add support for UNIX Character devices

This commit is contained in:
pancake 2015-02-16 22:40:05 +01:00
parent a8e50839ee
commit ae367b65be
4 changed files with 34 additions and 1 deletions

View File

@ -120,6 +120,8 @@ static void r_core_file_info (RCore *core, int mode) {
pair ("referer", cf->desc->referer);
pair ("fd", sdb_fmt (0, "%d", cf->desc->fd));
pair ("size", sdb_fmt (0,"0x%"PFMT64x, r_io_desc_size (core->io, cf->desc)));
pair ("blksize", sdb_fmt (0, "0x%"PFMT64x,
(ut64)core->io->desc->obsz));
pair ("mode", r_str_rwx_i (cf->desc->flags & 7));
pair ("uri", cf->desc->uri);
}

View File

@ -64,6 +64,7 @@ typedef struct r_io_desc_t {
int fd;
int flags;
int state;
int obsz; // optimal block size
char *uri;
char *referer;
char *name;

View File

@ -741,11 +741,19 @@ R_API int r_io_is_blockdevice (RIO *io) {
#if __UNIX__
if (io && io->desc && io->desc->fd) {
struct stat buf;
if (io->desc->obsz) {
return 1;
}
if (fstat (io->desc->fd , &buf)==-1)
return 0;
if (io->plugin == &r_io_plugin_default) {
// TODO: optimal blocksize = 2048 for disk, 4096 for files
//eprintf ("OPtimal blocksize : %d\n", buf.st_blksize);
// usually is 128K
// eprintf ("OPtimal blocksize : %d\n", buf.st_blksize);
if ((buf.st_mode & S_IFCHR) == S_IFCHR) {
io->desc->obsz = buf.st_blksize;
return 1;
}
return ((buf.st_mode & S_IFBLK) == S_IFBLK);
}
}

View File

@ -147,11 +147,33 @@ static int r_io_def_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
// in this case we fallback reopening in raw mode
return -1;
}
if (io->off==UT64_MAX) {
memset (buf, 0xff, count);
return count;
}
mmo = fd->data;
if (!mmo)
return -1;
if (fd->obsz) {
char *a_buf;
ssize_t a_count;
// only do aligned reads in aligned offsets
const int aligned = 512; // XXX obey fd->obsz? or it may be too slow? 128K..
ut64 a_off = (io->off >> 9 ) << 9; //- (io->off & aligned);
int a_delta = io->off - a_off;
a_count = ((count + aligned)>>9)<<9;
a_buf = malloc (a_count);
if (a_buf) {
memset (a_buf, 0xff, a_count);
lseek (mmo->fd, a_off, SEEK_SET);
(void)read (mmo->fd, a_buf, a_count);
memcpy (buf, a_buf+a_delta, count);
} else {
memset (buf, 0xff, count);
}
return count;
}
if (mmo->rawio) {
return read (mmo->fd, buf, count);
}