diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index 820f68949..43cce17ca 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -81,7 +81,17 @@ func init() { mountFlags.StringVar(&mountOptions.SnapshotTemplate, "snapshot-template", time.RFC3339, "set `template` to use for snapshot dirs") } -func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { +func runMount(opts MountOptions, gopts GlobalOptions, args []string) error { + if opts.SnapshotTemplate == "" { + return errors.Fatal("snapshot template string cannot be empty") + } + if strings.ContainsAny(opts.SnapshotTemplate, `\/`) { + return errors.Fatal("snapshot template string contains a slash (/) or backslash (\\) character") + } + if len(args) == 0 { + return errors.Fatal("wrong number of parameters") + } + debug.Log("start mount") defer debug.Log("finish mount") @@ -103,6 +113,8 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { return err } + mountpoint := args[0] + if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) { Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint) err = resticfs.Mkdir(mountpoint, os.ModeDir|0700) @@ -110,7 +122,6 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { return err } } - mountOptions := []systemFuse.MountOption{ systemFuse.ReadOnly(), systemFuse.FSName("restic"), @@ -125,6 +136,15 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { } } + AddCleanupHandler(func() error { + debug.Log("running umount cleanup handler for mount at %v", mountpoint) + err := umount(mountpoint) + if err != nil { + Warnf("unable to umount (maybe already umounted or still in use?): %v\n", err) + } + return nil + }) + c, err := systemFuse.Mount(mountpoint, mountOptions...) if err != nil { return err @@ -159,30 +179,3 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error { func umount(mountpoint string) error { return systemFuse.Unmount(mountpoint) } - -func runMount(opts MountOptions, gopts GlobalOptions, args []string) error { - if opts.SnapshotTemplate == "" { - return errors.Fatal("snapshot template string cannot be empty") - } - - if strings.ContainsAny(opts.SnapshotTemplate, `\/`) { - return errors.Fatal("snapshot template string contains a slash (/) or backslash (\\) character") - } - - if len(args) == 0 { - return errors.Fatal("wrong number of parameters") - } - - mountpoint := args[0] - - AddCleanupHandler(func() error { - debug.Log("running umount cleanup handler for mount at %v", mountpoint) - err := umount(mountpoint) - if err != nil { - Warnf("unable to umount (maybe already umounted or still in use?): %v\n", err) - } - return nil - }) - - return mount(opts, gopts, mountpoint) -}