Add Pd to delete projects, Remove rmrf util function, better project file checks

This commit is contained in:
pancake 2015-01-22 00:30:19 +01:00
parent c8a5b8049c
commit ac036b6301
6 changed files with 111 additions and 53 deletions

View File

@ -328,29 +328,7 @@ int main(int argc, char **argv, char **envp) {
case 'n': run_anal --; break;
case 'N': run_rc = 0; break;
case 'p':
if (*optarg == '-') {
char *path, repath[128];
snprintf (repath, sizeof (repath),
R2_HOMEDIR"/projects/%s.d", optarg+1);
path = r_str_home (repath);
if (r_file_exists (path)) {
if (r_file_rmrf (path) == R_FALSE) {
eprintf ("Unable to recursively remove %s\n", path);
free (path);
return 1;
}
path [strlen (path)-2] = 0;
if (r_file_rm (path) == R_FALSE) {
eprintf ("Unable to remove %s\n", path);
free (path);
return 1;
}
free (path);
return 0;
}
eprintf ("Can't find project '%s'\n", optarg+1);
return 1;
} else r_config_set (r.config, "file.project", optarg);
r_config_set (r.config, "file.project", optarg);
break;
case 'P': patchfile = optarg; break;
case 'q':
@ -758,6 +736,7 @@ int main(int argc, char **argv, char **envp) {
#endif
ret = r.num->value;
if (ret != -1 && r_config_get_i (r.config, "scr.interactive")) {
char *question;
if (debug) {
if (r_cons_yesno ('y', "Do you want to quit? (Y/n)")) {
if (r_cons_yesno ('y', "Do you want to kill the process? (Y/n)"))
@ -765,8 +744,10 @@ int main(int argc, char **argv, char **envp) {
} else continue;
}
prj = r_config_get (r.config, "file.project");
if (prj && *prj && r_cons_yesno ('y', "Do you want to save the project? (Y/n)"))
question = r_str_newf ("Do you want to save the '%s' project? (Y/n)", prj);
if (prj && *prj && r_cons_yesno ('y', question))
r_core_project_save (&r, prj);
free (question);
} else {
// r_core_project_save (&r, prj);
if (debug) {

View File

@ -14,9 +14,14 @@ static int cmd_project(void *data, const char *input) {
case 'l':
r_core_project_list (core, input[1]);
break;
case 'd':
r_core_project_delete (core, file);
break;
case 's':
r_core_project_save (core, file);
r_config_set (core->config, "file.project", file);
if (r_core_project_save (core, file)) {
r_config_set (core->config, "file.project", file);
r_cons_printf ("%s\n", file);
}
break;
case 'i':
// if (r_file_is_regular (file))
@ -27,6 +32,7 @@ static int cmd_project(void *data, const char *input) {
"Usage:", "P[?osi] [file]", "Project management",
"Po", " [file]", "open project",
"Ps", " [file]", "save project",
"Pd", " [file]", "delete project",
"Pi", " [file]", "show project information",
"Pl", "", "list all projects",
"NOTE:", "", "See 'e file.project'",

View File

@ -5,14 +5,39 @@
#include <r_flags.h>
#include <r_core.h>
static char *r_core_project_file(RCore *core, const char *file) {
if (*file != '/') {
char *ret = r_file_abspath (r_config_get (
core->config, "dir.projects"));
ret = r_str_concat (ret, "/");
return r_str_concat (ret, file);
static int is_valid_project_name (const char *name) {
int i;
for (i=0; name[i]; i++) {
if (name[i] >= 'a' && name[i] <= 'z')
continue;
if (name[i] >= 'A' && name[i] <= 'Z')
continue;
if (name[i] >= '0' && name[i] <= '9')
continue;
return 0;
}
return strdup (file);
return 1;
}
static char *r_core_project_file(RCore *core, const char *file) {
const char *magic = "# r2 rdb project file";
char *data, *prjfile;
if (*file != '/') {
if (!is_valid_project_name (file))
return NULL;
prjfile = r_file_abspath (r_config_get (
core->config, "dir.projects"));
prjfile = r_str_concat (prjfile, "/");
prjfile = r_str_concat (prjfile, file);
} else prjfile = strdup (file);
data = r_file_slurp (prjfile, NULL);
if (data) {
if (strncmp (data, magic, strlen (magic))) {
R_FREE (prjfile);
}
}
free (data);
return prjfile;
}
static int r_core_project_init(RCore *core) {
@ -28,6 +53,8 @@ static int r_core_is_project(RCore *core, const char *name) {
int ret = 0;
if (name && *name && *name!='.') {
char *path = r_core_project_file (core, name);
if (!path)
return 0;
path = r_str_concat (path, ".d");
if (r_file_is_directory (path))
ret = 1;
@ -69,12 +96,56 @@ R_API int r_core_project_list(RCore *core, int mode) {
return 0;
}
R_API int r_core_project_delete(RCore *core, const char *prjfile) {
char *path;
if (r_sandbox_enable (0)) {
eprintf ("Cant delete project in sandbox mode\n");
return 0;
}
path = r_core_project_file (core, prjfile);
if (!path) {
eprintf ("Invalid project name '%s'\n", prjfile);
return R_FALSE;
}
if (r_core_is_project (core, prjfile)) {
// rm project file
r_file_rm (path);
eprintf ("rm %s\n", path);
path = r_str_concat (path, ".d");
if (r_file_is_directory (path)) {
char *f;
RListIter *iter;
RList *files = r_sys_dir (path);
r_list_foreach (files, iter, f) {
char *filepath = r_str_concat (strdup (path), "/");
filepath =r_str_concat (filepath, f);
if (r_file_is_directory (filepath))
continue;
eprintf ("rm %s\n", filepath);
r_file_rm (filepath);
free (filepath);
}
r_file_rm (path);
eprintf ("rm %s\n", path);
r_list_free (files);
}
// TODO: remove .d directory (BEWARE OF ROOT RIMRAFS!)
// TODO: r_file_rmrf (path);
}
free (path);
return 0;
}
R_API int r_core_project_open(RCore *core, const char *prjfile) {
int ret;
char *prj;
if (!prjfile || !*prjfile)
return R_FALSE;
prj = r_core_project_file (core, prjfile);
if (!prj) {
eprintf ("Invalid project name '%s'\n", prjfile);
return R_FALSE;
}
ret = r_core_cmd_file (core, prj);
r_anal_project_load (core->anal, prjfile);
free (prj);
@ -82,8 +153,13 @@ R_API int r_core_project_open(RCore *core, const char *prjfile) {
}
R_API char *r_core_project_info(RCore *core, const char *prjfile) {
FILE *fd;
char buf[256], *file = NULL, *prj = r_core_project_file (core, prjfile);
FILE *fd = prj? r_sandbox_fopen (prj, "r"): NULL;
if (!prj) {
eprintf ("Invalid project name '%s'\n", prjfile);
return R_FALSE;
}
fd = prj? r_sandbox_fopen (prj, "r"): NULL;
for (;fd;) {
fgets (buf, sizeof (buf), fd);
if (feof (fd))
@ -115,6 +191,10 @@ R_API int r_core_project_save(RCore *core, const char *file) {
return R_FALSE;
prj = r_core_project_file (core, file);
if (!prj) {
eprintf ("Invalid project name '%s'\n", file);
return R_FALSE;
}
if (r_file_is_directory (prj)) {
eprintf ("Error: Target is a directory\n");
free (prj);

View File

@ -349,6 +349,7 @@ R_API int r_core_gdiff(RCore *core1, RCore *core2, int anal_all);
R_API int r_core_gdiff_fcn(RCore *c, ut64 addr, ut64 addr2);
R_API int r_core_project_open(RCore *core, const char *file);
R_API int r_core_project_delete(RCore *core, const char *prjfile);
R_API int r_core_project_list(RCore *core, int mode);
R_API int r_core_project_save(RCore *core, const char *file);
R_API char *r_core_project_info(RCore *core, const char *file);

View File

@ -479,7 +479,6 @@ R_API char *r_file_slurp_random_line_count(const char *file, int *linecount);
R_API ut8 *r_file_slurp_hexpairs(const char *str, int *usz);
R_API boolt r_file_dump(const char *file, const ut8 *buf, int len);
R_API boolt r_file_rm(const char *file);
R_API boolt r_file_rmrf(const char *file);
R_API boolt r_file_exists(const char *str);
R_API boolt r_file_fexists(const char *fmt, ...);
R_API char *r_file_slurp_line(const char *file, int line, int context);

View File

@ -407,28 +407,19 @@ R_API boolt r_file_dump(const char *file, const ut8 *buf, int len) {
R_API boolt r_file_rm(const char *file) {
if (r_sandbox_enable (0)) return R_FALSE;
if (r_file_is_directory (file)) {
#if __WINDOWS__
return (DeleteFile (file)==0)? R_TRUE: R_FALSE;
return (DeleteDirectory (file)==0)? R_TRUE: R_FALSE;
#else
return (unlink (file)==0)? R_TRUE: R_FALSE;
return (rmdir (file)==0)? R_TRUE: R_FALSE;
#endif
} else {
#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) {
if (r_sandbox_enable (0))
return R_FALSE;
else {
char *nfile = strdup (file);
nfile[ strlen (nfile)-1 ] = '_';
nfile[ strlen (nfile)-2 ] = '_';
if (rename (file, nfile)) {
free (nfile);
return R_FALSE;
}
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) {