BlueGit

timfa bluegit

Files

bluegit / internal / state / state.go

state.go

feat: nieuw-formulier, groepsoverzicht, logo's en talen
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed d1203d3b History
state.go 3.2 KiB Download Raw
1// Package state bewaart wat BlueGit nodig heeft vóór de datadisk gekozen is:
2// de database, de sleutel en het gekozen opslagpad. De standaardmap is
3// /var/lib/bluegit, de StateDirectory van systemd.
4package state
5
6import (
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12 "syscall"
13
14 "gitlab.com/Timfa/bluegit/internal/i18n"
15)
16
17// DefaultDir is de state-map als state_path leeg is.
18const DefaultDir = "/var/lib/bluegit"
19
20// File is de naam van het bestand met het gekozen opslagpad.
21const File = "storage_path"
22
23// MinFreeBytes is de kleinste vrije ruimte die een datadisk mag hebben.
24const MinFreeBytes = 1 << 30
25
26// Dir kiest de state-map. Een gezet pad wint van de standaard.
27func Dir(configured string) string {
28 if strings.TrimSpace(configured) != "" {
29 return strings.TrimSpace(configured)
30 }
31 return DefaultDir
32}
33
34// Read leest het opgeslagen pad. Een ontbrekend bestand is geen fout
35// en geeft een lege string.
36func Read(dir string) (string, error) {
37 b, err := os.ReadFile(filepath.Join(dir, File))
38 if errors.Is(err, os.ErrNotExist) {
39 return "", nil
40 }
41 if err != nil {
42 return "", err
43 }
44 return strings.TrimSpace(string(b)), nil
45}
46
47// Write zet het pad atomisch neer.
48func Write(dir, path string) error {
49 if err := os.MkdirAll(dir, 0o750); err != nil {
50 return err
51 }
52 target := filepath.Join(dir, File)
53 tmp := target + ".tmp"
54 if err := os.WriteFile(tmp, []byte(path+"\n"), 0o640); err != nil {
55 return err
56 }
57 return os.Rename(tmp, target)
58}
59
60// Info is het resultaat van een schijfcontrole.
61type Info struct {
62 Path string
63 Free uint64
64 Total uint64
65}
66
67// Inspect eist een absoluut pad dat bestaat, een map is, beschrijfbaar is
68// en minstens MinFreeBytes vrij heeft. Bij een te volle of niet beschrijfbare
69// map zitten Free en Total alsnog in het resultaat.
70func Inspect(path string) (Info, error) {
71 path = strings.TrimSpace(path)
72 if path == "" {
73 return Info{}, i18n.E("err.pathMissing")
74 }
75 if strings.ContainsAny(path, "\r\n") {
76 return Info{}, i18n.E("err.pathNewline")
77 }
78 if !filepath.IsAbs(path) {
79 return Info{}, i18n.E("err.pathAbsolute")
80 }
81 path = filepath.Clean(path)
82 info := Info{Path: path}
83 st, err := os.Stat(path)
84 if err != nil {
85 if errors.Is(err, os.ErrNotExist) {
86 return info, i18n.E("err.pathAbsent")
87 }
88 return info, i18n.E("err.pathRead", err.Error())
89 }
90 if !st.IsDir() {
91 return info, i18n.E("err.pathDir")
92 }
93 var fs syscall.Statfs_t
94 if err := syscall.Statfs(path, &fs); err != nil {
95 return info, i18n.E("err.pathFree", err.Error())
96 }
97 info.Free = uint64(fs.Bavail) * uint64(fs.Bsize)
98 info.Total = uint64(fs.Blocks) * uint64(fs.Bsize)
99 probe := filepath.Join(path, ".bluegit-write-test")
100 f, err := os.OpenFile(probe, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
101 if err != nil {
102 return info, i18n.E("err.pathWrite")
103 }
104 _ = f.Close()
105 _ = os.Remove(probe)
106 if info.Free < MinFreeBytes {
107 return info, i18n.E("err.pathSpace", Format(info.Free), Format(MinFreeBytes))
108 }
109 return info, nil
110}
111
112// Format maakt een korte grootte, bijvoorbeeld "1.5 GiB".
113func Format(n uint64) string {
114 const unit = 1024
115 if n < unit {
116 return fmt.Sprintf("%d B", n)
117 }
118 div := uint64(unit)
119 exp := 0
120 for n/div >= unit && exp < 4 {
121 div *= unit
122 exp++
123 }
124 value := float64(n) / float64(div)
125 return fmt.Sprintf("%.1f %ciB", value, "KMGT"[exp])
126}