From 2efa75a0a9ca73f2a5b6ec71e5ae6cb43cdab580 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 15 Sep 2025 09:58:01 +0200 Subject: Allow specifying configuration directory to use --- src/conf.c | 45 ++++++++++++++++++++++++++++++++++----------- src/conf.h | 1 + src/main.c | 19 +++++++++++++++---- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/conf.c b/src/conf.c index 532dedc..5d2411d 100644 --- a/src/conf.c +++ b/src/conf.c @@ -35,10 +35,6 @@ #include #include -#ifdef WIN32 -#include -#endif - #include #include @@ -47,6 +43,8 @@ #include "log.h" #ifdef WIN32 +#include + #define DIR_SEP '\\' #define DIR_SEP_S "\\" #else @@ -180,13 +178,26 @@ static int mkdir_with_parents(const char *dir, int mode) /** * Creates a freedesktop compatible configuration directory. */ -static void config_create_config_dir(void) +static int config_create_config_dir(void) { const char *config_path = config_get_config_dir(); struct stat st; - if (stat(config_path, &st) != 0) { - mkdir_with_parents(config_path, 0755); + int res = stat(config_path, &st); + if (res != 0) { + res = mkdir_with_parents(config_path, 0755); } + return res; +} + +int config_set_config_dir(const char* path) +{ + if (!path) { + return -1; + } + free(__config_dir); + __config_dir = strdup(path); + usbmuxd_log(LL_DEBUG, "Setting config_dir to %s", __config_dir); + return config_create_config_dir(); } static int get_rand(int min, int max) @@ -270,7 +281,10 @@ static int config_set_value(const char *key, plist_t value) char *config_file = NULL; /* Make sure config directory exists */ - config_create_config_dir(); + if (config_create_config_dir() < 0) { + usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n"); + return -1; + } config_path = config_get_config_dir(); config_file = string_concat(config_path, DIR_SEP_S, CONFIG_FILE, NULL); @@ -342,7 +356,10 @@ int config_has_device_record(const char *udid) if (!udid) return 0; /* ensure config directory exists */ - config_create_config_dir(); + if (config_create_config_dir() < 0) { + usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n"); + return -1; + } /* build file path */ const char *config_path = config_get_config_dir(); @@ -422,7 +439,10 @@ int config_set_device_record(const char *udid, char* record_data, uint64_t recor } /* ensure config directory exists */ - config_create_config_dir(); + if (config_create_config_dir() < 0) { + usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n"); + return -1; + } /* build file path */ const char *config_path = config_get_config_dir(); @@ -458,7 +478,10 @@ int config_get_device_record(const char *udid, char **record_data, uint64_t *rec int res = 0; /* ensure config directory exists */ - config_create_config_dir(); + if (config_create_config_dir() < 0) { + usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n"); + return -1; + } /* build file path */ const char *config_path = config_get_config_dir(); diff --git a/src/conf.h b/src/conf.h index bbfa965..294ca18 100644 --- a/src/conf.h +++ b/src/conf.h @@ -27,6 +27,7 @@ #include const char *config_get_config_dir(); +int config_set_config_dir(const char* path); void config_get_system_buid(char **system_buid); diff --git a/src/main.c b/src/main.c index 836ac97..b09a187 100644 --- a/src/main.c +++ b/src/main.c @@ -517,6 +517,7 @@ static void usage() #ifdef HAVE_SYSTEMD printf(" -s, --systemd\t\tRun in systemd operation mode (implies -z and -f).\n"); #endif + printf(" -C, --config-dir PATH\tSpecify different configuration directory.\n"); printf(" -S, --socket ADDR:PORT | PATH Specify source ADDR and PORT or a UNIX\n"); printf(" \t\tsocket PATH to use for the listening socket.\n"); printf(" \t\tDefault: %s\n", socket_path); @@ -549,6 +550,7 @@ static void parse_opts(int argc, char **argv) #ifdef HAVE_SYSTEMD {"systemd", no_argument, NULL, 's'}, #endif + {"config-dir", required_argument, NULL, 'C'}, {"socket", required_argument, NULL, 'S'}, {"pidfile", required_argument, NULL, 'P'}, {"exit", no_argument, NULL, 'x'}, @@ -560,11 +562,11 @@ static void parse_opts(int argc, char **argv) int c; #ifdef HAVE_SYSTEMD - const char* opts_spec = "hfvVuU:xXsnzl:pS:P:"; + const char* opts_spec = "hfvVuU:xXsnzl:pC:S:P:"; #elif HAVE_UDEV - const char* opts_spec = "hfvVuU:xXnzl:pS:P:"; + const char* opts_spec = "hfvVuU:xXnzl:pC:S:P:"; #else - const char* opts_spec = "hfvVU:xXnzl:pS:P:"; + const char* opts_spec = "hfvVU:xXnzl:pC:S:P:"; #endif while (1) { @@ -611,6 +613,14 @@ static void parse_opts(int argc, char **argv) case 'z': opt_enable_exit = 1; break; + case 'C': + if (!*optarg) { + usbmuxd_log(LL_FATAL, "ERROR: --config-dir requires an argument"); + usage(); + exit(2); + } + config_set_config_dir(optarg); + break; case 'S': if (!*optarg || *optarg == '-') { usbmuxd_log(LL_FATAL, "ERROR: --socket requires an argument"); @@ -796,11 +806,12 @@ int main(int argc, char *argv[]) #ifdef HAVE_LIBIMOBILEDEVICE const char* userprefdir = config_get_config_dir(); + usbmuxd_log(LL_NOTICE, "Configuration directory: %s", userprefdir); struct stat fst; memset(&fst, '\0', sizeof(struct stat)); if (stat(userprefdir, &fst) < 0) { if (mkdir(userprefdir, 0775) < 0) { - usbmuxd_log(LL_FATAL, "Failed to create required directory '%s': %s", userprefdir, strerror(errno)); + usbmuxd_log(LL_FATAL, "Failed to create configuration directory '%s': %s", userprefdir, strerror(errno)); res = -1; goto terminate; } -- cgit v1.1-32-gdbae