mirror of
https://github.com/rclone/rclone.git
synced 2025-01-20 03:02:46 +08:00
mount: fix check for empty mount point on Linux #3562
This commit is contained in:
parent
37db2abecd
commit
267a09001d
|
@ -33,12 +33,20 @@ func CheckMountEmpty(mountpoint string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot read %s: %w", mtabPath, err)
|
return fmt.Errorf("cannot read %s: %w", mtabPath, err)
|
||||||
}
|
}
|
||||||
|
foundAutofs := false
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if entry.Dir == mountpointAbs && entry.Type != "autofs" {
|
if entry.Dir == mountpointAbs {
|
||||||
return fmt.Errorf(msg, mountpointAbs)
|
if entry.Type != "autofs" {
|
||||||
|
return fmt.Errorf(msg, mountpointAbs)
|
||||||
|
}
|
||||||
|
foundAutofs = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
// It isn't safe to list an autofs in the middle of mounting
|
||||||
|
if foundAutofs {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return checkMountEmpty(mountpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckMountReady checks whether mountpoint is mounted by rclone.
|
// CheckMountReady checks whether mountpoint is mounted by rclone.
|
||||||
|
|
|
@ -4,33 +4,13 @@
|
||||||
package mountlib
|
package mountlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rclone/rclone/fs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckMountEmpty checks if mountpoint folder is empty.
|
// CheckMountEmpty checks if mountpoint folder is empty.
|
||||||
// On non-Linux unixes we list directory to ensure that.
|
// On non-Linux unixes we list directory to ensure that.
|
||||||
func CheckMountEmpty(mountpoint string) error {
|
func CheckMountEmpty(mountpoint string) error {
|
||||||
fp, err := os.Open(mountpoint)
|
return checkMountEmpty(mountpoint)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("cannot open: %s: %w", mountpoint, err)
|
|
||||||
}
|
|
||||||
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 fmt.Errorf(msg, mountpoint)
|
|
||||||
}
|
|
||||||
return fmt.Errorf(msg+": %w", mountpoint, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckMountReady should check if mountpoint is mounted by rclone.
|
// CheckMountReady should check if mountpoint is mounted by rclone.
|
||||||
|
|
|
@ -2,6 +2,8 @@ package mountlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -84,6 +86,26 @@ func (m *MountPoint) CheckAllowed() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkMountEmpty checks if mountpoint folder is empty by listing it.
|
||||||
|
func checkMountEmpty(mountpoint string) error {
|
||||||
|
fp, err := os.Open(mountpoint)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot open: %s: %w", mountpoint, err)
|
||||||
|
}
|
||||||
|
defer fs.CheckClose(fp, &err)
|
||||||
|
|
||||||
|
_, err = fp.Readdirnames(1)
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const msg = "%q is not empty, use --allow-non-empty to mount anyway"
|
||||||
|
if err == nil {
|
||||||
|
return fmt.Errorf(msg, mountpoint)
|
||||||
|
}
|
||||||
|
return fmt.Errorf(msg+": %w", mountpoint, err)
|
||||||
|
}
|
||||||
|
|
||||||
// SetVolumeName with sensible default
|
// SetVolumeName with sensible default
|
||||||
func (m *MountPoint) SetVolumeName(vol string) {
|
func (m *MountPoint) SetVolumeName(vol string) {
|
||||||
if vol == "" {
|
if vol == "" {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user