diff options
Diffstat (limited to 'spm.c')
-rw-r--r-- | spm.c | 152 |
1 files changed, 114 insertions, 38 deletions
@@ -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; } |