From ec174a72328efa7beeef03a85543fdff7cdacc26 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 24 Aug 2019 07:51:24 +0200 Subject: [PATCH] better searchFile api --- client/cmdscript.c | 13 ++++------- client/fileutils.c | 58 ++++++++++++++++++++++++++++++---------------- client/fileutils.h | 3 +-- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/client/cmdscript.c b/client/cmdscript.c index 37778d52e..318cc1dd4 100644 --- a/client/cmdscript.c +++ b/client/cmdscript.c @@ -66,16 +66,11 @@ static int CmdScriptRun(const char *Cmd) { int arg_len = 0; sscanf(Cmd, "%127s%n %255[^\n\r]%n", preferredName, &name_len, arguments, &arg_len); - char *script_name = filenamemcopy(preferredName, ".lua"); - if (script_name == NULL) return PM3_EMALLOC; - char *script_path = searchFile(LUA_SCRIPTS_SUBDIR, script_name); + char *script_path; + int res = searchFile(&script_path, LUA_SCRIPTS_SUBDIR, preferredName, ".lua"); + if (res != PM3_SUCCESS) + return res; - if (script_path == NULL) { - PrintAndLogEx(FAILED, "Error - can't find script %s", script_name); - free(script_name); - return PM3_EFILE; - } - free(script_name); int error; PrintAndLogEx(SUCCESS, "Executing: %s, args '%s'\n", script_path, arguments); error = luaL_loadfile(lua_state, script_path); diff --git a/client/fileutils.c b/client/fileutils.c index 31e48601d..622bb88de 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -69,7 +69,7 @@ int fileExists(const char *filename) { return result == 0; } -char *filenamemcopy(const char *preferredName, const char *suffix) { +static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; if (suffix == NULL) return NULL; char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t)); @@ -522,15 +522,10 @@ out: int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint16_t *keycnt) { if (data == NULL) return PM3_ESOFT; - char *fileName = filenamemcopy(preferredName, ".dic"); - if (fileName == NULL) return PM3_EMALLOC; - char *path = searchFile(DICTIONARIES_SUBDIR, fileName); - if (path == NULL) { - PrintAndLogEx(WARNING, "file not found or locked. '" _YELLOW_("%s")"'", fileName); - free(fileName); + char *path; + if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic") != PM3_SUCCESS) return PM3_EFILE; - } - free(fileName); + // t5577 == 4bytes // mifare == 6 bytes // iclass == 8 bytes @@ -667,25 +662,31 @@ int searchAndList(const char *pm3dir, const char *ext) { return PM3_SUCCESS; } -char *searchFile(const char *pm3dir, const char *searchname) { +static int searchFinalFile(char **foundpath, const char *pm3dir, const char *searchname) { + if ((foundpath == NULL)||(pm3dir == NULL)||(searchname == NULL)) return PM3_ESOFT; // explicit absolute (/) or relative path (./) => try only to match it directly char *filename = calloc(strlen(searchname) + 1, sizeof(char)); - if (filename == NULL) return NULL; + if (filename == NULL) return PM3_EMALLOC; strcpy(filename, searchname); if (((strlen(filename) > 1) && (filename[0] == '/')) || ((strlen(filename) > 2) && (filename[0] == '.') && (filename[1] == '/'))) { - if (fileExists(filename)) - return filename; - else + if (fileExists(filename)) { + *foundpath = filename; + return PM3_SUCCESS; + } + else { goto out; + } } // else // try implicit relative path { - if (fileExists(filename)) - return filename; + if (fileExists(filename)) { + *foundpath = filename; + return PM3_SUCCESS; + } } // try pm3 dirs in current workdir (dev mode) const char *exec_path = get_my_executable_directory(); @@ -698,7 +699,8 @@ char *searchFile(const char *pm3dir, const char *searchname) { strcat(path, filename); if (fileExists(path)) { free(filename); - return path; + *foundpath = path; + return PM3_SUCCESS; } else { free(path); } @@ -715,7 +717,8 @@ char *searchFile(const char *pm3dir, const char *searchname) { strcat(path, filename); if (fileExists(path)) { free(filename); - return path; + *foundpath = path; + return PM3_SUCCESS; } else { free(path); } @@ -730,12 +733,27 @@ char *searchFile(const char *pm3dir, const char *searchname) { strcat(path, filename); if (fileExists(path)) { free(filename); - return path; + *foundpath = path; + return PM3_SUCCESS; } else { free(path); } } out: free(filename); - return NULL; + return PM3_EFILE; +} + +int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix) { + char *filename = filenamemcopy(searchname, suffix); + if (filename == NULL) return PM3_EMALLOC; + int res = searchFinalFile(foundpath, pm3dir, filename); + if (res != PM3_SUCCESS) { + if (res == PM3_EFILE) + PrintAndLogEx(FAILED, "Error - can't find %s", filename); + free(filename); + return res; + } + free(filename); + return PM3_SUCCESS; } diff --git a/client/fileutils.h b/client/fileutils.h index bb229efb5..1e912a899 100644 --- a/client/fileutils.h +++ b/client/fileutils.h @@ -160,8 +160,7 @@ int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, u */ int convertOldMfuDump(uint8_t **dump, size_t *dumplen); -char *filenamemcopy(const char *preferredName, const char *suffix); int searchAndList(const char *pm3dir, const char *ext); -char *searchFile(const char *pm3dir, const char *searchname); +int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix); #endif // FILEUTILS_H