restic/src/cmds/restic/cmd_mount.go

109 lines
2.2 KiB
Go
Raw Normal View History

// +build !openbsd
// +build !windows
2015-04-07 19:10:53 +00:00
package main
import (
"os"
2016-09-15 19:17:20 +00:00
"restic/debug"
2016-09-01 20:17:37 +00:00
"restic/errors"
resticfs "restic/fs"
"restic/fuse"
2015-04-07 19:10:53 +00:00
2015-07-19 12:28:11 +00:00
systemFuse "bazil.org/fuse"
2015-04-07 19:10:53 +00:00
"bazil.org/fuse/fs"
)
type CmdMount struct {
2016-09-15 20:29:49 +00:00
Root bool `long:"owner-root" description:"use 'root' as the owner of files and dirs"`
2015-04-07 19:10:53 +00:00
global *GlobalOptions
}
func init() {
_, err := parser.AddCommand("mount",
"mount a repository",
"The mount command mounts a repository read-only to a given directory",
&CmdMount{
global: &globalOpts,
})
2015-04-07 19:10:53 +00:00
if err != nil {
panic(err)
}
}
func (cmd CmdMount) Usage() string {
return "MOUNTPOINT"
}
2016-09-15 19:17:20 +00:00
func (cmd CmdMount) Mount(mountpoint string) error {
debug.Log("mount", "start mount")
defer debug.Log("mount", "finish mount")
2015-04-07 19:10:53 +00:00
repo, err := cmd.global.OpenRepository()
if err != nil {
return err
}
err = repo.LoadIndex()
if err != nil {
return err
}
if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) {
2015-07-19 12:08:34 +00:00
cmd.global.Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint)
err = resticfs.Mkdir(mountpoint, os.ModeDir|0700)
2015-07-19 12:08:34 +00:00
if err != nil {
return err
2015-04-07 19:10:53 +00:00
}
}
2015-07-19 12:28:11 +00:00
c, err := systemFuse.Mount(
2015-04-07 19:10:53 +00:00
mountpoint,
2015-07-19 12:28:11 +00:00
systemFuse.ReadOnly(),
systemFuse.FSName("restic"),
2015-04-07 19:10:53 +00:00
)
if err != nil {
return err
}
root := fs.Tree{}
root.Add("snapshots", fuse.NewSnapshotsDir(repo, cmd.Root))
2015-04-07 19:10:53 +00:00
2016-09-15 19:17:20 +00:00
debug.Log("mount", "serving mount at %v", mountpoint)
err = fs.Serve(c, &root)
if err != nil {
return err
}
<-c.Ready
return c.MountError
}
func (cmd CmdMount) Umount(mountpoint string) error {
return systemFuse.Unmount(mountpoint)
}
func (cmd CmdMount) Execute(args []string) error {
if len(args) == 0 {
return errors.Fatalf("wrong number of parameters, Usage: %s", cmd.Usage())
}
mountpoint := args[0]
2015-04-07 19:10:53 +00:00
AddCleanupHandler(func() error {
2016-09-15 19:17:20 +00:00
debug.Log("mount", "running umount cleanup handler for mount at %v", mountpoint)
err := cmd.Umount(mountpoint)
if err != nil {
2016-09-15 17:59:07 +00:00
cmd.global.Warnf("unable to umount (maybe already umounted?): %v\n", err)
}
2016-09-15 17:59:07 +00:00
return nil
})
2016-09-15 19:17:20 +00:00
cmd.global.Printf("Now serving the repository at %s\n", mountpoint)
cmd.global.Printf("Don't forget to umount after quitting!\n")
2016-09-15 17:59:07 +00:00
2016-09-15 19:17:20 +00:00
return cmd.Mount(mountpoint)
2015-04-07 19:10:53 +00:00
}