2021-01-03 00:05:52 +00:00
|
|
|
package mountlib
|
|
|
|
|
|
|
|
import (
|
2021-11-04 10:12:57 +00:00
|
|
|
"fmt"
|
2023-01-19 15:54:10 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2021-07-24 13:27:48 +00:00
|
|
|
"path/filepath"
|
2021-01-03 00:05:52 +00:00
|
|
|
"runtime"
|
2021-07-24 13:27:48 +00:00
|
|
|
"strings"
|
2021-01-03 00:05:52 +00:00
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ClipBlocks clips the blocks pointed to the OS max
|
|
|
|
func ClipBlocks(b *uint64) {
|
|
|
|
var max uint64
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
if runtime.GOARCH == "386" {
|
|
|
|
max = (1 << 32) - 1
|
|
|
|
} else {
|
|
|
|
max = (1 << 43) - 1
|
|
|
|
}
|
|
|
|
case "darwin":
|
|
|
|
// OSX FUSE only supports 32 bit number of blocks
|
|
|
|
// https://github.com/osxfuse/osxfuse/issues/396
|
|
|
|
max = (1 << 32) - 1
|
|
|
|
default:
|
|
|
|
// no clipping
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if *b > max {
|
|
|
|
*b = max
|
|
|
|
}
|
|
|
|
}
|
2021-07-24 13:27:48 +00:00
|
|
|
|
2022-06-11 14:18:48 +00:00
|
|
|
// CheckOverlap checks that root doesn't overlap with a mountpoint
|
|
|
|
func CheckOverlap(f fs.Fs, mountpoint string) error {
|
|
|
|
name := f.Name()
|
2021-07-24 13:27:48 +00:00
|
|
|
if name != "" && name != "local" {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-11 14:18:48 +00:00
|
|
|
rootAbs := absPath(f.Root())
|
|
|
|
mountpointAbs := absPath(mountpoint)
|
2021-07-24 13:27:48 +00:00
|
|
|
if strings.HasPrefix(rootAbs, mountpointAbs) || strings.HasPrefix(mountpointAbs, rootAbs) {
|
2022-06-11 14:18:48 +00:00
|
|
|
const msg = "mount point %q (%q) and directory to be mounted %q (%q) mustn't overlap"
|
|
|
|
return fmt.Errorf(msg, mountpoint, mountpointAbs, f.Root(), rootAbs)
|
2021-07-24 13:27:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-11 14:18:48 +00:00
|
|
|
// absPath is a helper function for CheckOverlap
|
2021-07-24 13:27:48 +00:00
|
|
|
func absPath(path string) string {
|
|
|
|
if abs, err := filepath.EvalSymlinks(path); err == nil {
|
|
|
|
path = abs
|
|
|
|
}
|
|
|
|
if abs, err := filepath.Abs(path); err == nil {
|
|
|
|
path = abs
|
|
|
|
}
|
|
|
|
path = filepath.ToSlash(path)
|
2022-06-11 14:18:48 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Removes any UNC long path prefix to make sure a simple HasPrefix test
|
|
|
|
// in CheckOverlap works when one is UNC (root) and one is not (mountpoint).
|
|
|
|
path = strings.TrimPrefix(path, `//?/`)
|
|
|
|
}
|
2021-07-24 13:27:48 +00:00
|
|
|
if !strings.HasSuffix(path, "/") {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2022-06-11 14:18:48 +00:00
|
|
|
// CheckAllowNonEmpty checks --allow-non-empty flag, and if not used verifies that mountpoint is empty.
|
|
|
|
func CheckAllowNonEmpty(mountpoint string, opt *Options) error {
|
2021-07-24 13:27:48 +00:00
|
|
|
if !opt.AllowNonEmpty {
|
2022-06-11 14:18:48 +00:00
|
|
|
return CheckMountEmpty(mountpoint)
|
2021-07-24 13:27:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:54:10 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:27:48 +00:00
|
|
|
// SetVolumeName with sensible default
|
|
|
|
func (m *MountPoint) SetVolumeName(vol string) {
|
|
|
|
if vol == "" {
|
2022-02-09 11:56:43 +00:00
|
|
|
vol = fs.ConfigString(m.Fs)
|
2021-07-24 13:27:48 +00:00
|
|
|
}
|
|
|
|
m.MountOpt.SetVolumeName(vol)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVolumeName removes special characters from volume name if necessary
|
|
|
|
func (o *Options) SetVolumeName(vol string) {
|
|
|
|
vol = strings.ReplaceAll(vol, ":", " ")
|
|
|
|
vol = strings.ReplaceAll(vol, "/", " ")
|
|
|
|
vol = strings.TrimSpace(vol)
|
|
|
|
if runtime.GOOS == "windows" && len(vol) > 32 {
|
|
|
|
vol = vol[:32]
|
|
|
|
}
|
|
|
|
o.VolumeName = vol
|
|
|
|
}
|
2022-02-09 11:56:43 +00:00
|
|
|
|
|
|
|
// SetDeviceName with sensible default
|
|
|
|
func (m *MountPoint) SetDeviceName(dev string) {
|
|
|
|
if dev == "" {
|
|
|
|
dev = fs.ConfigString(m.Fs)
|
|
|
|
}
|
|
|
|
m.MountOpt.DeviceName = dev
|
|
|
|
}
|