From 81de8b438db3d1ace0219993df490db73431a975 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Thu, 16 May 2024 10:17:59 -0700 Subject: [PATCH] archive,hookfiles: convert paths to usr-merge when necessary (MR 39) Co-authored-by: Caleb Connolly --- internal/archive/archive.go | 5 ++- internal/filelist/hookfiles/hookfiles.go | 4 ++ internal/osutil/osutil.go | 21 ++++++++++ internal/osutil/osutil_test.go | 49 ++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 internal/osutil/osutil_test.go diff --git a/internal/archive/archive.go b/internal/archive/archive.go index aaffdd4..9c639bf 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -237,7 +237,10 @@ func (archive *Archive) AddItemsExclude(flister filelist.FileLister, exclude fil // Adds the given file or directory at "source" to the archive at "dest" func (archive *Archive) AddItem(source string, dest string) error { - + if osutil.HasMergedUsr() { + source = osutil.MergeUsr(source) + dest = osutil.MergeUsr(dest) + } sourceStat, err := os.Lstat(source) if err != nil { e, ok := err.(*os.PathError) diff --git a/internal/filelist/hookfiles/hookfiles.go b/internal/filelist/hookfiles/hookfiles.go index 0ad7a17..5a093ad 100644 --- a/internal/filelist/hookfiles/hookfiles.go +++ b/internal/filelist/hookfiles/hookfiles.go @@ -11,6 +11,7 @@ import ( "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist" "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc" + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil" ) type HookFiles struct { @@ -64,6 +65,9 @@ func slurpFiles(fd io.Reader) (*filelist.FileList, error) { } src, dest, has_dest := strings.Cut(line, ":") + if osutil.HasMergedUsr() { + src = osutil.MergeUsr(src) + } fFiles, err := misc.GetFiles([]string{src}, true) if err != nil { diff --git a/internal/osutil/osutil.go b/internal/osutil/osutil.go index 85ec867..2fce65e 100644 --- a/internal/osutil/osutil.go +++ b/internal/osutil/osutil.go @@ -27,6 +27,27 @@ func HasMergedUsr() bool { return true } +// Converts given path to one supported by a merged /usr config. +// E.g., /bin/foo becomes /usr/bin/foo, /lib/bar becomes /usr/lib/bar +// See: https://www.freedesktop.org/wiki/Software/systemd/TheCaseForTheUsrMerge +func MergeUsr(file string) string { + + // Prepend /usr to supported paths + for _, prefix := range []string{"/bin", "/sbin", "/lib", "/lib64"} { + if strings.HasPrefix(file, prefix) { + file = filepath.Join("/usr", file) + break + } + } + + // Convert /usr/sbin --> /usr/bin + if part, found := strings.CutPrefix(file, "/usr/sbin"); found { + file = filepath.Join("/usr/bin/", part) + } + + return file +} + // Converts a relative symlink target path (e.g. ../../lib/foo.so), that is // absolute path func RelativeSymlinkTargetToDir(symPath string, dir string) (string, error) { diff --git a/internal/osutil/osutil_test.go b/internal/osutil/osutil_test.go new file mode 100644 index 0000000..0d24158 --- /dev/null +++ b/internal/osutil/osutil_test.go @@ -0,0 +1,49 @@ +// Copyright 2024 Clayton Craft +// SPDX-License-Identifier: GPL-3.0-or-later + +package osutil + +import ( + "testing" +) + +func TestMergeUsr(t *testing.T) { + subtests := []struct { + in string + expected string + }{ + { + in: "/bin/foo", + expected: "/usr/bin/foo", + }, + { + in: "/sbin/foo", + expected: "/usr/bin/foo", + }, + { + in: "/usr/sbin/foo", + expected: "/usr/bin/foo", + }, + { + in: "/usr/bin/foo", + expected: "/usr/bin/foo", + }, + { + in: "/lib/foo.so", + expected: "/usr/lib/foo.so", + }, + { + in: "/lib64/foo.so", + expected: "/usr/lib64/foo.so", + }, + } + + for _, st := range subtests { + t.Run(st.in, func(t *testing.T) { + out := MergeUsr(st.in) + if out != st.expected { + t.Fatalf("expected: %q, got: %q\n", st.expected, out) + } + }) + } +}