|
26 | 26 | #include <sys/stat.h>
|
27 | 27 | #include <fcntl.h>
|
28 | 28 |
|
| 29 | +#ifdef __APPLE__ |
| 30 | +# include <CoreFoundation/CoreFoundation.h> |
| 31 | +#endif /* __APPLE__ */ |
| 32 | + |
29 | 33 | #define SU_LOG_DOMAIN "confdb"
|
30 | 34 |
|
31 | 35 | #include <sigutils/log.h>
|
32 | 36 | #include "confdb.h"
|
33 | 37 |
|
34 | 38 | PTR_LIST(SUPRIVATE suscan_config_context_t, context);
|
35 | 39 |
|
36 |
| -SUPRIVATE const char *confdb_system_path = PKGDATADIR "/config"; |
| 40 | +SUPRIVATE const char *confdb_system_path; |
37 | 41 | SUPRIVATE const char *confdb_user_path;
|
38 | 42 |
|
| 43 | +#ifdef __APPLE__ |
39 | 44 | const char *
|
40 |
| -suscan_confdb_get_system_path(void) |
| 45 | +suscan_confdb_get_bundle_path(void) |
41 | 46 | {
|
| 47 | + CFBundleRef main_bundle = NULL; |
| 48 | + CFURLRef dir_url = NULL; |
| 49 | + CFStringRef dir_path = NULL; |
| 50 | + CFStringEncoding encmethod; |
42 | 51 | const char *path = NULL;
|
| 52 | + |
| 53 | + if ((main_bundle = CFBundleGetMainBundle()) != NULL) { |
| 54 | + SU_TRYCATCH( |
| 55 | + dir_url = CFBundleCopyResourceURL( |
| 56 | + main_bundle, |
| 57 | + CFSTR("suscan/config"), |
| 58 | + NULL, /* resourceType */ |
| 59 | + NULL /* dirName */), |
| 60 | + goto done); |
| 61 | + |
| 62 | + SU_TRYCATCH( |
| 63 | + dir_path = CFURLCopyFileSystemPath(dir_url, kCFURLPOSIXPathStyle), |
| 64 | + goto done); |
| 65 | + |
| 66 | + encmethod = CFStringGetSystemEncoding(); |
| 67 | + path = CFStringGetCStringPtr(dir_path, encmethod); |
| 68 | + } |
| 69 | + |
| 70 | +done: |
| 71 | + return path; |
| 72 | +} |
| 73 | +#endif /* __APPLE__ */ |
43 | 74 |
|
44 |
| - if ((path = getenv("SUSCAN_CONFIG_PATH")) != NULL) |
45 |
| - confdb_system_path = path; |
| 75 | +const char * |
| 76 | +suscan_confdb_get_system_path(void) |
| 77 | +{ |
| 78 | + const char *path = NULL; |
| 79 | + |
| 80 | + if (confdb_system_path == NULL) { |
| 81 | + if ((path = getenv("SUSCAN_CONFIG_PATH")) != NULL) { |
| 82 | + confdb_system_path = path; |
| 83 | + } else { |
| 84 | + confdb_system_path = PKGDATADIR "/config"; |
| 85 | + |
| 86 | + /* For MacOS there are better alternatives */ |
| 87 | +#ifdef __APPLE__ |
| 88 | + path = suscan_confdb_get_bundle_path(); |
| 89 | + if (path != NULL) |
| 90 | + confdb_system_path = path; |
| 91 | +#endif /* __APPLE__ */ |
| 92 | + } |
| 93 | + } |
46 | 94 |
|
47 | 95 | return confdb_system_path;
|
48 | 96 | }
|
|
0 commit comments