aboutsummaryrefslogtreecommitdiff
path: root/config_global.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-12-18 01:14:48 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-12-18 01:14:48 -0500
commit2be3d5c5d905bd748b8ce511033065fa5a83a59c (patch)
tree740de985535e765c37deb61dd87b03e131c5d0bb /config_global.c
parentfa992c8655f2fe27a97fe0e6768800a356de3744 (diff)
downloadspmc-2be3d5c5d905bd748b8ce511033065fa5a83a59c.tar.gz
Split up functions into different source files
Diffstat (limited to 'config_global.c')
-rw-r--r--config_global.c186
1 files changed, 186 insertions, 0 deletions
diff --git a/config_global.c b/config_global.c
new file mode 100644
index 0000000..b1c6dc2
--- /dev/null
+++ b/config_global.c
@@ -0,0 +1,186 @@
+#include "spm.h"
+
+char *get_user_conf_dir(void) {
+ char *result = NULL;
+ wordexp_t wexp;
+ wordexp("~/.spm", &wexp, 0);
+ if (wexp.we_wordc != 0) {
+ result = (char *)calloc(strlen(wexp.we_wordv[0]) + 1, sizeof(char));
+ if (!result) {
+ wordfree(&wexp);
+ return NULL;
+ }
+ strncpy(result, wexp.we_wordv[0], strlen(wexp.we_wordv[0]));
+ if (access(result, F_OK) != 0) {
+ mkdirs(result, 0755);
+ }
+ }
+ wordfree(&wexp);
+ return result;
+}
+
+char *get_user_config_file(void) {
+ const char *filename = "spm.conf";
+ char template[PATH_MAX];
+ char *ucd = get_user_conf_dir();
+ if (!ucd) {
+ return NULL;
+ }
+ // Initialize temporary path
+ template[0] = '\0';
+
+ sprintf(template, "%s%c%s", ucd, DIRSEP, filename);
+ if (access(template, F_OK) != 0) {
+ // No configuration exists, so fail
+ return NULL;
+ }
+ free(ucd);
+ // Allocate and return path to configuration file
+ return strdup(template);
+}
+
+char *get_user_tmp_dir(void) {
+ char template[PATH_MAX];
+ char *ucd = get_user_conf_dir();
+ sprintf(template, "%s%ctmp", ucd, DIRSEP);
+
+ if (access(template, F_OK) != 0) {
+ if (mkdirs(template, 0755) != 0) {
+ return NULL;
+ }
+ }
+
+ free(ucd);
+ return strdup(template);
+}
+
+char *get_user_package_dir(void) {
+ char template[PATH_MAX];
+ char *ucd = get_user_conf_dir();
+ sprintf(template, "%s%cpkgs", ucd, DIRSEP);
+
+ if (access(template, F_OK) != 0) {
+ if (mkdirs(template, 0755) != 0) {
+ return NULL;
+ }
+ }
+
+ free(ucd);
+ return strdup(template);
+}
+
+/**
+ * Check whether SPM has access to external programs it needs
+ */
+void check_runtime_environment(void) {
+ int bad_rt = 0;
+ char *required[] = {
+ "patchelf",
+ "rsync",
+ "tar",
+ "bash",
+ "reloc",
+ NULL,
+ };
+ for (int i = 0; required[i] != NULL; i++) {
+ char *result = find_executable(required[i]);
+ if (!result) {
+ fprintf(stderr, "Required program '%s' is not installed\n", required[i]);
+ bad_rt = 1;
+ }
+ free(result);
+ }
+ if (bad_rt) {
+ exit(1);
+ }
+}
+
+
+
+void init_config_global(void) {
+ SPM_GLOBAL.user_config_basedir = NULL;
+ SPM_GLOBAL.user_config_file = NULL;
+ SPM_GLOBAL.package_dir = NULL;
+ SPM_GLOBAL.tmp_dir = NULL;
+ SPM_GLOBAL.config = NULL;
+
+ if (uname(&SPM_GLOBAL.sysinfo) != 0) {
+ fprintf(SYSERROR);
+ exit(1);
+ }
+
+ SPM_GLOBAL.user_config_basedir = get_user_conf_dir();
+ SPM_GLOBAL.user_config_file = get_user_config_file();
+ if (SPM_GLOBAL.user_config_file) {
+ SPM_GLOBAL.config = config_read(SPM_GLOBAL.user_config_file);
+ }
+
+ ConfigItem *item = NULL;
+
+ // Initialize temp directory
+ item = config_get(SPM_GLOBAL.config, "tmp_dir");
+ if (item) {
+ SPM_GLOBAL.tmp_dir = item->value;
+ if (access(SPM_GLOBAL.tmp_dir, F_OK) != 0) {
+ if (mkdirs(SPM_GLOBAL.tmp_dir, 0755) != 0) {
+ fprintf(stderr, "Unable to create global temporary directory: %s\n", SPM_GLOBAL.tmp_dir);
+ fprintf(SYSERROR);
+ exit(1);
+ }
+ }
+ }
+ else {
+ SPM_GLOBAL.tmp_dir = get_user_tmp_dir();
+ }
+
+ // Initialize package directory
+ item = config_get(SPM_GLOBAL.config, "package_dir");
+ if (item) {
+ SPM_GLOBAL.package_dir = item->value;
+ if (access(SPM_GLOBAL.package_dir, F_OK) != 0) {
+ if (mkdirs(SPM_GLOBAL.package_dir, 0755) != 0) {
+ fprintf(stderr, "Unable to create global package directory: %s\n", SPM_GLOBAL.package_dir);
+ fprintf(SYSERROR);
+ exit(1);
+ }
+ }
+ }
+ else {
+ SPM_GLOBAL.package_dir = get_user_package_dir();
+ }
+}
+
+void free_global_config(void) {
+ if (SPM_GLOBAL.package_dir) {
+ free(SPM_GLOBAL.package_dir);
+ }
+ if (SPM_GLOBAL.tmp_dir) {
+ free(SPM_GLOBAL.tmp_dir);
+ }
+ if (SPM_GLOBAL.user_config_basedir) {
+ free(SPM_GLOBAL.user_config_basedir);
+ }
+ if (SPM_GLOBAL.user_config_file) {
+ free(SPM_GLOBAL.user_config_file);
+ }
+ if (SPM_GLOBAL.config) {
+ config_free(SPM_GLOBAL.config);
+ }
+}
+
+void show_global_config(void) {
+ printf("#---------------------------\n");
+ printf("#---- SPM CONFIGURATION ----\n");
+ printf("#---------------------------\n");
+ printf("# base dir: %s\n", SPM_GLOBAL.user_config_basedir ? SPM_GLOBAL.user_config_basedir : "none (check write permission on home directory)");
+ printf("# config file: %s\n", SPM_GLOBAL.user_config_file ? SPM_GLOBAL.user_config_file : "none");
+ if (SPM_GLOBAL.user_config_file) {
+ printf("# config file contents:\n");
+ for (int i = 0; SPM_GLOBAL.config[i] != NULL; i++) {
+ printf("# -> %s: %s\n", SPM_GLOBAL.config[i]->key, SPM_GLOBAL.config[i]->value);
+ }
+ }
+ printf("# package storage: %s\n", SPM_GLOBAL.package_dir);
+ printf("# temp storage: %s\n", SPM_GLOBAL.tmp_dir);
+ printf("\n");
+}