feat: relative parent paths on bind mount src

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
This commit is contained in:
Alano Terblanche 2024-03-26 12:54:22 +01:00
parent b39bbb4e3b
commit 3342b070b4
No known key found for this signature in database
GPG Key ID: 0E8FACD1BA98DE27
3 changed files with 14 additions and 3 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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