mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-27 09:35:49 +00:00
Add RFile.new and RFile.move APIs ##util
This commit is contained in:
parent
cf91324937
commit
898173aa38
@ -42,15 +42,14 @@ static void cmd_project_init(RCore *core, RCmdDesc *parent) {
|
||||
|
||||
static int cmd_project(void *data, const char *input) {
|
||||
RCore *core = (RCore *) data;
|
||||
const char *file, *arg;
|
||||
const char *file;
|
||||
const char *fileproject = r_config_get (core->config, "prj.name");
|
||||
char *str = NULL;
|
||||
|
||||
if (!input) {
|
||||
return false;
|
||||
}
|
||||
str = strdup (fileproject);
|
||||
arg = strchr (input, ' ');
|
||||
char *str = strdup (fileproject);
|
||||
const char *arg = strchr (input, ' ');
|
||||
if (arg) {
|
||||
arg++;
|
||||
} else {
|
||||
|
@ -750,12 +750,11 @@ R_API bool r_core_project_save_script(RCore *core, const char *file, int opts) {
|
||||
R_API bool r_core_project_save(RCore *core, const char *prj_name) {
|
||||
bool scr_null = false;
|
||||
bool ret = true;
|
||||
char *script_path, *prj_dir;
|
||||
SdbListIter *it;
|
||||
SdbNs *ns;
|
||||
char *old_prj_name = NULL;
|
||||
r_return_val_if_fail (prj_name && *prj_name, false);
|
||||
script_path = get_project_script_path (core, prj_name);
|
||||
char *script_path = get_project_script_path (core, prj_name);
|
||||
if (r_config_get_i (core->config, "cfg.debug")) {
|
||||
eprintf ("radare2 does not support projects on debugged bins\n");
|
||||
return false;
|
||||
@ -764,7 +763,8 @@ R_API bool r_core_project_save(RCore *core, const char *prj_name) {
|
||||
eprintf ("Invalid project name '%s'\n", prj_name);
|
||||
return false;
|
||||
}
|
||||
if (r_str_endswith (script_path, R_SYS_DIR "rc")) {
|
||||
char *prj_dir = NULL;
|
||||
if (r_str_endswith (script_path, R_SYS_DIR "rc.r2")) {
|
||||
/* new project format */
|
||||
prj_dir = r_file_dirname (script_path);
|
||||
} else {
|
||||
@ -780,7 +780,7 @@ R_API bool r_core_project_save(RCore *core, const char *prj_name) {
|
||||
r_file_rm (script_path);
|
||||
r_sys_mkdirp (prj_dir);
|
||||
eprintf ("Please remove: rm -rf %s %s.d\n", prj_name, prj_name);
|
||||
char *rc = r_str_newf ("%s" R_SYS_DIR "rc", prj_dir);
|
||||
char *rc = r_str_newf ("%s" R_SYS_DIR "rc.r2", prj_dir);
|
||||
if (!rc) {
|
||||
free (prj_dir);
|
||||
free (script_path);
|
||||
@ -835,7 +835,7 @@ R_API bool r_core_project_save(RCore *core, const char *prj_name) {
|
||||
|
||||
if (r_config_get_i (core->config, "prj.files")) {
|
||||
eprintf ("TODO: prj.files: support copying more than one file into the project directory\n");
|
||||
char *bin_file = r_core_project_info (core, prj_name);
|
||||
char *bin_file = r_core_project_name (core, prj_name);
|
||||
const char *bin_filename = r_file_basename (bin_file);
|
||||
char *prj_bin_dir = r_str_newf ("%s" R_SYS_DIR "bin", prj_dir);
|
||||
char *prj_bin_file = r_str_newf ("%s" R_SYS_DIR "%s", prj_bin_dir, bin_filename);
|
||||
|
@ -14,6 +14,7 @@ R_API bool r_file_is_directory(const char *str);
|
||||
R_API bool r_file_is_regular(const char *str);
|
||||
|
||||
R_API bool r_file_truncate(const char *filename, ut64 newsize);
|
||||
R_API char *r_file_new(const char *root, ...);
|
||||
R_API ut64 r_file_size(const char *str);
|
||||
R_API char *r_file_root(const char *root, const char *path);
|
||||
R_API RMmap *r_file_mmap(const char *file, bool rw, ut64 base);
|
||||
@ -48,7 +49,8 @@ R_API char *r_file_slurp_lines_from_bottom(const char *file, int line);
|
||||
R_API int r_file_mkstemp(const char *prefix, char **oname);
|
||||
R_API char *r_file_tmpdir(void);
|
||||
R_API char *r_file_readlink(const char *path);
|
||||
R_API bool r_file_copy (const char *src, const char *dst);
|
||||
R_API bool r_file_copy(const char *src, const char *dst);
|
||||
R_API bool r_file_move(const char *src, const char *dst);
|
||||
R_API RList* r_file_globsearch (const char *globbed_path, int maxdepth);
|
||||
R_API RMmap *r_file_mmap_arch (RMmap *map, const char *filename, int fd);
|
||||
|
||||
|
@ -339,7 +339,6 @@ R_API int r_main_radare2(int argc, const char **argv) {
|
||||
int zflag = 0;
|
||||
bool do_connect = false;
|
||||
bool fullfile = false;
|
||||
int has_project;
|
||||
bool zerosep = false;
|
||||
int help = 0;
|
||||
enum { LOAD_BIN_ALL, LOAD_BIN_NOTHING, LOAD_BIN_STRUCTURES_ONLY } load_bin = LOAD_BIN_ALL;
|
||||
|
@ -42,6 +42,33 @@ static int file_stat(const char *file, struct stat* const pStat) {
|
||||
#endif // __WINDOWS__
|
||||
}
|
||||
|
||||
// r_file_new("", "bin", NULL) -> /bin
|
||||
// r_file_new(".", "bin", NULL) -> ./bin
|
||||
// r_file_new("/", "bin", NULL) -> //bin # shall we be stricts?
|
||||
R_API char *r_file_new(const char *root, ...) {
|
||||
va_list ap;
|
||||
va_start (ap, root);
|
||||
RStrBuf *sb = r_strbuf_new ("");
|
||||
char *home = r_str_home (NULL);
|
||||
const char *arg = va_arg (ap, char *);
|
||||
r_strbuf_append (sb, arg);
|
||||
arg = va_arg (ap, char *);
|
||||
while (arg) {
|
||||
if (!strcmp (arg, "~")) {
|
||||
arg = home;
|
||||
}
|
||||
r_strbuf_append (sb, R_SYS_DIR);
|
||||
r_strbuf_append (sb, arg);
|
||||
arg = va_arg (ap, char *);
|
||||
}
|
||||
va_end (ap);
|
||||
free (home);
|
||||
char *path = r_strbuf_drain (sb);
|
||||
char *abs = r_file_abspath (path);
|
||||
free (path);
|
||||
return abs;
|
||||
}
|
||||
|
||||
R_API bool r_file_truncate(const char *filename, ut64 newsize) {
|
||||
r_return_val_if_fail (filename, false);
|
||||
int fd;
|
||||
@ -740,6 +767,29 @@ R_API bool r_file_dump(const char *file, const ut8 *buf, int len, bool append) {
|
||||
return true;
|
||||
}
|
||||
|
||||
R_API bool r_file_move(const char *src, const char *dst) {
|
||||
r_return_val_if_fail (!R_STR_ISEMPTY (src) && !R_STR_ISEMPTY (dst), false);
|
||||
if (r_sandbox_enable (0)) {
|
||||
return false;
|
||||
}
|
||||
// rename fails when files are in different mountpoints
|
||||
// in this situation it needs to be copied and removed
|
||||
if (rename (src, dst) != 0) {
|
||||
char *a = r_str_escape (src);
|
||||
char *b = r_str_escape (dst);
|
||||
char *input = r_str_newf ("\"%s\" \"%s\"", a, b);
|
||||
#if __WINDOWS__
|
||||
int rc = r_sys_cmdf ("move %s", input);
|
||||
#else
|
||||
int rc = r_sys_cmdf ("mv %s", input);
|
||||
#endif
|
||||
free (a);
|
||||
free (b);
|
||||
return rc == 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
R_API bool r_file_rm(const char *file) {
|
||||
r_return_val_if_fail (!R_STR_ISEMPTY (file), false);
|
||||
if (r_sandbox_enable (0)) {
|
||||
|
@ -486,13 +486,17 @@ R_API bool r_syscmd_mv(const char *input) {
|
||||
eprintf ("Usage: mv src dst\n");
|
||||
return false;
|
||||
}
|
||||
input = input + 2;
|
||||
if (!r_sandbox_enable (0)) {
|
||||
#if __WINDOWS__
|
||||
r_sys_cmdf ("move %s", input);
|
||||
#else
|
||||
r_sys_cmdf ("mv %s", input);
|
||||
#endif
|
||||
char *inp = strdup (input + 2);
|
||||
char *arg = strchr (inp, ' ');
|
||||
bool rc = false;
|
||||
if (arg) {
|
||||
*arg++ = 0;
|
||||
if (!(rc = r_file_move (inp, arg))) {
|
||||
eprintf ("Cannot move file\n");
|
||||
}
|
||||
} else {
|
||||
eprintf ("Usage: mv src dst\n");
|
||||
}
|
||||
return false;
|
||||
free (inp);
|
||||
return rc;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user