Put logfiles in subdir

This commit is contained in:
Philippe Teuwen 2020-05-24 01:20:43 +02:00
commit a6f76444de
5 changed files with 46 additions and 7 deletions

View file

@ -32,7 +32,7 @@ static int setCmdHelp(const char *Cmd);
static char *prefGetFilename(void) { static char *prefGetFilename(void) {
char *path; char *path;
if (searchHomeFilePath(&path, preferencesFilename, false) == PM3_SUCCESS) if (searchHomeFilePath(&path, NULL, preferencesFilename, false) == PM3_SUCCESS)
return path; return path;
else else
return strdup(preferencesFilename); return strdup(preferencesFilename);

View file

@ -206,7 +206,7 @@ main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) {
} }
char *my_history_path = NULL; char *my_history_path = NULL;
if (searchHomeFilePath(&my_history_path, PROXHISTORY, true) != PM3_SUCCESS) { if (searchHomeFilePath(&my_history_path, NULL, PROXHISTORY, true) != PM3_SUCCESS) {
PrintAndLogEx(ERR, "No history will be recorded"); PrintAndLogEx(ERR, "No history will be recorded");
my_history_path = NULL; my_history_path = NULL;
} else { } else {

View file

@ -54,7 +54,7 @@ pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
static void fPrintAndLog(FILE *stream, const char *fmt, ...); static void fPrintAndLog(FILE *stream, const char *fmt, ...);
// needed by flasher, so let's put it here instead of fileutils.c // needed by flasher, so let's put it here instead of fileutils.c
int searchHomeFilePath(char **foundpath, const char *filename, bool create_home) { int searchHomeFilePath(char **foundpath, const char *subdir, const char *filename, bool create_home) {
if (foundpath == NULL) if (foundpath == NULL)
return PM3_EINVARG; return PM3_EINVARG;
const char *user_path = get_my_user_directory(); const char *user_path = get_my_user_directory();
@ -62,7 +62,8 @@ int searchHomeFilePath(char **foundpath, const char *filename, bool create_home)
fprintf(stderr, "Could not retrieve $HOME from the environment\n"); fprintf(stderr, "Could not retrieve $HOME from the environment\n");
return PM3_EFILE; return PM3_EFILE;
} }
char *path = calloc(strlen(user_path) + strlen(PM3_USER_DIRECTORY) + 1, sizeof(char)); size_t pathlen = strlen(user_path) + strlen(PM3_USER_DIRECTORY) + 1;
char *path = calloc(pathlen, sizeof(char));
if (path == NULL) if (path == NULL)
return PM3_EMALLOC; return PM3_EMALLOC;
strcpy(path, user_path); strcpy(path, user_path);
@ -96,11 +97,48 @@ int searchHomeFilePath(char **foundpath, const char *filename, bool create_home)
return PM3_EFILE; return PM3_EFILE;
} }
} }
if (subdir != NULL) {
pathlen += strlen(subdir);
path = realloc(path, pathlen * sizeof(char));
if (path == NULL)
return PM3_EMALLOC;
strcat(path, subdir);
#ifdef _WIN32
// Mingw _stat fails if path ends with /, so let's use a stripped path
if (path[strlen(path) - 1] == '/') {
path[strlen(path) - 1] = '\0';
result = _stat(path, &st);
path[strlen(path)] = '/';
} else {
result = _stat(path, &st);
}
#else
result = stat(path, &st);
#endif
if ((result != 0) && create_home) {
#ifdef _WIN32
if (_mkdir(path))
#else
if (mkdir(path, 0700))
#endif
{
fprintf(stderr, "Could not create user directory %s\n", path);
free(path);
return PM3_EFILE;
}
}
}
if (filename == NULL) { if (filename == NULL) {
*foundpath = path; *foundpath = path;
return PM3_SUCCESS; return PM3_SUCCESS;
} }
path = realloc(path, (strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(filename) + 1) * sizeof(char)); pathlen += strlen(filename);
path = realloc(path, pathlen * sizeof(char));
if (path == NULL)
return PM3_EMALLOC;
strcat(path, filename); strcat(path, filename);
*foundpath = path; *foundpath = path;
return PM3_SUCCESS; return PM3_SUCCESS;
@ -266,7 +304,7 @@ static void fPrintAndLog(FILE *stream, const char *fmt, ...) {
time_t now = time(NULL); time_t now = time(NULL);
timenow = gmtime(&now); timenow = gmtime(&now);
strftime(filename, sizeof(filename), PROXLOG, timenow); strftime(filename, sizeof(filename), PROXLOG, timenow);
if (searchHomeFilePath(&my_logfile_path, filename, true) != PM3_SUCCESS) { if (searchHomeFilePath(&my_logfile_path, LOGS_SUBDIR, filename, true) != PM3_SUCCESS) {
fprintf(stderr, "[-] Logging disabled!\n\n"); fprintf(stderr, "[-] Logging disabled!\n\n");
my_logfile_path = NULL; my_logfile_path = NULL;
logging = 0; logging = 0;

View file

@ -59,7 +59,7 @@ void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter);
void memcpy_filter_rlmarkers(void *dest, const void *src, size_t n); void memcpy_filter_rlmarkers(void *dest, const void *src, size_t n);
void memcpy_filter_emoji(void *dest, const void *src, size_t n, emojiMode_t mode); void memcpy_filter_emoji(void *dest, const void *src, size_t n, emojiMode_t mode);
int searchHomeFilePath(char **foundpath, const char *filename, bool create_home); int searchHomeFilePath(char **foundpath, const char *subdir, const char *filename, bool create_home);
extern pthread_mutex_t print_lock; extern pthread_mutex_t print_lock;

View file

@ -30,6 +30,7 @@
#define LUA_SCRIPTS_SUBDIR "luascripts" PATHSEP #define LUA_SCRIPTS_SUBDIR "luascripts" PATHSEP
#define RESOURCES_SUBDIR "resources" PATHSEP #define RESOURCES_SUBDIR "resources" PATHSEP
#define TRACES_SUBDIR "traces" PATHSEP #define TRACES_SUBDIR "traces" PATHSEP
#define LOGS_SUBDIR "logs" PATHSEP
#define FIRMWARES_SUBDIR "firmware" PATHSEP #define FIRMWARES_SUBDIR "firmware" PATHSEP
#define BOOTROM_SUBDIR "bootrom" PATHSEP "obj" PATHSEP #define BOOTROM_SUBDIR "bootrom" PATHSEP "obj" PATHSEP
#define FULLIMAGE_SUBDIR "armsrc" PATHSEP "obj" PATHSEP #define FULLIMAGE_SUBDIR "armsrc" PATHSEP "obj" PATHSEP