Introduce rz_file_dos_basename() API

Use it whenever we need to interact with paths that can contain `\` as a separator as well
This commit is contained in:
GustavoLCR 2022-03-28 19:43:37 -03:00 committed by Anton Kochkov
parent 77c46e1c9d
commit e945fb0e31
6 changed files with 33 additions and 7 deletions

View file

@ -215,7 +215,7 @@ RZ_API int rz_bin_pdb_download(RZ_NONNULL RzBin *bin, RZ_NULLABLE PJ *pj, int is
return 1;
}
opt.dbg_file = rz_file_basename(info->debug_file_name);
opt.dbg_file = rz_file_dos_basename(info->debug_file_name);
opt.guid = info->guid;
opt.symbol_server = options->symbol_server;
opt.symbol_store_path = options->symbol_store_path;

View file

@ -4823,12 +4823,12 @@ RZ_API char *rz_core_bin_pdb_get_filename(RzCore *core) {
return NULL;
}
// Check raw path for debug filename
bool file_found = rz_file_exists(rz_file_basename(info->debug_file_name));
bool file_found = rz_file_exists(info->debug_file_name);
if (file_found) {
return strdup(rz_file_basename(info->debug_file_name));
return strdup(info->debug_file_name);
}
// Check debug filename basename in current directory
char *basename = (char *)rz_file_basename(info->debug_file_name);
const char *basename = rz_file_dos_basename(info->debug_file_name);
file_found = rz_file_exists(basename);
if (file_found) {
return strdup(basename);
@ -4845,9 +4845,8 @@ RZ_API char *rz_core_bin_pdb_get_filename(RzCore *core) {
// Last chance: Check if file is in downstream symbol store
const char *symstore_path = rz_config_get(core->config, "pdb.symstore");
const char *base_file = rz_file_basename(info->debug_file_name);
return rz_str_newf("%s" RZ_SYS_DIR "%s" RZ_SYS_DIR "%s" RZ_SYS_DIR "%s",
symstore_path, base_file, info->guid, base_file);
symstore_path, basename, info->guid, basename);
}
static void bin_memory_print_rec(RzCmdStateOutput *state, RzBinMem *mirror, const RzList *mems, int perms) {

View file

@ -449,7 +449,7 @@ static RzList *rz_debug_dmp_modules(RzDebug *dbg) {
return NULL;
}
mod->file = strdup(m->name);
mod->name = strdup(rz_file_basename(m->name));
mod->name = strdup(rz_file_dos_basename(m->name));
mod->size = m->size;
mod->addr = m->addr;
mod->addr_end = m->addr + m->size;

View file

@ -37,6 +37,7 @@ RZ_API char *rz_file_temp(const char *prefix);
RZ_API char *rz_file_path(const char *bin);
RZ_API RZ_OWN char *rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2);
RZ_API const char *rz_file_basename(const char *path);
RZ_API const char *rz_file_dos_basename(RZ_BORROW RZ_NONNULL const char *path);
RZ_API char *rz_file_dirname(const char *path);
RZ_API char *rz_file_abspath_rel(const char *cwd, const char *file);
RZ_API char *rz_file_abspath(const char *file);

View file

@ -94,6 +94,23 @@ RZ_API const char *rz_file_basename(const char *path) {
return path;
}
/* \brief Returns file name from a path accepting both `/` and `\` as directory separators
*
* \param path Path of file to get the file name
* \return const char * Pointer to the file name
*/
RZ_API const char *rz_file_dos_basename(RZ_BORROW RZ_NONNULL const char *path) {
rz_return_val_if_fail(path, NULL);
const char *ptr = rz_str_rchr(path, NULL, '/');
if (ptr) {
path = ptr + 1;
}
if ((ptr = rz_str_rchr(path, NULL, '\\'))) {
path = ptr + 1;
}
return path;
}
/*
Example:
str = rz_file_dirname ("home/inisider/Downloads/user32.dll");

View file

@ -72,6 +72,15 @@ bool test_rz_file_basename(void) {
mu_assert_notnull(s, "basename not null");
mu_assert_streq(s, "o.txt", "basename is supposed to be o.txt");
mu_end;
}
bool test_rz_file_dos_basename(void) {
const char *s = rz_file_dos_basename("./\\test/\\//\\abc//\\/123/\\o.txt");
mu_assert_notnull(s, "basename not null");
mu_assert_streq(s, "o.txt", "basename is supposed to be o.txt");
s = rz_file_basename("./\\test/\\//\\abc//\\/123\\/o.txt");
mu_assert_notnull(s, "basename not null");
mu_assert_streq(s, "o.txt", "basename is supposed to be o.txt");
mu_end;
}