diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-10 12:51:30 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-10 12:51:30 -0500 |
commit | 0627d1f093db4064c114e8fb2a2a8e824fb606bb (patch) | |
tree | fa2db4b4b298d004d0041dff3e971aa3ca9c61cf | |
parent | c9ed1675a99a2a10eebbc2acd046aeb0e3e9fa77 (diff) | |
download | spmc-0627d1f093db4064c114e8fb2a2a8e824fb606bb.tar.gz |
Configuration++
-rw-r--r-- | config.c | 3 | ||||
-rw-r--r-- | spm.c | 152 | ||||
-rw-r--r-- | spm.h | 10 |
3 files changed, 124 insertions, 41 deletions
@@ -154,6 +154,9 @@ void config_free(ConfigItem **item) { /// \param key search for key in config records /// \return success=pointer to record, failure=NULL ConfigItem *config_get(ConfigItem **item, const char *key) { + if (!item) { + return NULL; + } for (int i = 0; item[i] != NULL; i++) { if (!strcmp(item[i]->key, key)) { return item[i]; @@ -14,7 +14,7 @@ char *get_user_conf_dir() { result = (char *)calloc(strlen(wexp.we_wordv[0]) + 1, sizeof(char)); if (!result) { wordfree(&wexp); - NULL; + return NULL; } strncpy(result, wexp.we_wordv[0], strlen(wexp.we_wordv[0])); if (access(result, F_OK) != 0) { @@ -27,29 +27,52 @@ char *get_user_conf_dir() { char *get_user_config_file() { const char *filename = "spm.conf"; - char *result = NULL; - char tmp[PATH_MAX]; + char template[PATH_MAX]; char *ucb = get_user_conf_dir(); if (!ucb) { return NULL; } // Initialize temporary path - tmp[0] = '\0'; + template[0] = '\0'; - sprintf(tmp, "%s%c%s", ucb, DIRSEP, filename); - if (access(tmp, F_OK) != 0) { + sprintf(template, "%s%c%s", ucb, DIRSEP, filename); + if (access(template, F_OK) != 0) { // No configuration exists, so fail return NULL; } // Allocate and return path to configuration file - result = (char *)calloc(strlen(tmp) + 1, sizeof(char)); - if (!result) { - return NULL; + return strdup(template); +} + +char *get_user_tmp_dir() { + 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; + } } - strncpy(result, tmp, strlen(tmp)); - return result; + free(ucd); + return strdup(template); +} + +char *get_user_package_dir() { + 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); } /** @@ -1182,31 +1205,98 @@ int install(const char *destroot, const char *_package) { char template[PATH_MAX]; char suffix[PATH_MAX] = "spm_destroot_XXXXXX"; - char *ucd = get_user_conf_dir(); - sprintf(template, "%s%c%s", ucd, DIRSEP, suffix); + sprintf(template, "%s%c%s", TMP_DIR, DIRSEP, suffix); char *tmpdir = mkdtemp(template); tar_extract_archive(package, tmpdir); rmdirs(tmpdir); free(package); - free(ucd); } int init_config_global() { + SPM_GLOBAL.user_config_basedir = NULL; + SPM_GLOBAL.user_config_file = NULL; + SPM_GLOBAL.package_dir = NULL; + SPM_GLOBAL.tmp_dir = 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(); - SPM_GLOBAL.package_dir = realpath(PKG_DIR, NULL); 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() { + 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() { - printf("configuration directory: %s\n", SPM_GLOBAL.user_config_basedir); - printf("configuration file: %s\n", SPM_GLOBAL.user_config_file); - printf("configuration:\n"); - for (int i = 0; SPM_GLOBAL.config[i] != NULL; i++) { - printf("-> %s\n", SPM_GLOBAL.config[i]); + 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"); } int main(int argc, char *argv[]) { @@ -1234,22 +1324,6 @@ int main(int argc, char *argv[]) { } /* - char **files = fstree("/bin"); - for (int i = 0; files[i] != NULL; i++) { - if (fstrstr(files[i], "/usr/lib") == 0) { - printf("%d: %s\n", i, files[i]); - } - free(files[i]); - } - free(files); - */ - - /* - if (mkdirs("/tmp/this/is/a/test", 0755) == -1 && errno) { - fprintf(SYSERROR); - } - */ - char *test_path = realpath("root/lib/python3.7/lib-dynload/_multiprocessing.cpython-37m-x86_64-linux-gnu.so", NULL); if (!test_path) { fprintf(stderr, "Unable to get absolute path for %s\n", test_path); @@ -1267,9 +1341,11 @@ int main(int argc, char *argv[]) { if (set_rpath(test_path, rpath) != 0) { fprintf(stderr, "RPATH assignment failed\n"); } + */ install("/tmp/root", "python"); - free(test_path); - free(rpath); + //free(test_path); + //free(rpath); + free_global_config(); return 0; } @@ -14,6 +14,9 @@ #include <time.h> #include <sys/stat.h> #include <wordexp.h> +#if !defined(_WIN32) +#include <sys/utsname.h> +#endif #include "config.h" @@ -29,7 +32,8 @@ #define NOT_DIRSEP DIRSEP_WIN32 #endif -#define PKG_DIR "../pkgs" +#define PKG_DIR SPM_GLOBAL.package_dir +#define TMP_DIR SPM_GLOBAL.tmp_dir #define SHELL_DEFAULT 1 << 0 #define SHELL_OUTPUT 1 << 1 @@ -41,12 +45,12 @@ typedef struct { } ConfigItem; typedef struct { - char *platform; - char *arch; char *package_dir; + char *tmp_dir; char *user_config_basedir; char *user_config_file; ConfigItem **config; + struct utsname sysinfo; } spm_vars; static spm_vars SPM_GLOBAL; |