From 5d4066dd1e90fa704b67384327af72b3ec121102 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Sat, 23 Apr 2022 09:46:41 -0500 Subject: installpkg: Avoid using /install on target dir Use a temporary randomly named directory named like 'installpkg-2052825695' for the package's metadata in its '/install' directory. It will help allow running multiple instances on the same target root directory by removing a point of conflict. Attempt to place this directory in the host's default temporary directory. If it does not exist, use the host's root path. --- filesystem.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'filesystem.go') diff --git a/filesystem.go b/filesystem.go index c33df15..dec07f3 100644 --- a/filesystem.go +++ b/filesystem.go @@ -4,7 +4,9 @@ import ( "io/fs" "math" "os" + "path/filepath" "strconv" + "strings" "time" ) @@ -46,6 +48,19 @@ func IsDir(path string) (bool, error) { return info.IsDir(), nil } +func IsParentDir(parent, sub string) (bool, error) { + up := ".." + string(os.PathSeparator) + + rel, err := filepath.Rel(parent, sub) + if err != nil { + return false, err + } + if !strings.HasPrefix(rel, up) && rel != ".." { + return true, nil + } + return false, nil +} + func IsSymlink(path string) (bool, error) { info, err := os.Stat(path) if err != nil { -- cgit v1.2.3