radare2/libr/util/file.c

537 lines
11 KiB
C
Raw Normal View History

2013-01-23 17:41:31 +00:00
/* radare - LGPL - Copyright 2007-2013 - pancake */
#include "r_types.h"
#include "r_util.h"
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#if __UNIX__
#include <sys/mman.h>
#endif
R_API const char *r_file_basename (const char *path) {
const char *ptr = strrchr (path, '/');
if (ptr) path = ptr + 1;
return path;
}
R_API boolt r_file_is_regular(const char *str) {
struct stat buf = {0};
if (!str||!*str)
return R_FALSE;
if (stat (str, &buf)==-1)
return R_FALSE;
return ((S_IFREG & buf.st_mode)==S_IFREG)? R_TRUE: R_FALSE;
}
R_API boolt r_file_is_directory(const char *str) {
struct stat buf = {0};
if (!str||!*str)
return R_FALSE;
if (stat (str, &buf)==-1)
return R_FALSE;
2014-01-21 01:08:51 +00:00
if ((S_IFBLK & buf.st_mode) == S_IFBLK)
return R_FALSE;
2014-01-21 01:08:51 +00:00
return (S_IFDIR==(S_IFDIR & buf.st_mode))? R_TRUE: R_FALSE;
}
R_API boolt r_file_fexists(const char *fmt, ...) {
int ret;
char string[1024];
va_list ap;
va_start (ap, fmt);
vsnprintf (string, sizeof (string), fmt, ap);
ret = r_file_exists (string);
va_end (ap);
return ret;
}
2012-09-06 06:59:13 +00:00
R_API boolt r_file_exists(const char *str) {
struct stat buf = {0};
if (str && *str && stat (str, &buf)==-1)
return R_FALSE;
return (S_ISREG (buf.st_mode))? R_TRUE: R_FALSE;
}
R_API int r_file_size(const char *str) {
struct stat buf = {0};
if (stat (str, &buf)==-1)
return 0;
return buf.st_size;
}
R_API char *r_file_abspath(const char *file) {
char *ret = NULL;
char *cwd = r_sys_getdir ();
if (!memcmp (file, "~/", 2))
return r_str_home (file+2);
2010-02-20 05:40:02 +00:00
#if __UNIX__
if (cwd && *file != '/')
ret = r_str_newf ("%s/%s", cwd, file);
2010-02-20 05:40:02 +00:00
#elif __WINDOWS__
if (cwd && !strchr (file, ':'))
ret = r_str_newf ("%s\\%s", cwd, file);
2010-02-20 05:40:02 +00:00
#endif
free (cwd);
// TODO: remove // and ./
return ret? ret: strdup (file);
2010-02-20 05:40:02 +00:00
}
R_API char *r_file_path(const char *bin) {
char file[1024];
char *path_env = (char *)r_sys_getenv ("PATH");
char *path = NULL;
char *str, *ptr;
if (path_env) {
str = path = strdup (path_env);
do {
ptr = strchr (str, ':');
if (ptr) {
*ptr = '\0';
snprintf (file, sizeof (file), "%s/%s", str, bin);
2012-09-06 06:59:13 +00:00
if (r_file_exists (file)) {
free (path);
2011-08-27 22:01:03 +00:00
free (path_env);
return strdup (file);
}
str = ptr + 1;
}
} while (ptr);
2011-08-27 22:01:03 +00:00
}
free (path_env);
free (path);
return strdup (bin);
}
R_API char *r_file_slurp(const char *str, int *usz) {
size_t rsz;
char *ret;
FILE *fd;
long sz;
2012-09-06 06:59:13 +00:00
if (!r_file_exists (str))
return NULL;
fd = r_sandbox_fopen (str, "rb");
if (fd == NULL)
return NULL;
fseek (fd, 0, SEEK_END);
sz = ftell (fd);
if (sz <0) {
fclose (fd);
return NULL;
}
fseek (fd, 0, SEEK_SET);
ret = (char *)malloc (sz+1);
rsz = fread (ret, 1, sz, fd);
if (rsz != sz)
eprintf ("r_file_slurp: fread: error\n");
fclose (fd);
ret[sz]='\0';
if (usz)
*usz = (ut32)sz;
return ret;
}
R_API ut8 *r_file_slurp_hexpairs(const char *str, int *usz) {
ut8 *ret;
long sz;
int c, bytes = 0;
FILE *fd = r_sandbox_fopen (str, "r");
if (fd == NULL)
return NULL;
fseek (fd, 0, SEEK_END);
sz = ftell (fd);
fseek (fd, 0, SEEK_SET);
ret = (ut8*)malloc ((sz>>1)+1);
2014-03-08 06:41:22 +00:00
if (!ret) {
fclose (fd);
return NULL;
2014-03-08 06:41:22 +00:00
}
for (;;) {
if (fscanf (fd, " #%*[^\n]") == 1)
continue;
if (fscanf (fd, "%02x", &c) == 1) {
ret[bytes++] = c;
continue;
}
if (feof (fd))
break;
free (ret);
2014-03-08 06:41:22 +00:00
fclose (fd);
return NULL;
}
ret[bytes] = '\0';
fclose (fd);
if (usz) *usz = bytes;
return ret;
}
R_API char *r_file_slurp_range(const char *str, ut64 off, int sz, int *osz) {
char *ret;
2014-03-08 06:41:22 +00:00
size_t read_items;
FILE *fd = r_sandbox_fopen (str, "rb");
if (fd == NULL)
return NULL;
// XXX handle out of bound reads (eof)
2014-03-08 06:41:22 +00:00
if (fseek (fd, off, SEEK_SET) < 0) {
fclose (fd);
return NULL;
}
ret = (char *)malloc (sz+1);
if (ret != NULL) {
if (osz)
*osz = (int)(size_t)fread (ret, 1, sz, fd);
2014-03-08 06:41:22 +00:00
else {
read_items = fread (ret, 1, sz, fd);
if (!read_items) {
fclose (fd);
return ret;
}
}
ret[sz] = '\0';
}
fclose (fd);
return ret;
}
R_API char *r_file_slurp_random_line(const char *file) {
char *ptr = NULL, *str;
int sz, i, lines = 0;
struct timeval tv;
if ((str = r_file_slurp (file, &sz))) {
gettimeofday (&tv,NULL);
srand (getpid()+tv.tv_usec);
for (i=0; str[i]; i++)
if (str[i]=='\n')
lines++;
if (lines>0) {
lines = (rand()%lines);
for (i=0; str[i] && lines; i++)
if (str[i]=='\n')
lines--;
ptr = str+i;
for (i=0; ptr[i]; i++)
if (ptr[i]=='\n') {
ptr[i]='\0';
break;
}
ptr = strdup (ptr);
}
free (str);
}
return ptr;
}
R_API char *r_file_slurp_line(const char *file, int line, int context) {
int i, lines = 0;
int sz;
2010-04-20 17:32:04 +00:00
char *ptr = NULL, *str = r_file_slurp (file, &sz);
// TODO: Implement context
if (str) {
for (i=0; str[i]; i++)
if (str[i]=='\n')
lines++;
if (line > lines) {
free (str);
return NULL;
}
lines = line;
for (i=0; str[i]&&lines; i++)
if (str[i]=='\n')
lines--;
ptr = str+i;
for (i=0; ptr[i]; i++)
if (ptr[i]=='\n') {
ptr[i]='\0';
break;
}
ptr = strdup (ptr);
free (str);
}
return ptr;
}
R_API char *r_file_root(const char *root, const char *path) {
char *ret, *s = r_str_replace (strdup (path), "..", "", 1);
// XXX ugly hack
while (strstr (s, "..")) s = r_str_replace (s, "..", "", 1);
while (strstr (s, "./")) s = r_str_replace (s, "./", "", 1);
while (strstr (s, "//")) s = r_str_replace (s, "//", "", 1);
ret = r_str_concat (strdup (root), "/");
ret = r_str_concat (ret, s);
free (s);
return ret;
}
R_API boolt r_file_dump(const char *file, const ut8 *buf, int len) {
int ret;
FILE *fd;
if (!file || !*file || !buf)
return R_FALSE;
fd = r_sandbox_fopen (file, "wb");
if (fd == NULL) {
eprintf ("Cannot open '%s' for writing\n", file);
return R_FALSE;
}
2010-11-09 17:09:37 +00:00
ret = fwrite (buf, 1, len, fd) == len;
if (!ret) eprintf ("r_file_dump: fwrite: error\n");
fclose (fd);
return ret;
}
R_API boolt r_file_rm(const char *file) {
if (r_sandbox_enable (0)) return R_FALSE;
#if __WINDOWS__
return (DeleteFile (file)==0)? R_TRUE: R_FALSE;
#else
return (unlink (file)==0)? R_TRUE: R_FALSE;
#endif
}
R_API boolt r_file_rmrf(const char *file) {
2013-09-25 18:34:01 +00:00
if (r_sandbox_enable (0))
return R_FALSE;
else {
char *nfile = strdup (file);
nfile[ strlen (nfile)-1 ] = '_';
nfile[ strlen (nfile)-2 ] = '_';
2014-03-08 06:41:22 +00:00
if (rename (file, nfile)) {
return R_FALSE;
}
2013-09-25 18:34:01 +00:00
eprintf ("mv %s %s\n", file, nfile);
free (nfile);
return R_TRUE;
}
}
R_API int r_file_mmap_write(const char *file, ut64 addr, const ut8 *buf, int len) {
#if __WINDOWS__
2013-03-12 12:24:48 +00:00
HANDLE fm, fh;
if (r_sandbox_enable (0)) return -1;
fh = CreateFile (file, GENERIC_WRITE,
2013-03-12 12:24:48 +00:00
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
return -1;
}
fm = CreateFileMapping (fh, NULL,
PAGE_READWRITE, 0, 0, NULL);
2013-03-12 12:24:48 +00:00
if (fm == NULL) {
CloseHandle (fh);
return -1;
}
2013-03-12 12:24:48 +00:00
if (fm != INVALID_HANDLE_VALUE) {
ut8 *obuf = MapViewOfFile (fm,
FILE_MAP_READ|FILE_MAP_WRITE,
0, 0, len);
2013-03-12 12:24:48 +00:00
memcpy (obuf, buf, len);
UnmapViewOfFile (obuf);
}
CloseHandle (fh);
CloseHandle (fm);
2013-08-26 23:18:42 +00:00
return len;
2013-03-12 12:24:48 +00:00
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDWR|O_SYNC, 0644);
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
ut8 *mmap_buf;
if (fd == -1) return -1;
mmap_buf = mmap (NULL, mmlen*2, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, (off_t)addr-rest);
if (((int)(size_t)mmap_buf)==-1)
return -1;
memcpy (mmap_buf+rest, buf, len);
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
2013-03-12 12:24:48 +00:00
#else
return -1;
#endif
}
R_API int r_file_mmap_read (const char *file, ut64 addr, ut8 *buf, int len) {
#if __WINDOWS__
2013-03-12 12:24:48 +00:00
HANDLE fm, fh;
2013-03-12 12:24:48 +00:00
if (r_sandbox_enable (0)) return -1;
fh = CreateFile (file, GENERIC_READ,
2013-03-12 12:24:48 +00:00
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2013-03-12 12:24:48 +00:00
if (fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
return -1;
}
fm = CreateFileMapping (fh, NULL,
PAGE_READONLY, 0, 0, NULL);
2013-03-12 12:24:48 +00:00
if (fm == NULL) {
CloseHandle (fh);
return -1;
}
2013-03-12 12:24:48 +00:00
if (fm != INVALID_HANDLE_VALUE) {
ut8 *obuf = MapViewOfFile (fm,
FILE_MAP_READ,
0, 0, len);
2013-03-12 12:24:48 +00:00
memcpy (obuf, buf, len);
UnmapViewOfFile (obuf);
}
CloseHandle (fh);
CloseHandle (fm);
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDONLY, 0644);
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
ut8 *mmap_buf;
if (fd == -1) return -1;
mmap_buf = mmap (NULL, mmlen*2, PROT_READ,
MAP_SHARED, fd, (off_t)addr-rest);
if (((int)(size_t)mmap_buf)==-1)
return -1;
memcpy (buf, mmap_buf+rest, len);
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#endif
return 0;
}
// TODO: add rwx support?
R_API RMmap *r_file_mmap (const char *file, boolt rw, ut64 base) {
2013-03-12 12:24:48 +00:00
RMmap *m;
int fd = r_sandbox_open (file, rw? O_RDWR: O_RDONLY, 0644);
2013-03-12 12:24:48 +00:00
if (fd == -1) return NULL;
m = R_NEW (RMmap);
if (!m) {
close (fd);
return NULL;
}
m->base = base;
m->rw = rw;
m->fd = fd;
m->len = lseek (fd, (off_t)0, SEEK_END);
2014-03-08 06:41:22 +00:00
if (m->len == (off_t)-1) {
close (fd);
R_FREE (m);
return NULL;
}
#if __UNIX__
2013-03-12 12:24:48 +00:00
m->buf = mmap (NULL, m->len,
rw?PROT_READ|PROT_WRITE:PROT_READ,
MAP_SHARED, fd, (off_t)base);
if (m->buf == MAP_FAILED) {
free (m);
m = NULL;
}
#elif __WINDOWS__
2013-03-12 12:24:48 +00:00
close (fd);
m->fh = CreateFile (file, rw?GENERIC_WRITE:GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (m->fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
free (m);
return NULL;
}
m->fm = CreateFileMapping (m->fh, NULL,
rw?PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
if (m->fm == NULL) {
CloseHandle (m->fh);
free (m);
return NULL;
}
if (m->fm != INVALID_HANDLE_VALUE) {
m->buf = MapViewOfFile (m->fm, rw?
FILE_MAP_READ|FILE_MAP_WRITE:FILE_MAP_READ,
UT32_HI (base), UT32_LO (base), 0);
} else {
CloseHandle (m->fh);
free (m);
m = NULL;
}
#else
2013-03-12 12:24:48 +00:00
m->buf = malloc (m->len);
if (m->buf) {
lseek (fd, (off_t)0, SEEK_SET);
read (fd, m->buf, m->len);
} else {
free (m);
m = NULL;
}
2013-03-12 12:24:48 +00:00
#endif
return m;
}
R_API void r_file_mmap_free (RMmap *m) {
if (!m) return;
#if __UNIX__
munmap (m->buf, m->len);
#elif __WINDOWS__
CloseHandle (m->fm);
CloseHandle (m->fh);
UnmapViewOfFile (m->buf);
#endif
close (m->fd);
free (m);
}
R_API char *r_file_temp (const char *prefix) {
int namesz;
char *name;
2011-08-27 22:01:03 +00:00
char *path = r_file_tmpdir ();
namesz = strlen (prefix) + strlen (path) + 32;
name = malloc (namesz);
snprintf (name, namesz, "%s/%s.%"PFMT64x, path, prefix, r_sys_now ());
2011-08-27 22:01:03 +00:00
free (path);
return name;
}
R_API int r_file_mkstemp (const char *prefix, char **oname) {
int h;
2011-08-27 22:01:03 +00:00
char *path = r_file_tmpdir ();
char name[1024];
2014-03-08 06:41:22 +00:00
mode_t mask;
#if __WINDOWS__
if (GetTempFileName (path, prefix, 0, name))
h = r_sandbox_open (name, O_RDWR|O_EXCL|O_BINARY, 0644);
else h = -1;
#else
snprintf (name, sizeof (name), "%s/%sXXXXXX", path, prefix);
2014-03-08 20:01:04 +00:00
mask = umask(S_IWGRP | S_IWOTH);
h = mkstemp (name);
2014-03-08 06:41:22 +00:00
umask(mask);
#endif
if (oname) *oname = (h!=-1)? strdup (name): NULL;
2011-08-27 22:01:03 +00:00
free (path);
return h;
}
2011-05-03 17:36:06 +00:00
2011-08-27 22:01:03 +00:00
R_API char *r_file_tmpdir() {
2011-05-03 17:36:06 +00:00
#if __WINDOWS__
2011-08-27 22:01:03 +00:00
char *path = r_sys_getenv ("TEMP");
if (!path) path = strdup ("C:\\WINDOWS\\Temp\\");
#elif __ANDROID__
char *path = strdup ("/data/data/org.radare.installer/radare2/tmp");
2011-05-03 17:36:06 +00:00
#else
2011-08-27 22:01:03 +00:00
char *path = r_sys_getenv ("TMPDIR");
if (path && !*path) {
free (path);
path = NULL;
}
2011-08-27 22:01:03 +00:00
if (!path) path = strdup ("/tmp");
2011-05-03 17:36:06 +00:00
#endif
if (!r_file_is_directory (path)) {
eprintf ("Cannot find temporary directory '%s'\n", path);
}
2011-05-03 17:36:06 +00:00
return path;
}