aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-02-10 15:28:57 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-02-10 15:28:57 -0500
commit1ade521a4261b6e02d0a02622dacbdb2d0ff731a (patch)
tree9f41a7bfd0b426cc1a0e8b57e0fdc491c0866a0d
parent0c13cc71617550940638a143b626c426769c803f (diff)
downloadspmc-1ade521a4261b6e02d0a02622dacbdb2d0ff731a.tar.gz
Add relocate_auto()
* Move relocation code out of install()
-rw-r--r--include/spm.h1
-rw-r--r--src/install.c43
-rw-r--r--src/relocation.c49
3 files changed, 53 insertions, 40 deletions
diff --git a/include/spm.h b/include/spm.h
index 80db07a..d62cb42 100644
--- a/include/spm.h
+++ b/include/spm.h
@@ -205,6 +205,7 @@ int tar_extract_file(const char *archive, const char* filename, const char *dest
// relocation.c
int relocate(const char *filename, const char *_oldstr, const char *_newstr);
+void relocate_root(const char *destroot, const char *baseroot);
int replace_text(char *data, const char *_spattern, const char *_sreplacement);
int file_replace_text(char *filename, const char *spattern, const char *sreplacement);
RelocationEntry **prefixes_read(const char *filename);
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);
+}
+