From 42e4c74b53993f266bdd747cecfbf75d517303e0 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Mon, 25 Apr 2022 14:55:35 +0000 Subject: [PATCH] refactor: move mount/unmount to user package Signed-off-by: Edita Kizinevic --- internal/app/siftool/mount.go | 8 +++--- internal/app/siftool/unmount.go | 10 +++---- pkg/siftool/unmount_test.go | 4 +-- pkg/{sif => user}/mount.go | 46 ++++++++++++++++--------------- pkg/{sif => user}/unmount.go | 40 +++++++++++++-------------- pkg/{sif => user}/unmount_test.go | 16 ++++++----- 6 files changed, 64 insertions(+), 60 deletions(-) rename pkg/{sif => user}/mount.go (67%) rename pkg/{sif => user}/unmount.go (62%) rename pkg/{sif => user}/unmount_test.go (88%) diff --git a/internal/app/siftool/mount.go b/internal/app/siftool/mount.go index f3cd9c23..1287c61c 100644 --- a/internal/app/siftool/mount.go +++ b/internal/app/siftool/mount.go @@ -12,13 +12,13 @@ package siftool import ( "context" - "github.com/apptainer/sif/v2/pkg/sif" + "github.com/apptainer/sif/v2/pkg/user" ) // Mount mounts the primary system partition of the SIF file at path into mountPath. func (a *App) Mount(ctx context.Context, path, mountPath string) error { - return sif.MountFUSE(ctx, path, mountPath, - sif.OptMountFUSEStdout(a.opts.out), - sif.OptMountFUSEStderr(a.opts.err), + return user.Mount(ctx, path, mountPath, + user.OptMountStdout(a.opts.out), + user.OptMountStderr(a.opts.err), ) } diff --git a/internal/app/siftool/unmount.go b/internal/app/siftool/unmount.go index 86d1ef99..915df3f9 100644 --- a/internal/app/siftool/unmount.go +++ b/internal/app/siftool/unmount.go @@ -12,13 +12,13 @@ package siftool import ( "context" - "github.com/apptainer/sif/v2/pkg/sif" + "github.com/apptainer/sif/v2/pkg/user" ) -// Unmounts the FUSE mounted filesystem at mountPath. +// Unmounts the filesystem at mountPath. func (a *App) Unmount(ctx context.Context, mountPath string) error { - return sif.UnmountFUSE(ctx, mountPath, - sif.OptUnmountFUSEStdout(a.opts.out), - sif.OptUnmountFUSEStderr(a.opts.err), + return user.Unmount(ctx, mountPath, + user.OptUnmountStdout(a.opts.out), + user.OptUnmountStderr(a.opts.err), ) } diff --git a/pkg/siftool/unmount_test.go b/pkg/siftool/unmount_test.go index 676656b8..1cfd2656 100644 --- a/pkg/siftool/unmount_test.go +++ b/pkg/siftool/unmount_test.go @@ -16,7 +16,7 @@ import ( "path/filepath" "testing" - "github.com/apptainer/sif/v2/pkg/sif" + "github.com/apptainer/sif/v2/pkg/user" ) func Test_command_getUnmount(t *testing.T) { @@ -36,7 +36,7 @@ func Test_command_getUnmount(t *testing.T) { }) testSIF := filepath.Join(corpus, "one-group.sif") - if err := sif.MountFUSE(context.Background(), testSIF, path); err != nil { + if err := user.Mount(context.Background(), testSIF, path); err != nil { t.Fatal(err) } diff --git a/pkg/sif/mount.go b/pkg/user/mount.go similarity index 67% rename from pkg/sif/mount.go rename to pkg/user/mount.go index 7c5457fa..9283773c 100644 --- a/pkg/sif/mount.go +++ b/pkg/user/mount.go @@ -7,7 +7,7 @@ // LICENSE file distributed with the sources of this project regarding your // rights to use or distribute this software. -package sif +package user import ( "context" @@ -17,10 +17,12 @@ import ( "os" "os/exec" "path/filepath" + + "github.com/apptainer/sif/v2/pkg/sif" ) // mountSquashFS mounts the SquashFS filesystem from path at offset into mountPath. -func mountSquashFS(ctx context.Context, offset int64, path, mountPath string, mo mountFUSEOpts) error { +func mountSquashFS(ctx context.Context, offset int64, path, mountPath string, mo mountOpts) error { args := []string{ "-o", fmt.Sprintf("ro,offset=%d", offset), filepath.Clean(path), @@ -38,27 +40,27 @@ func mountSquashFS(ctx context.Context, offset int64, path, mountPath string, mo return nil } -// mountFUSEOpts accumulates mount options. -type mountFUSEOpts struct { +// mountOpts accumulates mount options. +type mountOpts struct { stdout io.Writer stderr io.Writer squashfusePath string } -// MountFUSEOpt are used to specify mount options. -type MountFUSEOpt func(*mountFUSEOpts) error +// MountOpt are used to specify mount options. +type MountOpt func(*mountOpts) error // OptMountStdout writes standard output to w. -func OptMountFUSEStdout(w io.Writer) MountFUSEOpt { - return func(mo *mountFUSEOpts) error { +func OptMountStdout(w io.Writer) MountOpt { + return func(mo *mountOpts) error { mo.stdout = w return nil } } -// OptMountFUSEStderr writes standard error to w. -func OptMountFUSEStderr(w io.Writer) MountFUSEOpt { - return func(mo *mountFUSEOpts) error { +// OptMountStderr writes standard error to w. +func OptMountStderr(w io.Writer) MountOpt { + return func(mo *mountOpts) error { mo.stderr = w return nil } @@ -66,10 +68,10 @@ func OptMountFUSEStderr(w io.Writer) MountFUSEOpt { var errSquashfusePathInvalid = errors.New("squashfuse path must be relative or absolute") -// OptMountFUSESquashfusePath sets an explicit path to the squashfuse binary. The path must be an +// OptMountSquashfusePath sets an explicit path to the squashfuse binary. The path must be an // absolute or relative path. -func OptMountFUSESquashfusePath(path string) MountFUSEOpt { - return func(mo *mountFUSEOpts) error { +func OptMountSquashfusePath(path string) MountOpt { + return func(mo *mountOpts) error { if filepath.Base(path) == path { return errSquashfusePathInvalid } @@ -80,16 +82,16 @@ func OptMountFUSESquashfusePath(path string) MountFUSEOpt { var errUnsupportedFSType = errors.New("unrecognized filesystem type") -// MountFUSE mounts the primary system partition of the SIF file at path into mountPath. +// Mount mounts the primary system partition of the SIF file at path into mountPath. // -// MountFUSE may start one or more underlying processes. By default, stdout and stderr of these +// Mount may start one or more underlying processes. By default, stdout and stderr of these // processes is discarded. To modify this behavior, consider using OptMountStdout and/or // OptMountStderr. // -// By default, MountFUSE searches for a squashfuse binary in the directories named by the PATH +// By default, Mount searches for a squashfuse binary in the directories named by the PATH // environment variable. To override this behavior, consider using OptMountSquashfusePath(). -func MountFUSE(ctx context.Context, path, mountPath string, opts ...MountFUSEOpt) error { - mo := mountFUSEOpts{ +func Mount(ctx context.Context, path, mountPath string, opts ...MountOpt) error { + mo := mountOpts{ squashfusePath: "squashfuse", } @@ -99,13 +101,13 @@ func MountFUSE(ctx context.Context, path, mountPath string, opts ...MountFUSEOpt } } - f, err := LoadContainerFromPath(path, OptLoadWithFlag(os.O_RDONLY)) + f, err := sif.LoadContainerFromPath(path, sif.OptLoadWithFlag(os.O_RDONLY)) if err != nil { return fmt.Errorf("failed to load image: %w", err) } defer func() { _ = f.UnloadContainer() }() - d, err := f.GetDescriptor(WithPartitionType(PartPrimSys)) + d, err := f.GetDescriptor(sif.WithPartitionType(sif.PartPrimSys)) if err != nil { return fmt.Errorf("failed to get partition descriptor: %w", err) } @@ -116,7 +118,7 @@ func MountFUSE(ctx context.Context, path, mountPath string, opts ...MountFUSEOpt } switch fs { - case FsSquash: + case sif.FsSquash: return mountSquashFS(ctx, d.Offset(), path, mountPath, mo) default: return errUnsupportedFSType diff --git a/pkg/sif/unmount.go b/pkg/user/unmount.go similarity index 62% rename from pkg/sif/unmount.go rename to pkg/user/unmount.go index 4531b90a..4c48f79c 100644 --- a/pkg/sif/unmount.go +++ b/pkg/user/unmount.go @@ -7,7 +7,7 @@ // LICENSE file distributed with the sources of this project regarding your // rights to use or distribute this software. -package sif +package user import ( "context" @@ -19,7 +19,7 @@ import ( ) // unmountSquashFS unmounts the filesystem at mountPath. -func unmountSquashFS(ctx context.Context, mountPath string, uo unmountFUSEOpts) error { +func unmountSquashFS(ctx context.Context, mountPath string, uo unmountOpts) error { args := []string{ "-u", filepath.Clean(mountPath), @@ -35,27 +35,27 @@ func unmountSquashFS(ctx context.Context, mountPath string, uo unmountFUSEOpts) return nil } -// unmountFUSEOpts accumulates unmount options. -type unmountFUSEOpts struct { +// unmountOpts accumulates unmount options. +type unmountOpts struct { stdout io.Writer stderr io.Writer fusermountPath string } -// UnmountFUSEOpt are used to specify unmount options. -type UnmountFUSEOpt func(*unmountFUSEOpts) error +// UnmountOpt are used to specify unmount options. +type UnmountOpt func(*unmountOpts) error -// OptUnmountFUSEStdout writes standard output to w. -func OptUnmountFUSEStdout(w io.Writer) UnmountFUSEOpt { - return func(mo *unmountFUSEOpts) error { +// OptUnmountStdout writes standard output to w. +func OptUnmountStdout(w io.Writer) UnmountOpt { + return func(mo *unmountOpts) error { mo.stdout = w return nil } } -// OptUnmountFUSEStderr writes standard error to w. -func OptUnmountFUSEStderr(w io.Writer) UnmountFUSEOpt { - return func(mo *unmountFUSEOpts) error { +// OptUnmountStderr writes standard error to w. +func OptUnmountStderr(w io.Writer) UnmountOpt { + return func(mo *unmountOpts) error { mo.stderr = w return nil } @@ -63,9 +63,9 @@ func OptUnmountFUSEStderr(w io.Writer) UnmountFUSEOpt { var errFusermountPathInvalid = errors.New("fusermount path must be relative or absolute") -// OptUnmountFUSEFusermountPath sets the path to the fusermount binary. -func OptUnmountFUSEFusermountPath(path string) UnmountFUSEOpt { - return func(mo *unmountFUSEOpts) error { +// OptUnmountFusermountPath sets the path to the fusermount binary. +func OptUnmountFusermountPath(path string) UnmountOpt { + return func(mo *unmountOpts) error { if filepath.Base(path) == path { return errFusermountPathInvalid } @@ -74,16 +74,16 @@ func OptUnmountFUSEFusermountPath(path string) UnmountFUSEOpt { } } -// UnmountFUSE unmounts the FUSE mounted filesystem at mountPath. +// Unmount unmounts the filesystem at mountPath. // -// UnmountFUSE may start one or more underlying processes. By default, stdout and stderr of these +// Unmount may start one or more underlying processes. By default, stdout and stderr of these // processes is discarded. To modify this behavior, consider using OptUnmountStdout and/or // OptUnmountStderr. // -// By default, UnmountFUSE searches for a fusermount binary in the directories named by the PATH +// By default, Unmount searches for a fusermount binary in the directories named by the PATH // environment variable. To override this behavior, consider using OptUnmountFusermountPath(). -func UnmountFUSE(ctx context.Context, mountPath string, opts ...UnmountFUSEOpt) error { - uo := unmountFUSEOpts{ +func Unmount(ctx context.Context, mountPath string, opts ...UnmountOpt) error { + uo := unmountOpts{ fusermountPath: "fusermount", } diff --git a/pkg/sif/unmount_test.go b/pkg/user/unmount_test.go similarity index 88% rename from pkg/sif/unmount_test.go rename to pkg/user/unmount_test.go index 002c13e9..aea98658 100644 --- a/pkg/sif/unmount_test.go +++ b/pkg/user/unmount_test.go @@ -7,7 +7,7 @@ // LICENSE file distributed with the sources of this project regarding your // rights to use or distribute this software. -package sif +package user import ( "bufio" @@ -21,7 +21,9 @@ import ( "testing" ) -func Test_UnmountFUSE(t *testing.T) { +var corpus = filepath.Join("..", "..", "test", "images") + +func Test_Unmount(t *testing.T) { if _, err := exec.LookPath("squashfuse"); err != nil { t.Skip(" not found, skipping mount tests") } @@ -42,7 +44,7 @@ func Test_UnmountFUSE(t *testing.T) { name string mountSIF string mountPath string - opts []UnmountFUSEOpt + opts []UnmountOpt wantErr bool wantUnmounted bool }{ @@ -69,14 +71,14 @@ func Test_UnmountFUSE(t *testing.T) { name: "FusermountBare", mountSIF: "", mountPath: path, - opts: []UnmountFUSEOpt{OptUnmountFUSEFusermountPath("fusermount")}, + opts: []UnmountOpt{OptUnmountFusermountPath("fusermount")}, wantErr: true, }, { name: "FusermountValid", mountSIF: filepath.Join(corpus, "one-group.sif"), mountPath: path, - opts: []UnmountFUSEOpt{OptUnmountFUSEFusermountPath(fusermountPath)}, + opts: []UnmountOpt{OptUnmountFusermountPath(fusermountPath)}, wantErr: false, wantUnmounted: true, }, @@ -84,13 +86,13 @@ func Test_UnmountFUSE(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.mountSIF != "" { - err := MountFUSE(context.Background(), tt.mountSIF, path) + err := Mount(context.Background(), tt.mountSIF, path) if err != nil { t.Fatal(err) } } - err := UnmountFUSE(context.Background(), tt.mountPath, tt.opts...) + err := Unmount(context.Background(), tt.mountPath, tt.opts...) if err != nil && !tt.wantErr { t.Errorf("Unexpected error: %s", err)