From 3a70ee70df506e055a9b5e909c54f0619090d09c Mon Sep 17 00:00:00 2001 From: sumantro93 Date: Thu, 31 Oct 2024 19:43:21 +0530 Subject: [PATCH 1/5] fixes #1564 usr /home/sumantro --- src/cmd/completion.go | 1 + src/cmd/create.go | 3 ++- src/cmd/enter.go | 3 ++- src/cmd/help.go | 1 + src/cmd/list.go | 1 + src/cmd/rm.go | 1 + src/cmd/rmi.go | 1 + src/cmd/rootMigrationPath.go | 1 + src/cmd/run.go | 3 ++- src/cmd/utils.go | 1 + 10 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cmd/completion.go b/src/cmd/completion.go index 9a391b003..e3e1601d3 100644 --- a/src/cmd/completion.go +++ b/src/cmd/completion.go @@ -18,6 +18,7 @@ package cmd import ( "os" + "os/user" "strings" "github.com/containers/toolbox/pkg/utils" diff --git a/src/cmd/create.go b/src/cmd/create.go index cc84ec689..5cd5a198e 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "os" + "os/user" "path/filepath" "strings" "time" @@ -300,7 +301,7 @@ func createContainer(container, image, release, authFile string, showCommandToEn dbusSystemSocketMountArg := dbusSystemSocket + ":" + dbusSystemSocket - homeDirEvaled, err := filepath.EvalSymlinks(currentUser.HomeDir) + homeDirEvaled, err := filepath.EvalSymlinks(os.UserHomeDir) if err != nil { return fmt.Errorf("failed to canonicalize %s", currentUser.HomeDir) } diff --git a/src/cmd/enter.go b/src/cmd/enter.go index 8602b744b..71701314f 100644 --- a/src/cmd/enter.go +++ b/src/cmd/enter.go @@ -20,7 +20,8 @@ import ( "errors" "fmt" "os" - + "os/user" + "github.com/containers/toolbox/pkg/utils" "github.com/spf13/cobra" ) diff --git a/src/cmd/help.go b/src/cmd/help.go index bc153d2a0..9a5b1e2f8 100644 --- a/src/cmd/help.go +++ b/src/cmd/help.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "os/user" "github.com/containers/toolbox/pkg/utils" "github.com/spf13/cobra" diff --git a/src/cmd/list.go b/src/cmd/list.go index c97f88eea..a87c34956 100644 --- a/src/cmd/list.go +++ b/src/cmd/list.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "os/user" "sort" "text/tabwriter" diff --git a/src/cmd/rm.go b/src/cmd/rm.go index 8e7c5b941..b10e15969 100644 --- a/src/cmd/rm.go +++ b/src/cmd/rm.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "os/user" "strings" "github.com/containers/toolbox/pkg/podman" diff --git a/src/cmd/rmi.go b/src/cmd/rmi.go index f10b33da2..6921d5877 100644 --- a/src/cmd/rmi.go +++ b/src/cmd/rmi.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "os/user" "strings" "github.com/containers/toolbox/pkg/podman" diff --git a/src/cmd/rootMigrationPath.go b/src/cmd/rootMigrationPath.go index b5cbaedee..4c380612f 100644 --- a/src/cmd/rootMigrationPath.go +++ b/src/cmd/rootMigrationPath.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "os" + "os/user" "strings" "github.com/containers/toolbox/pkg/utils" diff --git a/src/cmd/run.go b/src/cmd/run.go index 719c0d6ab..eda4c48ec 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -24,6 +24,7 @@ import ( "fmt" "io" "os" + "os/user" "path/filepath" "strconv" "strings" @@ -441,7 +442,7 @@ func runCommandWithFallbacks(container string, workDir = runFallbackWorkDirs[runFallbackWorkDirsIndex] if workDir == "" { - workDir = currentUser.HomeDir + workDir = os.UserHomeDir() } fmt.Fprintf(os.Stderr, "Using %s instead.\n", workDir) diff --git a/src/cmd/utils.go b/src/cmd/utils.go index 6b1519f24..250c6d3df 100644 --- a/src/cmd/utils.go +++ b/src/cmd/utils.go @@ -25,6 +25,7 @@ import ( "fmt" "io" "os" + "os/user" "os/exec" "os/user" "path/filepath" From 5496938ca9495711ab0e87054c564880241882c2 Mon Sep 17 00:00:00 2001 From: sumantro93 Date: Thu, 31 Oct 2024 20:28:46 +0530 Subject: [PATCH 2/5] remove unused imports and fix code --- src/cmd/completion.go | 1 - src/cmd/create.go | 3 ++- src/cmd/enter.go | 3 +-- src/cmd/help.go | 2 +- src/cmd/initContainer.go | 1 - src/cmd/list.go | 1 - src/cmd/rm.go | 1 - src/cmd/rmi.go | 1 - 8 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/cmd/completion.go b/src/cmd/completion.go index e3e1601d3..9a391b003 100644 --- a/src/cmd/completion.go +++ b/src/cmd/completion.go @@ -18,7 +18,6 @@ package cmd import ( "os" - "os/user" "strings" "github.com/containers/toolbox/pkg/utils" diff --git a/src/cmd/create.go b/src/cmd/create.go index 5cd5a198e..908893065 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -301,7 +301,8 @@ func createContainer(container, image, release, authFile string, showCommandToEn dbusSystemSocketMountArg := dbusSystemSocket + ":" + dbusSystemSocket - homeDirEvaled, err := filepath.EvalSymlinks(os.UserHomeDir) + homeDirEvaled = os.UserHomeDir() + homeDirEvaled, err := filepath.EvalSymlinks(homeDirEvaled) if err != nil { return fmt.Errorf("failed to canonicalize %s", currentUser.HomeDir) } diff --git a/src/cmd/enter.go b/src/cmd/enter.go index 71701314f..8602b744b 100644 --- a/src/cmd/enter.go +++ b/src/cmd/enter.go @@ -20,8 +20,7 @@ import ( "errors" "fmt" "os" - "os/user" - + "github.com/containers/toolbox/pkg/utils" "github.com/spf13/cobra" ) diff --git a/src/cmd/help.go b/src/cmd/help.go index 9a5b1e2f8..7317ba3f7 100644 --- a/src/cmd/help.go +++ b/src/cmd/help.go @@ -20,7 +20,7 @@ import ( "errors" "fmt" "os" - "os/user" + "github.com/containers/toolbox/pkg/utils" "github.com/spf13/cobra" diff --git a/src/cmd/initContainer.go b/src/cmd/initContainer.go index 4b9011940..d1ade5848 100644 --- a/src/cmd/initContainer.go +++ b/src/cmd/initContainer.go @@ -21,7 +21,6 @@ import ( "fmt" "io/ioutil" "os" - "os/user" "path/filepath" "strconv" "strings" diff --git a/src/cmd/list.go b/src/cmd/list.go index a87c34956..c97f88eea 100644 --- a/src/cmd/list.go +++ b/src/cmd/list.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "os" - "os/user" "sort" "text/tabwriter" diff --git a/src/cmd/rm.go b/src/cmd/rm.go index b10e15969..8e7c5b941 100644 --- a/src/cmd/rm.go +++ b/src/cmd/rm.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "os" - "os/user" "strings" "github.com/containers/toolbox/pkg/podman" diff --git a/src/cmd/rmi.go b/src/cmd/rmi.go index 6921d5877..f10b33da2 100644 --- a/src/cmd/rmi.go +++ b/src/cmd/rmi.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "os" - "os/user" "strings" "github.com/containers/toolbox/pkg/podman" From ed57ca64e2cec79896931bed188c6efa88c1a1f6 Mon Sep 17 00:00:00 2001 From: sumantro93 Date: Thu, 31 Oct 2024 21:25:21 +0530 Subject: [PATCH 3/5] fix code --- src/cmd/create.go | 15 +++++++++++++-- src/cmd/run.go | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/cmd/create.go b/src/cmd/create.go index 908893065..bb8b3b8c2 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -301,14 +301,25 @@ func createContainer(container, image, release, authFile string, showCommandToEn dbusSystemSocketMountArg := dbusSystemSocket + ":" + dbusSystemSocket - homeDirEvaled = os.UserHomeDir() + user, err := user.Current() + if err != nil{ + logrus.Errorf("Failed to get HOME directory: %v", err) + } + resolvedPath, err := filepath.EvalSymlinks(user) + if err != nil{ + logrus.Errorf("Failed to evaluate symlinks for HOME directory: %v", err) + return err + } + + + /*homeDirEvaled = os.UserHomeDir() homeDirEvaled, err := filepath.EvalSymlinks(homeDirEvaled) if err != nil { return fmt.Errorf("failed to canonicalize %s", currentUser.HomeDir) } logrus.Debugf("%s canonicalized to %s", currentUser.HomeDir, homeDirEvaled) - homeDirMountArg := homeDirEvaled + ":" + homeDirEvaled + ":rslave" + homeDirMountArg := homeDirEvaled + ":" + homeDirEvaled + ":rslave"*/ var avahiSocketMount []string diff --git a/src/cmd/run.go b/src/cmd/run.go index eda4c48ec..d57354399 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -442,7 +442,7 @@ func runCommandWithFallbacks(container string, workDir = runFallbackWorkDirs[runFallbackWorkDirsIndex] if workDir == "" { - workDir = os.UserHomeDir() + workDir = user.Current() } fmt.Fprintf(os.Stderr, "Using %s instead.\n", workDir) From a264586f138ffa83fbc5dddbee7bffffa7ded976 Mon Sep 17 00:00:00 2001 From: sumantro93 Date: Thu, 31 Oct 2024 21:46:17 +0530 Subject: [PATCH 4/5] fix err --- src/cmd/create.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cmd/create.go b/src/cmd/create.go index bb8b3b8c2..cd9d256e2 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -305,7 +305,8 @@ func createContainer(container, image, release, authFile string, showCommandToEn if err != nil{ logrus.Errorf("Failed to get HOME directory: %v", err) } - resolvedPath, err := filepath.EvalSymlinks(user) + homeDir := user.HomeDir + resolvedPath, err := filepath.EvalSymlinks(HomeDir) if err != nil{ logrus.Errorf("Failed to evaluate symlinks for HOME directory: %v", err) return err From 4c3aabad3a5910b6eb07f098cfb69ef945bcc14b Mon Sep 17 00:00:00 2001 From: sumantro93 Date: Fri, 1 Nov 2024 08:15:34 +0530 Subject: [PATCH 5/5] handle err --- src/cmd/initContainer.go | 1 + src/cmd/utils.go | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cmd/initContainer.go b/src/cmd/initContainer.go index d1ade5848..4b9011940 100644 --- a/src/cmd/initContainer.go +++ b/src/cmd/initContainer.go @@ -21,6 +21,7 @@ import ( "fmt" "io/ioutil" "os" + "os/user" "path/filepath" "strconv" "strings" diff --git a/src/cmd/utils.go b/src/cmd/utils.go index 250c6d3df..ccc9c778c 100644 --- a/src/cmd/utils.go +++ b/src/cmd/utils.go @@ -25,9 +25,7 @@ import ( "fmt" "io" "os" - "os/user" "os/exec" - "os/user" "path/filepath" "strings" "syscall"