Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added r_file_globsearch and zfs **.sig support #10793

Merged
merged 4 commits into from Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions libr/anal/flirt.c
Expand Up @@ -1471,12 +1471,12 @@ R_API void r_sign_flirt_scan(const RAnal *anal, const char *flirt_file) {
r_buf_free (flirt_buf);
if (node) {
if (!node_match_functions (anal, node)) {
eprintf ("Error while scanning the file\n");
eprintf ("Error while scanning the file %s\n", flirt_file);
}
node_free (node);
return;
} else {
eprintf ("We encountered an error while parsing the file. Sorry.\n");
eprintf ("We encountered an error while parsing the file %s. Sorry.\n", flirt_file);
return;
}
}
1 change: 1 addition & 0 deletions libr/core/cconfig.c
Expand Up @@ -2650,6 +2650,7 @@ R_API int r_core_config_init(RCore *core) {
SETPREF ("diff.levenstein", "false", "Use faster (and buggy) levenstein algorithm for buffer distance diffing");

/* dir */
SETI ("dir.depth", 10, "Maximum depth when searching recursively for files");
SETCB ("dir.dbgsnap", ".", &cb_dbgsnap, "Path to session dump files");
{
char *path = r_str_newf (R_JOIN_2_PATHS ("%s", R2_SDB_MAGIC), r_config_get (core->config, "dir.prefix"));
Expand Down
10 changes: 9 additions & 1 deletion libr/core/cmd_zign.c
Expand Up @@ -45,6 +45,7 @@ static const char *help_msg_zf[] = {
"Usage:", "zf[dsz] filename ", "# Manage FLIRT signatures",
"zfd ", "filename", "open FLIRT file and dump",
"zfs ", "filename", "open FLIRT file and scan",
"zfs ", "/path/**.sig", "recursively search for FLIRT files and scan them (see dir.depth)",
"zfz ", "filename", "open FLIRT file and get sig commands (zfz flirt_file > zignatures.sig)",
NULL
};
Expand Down Expand Up @@ -507,7 +508,14 @@ static int cmdFlirt(void *data, const char *input) {
eprintf ("usage: zfs filename\n");
return false;
}
r_sign_flirt_scan (core->anal, input + 2);
int depth = r_config_get_i (core->config, "dir.depth");
char *file;
RListIter *iter;
RList *files = r_file_globsearch (input + 2, depth);
r_list_foreach (files, iter, file) {
r_sign_flirt_scan (core->anal, file);
}
r_list_free (files);
break;
case 'z':
// TODO
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_util/r_file.h
Expand Up @@ -43,6 +43,7 @@ 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 RList* r_file_globsearch (char *globbed_path, int maxdepth);

#ifdef __cplusplus
}
Expand Down
66 changes: 66 additions & 0 deletions libr/util/file.c
Expand Up @@ -1037,3 +1037,69 @@ R_API bool r_file_copy (const char *src, const char *dst) {
return rc == 0;
#endif
}

static void recursive_search_glob (const char *path, const char *glob, RList* list, int depth) {
if (depth < 1) {
return;
}
char* file;
RListIter *iter;
RList *dir = r_sys_dir (path);
r_list_foreach (dir, iter, file) {
if (!strcmp (file, ".") || !strcmp (file, "..")) {
continue;
}
char *filename = malloc (strlen (path) + strlen (file) + 2);
strcpy (filename, path);
strcat (filename, file);
if (r_file_is_directory (filename)) {
strcat (filename, R_SYS_DIR);
recursive_search_glob (filename, glob, list, depth - 1);
free (filename);
} else if (r_str_glob (file, glob)) {
r_list_append (list, filename);
} else {
free (filename);
}
}
r_list_free (dir);
}

R_API RList* r_file_globsearch (char *globbed_path, int maxdepth) {
RList *files = r_list_newf (free);
char *glob = strchr (globbed_path, '*');
if (!glob) {
r_list_append (files, strdup (globbed_path));
} else {
*glob = '\0';
char *last_slash = (char *)r_str_last (globbed_path, R_SYS_DIR);
*glob = '*';
char *path, *glob_ptr;
if (last_slash) {
glob_ptr = last_slash + 1;
if (globbed_path[0] == '~') {
char *rpath = r_str_newlen (globbed_path + 2, last_slash - globbed_path - 1);
path = r_str_home (rpath ? rpath : "");
free (rpath);
} else {
path = r_str_newlen (globbed_path, last_slash - globbed_path + 1);
}
} else {
glob_ptr = globbed_path;
path = r_str_newf (".%s", R_SYS_DIR);
}

if (!path) {
r_list_free (files);
return NULL;
}

if (*(glob + 1) == '*') { // "**"
recursive_search_glob (path, glob_ptr, files, maxdepth);
} else { // "*"
recursive_search_glob (path, glob_ptr, files, 1);
}
free (path);
}
return files;
}