From 3342b070b4070602c716260a9e95b81f24e8c621 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:54:22 +0100 Subject: [PATCH] feat: relative parent paths on bind mount src Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- cli/command/container/opts.go | 2 +- opts/mount.go | 2 +- opts/mount_test.go | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 9ecef8ed0279..0e89d631f75a 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -382,7 +382,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con if parsed.Type == string(mounttypes.TypeBind) { if hostPart, targetPath, ok := strings.Cut(bind, ":"); ok { - if strings.HasPrefix(hostPart, "."+string(filepath.Separator)) || hostPart == "." { + if !filepath.IsAbs(hostPart) && strings.HasPrefix(hostPart, ".") { if absHostPart, err := filepath.Abs(hostPart); err == nil { hostPart = absHostPart } diff --git a/opts/mount.go b/opts/mount.go index 430b858e8fae..d72050809d8f 100644 --- a/opts/mount.go +++ b/opts/mount.go @@ -93,7 +93,7 @@ func (m *MountOpt) Set(value string) error { mount.Type = mounttypes.Type(strings.ToLower(val)) case "source", "src": mount.Source = val - if strings.HasPrefix(val, "."+string(filepath.Separator)) || val == "." { + if !filepath.IsAbs(val) && strings.HasPrefix(val, ".") { if abs, err := filepath.Abs(val); err == nil { mount.Source = abs } diff --git a/opts/mount_test.go b/opts/mount_test.go index fb3cd0329a92..090fc2fadc60 100644 --- a/opts/mount_test.go +++ b/opts/mount_test.go @@ -39,11 +39,22 @@ func TestMountRelative(t *testing.T) { name: "Current path", path: ".", bind: "type=bind,source=.,target=/target", - }, { + }, + { name: "Current path with slash", path: "./", bind: "type=bind,source=./,target=/target", }, + { + name: "Parent path with slash", + path: "../", + bind: "type=bind,source=../,target=/target", + }, + { + name: "Parent path", + path: "..", + bind: "type=bind,source=..,target=/target", + }, } { t.Run(testcase.name, func(t *testing.T) { var mount MountOpt