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
11 changes: 10 additions & 1 deletion subcommands/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Mount struct {
SnapshotPath string
}

const snapshotRootPath = "/"

func init() {
subcommands.Register(func() subcommands.Subcommand { return &Mount{} }, 0, "mount")
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
}
22 changes: 21 additions & 1 deletion subcommands/mount/mount_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down