Tighten disk safety checks

This commit is contained in:
2026-04-24 07:18:17 +03:00
parent b8eabee393
commit 75c6b928ae
5 changed files with 92 additions and 8 deletions

View File

@@ -26,7 +26,7 @@ type DiskInfo struct {
MountPath string `json:"mount_path"`
}
const markerDir = ".jukebox"
const MarkerDir = ".jukebox"
const idFile = "disk.id"
func Probe(mountPath string) (DiskInfo, error) {
@@ -43,7 +43,7 @@ func Probe(mountPath string) (DiskInfo, error) {
info.TotalBytes = total
info.FreeBytes = free
idPath := filepath.Join(mountPath, markerDir, idFile)
idPath := filepath.Join(mountPath, MarkerDir, idFile)
data, err := os.ReadFile(idPath)
if errors.Is(err, os.ErrNotExist) {
info.State = DiskForeign
@@ -59,8 +59,45 @@ func Probe(mountPath string) (DiskInfo, error) {
return info, nil
}
func IsMountPoint(path string) bool {
pathInfo, err := os.Stat(path)
if err != nil {
return false
}
parent := filepath.Dir(filepath.Clean(path))
parentInfo, err := os.Stat(parent)
if err != nil {
return false
}
pathStat, ok := pathInfo.Sys().(*syscall.Stat_t)
if !ok {
return false
}
parentStat, ok := parentInfo.Sys().(*syscall.Stat_t)
if !ok {
return false
}
return pathStat.Dev != parentStat.Dev
}
func CheckWritable(path string) error {
f, err := os.CreateTemp(path, ".jukebox-writecheck-*")
if err != nil {
return err
}
name := f.Name()
if err := f.Close(); err != nil {
_ = os.Remove(name)
return err
}
return os.Remove(name)
}
func InitDisk(mountPath string) (string, error) {
dir := filepath.Join(mountPath, markerDir)
dir := filepath.Join(mountPath, MarkerDir)
if err := os.MkdirAll(dir, 0o755); err != nil {
return "", err
}
@@ -73,7 +110,7 @@ func InitDisk(mountPath string) (string, error) {
}
func DBPath(mountPath string) string {
return filepath.Join(mountPath, markerDir, "history.db")
return filepath.Join(mountPath, MarkerDir, "history.db")
}
func DiskUsage(mountPath string) (total, free int64, err error) {