mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 15:20:38 +08:00
fix: mount parsing for linux
This commit is contained in:
parent
08240c8cf5
commit
bb0cd76a5f
|
@ -4,22 +4,20 @@
|
||||||
package mountlib
|
package mountlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/artyom/mtab"
|
"github.com/moby/sys/mountinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
mtabPath = "/proc/mounts"
|
|
||||||
pollInterval = 100 * time.Millisecond
|
pollInterval = 100 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckMountEmpty checks if folder is not already a mountpoint.
|
// CheckMountEmpty checks if folder is not already a mountpoint.
|
||||||
// On Linux we use the OS-specific /proc/mount API so the check won't access the path.
|
// On Linux we use the OS-specific /proc/self/mountinfo API so the check won't access the path.
|
||||||
// Directories marked as "mounted" by autofs are considered not mounted.
|
// Directories marked as "mounted" by autofs are considered not mounted.
|
||||||
func CheckMountEmpty(mountpoint string) error {
|
func CheckMountEmpty(mountpoint string) error {
|
||||||
const msg = "directory already mounted, use --allow-non-empty to mount anyway: %s"
|
const msg = "directory already mounted, use --allow-non-empty to mount anyway: %s"
|
||||||
|
@ -29,43 +27,48 @@ func CheckMountEmpty(mountpoint string) error {
|
||||||
return fmt.Errorf("cannot get absolute path: %s: %w", mountpoint, err)
|
return fmt.Errorf("cannot get absolute path: %s: %w", mountpoint, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, err := mtab.Entries(mtabPath)
|
infos, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(mountpointAbs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot read %s: %w", mtabPath, err)
|
return fmt.Errorf("cannot get mounts: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
foundAutofs := false
|
foundAutofs := false
|
||||||
for _, entry := range entries {
|
for _, info := range infos {
|
||||||
if entry.Dir == mountpointAbs {
|
if info.FSType != "autofs" {
|
||||||
if entry.Type != "autofs" {
|
return fmt.Errorf(msg, mountpointAbs)
|
||||||
return fmt.Errorf(msg, mountpointAbs)
|
|
||||||
}
|
|
||||||
foundAutofs = true
|
|
||||||
}
|
}
|
||||||
|
foundAutofs = true
|
||||||
}
|
}
|
||||||
// It isn't safe to list an autofs in the middle of mounting
|
// It isn't safe to list an autofs in the middle of mounting
|
||||||
if foundAutofs {
|
if foundAutofs {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkMountEmpty(mountpoint)
|
return checkMountEmpty(mountpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckMountReady checks whether mountpoint is mounted by rclone.
|
// CheckMountReady checks whether mountpoint is mounted by rclone.
|
||||||
// Only mounts with type "rclone" or "fuse.rclone" count.
|
// Only mounts with type "rclone" or "fuse.rclone" count.
|
||||||
func CheckMountReady(mountpoint string) error {
|
func CheckMountReady(mountpoint string) error {
|
||||||
|
const msg = "mount not ready: %s"
|
||||||
|
|
||||||
mountpointAbs, err := filepath.Abs(mountpoint)
|
mountpointAbs, err := filepath.Abs(mountpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot get absolute path: %s: %w", mountpoint, err)
|
return fmt.Errorf("cannot get absolute path: %s: %w", mountpoint, err)
|
||||||
}
|
}
|
||||||
entries, err := mtab.Entries(mtabPath)
|
|
||||||
|
infos, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(mountpointAbs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot read %s: %w", mtabPath, err)
|
return fmt.Errorf("cannot get mounts: %w", err)
|
||||||
}
|
}
|
||||||
for _, entry := range entries {
|
|
||||||
if entry.Dir == mountpointAbs && strings.Contains(entry.Type, "rclone") {
|
for _, info := range infos {
|
||||||
|
if strings.Contains(info.FSType, "rclone") {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors.New("mount not ready")
|
|
||||||
|
return fmt.Errorf(msg, mountpointAbs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitMountReady waits until mountpoint is mounted by rclone.
|
// WaitMountReady waits until mountpoint is mounted by rclone.
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -15,7 +15,6 @@ require (
|
||||||
github.com/abbot/go-http-auth v0.4.0
|
github.com/abbot/go-http-auth v0.4.0
|
||||||
github.com/anacrolix/dms v1.5.0
|
github.com/anacrolix/dms v1.5.0
|
||||||
github.com/anacrolix/log v0.13.1
|
github.com/anacrolix/log v0.13.1
|
||||||
github.com/artyom/mtab v1.0.0
|
|
||||||
github.com/atotto/clipboard v0.1.4
|
github.com/atotto/clipboard v0.1.4
|
||||||
github.com/aws/aws-sdk-go v1.44.246
|
github.com/aws/aws-sdk-go v1.44.246
|
||||||
github.com/buengese/sgzip v0.1.1
|
github.com/buengese/sgzip v0.1.1
|
||||||
|
@ -41,6 +40,7 @@ require (
|
||||||
github.com/mattn/go-colorable v0.1.13
|
github.com/mattn/go-colorable v0.1.13
|
||||||
github.com/mattn/go-runewidth v0.0.14
|
github.com/mattn/go-runewidth v0.0.14
|
||||||
github.com/mitchellh/go-homedir v1.1.0
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
|
github.com/moby/sys/mountinfo v0.6.2
|
||||||
github.com/ncw/go-acd v0.0.0-20201019170801-fe55f33415b1
|
github.com/ncw/go-acd v0.0.0-20201019170801-fe55f33415b1
|
||||||
github.com/ncw/swift/v2 v2.0.1
|
github.com/ncw/swift/v2 v2.0.1
|
||||||
github.com/oracle/oci-go-sdk/v65 v65.34.0
|
github.com/oracle/oci-go-sdk/v65 v65.34.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -100,8 +100,6 @@ github.com/anacrolix/tagflag v0.0.0-20180109131632-2146c8d41bf0/go.mod h1:1m2U/K
|
||||||
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
||||||
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
||||||
github.com/artyom/mtab v1.0.0 h1:r7OSVo5Jeqi8+LotZ0rT2kzfPIBp9KCpEJP8RQqGmSE=
|
|
||||||
github.com/artyom/mtab v1.0.0/go.mod h1:EHpkp5OmPfS1yZX+/DFTztlJ9di5UzdDLX1/XzWPXw8=
|
|
||||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
github.com/aws/aws-sdk-go v1.44.246 h1:iLxPX6JU0bxAci9R6/bp8rX0kL871ByCTx0MZlQWv1U=
|
github.com/aws/aws-sdk-go v1.44.246 h1:iLxPX6JU0bxAci9R6/bp8rX0kL871ByCTx0MZlQWv1U=
|
||||||
|
|
Loading…
Reference in New Issue
Block a user