From 07719b7c125321d65369f2d98ee05f3c0cd18df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Tue, 12 Jul 2022 21:13:30 +0200 Subject: [PATCH] Expand tilde using the current home path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For instance it might be C:\Users\anders on the host, but translated to /c/Users/anders path in the guest. Signed-off-by: Anders F Björklund --- pkg/cidata/cidata.go | 4 ++-- pkg/hostagent/mount.go | 7 +++++- pkg/localpathutil/localpathutil.go | 19 +++++++++++----- pkg/localpathutil/localpathutil_test.go | 29 +++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 pkg/localpathutil/localpathutil_test.go diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go index f695cfbdef8..64df50193e1 100644 --- a/pkg/cidata/cidata.go +++ b/pkg/cidata/cidata.go @@ -134,11 +134,11 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort } for i, f := range y.Mounts { tag := fmt.Sprintf("mount%d", i) - location, err := localpathutil.Expand(f.Location) + location, err := localpathutil.ExpandHome(f.Location, hostHome) if err != nil { return err } - mountPoint, err := localpathutil.Expand(f.MountPoint) + mountPoint, err := localpathutil.ExpandHome(f.MountPoint, u.HomeDir) if err != nil { return err } diff --git a/pkg/hostagent/mount.go b/pkg/hostagent/mount.go index cfe870e20c3..22a4e10d5d6 100644 --- a/pkg/hostagent/mount.go +++ b/pkg/hostagent/mount.go @@ -9,6 +9,7 @@ import ( "github.com/lima-vm/lima/pkg/limayaml" "github.com/lima-vm/lima/pkg/localpathutil" + "github.com/lima-vm/lima/pkg/osutil" "github.com/lima-vm/sshocker/pkg/reversesshfs" "github.com/sirupsen/logrus" ) @@ -39,7 +40,11 @@ func (a *HostAgent) setupMount(ctx context.Context, m limayaml.Mount) (*mount, e return nil, err } - mountPoint, err := localpathutil.Expand(m.MountPoint) + u, err := osutil.LimaUser(false) + if err != nil { + return nil, err + } + mountPoint, err := localpathutil.ExpandHome(m.MountPoint, u.HomeDir) if err != nil { return nil, err } diff --git a/pkg/localpathutil/localpathutil.go b/pkg/localpathutil/localpathutil.go index ba5124dceb7..aaf71640e1d 100644 --- a/pkg/localpathutil/localpathutil.go +++ b/pkg/localpathutil/localpathutil.go @@ -12,15 +12,11 @@ import ( // Paths like "~foo/bar" are unsupported. // // FIXME: is there an existing library for this? -func Expand(orig string) (string, error) { +func ExpandHome(orig string, homeDir string) (string, error) { s := orig if s == "" { return "", errors.New("empty path") } - homeDir, err := os.UserHomeDir() - if err != nil { - return "", err - } if strings.HasPrefix(s, "~") { if s == "~" || strings.HasPrefix(s, "~/") { @@ -30,5 +26,18 @@ func Expand(orig string) (string, error) { return "", fmt.Errorf("unexpandable path %q", orig) } } + return s, nil +} + +// Expand expands a path like "~", "~/", "~/foo", on the host. +func Expand(orig string) (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + s, err := ExpandHome(orig, homeDir) + if err != nil { + return "", err + } return filepath.Abs(s) } diff --git a/pkg/localpathutil/localpathutil_test.go b/pkg/localpathutil/localpathutil_test.go new file mode 100644 index 00000000000..6f28b998c93 --- /dev/null +++ b/pkg/localpathutil/localpathutil_test.go @@ -0,0 +1,29 @@ +package localpathutil + +import ( + "path/filepath" + "testing" + + "gotest.tools/v3/assert" +) + +func TestExpandTilde(t *testing.T) { + _, err := Expand("~") + assert.NilError(t, err) +} + +func TestExpandDir(t *testing.T) { + h, err := Expand("~") + assert.NilError(t, err) + d, err := Expand("~/foo") + assert.NilError(t, err) + // make sure this uses the local filepath + assert.Equal(t, d, filepath.Join(h, "foo")) +} + +func TestExpandHome(t *testing.T) { + p, err := ExpandHome("~/bar", "/home/user") + assert.NilError(t, err) + // make sure this uses the unix path + assert.Equal(t, p, "/home/user/bar") +}