// Package state bewaart wat BlueGit nodig heeft vóór de datadisk gekozen is: // de database, de sleutel en het gekozen opslagpad. De standaardmap is // /var/lib/bluegit, de StateDirectory van systemd. package state import ( "errors" "fmt" "os" "path/filepath" "strings" "syscall" "gitlab.com/Timfa/bluegit/internal/i18n" ) // DefaultDir is de state-map als state_path leeg is. const DefaultDir = "/var/lib/bluegit" // File is de naam van het bestand met het gekozen opslagpad. const File = "storage_path" // MinFreeBytes is de kleinste vrije ruimte die een datadisk mag hebben. const MinFreeBytes = 1 << 30 // Dir kiest de state-map. Een gezet pad wint van de standaard. func Dir(configured string) string { if strings.TrimSpace(configured) != "" { return strings.TrimSpace(configured) } return DefaultDir } // Read leest het opgeslagen pad. Een ontbrekend bestand is geen fout // en geeft een lege string. func Read(dir string) (string, error) { b, err := os.ReadFile(filepath.Join(dir, File)) if errors.Is(err, os.ErrNotExist) { return "", nil } if err != nil { return "", err } return strings.TrimSpace(string(b)), nil } // Write zet het pad atomisch neer. func Write(dir, path string) error { if err := os.MkdirAll(dir, 0o750); err != nil { return err } target := filepath.Join(dir, File) tmp := target + ".tmp" if err := os.WriteFile(tmp, []byte(path+"\n"), 0o640); err != nil { return err } return os.Rename(tmp, target) } // Info is het resultaat van een schijfcontrole. type Info struct { Path string Free uint64 Total uint64 } // Inspect eist een absoluut pad dat bestaat, een map is, beschrijfbaar is // en minstens MinFreeBytes vrij heeft. Bij een te volle of niet beschrijfbare // map zitten Free en Total alsnog in het resultaat. func Inspect(path string) (Info, error) { path = strings.TrimSpace(path) if path == "" { return Info{}, i18n.E("err.pathMissing") } if strings.ContainsAny(path, "\r\n") { return Info{}, i18n.E("err.pathNewline") } if !filepath.IsAbs(path) { return Info{}, i18n.E("err.pathAbsolute") } path = filepath.Clean(path) info := Info{Path: path} st, err := os.Stat(path) if err != nil { if errors.Is(err, os.ErrNotExist) { return info, i18n.E("err.pathAbsent") } return info, i18n.E("err.pathRead", err.Error()) } if !st.IsDir() { return info, i18n.E("err.pathDir") } var fs syscall.Statfs_t if err := syscall.Statfs(path, &fs); err != nil { return info, i18n.E("err.pathFree", err.Error()) } info.Free = uint64(fs.Bavail) * uint64(fs.Bsize) info.Total = uint64(fs.Blocks) * uint64(fs.Bsize) probe := filepath.Join(path, ".bluegit-write-test") f, err := os.OpenFile(probe, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) if err != nil { return info, i18n.E("err.pathWrite") } _ = f.Close() _ = os.Remove(probe) if info.Free < MinFreeBytes { return info, i18n.E("err.pathSpace", Format(info.Free), Format(MinFreeBytes)) } return info, nil } // Format maakt een korte grootte, bijvoorbeeld "1.5 GiB". func Format(n uint64) string { const unit = 1024 if n < unit { return fmt.Sprintf("%d B", n) } div := uint64(unit) exp := 0 for n/div >= unit && exp < 4 { div *= unit exp++ } value := float64(n) / float64(div) return fmt.Sprintf("%.1f %ciB", value, "KMGT"[exp]) }