Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/specgen/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ func GenVolumeMounts(volumeFlag []string) (map[string]spec.Mount, map[string]*Na
splitVol[0] = src
}

// Resolve symlinks for absolute host paths
if filepath.IsAbs(src) {
if resolved := ResolveVolumeSourcePath(src); resolved != src {
src = resolved
splitVol[0] = resolved
}
}

if len(splitVol) == 1 {
// This is an anonymous named volume. Only thing given
// is destination.
Expand Down
16 changes: 16 additions & 0 deletions pkg/specgen/volumes_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build darwin

package specgen

import "path/filepath"

// ResolveVolumeSourcePath follows symlinks for absolute host paths so they
// line up with the actual on-disk location (e.g. /tmp -> /private/tmp).
// If resolution fails, the original path is returned unchanged.
func ResolveVolumeSourcePath(src string) string {
resolved, err := filepath.EvalSymlinks(src)
if err != nil {
return src
}
return resolved
}
24 changes: 24 additions & 0 deletions pkg/specgen/volumes_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build darwin

package specgen

import (
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestResolveVolumeSourcePathTmpSymlink(t *testing.T) {
dir, err := os.MkdirTemp("/tmp", "podman-vol-")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.TempDir() for automatic cleanup. According to the implementation, this calls MkdirTemp using the default temporary path derived from the TMPDIR environment variable (MkdirTemp, TempDir() string)). You can use t.Setenv to force this to /tmp.

Suggested change
dir, err := os.MkdirTemp("/tmp", "podman-vol-")
dir := t.TempDir()

require.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})

resolved := ResolveVolumeSourcePath(dir)
require.NotEmpty(t, resolved)
assert.True(t, strings.HasPrefix(resolved, "/private/tmp/"), "resolved path should point at /private/tmp")
}
8 changes: 8 additions & 0 deletions pkg/specgen/volumes_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !darwin

package specgen

// ResolveVolumeSourcePath is a no-op on non-macOS platforms.
func ResolveVolumeSourcePath(src string) string {
return src
}