1
mirror of https://github.com/rclone/rclone synced 2024-12-02 11:53:45 +01:00

mountlib: check if directory is not empty before mounting - fixes #1386

Signed-off-by: Ernest Borowski <er.borowski@gmail.com>
This commit is contained in:
Ernest Borowski 2017-11-09 01:37:27 +01:00 committed by Nick Craig-Wood
parent 413faa99cf
commit 9fbff7bcab

View File

@ -1,11 +1,14 @@
package mountlib package mountlib
import ( import (
"io"
"log" "log"
"os"
"github.com/ncw/rclone/cmd" "github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/vfs/vfsflags" "github.com/ncw/rclone/vfs/vfsflags"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -22,6 +25,31 @@ var (
ExtraFlags *[]string ExtraFlags *[]string
) )
// Check is folder is empty
func checkMountEmpty(mountpoint string) error {
fp, fpErr := os.Open(mountpoint)
if fpErr != nil {
return errors.Wrap(fpErr, "Can not open: "+mountpoint)
}
defer fs.CheckClose(fp, &fpErr)
_, fpErr = fp.Readdirnames(1)
// directory is not empty
if fpErr != io.EOF {
var e error
var errorMsg = "Directory is not empty: " + mountpoint + " If you want to mount it anyway use: --allow-non-empty option"
if fpErr == nil {
e = errors.New(errorMsg)
} else {
e = errors.Wrap(fpErr, errorMsg)
}
return e
}
return nil
}
// NewMountCommand makes a mount command with the given name and Mount function // NewMountCommand makes a mount command with the given name and Mount function
func NewMountCommand(commandName string, Mount func(f fs.Fs, mountpoint string) error) *cobra.Command { func NewMountCommand(commandName string, Mount func(f fs.Fs, mountpoint string) error) *cobra.Command {
var commandDefintion = &cobra.Command{ var commandDefintion = &cobra.Command{
@ -138,6 +166,13 @@ like this:
defer close(stopStats) defer close(stopStats)
} }
if !AllowNonEmpty {
err := checkMountEmpty(args[1])
if err != nil {
log.Fatalf("Fatal error: %v", err)
}
}
err := Mount(fdst, args[1]) err := Mount(fdst, args[1])
if err != nil { if err != nil {
log.Fatalf("Fatal error: %v", err) log.Fatalf("Fatal error: %v", err)