From 1ade521a4261b6e02d0a02622dacbdb2d0ff731a Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 10 Feb 2020 15:28:57 -0500 Subject: Add relocate_auto() * Move relocation code out of install() --- src/install.c | 43 +++---------------------------------------- src/relocation.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/install.c b/src/install.c index 29cee37..24725f8 100644 --- a/src/install.c +++ b/src/install.c @@ -42,7 +42,7 @@ int metadata_remove(const char *_path) { /** * Install a package and its dependencies into a destination root. * The destination is created if it does not exist. - * @param destroot directory to install package + * @param _destroot directory to install package * @param _package name of archive to install (not a path) * @return success=0, error=-1 (general), -2 (unable to create `destroot`) */ @@ -63,7 +63,6 @@ int install(const char *destroot, const char *_package) { } } - char cwd[PATH_MAX]; char source[PATH_MAX]; char template[PATH_MAX]; @@ -90,44 +89,8 @@ int install(const char *destroot, const char *_package) { } tar_extract_archive(package, tmpdir); - getcwd(cwd, sizeof(cwd)); - - RelocationEntry **b_record = NULL; - RelocationEntry **t_record = NULL; - chdir(tmpdir); - { - // Rewrite binary prefixes - b_record = prefixes_read(SPM_META_PREFIX_BIN); - if (b_record) { - for (int i = 0; b_record[i] != NULL; i++) { - if (file_is_binexec(b_record[i]->path)) { - if (SPM_GLOBAL.verbose) { - printf("Relocate RPATH: %s\n", b_record[i]->path); - } - rpath_autoset(b_record[i]->path); - } - if (SPM_GLOBAL.verbose) { - printf("Relocate DATA : %s\n", b_record[i]->path); - } - relocate(b_record[i]->path, b_record[i]->prefix, destroot); - } - } - - // Rewrite text prefixes - t_record = prefixes_read(SPM_META_PREFIX_TEXT); - if (t_record) { - for (int i = 0; t_record[i] != NULL; i++) { - if (SPM_GLOBAL.verbose) { - printf("Relocate TEXT : %s\n", t_record[i]->path); - } - file_replace_text(t_record[i]->path, t_record[i]->prefix, destroot); - } - } - - prefixes_free(b_record); - prefixes_free(t_record); - } - chdir(cwd); + // Relocate temporary directory + relocate_root(destroot, tmpdir); // Append a trailing slash to tmpdir to direct rsync to copy files, not the directory, into destroot sprintf(source, "%s%c", tmpdir, DIRSEP); diff --git a/src/relocation.c b/src/relocation.c index 4dc784f..ce6a896 100644 --- a/src/relocation.c +++ b/src/relocation.c @@ -324,3 +324,52 @@ int relocate(const char *_filename, const char *_oldstr, const char *_newstr) { return returncode; } +/** + * Parse package metadata and set `baseroot` binaries/text to point to `destroot`. + * `baseroot` should be a temporary directory because its contents are modified + * + * @param destroot + * @param baseroot + */ +void relocate_root(const char *destroot, const char *baseroot) { + RelocationEntry **b_record = NULL; + RelocationEntry **t_record = NULL; + char cwd[PATH_MAX]; + + getcwd(cwd, sizeof(cwd)); + chdir(baseroot); + { + // Rewrite binary prefixes + b_record = prefixes_read(SPM_META_PREFIX_BIN); + if (b_record) { + for (int i = 0; b_record[i] != NULL; i++) { + if (file_is_binexec(b_record[i]->path)) { + if (SPM_GLOBAL.verbose) { + printf("Relocate RPATH: %s\n", b_record[i]->path); + } + rpath_autoset(b_record[i]->path); + } + if (SPM_GLOBAL.verbose) { + printf("Relocate DATA : %s\n", b_record[i]->path); + } + relocate(b_record[i]->path, b_record[i]->prefix, destroot); + } + } + + // Rewrite text prefixes + t_record = prefixes_read(SPM_META_PREFIX_TEXT); + if (t_record) { + for (int i = 0; t_record[i] != NULL; i++) { + if (SPM_GLOBAL.verbose) { + printf("Relocate TEXT : %s (%s -> %s)\n", t_record[i]->path, t_record[i]->prefix, destroot); + } + file_replace_text(t_record[i]->path, t_record[i]->prefix, destroot); + } + } + + prefixes_free(b_record); + prefixes_free(t_record); + } + chdir(cwd); +} + -- cgit