Fix isMounted to resolve symlinks and normalize paths
The target path may contain symlinks (e.g. /media/Storage/ -> /mnt/merged/raid/Storage/) but /proc/mounts records the resolved real path. Use filepath.EvalSymlinks and filepath.Clean so the comparison matches regardless of symlinks or trailing slashes.
This commit is contained in:
parent
d5a3653915
commit
ac0ea56f98
10
main.go
10
main.go
@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -34,7 +35,14 @@ type App struct {
|
|||||||
var app *App
|
var app *App
|
||||||
|
|
||||||
// isMounted checks /proc/mounts for a target mountpoint to determine if it is mounted.
|
// isMounted checks /proc/mounts for a target mountpoint to determine if it is mounted.
|
||||||
|
// Symlinks in the target path are resolved so the comparison matches the real
|
||||||
|
// paths recorded by the kernel.
|
||||||
func isMounted(target string) bool {
|
func isMounted(target string) bool {
|
||||||
|
cleanTarget := filepath.Clean(target)
|
||||||
|
if resolved, err := filepath.EvalSymlinks(cleanTarget); err == nil {
|
||||||
|
cleanTarget = resolved
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Open("/proc/mounts")
|
file, err := os.Open("/proc/mounts")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -47,7 +55,7 @@ func isMounted(target string) bool {
|
|||||||
if len(args) < 3 {
|
if len(args) < 3 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if args[1] == target {
|
if filepath.Clean(args[1]) == cleanTarget {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user