Skip to content

Commit

Permalink
Fix restic#1724 check the validity and permission of the mount point …
Browse files Browse the repository at this point in the history
…before trying to mount
  • Loading branch information
kitone committed Oct 17, 2020
1 parent aab4377 commit 103ee32
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions cmd/restic/cmd_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -13,7 +15,6 @@ import (
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"

resticfs "github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/fuse"

systemFuse "bazil.org/fuse"
Expand Down Expand Up @@ -103,11 +104,6 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error {
return err
}

if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) {
Verbosef("Mountpoint %s doesn't exist\n", mountpoint)
return err
}

mountOptions := []systemFuse.MountOption{
systemFuse.ReadOnly(),
systemFuse.FSName("restic"),
Expand Down Expand Up @@ -157,6 +153,32 @@ func umount(mountpoint string) error {
return systemFuse.Unmount(mountpoint)
}

func checkMountPointValidity(mountpoint string) error {
// check if the mountpoint exist and if it's a directory
info, err := os.Stat(mountpoint)
if err != nil {
if os.IsNotExist(err) {
return errors.Fatalf("the mount path doesn't exist: %q", mountpoint)
}
return errors.Fatalf("the mount path is not a directory or doesn't exist %q", mountpoint)
}
if !info.IsDir() {
return errors.Fatalf("the mount path is not a directory %q", mountpoint)
}
// create a temporary file to test permission whether it's writable directory.
tmpFile := filepath.Join(mountpoint, fmt.Sprintf("%d", time.Now().UnixNano()))
f, err := os.Create(tmpFile)
if err != nil {
// check with IsPermission(err) ?
return errors.Fatalf("the mount path is not writable directory: %q", mountpoint)
}
// fixme: check for error ?
f.Close()
os.Remove(tmpFile)

return nil
}

func runMount(opts MountOptions, gopts GlobalOptions, args []string) error {
if opts.SnapshotTemplate == "" {
return errors.Fatal("snapshot template string cannot be empty")
Expand All @@ -172,6 +194,12 @@ func runMount(opts MountOptions, gopts GlobalOptions, args []string) error {

mountpoint := args[0]

// avoid unnecessary work if the mount path doesn't exist or is not writable
err := checkMountPointValidity(mountpoint)
if err != nil {
return err
}

AddCleanupHandler(func() error {
debug.Log("running umount cleanup handler for mount at %v", mountpoint)
err := umount(mountpoint)
Expand Down

0 comments on commit 103ee32

Please sign in to comment.