Merge pull request #3035 from greatroar/no-unmount-on-failure

restic mount: don't umount unless we actually tried to mount
This commit is contained in:
MichaelEischer 2020-10-21 22:10:42 +02:00 committed by GitHub
commit 0590e3e12d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -81,7 +81,17 @@ func init() {
mountFlags.StringVar(&mountOptions.SnapshotTemplate, "snapshot-template", time.RFC3339, "set `template` to use for snapshot dirs") 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") debug.Log("start mount")
defer debug.Log("finish mount") defer debug.Log("finish mount")
@ -103,6 +113,8 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error {
return err return err
} }
mountpoint := args[0]
if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) { if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) {
Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint) Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint)
err = resticfs.Mkdir(mountpoint, os.ModeDir|0700) err = resticfs.Mkdir(mountpoint, os.ModeDir|0700)
@ -110,7 +122,6 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error {
return err return err
} }
} }
mountOptions := []systemFuse.MountOption{ mountOptions := []systemFuse.MountOption{
systemFuse.ReadOnly(), systemFuse.ReadOnly(),
systemFuse.FSName("restic"), 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...) c, err := systemFuse.Mount(mountpoint, mountOptions...)
if err != nil { if err != nil {
return err return err
@ -159,30 +179,3 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error {
func umount(mountpoint string) error { func umount(mountpoint string) error {
return systemFuse.Unmount(mountpoint) 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)
}