Cache file_exists when iterating over the source files ##bin

* Speedup large loading times for files with DWARF info
* Only speedups the cases where the files doesnt exist
* Need to refactor file_slurp_random_line() into str for better times
This commit is contained in:
Sergi Àlvarez i Capilla 2022-03-08 12:44:29 +01:00 committed by pancake
parent 97f0bf300d
commit 035e68e3f1
2 changed files with 49 additions and 29 deletions

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2009-2021 - pancake */ /* radare2 - LGPL - Copyright 2009-2022 - pancake */
#include "r_anal.h" #include "r_anal.h"
#include "r_bin.h" #include "r_bin.h"
@ -10,6 +10,11 @@
char *getcommapath(RCore *core); char *getcommapath(RCore *core);
static R_TH_LOCAL ut64 filter_offset = UT64_MAX;
static R_TH_LOCAL int filter_format = 0;
static R_TH_LOCAL size_t filter_count = 0;
static R_TH_LOCAL Sdb *fscache = NULL;
static const char *help_msg_C[] = { static const char *help_msg_C[] = {
"Usage:", "C[-LCvsdfm*?][*?] [...]", " # Metadata management", "Usage:", "C[-LCvsdfm*?][*?] [...]", " # Metadata management",
"C", "", "list meta info in human friendly form", "C", "", "list meta info in human friendly form",
@ -202,10 +207,6 @@ static int print_meta_fileline(RCore *core, const char *file_line) {
} }
#endif #endif
static ut64 filter_offset = UT64_MAX;
static int filter_format = 0;
static size_t filter_count = 0;
static bool print_addrinfo_json(void *user, const char *k, const char *v) { static bool print_addrinfo_json(void *user, const char *k, const char *v) {
ut64 offset = sdb_atoi (k); ut64 offset = sdb_atoi (k);
if (!offset || offset == UT64_MAX) { if (!offset || offset == UT64_MAX) {
@ -233,18 +234,29 @@ static bool print_addrinfo_json(void *user, const char *k, const char *v) {
int line = atoi (colonpos + 1); int line = atoi (colonpos + 1);
ut64 addr = offset; ut64 addr = offset;
PJ *pj = (PJ*)user; PJ *pj = (PJ*)user;
if (pj) {
pj_o (pj); pj_o (pj);
pj_ks (pj, "file", file); pj_ks (pj, "file", file);
pj_kn (pj, "line", line); pj_kn (pj, "line", line);
pj_kn (pj, "addr", addr); pj_kn (pj, "addr", addr);
const char *cached_existance = sdb_const_get (fscache, file, NULL);
bool file_exists = false;
if (cached_existance) {
file_exists = !strcmp (cached_existance, "1");
} else {
if (r_file_exists (file)) { if (r_file_exists (file)) {
sdb_set (fscache, file, "1", 0);
} else {
sdb_set (fscache, file, "0", 0);
}
}
if (file_exists) {
char *row = r_file_slurp_line (file, line, 0); char *row = r_file_slurp_line (file, line, 0);
pj_ks (pj, "text", file); pj_ks (pj, "text", file);
free (row); free (row);
} else {
// eprintf ("Cannot open '%s'\n", file);
} }
pj_end (pj); pj_end (pj);
}
free (subst); free (subst);
return true; return true;
} }
@ -384,23 +396,25 @@ static int cmd_meta_lineinfo(RCore *core, const char *input) {
// taken from r2 // TODO: we should move this addrinfo sdb logic into RBin.. use HT // taken from r2 // TODO: we should move this addrinfo sdb logic into RBin.. use HT
filter_offset = offset; filter_offset = offset;
filter_count = 0; filter_count = 0;
fscache = sdb_new0 ();
PJ *pj = NULL;
if (use_json) { if (use_json) {
PJ *pj = r_core_pj_new (core); pj = r_core_pj_new (core);
pj_a (pj); pj_a (pj);
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo_json, pj); sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo_json, pj);
} else {
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo, NULL);
}
if (filter_count == 0) { if (filter_count == 0) {
print_meta_offset (core, offset, pj); print_meta_offset (core, offset, pj);
} }
if (use_json) {
pj_end (pj); pj_end (pj);
char *s = pj_drain (pj); char *s = pj_drain (pj);
r_cons_printf ("%s\n", s); r_cons_printf ("%s\n", s);
free (s); free (s);
} else {
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo, NULL);
if (filter_count == 0) {
print_meta_offset (core, offset, NULL);
}
} }
sdb_free (fscache);
} }
free (pheap); free (pheap);
return 0; return 0;

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2021 - pancake */ /* radare - LGPL - Copyright 2007-2022 - pancake */
#include "r_types.h" #include "r_types.h"
#include "r_util.h" #include "r_util.h"
@ -43,9 +43,9 @@ static int file_stat(const char *file, struct stat* const pStat) {
int ret = _wstat (wfile, pStat); int ret = _wstat (wfile, pStat);
free (wfile); free (wfile);
return ret; return ret;
#else // __WINDOWS__ #else
return stat (file, pStat); return stat (file, pStat);
#endif // __WINDOWS__ #endif
} }
// r_file_new("", "bin", NULL) -> /bin // r_file_new("", "bin", NULL) -> /bin
@ -195,14 +195,20 @@ R_API bool r_file_fexists(const char *fmt, ...) {
} }
R_API bool r_file_exists(const char *str) { R_API bool r_file_exists(const char *str) {
char *absfile = r_file_abspath (str);
struct stat buf = {0}; struct stat buf = {0};
#if 1
if (file_stat (str, &buf) == -1) {
return false;
}
#else
char *absfile = r_file_abspath (str);
r_return_val_if_fail (!R_STR_ISEMPTY (str), false); r_return_val_if_fail (!R_STR_ISEMPTY (str), false);
if (file_stat (absfile, &buf) == -1) { if (file_stat (absfile, &buf) == -1) {
free (absfile); free (absfile);
return false; return false;
} }
free (absfile); free (absfile);
#endif
return S_IFREG == (S_IFREG & buf.st_mode); return S_IFREG == (S_IFREG & buf.st_mode);
} }