diff --git a/subcommands/mount/mount.go b/subcommands/mount/mount.go index 45e90541f..d0b71b3d1 100644 --- a/subcommands/mount/mount.go +++ b/subcommands/mount/mount.go @@ -40,6 +40,8 @@ type Mount struct { SnapshotPath string } +const snapshotRootPath = "/" + func init() { subcommands.Register(func() subcommands.Subcommand { return &Mount{} }, 0, "mount") } @@ -81,7 +83,7 @@ func (cmd *Mount) Execute(ctx *appcontext.AppContext, repo *repository.Repositor return 1, err } - subFS, err := fs.Sub(pvfs, path[1:]) + subFS, err := snapshotSubFS(pvfs, path) if err != nil { return 1, err } @@ -93,3 +95,10 @@ func (cmd *Mount) Execute(ctx *appcontext.AppContext, repo *repository.Repositor } return fuse.ExecuteFUSE(ctx, repo, cmd.Mountpoint, cmd.LocateOptions, chrootFS, cmd.AllowOthers) } + +func snapshotSubFS(snapshotFS fs.FS, snapshotPath string) (fs.FS, error) { + if snapshotPath == snapshotRootPath { + return snapshotFS, nil + } + return fs.Sub(snapshotFS, strings.TrimPrefix(snapshotPath, snapshotRootPath)) +} diff --git a/subcommands/mount/mount_parse_test.go b/subcommands/mount/mount_parse_test.go index 428b70ca0..f156f0955 100644 --- a/subcommands/mount/mount_parse_test.go +++ b/subcommands/mount/mount_parse_test.go @@ -2,14 +2,21 @@ package mount import ( "bytes" + "io/fs" "testing" + "testing/fstest" _ "github.com/PlakarKorp/integrations/fs/exporter" - ptesting "github.com/PlakarKorp/plakar/testing" "github.com/PlakarKorp/plakar/subcommands" + ptesting "github.com/PlakarKorp/plakar/testing" "github.com/stretchr/testify/require" ) +const ( + snapshotSubFSTestFile = "a.txt" + snapshotSubFSTestContent = "x" +) + func TestMountRegisteredFactory(t *testing.T) { cmd, _, _ := subcommands.Lookup([]string{"mount"}) require.NotNil(t, cmd) @@ -46,6 +53,19 @@ func TestMountParseAllowOthers(t *testing.T) { require.True(t, cmd.AllowOthers) } +func TestSnapshotSubFSRootPathUsesSnapshotRoot(t *testing.T) { + snapshotFS := fstest.MapFS{ + snapshotSubFSTestFile: &fstest.MapFile{Data: []byte(snapshotSubFSTestContent)}, + } + + subFS, err := snapshotSubFS(snapshotFS, snapshotRootPath) + require.NoError(t, err) + + data, err := fs.ReadFile(subFS, snapshotSubFSTestFile) + require.NoError(t, err) + require.Equal(t, []byte(snapshotSubFSTestContent), data) +} + func TestMountExecuteBadSnapshot(t *testing.T) { // A snapshot path that resolves to nothing fails before any mount is // attempted (so this is safe on all platforms, FUSE never engages).