Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

mount: umount dir when program ends with SIGINT (Ctrl+C) or SIGTERM

Jérôme Vizcaino788b6ce821fc
diff --git a/cmd/mount/mount.go b/cmd/mount/mount.go
index a8496f7a8..40788687a 100644
--- a/cmd/mount/mount.go
+++ b/cmd/mount/mount.go
@@ -7,6 +7,8 @@ package mount
import (
"log"
"os"
+ "os/signal"
+ "syscall"
"time"
"bazil.org/fuse"
@@ -73,20 +75,19 @@ This is **EXPERIMENTAL** - use with care.
First set up your remote using ` + "`rclone config`" + `. Check it works with ` + "`rclone ls`" + ` etc.
-Start the mount like this (note the & on the end to put rclone in the background).
+Start the mount like this
- rclone mount remote:path/to/files /path/to/local/mount &
+ rclone mount remote:path/to/files /path/to/local/mount
-Stop the mount with
+When the program ends, either via Ctrl+C or receiving a SIGINT or SIGTERM signal,
+the mount is automatically stopped.
- fusermount -u /path/to/local/mount
-
-Or if that fails try
-
- fusermount -z -u /path/to/local/mount
-
-Or with OS X
+The umount operation can fail, for example when the mountpoint is busy.
+When that happens, it is the user's responsibility to stop the mount manually with
+ # Linux
+ fusermount -u /path/to/local/mount
+ # OS X
umount /path/to/local/mount
### Limitations ###
@@ -168,8 +169,18 @@ func Mount(f fs.Fs, mountpoint string) error {
return errors.Wrap(err, "failed to mount FUSE fs")
}
- // Wait for umount
- err = <-errChan
+ sigChan := make(chan os.Signal, 1)
+ signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
+
+ select {
+ // umount triggered outside the app
+ case err = <-errChan:
+ break
+ // Program abort: umount
+ case <-sigChan:
+ err = fuse.Unmount(mountpoint)
+ }
+
if err != nil {
return errors.Wrap(err, "failed to umount FUSE fs")
}