Commits

Paul Ruane committed 583ba35

Issue #13: Ugly error if attempt made to mount to same directory twice.

  • Participants
  • Parent commits 3c9a581

Comments (0)

Files changed (1)

File src/tmsu/cli/mount.go

 	"fmt"
 	"os"
 	"os/exec"
+	"path/filepath"
 	"syscall"
 	"time"
 	"tmsu/log"
 	case 0:
 		err := listMounts()
 		if err != nil {
-			return fmt.Errorf("could not list mounts: %v", err)
+			return err
 		}
 	case 1:
 		mountPath := args[0]
 
 		err := mountDefault(mountPath, allowOther)
 		if err != nil {
-			return fmt.Errorf("could not mount database at '%v': %v", mountPath, err)
+			return err
 		}
 	case 2:
 		databasePath := args[0]
 
 		err := mountExplicit(databasePath, mountPath, allowOther)
 		if err != nil {
-			return fmt.Errorf("could not mount database '%v' at '%v': %v", databasePath, mountPath, err)
+			return err
 		}
 	default:
 		return fmt.Errorf("Too many arguments.")
 }
 
 func mountExplicit(databasePath string, mountPath string, allowOther bool) error {
+	if alreadyMounted(mountPath) {
+		return fmt.Errorf("%v: mount path already in use", mountPath)
+	}
+
 	stat, err := os.Stat(mountPath)
 	if err != nil {
 		return fmt.Errorf("%v: could not stat: %v", mountPath, err)
 
 	return nil
 }
+
+func alreadyMounted(path string) bool {
+	absPath, err := filepath.Abs(path)
+	if err != nil {
+		return false
+	}
+
+	mt, err := vfs.GetMountTable()
+	if err != nil {
+		return false
+	}
+
+	for _, mount := range mt {
+		if mount.MountPath == absPath {
+			return true
+		}
+	}
+
+	return false
+}