Skip to content

Commit 2efa75a

Browse files
committed
Allow specifying configuration directory to use
1 parent 08f4cd9 commit 2efa75a

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

src/conf.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
#include <sys/stat.h>
3636
#include <errno.h>
3737

38-
#ifdef WIN32
39-
#include <shlobj.h>
40-
#endif
41-
4238
#include <libimobiledevice-glue/utils.h>
4339
#include <plist/plist.h>
4440

@@ -47,6 +43,8 @@
4743
#include "log.h"
4844

4945
#ifdef WIN32
46+
#include <shlobj.h>
47+
5048
#define DIR_SEP '\\'
5149
#define DIR_SEP_S "\\"
5250
#else
@@ -180,13 +178,26 @@ static int mkdir_with_parents(const char *dir, int mode)
180178
/**
181179
* Creates a freedesktop compatible configuration directory.
182180
*/
183-
static void config_create_config_dir(void)
181+
static int config_create_config_dir(void)
184182
{
185183
const char *config_path = config_get_config_dir();
186184
struct stat st;
187-
if (stat(config_path, &st) != 0) {
188-
mkdir_with_parents(config_path, 0755);
185+
int res = stat(config_path, &st);
186+
if (res != 0) {
187+
res = mkdir_with_parents(config_path, 0755);
189188
}
189+
return res;
190+
}
191+
192+
int config_set_config_dir(const char* path)
193+
{
194+
if (!path) {
195+
return -1;
196+
}
197+
free(__config_dir);
198+
__config_dir = strdup(path);
199+
usbmuxd_log(LL_DEBUG, "Setting config_dir to %s", __config_dir);
200+
return config_create_config_dir();
190201
}
191202

192203
static int get_rand(int min, int max)
@@ -270,7 +281,10 @@ static int config_set_value(const char *key, plist_t value)
270281
char *config_file = NULL;
271282

272283
/* Make sure config directory exists */
273-
config_create_config_dir();
284+
if (config_create_config_dir() < 0) {
285+
usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n");
286+
return -1;
287+
}
274288

275289
config_path = config_get_config_dir();
276290
config_file = string_concat(config_path, DIR_SEP_S, CONFIG_FILE, NULL);
@@ -342,7 +356,10 @@ int config_has_device_record(const char *udid)
342356
if (!udid) return 0;
343357

344358
/* ensure config directory exists */
345-
config_create_config_dir();
359+
if (config_create_config_dir() < 0) {
360+
usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n");
361+
return -1;
362+
}
346363

347364
/* build file path */
348365
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
422439
}
423440

424441
/* ensure config directory exists */
425-
config_create_config_dir();
442+
if (config_create_config_dir() < 0) {
443+
usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n");
444+
return -1;
445+
}
426446

427447
/* build file path */
428448
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
458478
int res = 0;
459479

460480
/* ensure config directory exists */
461-
config_create_config_dir();
481+
if (config_create_config_dir() < 0) {
482+
usbmuxd_log(LL_ERROR, "ERROR: Failed to create config directory\n");
483+
return -1;
484+
}
462485

463486
/* build file path */
464487
const char *config_path = config_get_config_dir();

src/conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <plist/plist.h>
2828

2929
const char *config_get_config_dir();
30+
int config_set_config_dir(const char* path);
3031

3132
void config_get_system_buid(char **system_buid);
3233

src/main.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ static void usage()
517517
#ifdef HAVE_SYSTEMD
518518
printf(" -s, --systemd\t\tRun in systemd operation mode (implies -z and -f).\n");
519519
#endif
520+
printf(" -C, --config-dir PATH\tSpecify different configuration directory.\n");
520521
printf(" -S, --socket ADDR:PORT | PATH Specify source ADDR and PORT or a UNIX\n");
521522
printf(" \t\tsocket PATH to use for the listening socket.\n");
522523
printf(" \t\tDefault: %s\n", socket_path);
@@ -549,6 +550,7 @@ static void parse_opts(int argc, char **argv)
549550
#ifdef HAVE_SYSTEMD
550551
{"systemd", no_argument, NULL, 's'},
551552
#endif
553+
{"config-dir", required_argument, NULL, 'C'},
552554
{"socket", required_argument, NULL, 'S'},
553555
{"pidfile", required_argument, NULL, 'P'},
554556
{"exit", no_argument, NULL, 'x'},
@@ -560,11 +562,11 @@ static void parse_opts(int argc, char **argv)
560562
int c;
561563

562564
#ifdef HAVE_SYSTEMD
563-
const char* opts_spec = "hfvVuU:xXsnzl:pS:P:";
565+
const char* opts_spec = "hfvVuU:xXsnzl:pC:S:P:";
564566
#elif HAVE_UDEV
565-
const char* opts_spec = "hfvVuU:xXnzl:pS:P:";
567+
const char* opts_spec = "hfvVuU:xXnzl:pC:S:P:";
566568
#else
567-
const char* opts_spec = "hfvVU:xXnzl:pS:P:";
569+
const char* opts_spec = "hfvVU:xXnzl:pC:S:P:";
568570
#endif
569571

570572
while (1) {
@@ -611,6 +613,14 @@ static void parse_opts(int argc, char **argv)
611613
case 'z':
612614
opt_enable_exit = 1;
613615
break;
616+
case 'C':
617+
if (!*optarg) {
618+
usbmuxd_log(LL_FATAL, "ERROR: --config-dir requires an argument");
619+
usage();
620+
exit(2);
621+
}
622+
config_set_config_dir(optarg);
623+
break;
614624
case 'S':
615625
if (!*optarg || *optarg == '-') {
616626
usbmuxd_log(LL_FATAL, "ERROR: --socket requires an argument");
@@ -796,11 +806,12 @@ int main(int argc, char *argv[])
796806

797807
#ifdef HAVE_LIBIMOBILEDEVICE
798808
const char* userprefdir = config_get_config_dir();
809+
usbmuxd_log(LL_NOTICE, "Configuration directory: %s", userprefdir);
799810
struct stat fst;
800811
memset(&fst, '\0', sizeof(struct stat));
801812
if (stat(userprefdir, &fst) < 0) {
802813
if (mkdir(userprefdir, 0775) < 0) {
803-
usbmuxd_log(LL_FATAL, "Failed to create required directory '%s': %s", userprefdir, strerror(errno));
814+
usbmuxd_log(LL_FATAL, "Failed to create configuration directory '%s': %s", userprefdir, strerror(errno));
804815
res = -1;
805816
goto terminate;
806817
}

0 commit comments

Comments
 (0)