From aaa572a0eec12dc920606bfb2eeac8aa912307a8 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 2 Nov 2023 10:39:32 +0100 Subject: [PATCH] utils: use GetRuntimeDir() from c/storage Signed-off-by: Giuseppe Scrivano --- pkg/config/default.go | 3 +- pkg/netns/netns_linux.go | 4 +- pkg/util/util_supported.go | 91 -------------------------------------- pkg/util/util_windows.go | 13 ------ 4 files changed, 3 insertions(+), 108 deletions(-) delete mode 100644 pkg/util/util_supported.go delete mode 100644 pkg/util/util_windows.go diff --git a/pkg/config/default.go b/pkg/config/default.go index c6fd6365d..3b0bf913b 100644 --- a/pkg/config/default.go +++ b/pkg/config/default.go @@ -13,7 +13,6 @@ import ( nettypes "github.com/containers/common/libnetwork/types" "github.com/containers/common/pkg/apparmor" "github.com/containers/common/pkg/cgroupv2" - "github.com/containers/common/pkg/util" "github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/unshare" "github.com/containers/storage/types" @@ -489,7 +488,7 @@ func defaultTmpDir() (string, error) { return getLibpodTmpDir(), nil } - runtimeDir, err := util.GetRuntimeDir() + runtimeDir, err := homedir.GetRuntimeDir() if err != nil { return "", err } diff --git a/pkg/netns/netns_linux.go b/pkg/netns/netns_linux.go index f2569d379..9f0336bc0 100644 --- a/pkg/netns/netns_linux.go +++ b/pkg/netns/netns_linux.go @@ -30,7 +30,7 @@ import ( "sync" "github.com/containernetworking/plugins/pkg/ns" - "github.com/containers/common/pkg/util" + "github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/unshare" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" @@ -40,7 +40,7 @@ import ( // rootless, it needs to be at a location writable by user. func GetNSRunDir() (string, error) { if unshare.IsRootless() { - rootlessDir, err := util.GetRuntimeDir() + rootlessDir, err := homedir.GetRuntimeDir() if err != nil { return "", err } diff --git a/pkg/util/util_supported.go b/pkg/util/util_supported.go deleted file mode 100644 index 0cd53af53..000000000 --- a/pkg/util/util_supported.go +++ /dev/null @@ -1,91 +0,0 @@ -//go:build linux || darwin || freebsd -// +build linux darwin freebsd - -package util - -import ( - "errors" - "fmt" - "os" - "path/filepath" - "sync" - "syscall" - - "github.com/containers/storage/pkg/homedir" - "github.com/containers/storage/pkg/unshare" - "github.com/sirupsen/logrus" -) - -var ( - rootlessRuntimeDirOnce sync.Once - rootlessRuntimeDir string -) - -// isWriteableOnlyByOwner checks that the specified permission mask allows write -// access only to the owner. -func isWriteableOnlyByOwner(perm os.FileMode) bool { - return (perm & 0o722) == 0o700 -} - -// GetRuntimeDir returns the runtime directory -func GetRuntimeDir() (string, error) { - var rootlessRuntimeDirError error - - rootlessRuntimeDirOnce.Do(func() { - runtimeDir, err := homedir.GetRuntimeDir() - if err != nil { - logrus.Debug(err) - } - if runtimeDir != "" { - st, err := os.Stat(runtimeDir) - if err != nil { - rootlessRuntimeDirError = err - return - } - if int(st.Sys().(*syscall.Stat_t).Uid) != os.Geteuid() { - rootlessRuntimeDirError = fmt.Errorf("XDG_RUNTIME_DIR directory %q is not owned by the current user", runtimeDir) - return - } - } - uid := fmt.Sprintf("%d", unshare.GetRootlessUID()) - if runtimeDir == "" { - tmpDir := filepath.Join("/run", "user", uid) - if err := os.MkdirAll(tmpDir, 0o700); err != nil { - logrus.Debugf("unable to make temp dir: %v", err) - } - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && isWriteableOnlyByOwner(st.Mode().Perm()) { - runtimeDir = tmpDir - } - } - if runtimeDir == "" { - tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("podman-run-%s", uid)) - if err := os.MkdirAll(tmpDir, 0o700); err != nil { - logrus.Debugf("unable to make temp dir %v", err) - } - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && isWriteableOnlyByOwner(st.Mode().Perm()) { - runtimeDir = tmpDir - } - } - if runtimeDir == "" { - home := os.Getenv("HOME") - if home == "" { - rootlessRuntimeDirError = errors.New("neither XDG_RUNTIME_DIR nor HOME was set non-empty") - return - } - resolvedHome, err := filepath.EvalSymlinks(home) - if err != nil { - rootlessRuntimeDirError = fmt.Errorf("cannot resolve home: %w", err) - return - } - runtimeDir = filepath.Join(resolvedHome, "rundir") - } - rootlessRuntimeDir = runtimeDir - }) - - if rootlessRuntimeDirError != nil { - return "", rootlessRuntimeDirError - } - return rootlessRuntimeDir, nil -} diff --git a/pkg/util/util_windows.go b/pkg/util/util_windows.go deleted file mode 100644 index 1525bdc34..000000000 --- a/pkg/util/util_windows.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build windows -// +build windows - -package util - -import ( - "errors" -) - -// getRuntimeDir returns the runtime directory -func GetRuntimeDir() (string, error) { - return "", errors.New("this function is not implemented for windows") -}