mirror of
https://github.com/rclone/rclone.git
synced 2024-11-26 18:31:17 +08:00
8c10dee510
Current way of checking whether mountpoint has been already mounted (directory list) can result in race if rclone runs under Automount (classic or systemd). This patch adopts Linux ProcFS for the check. Note that mountpoint is considered empty if it's tagged as "mounted" by autofs. Also ProcFS is used to check whether rclone mount was successful (ie. tagged by a string containing "rclone"). On macOS/BSD where ProcFS is unavailable the old method is still used. This patch also moves a few utility functions unchanged to utils.go: CheckOverlap, CheckAllowings, SetVolumeName.
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
//go:build !linux
|
|
// +build !linux
|
|
|
|
package mountlib
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/rclone/rclone/fs"
|
|
)
|
|
|
|
// CheckMountEmpty checks if mountpoint folder is empty.
|
|
// On non-Linux unixes we list directory to ensure that.
|
|
func CheckMountEmpty(mountpoint string) error {
|
|
fp, err := os.Open(mountpoint)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "Can not open: %s", mountpoint)
|
|
}
|
|
defer fs.CheckClose(fp, &err)
|
|
|
|
_, err = fp.Readdirnames(1)
|
|
if err == io.EOF {
|
|
return nil
|
|
}
|
|
|
|
const msg = "Directory is not empty, use --allow-non-empty to mount anyway: %s"
|
|
if err == nil {
|
|
return errors.Errorf(msg, mountpoint)
|
|
}
|
|
return errors.Wrapf(err, msg, mountpoint)
|
|
}
|
|
|
|
// CheckMountReady should check if mountpoint is mounted by rclone.
|
|
// The check is implemented only for Linux so this does nothing.
|
|
func CheckMountReady(mountpoint string) error {
|
|
return nil
|
|
}
|
|
|
|
// WaitMountReady should wait until mountpoint is mounted by rclone.
|
|
// The check is implemented only for Linux so we just sleep a little.
|
|
func WaitMountReady(mountpoint string, timeout time.Duration) error {
|
|
time.Sleep(timeout)
|
|
return nil
|
|
}
|