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 11, 2020
1 parent cae88d1 commit e66248b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 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 Down Expand Up @@ -157,6 +159,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 +200,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 e66248b

Please sign in to comment.