From f7587be28fe03606e01cfe47237099243bc5bce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gross?= Date: Tue, 12 Dec 2023 21:57:59 +0100 Subject: [PATCH] mount: detect mountpoint does not exist before opening the repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug #1681 suggests that restic should not be nice to user and should refrain from creating a mountpoint if it does not exist. Nevertheless, it currently opens the repository before checking for the mountpoint's existence. In the case of large or remote repositories, this process can be time-consuming, delaying the inevitable outcome. /restic mount --repo=REMOTE --verbose /tmp/backup repository 33f14e42 opened (version 2, compression level max) [0:38] 100.00% 162 / 162 index files loaded Mountpoint /tmp/backup doesn't exist stat /tmp/backup: no such file or directory real 0m39.534s user 1m53.961s sys 0m3.044s In this scenario, 40 seconds could have been saved if the nonexistence of the path had been verified beforehand. This patch relocates the mountpoint check to the beginning of the runMount function, preceding the opening of the repository. /restic mount --repo=REMOTE --verbose /tmp/backup Mountpoint /tmp/backup doesn't exist stat /tmp/backup: no such file or directory real 0m0.136s user 0m0.018s sys 0m0.027s Signed-off-by: Sébastien Gross --- changelog/unreleased/pull-4590 | 7 +++++++ cmd/restic/cmd_mount.go | 15 +++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 changelog/unreleased/pull-4590 diff --git a/changelog/unreleased/pull-4590 b/changelog/unreleased/pull-4590 new file mode 100644 index 000000000..353d21616 --- /dev/null +++ b/changelog/unreleased/pull-4590 @@ -0,0 +1,7 @@ +Enhancement: `mount` tests mountpoint existence before opening the repository + +The restic `mount` command now checks for the existence of the +mountpoint before opening the repository, leading to quicker error +detection. + +https://github.com/restic/restic/pull/4590 diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index 04c072daf..5fd81b344 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -113,6 +113,15 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args return errors.Fatal("wrong number of parameters") } + mountpoint := args[0] + + // Check the existence of the mount point at the earliest stage to + // prevent unnecessary computations while opening the repository. + if _, err := resticfs.Stat(mountpoint); errors.Is(err, os.ErrNotExist) { + Verbosef("Mountpoint %s doesn't exist\n", mountpoint) + return err + } + debug.Log("start mount") defer debug.Log("finish mount") @@ -136,12 +145,6 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args return err } - mountpoint := args[0] - - if _, err := resticfs.Stat(mountpoint); errors.Is(err, os.ErrNotExist) { - Verbosef("Mountpoint %s doesn't exist\n", mountpoint) - return err - } mountOptions := []systemFuse.MountOption{ systemFuse.ReadOnly(), systemFuse.FSName("restic"),