Refactor projects code into separate snake_case functions ##projects (#17289)

This commit is contained in:
RHL120 2020-07-20 12:19:57 +02:00 committed by GitHub
parent c06d82eb9b
commit cb0b6d85a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,7 +35,7 @@ static bool is_valid_project_name(const char *name) {
return true; return true;
} }
static char *projectScriptPath(RCore *core, const char *file) { static char *get_project_script_path(RCore *core, const char *file) {
const char *magic = "# r2 rdb project file"; const char *magic = "# r2 rdb project file";
char *data, *prjfile; char *data, *prjfile;
if (r_file_is_abspath (file)) { if (r_file_is_abspath (file)) {
@ -61,7 +61,7 @@ static char *projectScriptPath(RCore *core, const char *file) {
return prjfile; return prjfile;
} }
static int projectInit(RCore *core) { static int make_projects_directory(RCore *core) {
char *prjdir = r_file_abspath (r_config_get (core->config, "dir.projects")); char *prjdir = r_file_abspath (r_config_get (core->config, "dir.projects"));
int ret = r_sys_mkdirp (prjdir); int ret = r_sys_mkdirp (prjdir);
if (!ret) { if (!ret) {
@ -74,7 +74,7 @@ static int projectInit(RCore *core) {
R_API bool r_core_is_project(RCore *core, const char *name) { R_API bool r_core_is_project(RCore *core, const char *name) {
bool ret = false; bool ret = false;
if (name && *name && *name != '.') { if (name && *name && *name != '.') {
char *path = projectScriptPath (core, name); char *path = get_project_script_path (core, name);
if (!path) { if (!path) {
return false; return false;
} }
@ -92,7 +92,7 @@ R_API bool r_core_is_project(RCore *core, const char *name) {
} }
R_API int r_core_project_cat(RCore *core, const char *name) { R_API int r_core_project_cat(RCore *core, const char *name) {
char *path = projectScriptPath (core, name); char *path = get_project_script_path (core, name);
if (path) { if (path) {
char *data = r_file_slurp (path, NULL); char *data = r_file_slurp (path, NULL);
if (data) { if (data) {
@ -144,37 +144,23 @@ R_API int r_core_project_list(RCore *core, int mode) {
return 0; return 0;
} }
R_API int r_core_project_delete(RCore *core, const char *prjfile) { static inline void remove_project_file(char * path) {
if (r_sandbox_enable (0)) {
eprintf ("Cannot delete project in sandbox mode\n");
return 0;
}
char *path = projectScriptPath (core, prjfile);
if (!path) {
eprintf ("Invalid project name '%s'\n", prjfile);
return false;
}
if (r_core_is_project (core, prjfile)) {
char *prjDir = r_file_dirname (path);
if (!prjDir) {
eprintf ("Cannot resolve directory\n");
free (path);
return false;
}
// rm project file
if (r_file_exists (path)) { if (r_file_exists (path)) {
r_file_rm (path); r_file_rm (path);
eprintf ("rm %s\n", path); eprintf ("rm %s\n", path);
} }
}
//rm notes.txt file static inline void remove_notes_file(char *prjDir) {
char *notes_txt = r_str_newf ("%s%s%s", prjDir, R_SYS_DIR, "notes.txt"); char *notes_txt = r_str_newf ("%s%s%s", prjDir, R_SYS_DIR, "notes.txt");
if (r_file_exists (notes_txt)) { if (r_file_exists (notes_txt)) {
r_file_rm (notes_txt); r_file_rm (notes_txt);
eprintf ("rm %s\n", notes_txt); eprintf ("rm %s\n", notes_txt);
} }
free(notes_txt); free(notes_txt);
}
static inline void remove_rop_directory(char *prjDir) {
char *rop_d = r_str_newf ("%s%s%s", prjDir, R_SYS_DIR, "rop.d"); char *rop_d = r_str_newf ("%s%s%s", prjDir, R_SYS_DIR, "rop.d");
if (r_file_is_directory (rop_d)) { if (r_file_is_directory (rop_d)) {
@ -188,13 +174,37 @@ R_API int r_core_project_delete(RCore *core, const char *prjfile) {
eprintf ("rm %s\n", filepath); eprintf ("rm %s\n", filepath);
r_file_rm (filepath); r_file_rm (filepath);
} }
free (filepath); free (filepath);
} }
r_file_rm (rop_d); r_file_rm (rop_d);
eprintf ("rm %s\n", rop_d); eprintf ("rm %s\n", rop_d);
r_list_free (files); r_list_free (files);
} }
free (rop_d); free (rop_d);
}
R_API int r_core_project_delete(RCore *core, const char *prjfile) {
if (r_sandbox_enable (0)) {
eprintf ("Cannot delete project in sandbox mode\n");
return 0;
}
char *path = get_project_script_path (core, prjfile);
if (!path) {
eprintf ("Invalid project name '%s'\n", prjfile);
return false;
}
if (r_core_is_project (core, prjfile)) {
char *prjDir = r_file_dirname (path);
if (!prjDir) {
eprintf ("Cannot resolve directory\n");
free (path);
return false;
}
remove_project_file (path);
remove_notes_file (prjDir);
remove_rop_directory (prjDir);
// remove directory only if it's empty // remove directory only if it's empty
r_file_rm (prjDir); r_file_rm (prjDir);
free (prjDir); free (prjDir);
@ -203,7 +213,7 @@ R_API int r_core_project_delete(RCore *core, const char *prjfile) {
return 0; return 0;
} }
static bool projectLoadRop(RCore *core, const char *prjfile) { static bool load_project_rop(RCore *core, const char *prjfile) {
char *path, *db = NULL, *path_ns; char *path, *db = NULL, *path_ns;
bool found = 0; bool found = 0;
SdbListIter *it; SdbListIter *it;
@ -220,7 +230,7 @@ static bool projectLoadRop(RCore *core, const char *prjfile) {
Sdb *arithm_db = sdb_ns (rop_db, "arithm", false); Sdb *arithm_db = sdb_ns (rop_db, "arithm", false);
Sdb *arithmct_db = sdb_ns (rop_db, "arithm_ct", false); Sdb *arithmct_db = sdb_ns (rop_db, "arithm_ct", false);
char *rcPath = projectScriptPath (core, prjfile); char *rcPath = get_project_script_path (core, prjfile);
char *prjDir = r_file_dirname (rcPath); char *prjDir = r_file_dirname (rcPath);
if (r_str_endswith (prjfile, R_SYS_DIR "rc")) { if (r_str_endswith (prjfile, R_SYS_DIR "rc")) {
@ -339,7 +349,7 @@ typedef struct {
char *rcPath; char *rcPath;
} ProjectState; } ProjectState;
static RThreadFunctionRet projectLoadBackground(RThread *th) { static RThreadFunctionRet project_load_background(RThread *th) {
ProjectState *ps = th->user; ProjectState *ps = th->user;
r_core_project_load (ps->core, ps->prjName, ps->rcPath); r_core_project_load (ps->core, ps->prjName, ps->rcPath);
free (ps->prjName); free (ps->prjName);
@ -353,7 +363,7 @@ R_API RThread *r_core_project_load_bg(RCore *core, const char *prjName, const ch
ps->core = core; ps->core = core;
ps->prjName = strdup (prjName); ps->prjName = strdup (prjName);
ps->rcPath = strdup (rcPath); ps->rcPath = strdup (rcPath);
RThread *th = r_th_new (projectLoadBackground, ps, false); RThread *th = r_th_new (project_load_background, ps, false);
if (th) { if (th) {
r_th_start (th, true); r_th_start (th, true);
char thname[16] = {0}; char thname[16] = {0};
@ -393,7 +403,7 @@ R_API bool r_core_project_open(RCore *core, const char *prjfile, bool thready) {
eprintf ("Loading projects in a thread has been deprecated. Use tasks\n"); eprintf ("Loading projects in a thread has been deprecated. Use tasks\n");
return false; return false;
} }
char *prj = projectScriptPath (core, prjfile); char *prj = get_project_script_path (core, prjfile);
if (!prj) { if (!prj) {
eprintf ("Invalid project name '%s'\n", prjfile); eprintf ("Invalid project name '%s'\n", prjfile);
return false; return false;
@ -487,7 +497,7 @@ beach:
R_API char *r_core_project_info(RCore *core, const char *prjfile) { R_API char *r_core_project_info(RCore *core, const char *prjfile) {
FILE *fd; FILE *fd;
char buf[256], *file = NULL; char buf[256], *file = NULL;
char *prj = projectScriptPath (core, prjfile); char *prj = get_project_script_path (core, prjfile);
if (!prj) { if (!prj) {
eprintf ("Invalid project name '%s'\n", prjfile); eprintf ("Invalid project name '%s'\n", prjfile);
return NULL; return NULL;
@ -555,7 +565,7 @@ static bool store_files_and_maps (RCore *core, RIODesc *desc, ut32 id) {
return true; return true;
} }
static bool simpleProjectSaveScript(RCore *core, const char *file, int opts) { static bool simple_project_save_script(RCore *core, const char *file, int opts) {
char *filename, *hl, *ohl = NULL; char *filename, *hl, *ohl = NULL;
int fd, fdold; int fd, fdold;
@ -628,7 +638,7 @@ static bool simpleProjectSaveScript(RCore *core, const char *file, int opts) {
return true; return true;
} }
static bool projectSaveScript(RCore *core, const char *file, int opts) { static bool project_save_script(RCore *core, const char *file, int opts) {
char *filename, *hl, *ohl = NULL; char *filename, *hl, *ohl = NULL;
int fd, fdold; int fd, fdold;
@ -743,7 +753,7 @@ static bool projectSaveScript(RCore *core, const char *file, int opts) {
// TODO: rename to r_core_project_save_script // TODO: rename to r_core_project_save_script
R_API bool r_core_project_save_script(RCore *core, const char *file, int opts) { R_API bool r_core_project_save_script(RCore *core, const char *file, int opts) {
return projectSaveScript (core, file, opts); return project_save_script (core, file, opts);
} }
#define TRANSITION 1 #define TRANSITION 1
@ -759,7 +769,7 @@ R_API bool r_core_project_save(RCore *core, const char *prjName) {
if (!prjName || !*prjName) { if (!prjName || !*prjName) {
return false; return false;
} }
scriptPath = projectScriptPath (core, prjName); scriptPath = get_project_script_path (core, prjName);
if (!scriptPath) { if (!scriptPath) {
eprintf ("Invalid project name '%s'\n", prjName); eprintf ("Invalid project name '%s'\n", prjName);
return false; return false;
@ -803,7 +813,7 @@ R_API bool r_core_project_save(RCore *core, const char *prjName) {
r_config_set_i (core->config, "scr.null", false); r_config_set_i (core->config, "scr.null", false);
scr_null = true; scr_null = true;
} }
projectInit (core); make_projects_directory (core);
Sdb *rop_db = sdb_ns (core->sdb, "rop", false); Sdb *rop_db = sdb_ns (core->sdb, "rop", false);
if (rop_db) { if (rop_db) {
@ -822,12 +832,12 @@ R_API bool r_core_project_save(RCore *core, const char *prjName) {
} }
r_config_set (core->config, "prj.name", prjName); r_config_set (core->config, "prj.name", prjName);
if (r_config_get_i (core->config, "prj.simple")) { if (r_config_get_i (core->config, "prj.simple")) {
if (!simpleProjectSaveScript (core, scriptPath, R_CORE_PRJ_ALL)) { if (!simple_project_save_script (core, scriptPath, R_CORE_PRJ_ALL)) {
eprintf ("Cannot open '%s' for writing\n", prjName); eprintf ("Cannot open '%s' for writing\n", prjName);
ret = false; ret = false;
} }
} else { } else {
if (!projectSaveScript (core, scriptPath, R_CORE_PRJ_ALL)) { if (!project_save_script (core, scriptPath, R_CORE_PRJ_ALL)) {
eprintf ("Cannot open '%s' for writing\n", prjName); eprintf ("Cannot open '%s' for writing\n", prjName);
ret = false; ret = false;
} }
@ -906,7 +916,7 @@ R_API bool r_core_project_load(RCore *core, const char *prjName, const char *rcp
const bool cfg_fortunes = r_config_get_i (core->config, "cfg.fortunes"); const bool cfg_fortunes = r_config_get_i (core->config, "cfg.fortunes");
const bool scr_interactive = r_cons_is_interactive (); const bool scr_interactive = r_cons_is_interactive ();
const bool scr_prompt = r_config_get_i (core->config, "scr.prompt"); const bool scr_prompt = r_config_get_i (core->config, "scr.prompt");
(void) projectLoadRop (core, prjName); (void) load_project_rop (core, prjName);
bool ret = r_core_cmd_file (core, rcpath); bool ret = r_core_cmd_file (core, rcpath);
r_config_set_i (core->config, "cfg.fortunes", cfg_fortunes); r_config_set_i (core->config, "cfg.fortunes", cfg_fortunes);
r_config_set_i (core->config, "scr.interactive", scr_interactive); r_config_set_i (core->config, "scr.interactive", scr_interactive);