BlueGit

timfa bluegit

Files

bluegit / internal / state / state.go

state.go

fix: webinterface spreekt de gebruiker aan
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 51faf12d History
state.go 3.3 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
15// DefaultDir is de state-map als state_path leeg is.
16const DefaultDir = "/var/lib/bluegit"
17
18// File is de naam van het bestand met het gekozen opslagpad.
19const File = "storage_path"
20
21// MinFreeBytes is de kleinste vrije ruimte die een datadisk mag hebben.
22const MinFreeBytes = 1 << 30
23
24// Dir kiest de state-map. Een gezet pad wint van de standaard.
25func Dir(configured string) string {
26 if strings.TrimSpace(configured) != "" {
27 return strings.TrimSpace(configured)
28 }
29 return DefaultDir
30}
31
32// Read leest het opgeslagen pad. Een ontbrekend bestand is geen fout
33// en geeft een lege string.
34func Read(dir string) (string, error) {
35 b, err := os.ReadFile(filepath.Join(dir, File))
36 if errors.Is(err, os.ErrNotExist) {
37 return "", nil
38 }
39 if err != nil {
40 return "", err
41 }
42 return strings.TrimSpace(string(b)), nil
43}
44
45// Write zet het pad atomisch neer.
46func Write(dir, path string) error {
47 if err := os.MkdirAll(dir, 0o750); err != nil {
48 return err
49 }
50 target := filepath.Join(dir, File)
51 tmp := target + ".tmp"
52 if err := os.WriteFile(tmp, []byte(path+"\n"), 0o640); err != nil {
53 return err
54 }
55 return os.Rename(tmp, target)
56}
57
58// Info is het resultaat van een schijfcontrole.
59type Info struct {
60 Path string
61 Free uint64
62 Total uint64
63}
64
65// Inspect eist een absoluut pad dat bestaat, een map is, beschrijfbaar is
66// en minstens MinFreeBytes vrij heeft. Bij een te volle of niet beschrijfbare
67// map zitten Free en Total alsnog in het resultaat.
68func Inspect(path string) (Info, error) {
69 path = strings.TrimSpace(path)
70 if path == "" {
71 return Info{}, fmt.Errorf("pad ontbreekt")
72 }
73 if strings.ContainsAny(path, "\r\n") {
74 return Info{}, fmt.Errorf("pad bevat een regeleinde")
75 }
76 if !filepath.IsAbs(path) {
77 return Info{}, fmt.Errorf("pad moet absoluut zijn")
78 }
79 path = filepath.Clean(path)
80 info := Info{Path: path}
81 st, err := os.Stat(path)
82 if err != nil {
83 if errors.Is(err, os.ErrNotExist) {
84 return info, fmt.Errorf("pad bestaat niet")
85 }
86 return info, fmt.Errorf("pad niet te lezen: %w", err)
87 }
88 if !st.IsDir() {
89 return info, fmt.Errorf("pad is geen map")
90 }
91 var fs syscall.Statfs_t
92 if err := syscall.Statfs(path, &fs); err != nil {
93 return info, fmt.Errorf("vrije ruimte niet te bepalen: %w", err)
94 }
95 info.Free = uint64(fs.Bavail) * uint64(fs.Bsize)
96 info.Total = uint64(fs.Blocks) * uint64(fs.Bsize)
97 probe := filepath.Join(path, ".bluegit-write-test")
98 f, err := os.OpenFile(probe, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
99 if err != nil {
100 return info, fmt.Errorf("pad is niet beschrijfbaar")
101 }
102 _ = f.Close()
103 _ = os.Remove(probe)
104 if info.Free < MinFreeBytes {
105 return info, fmt.Errorf("te weinig vrije ruimte: %s vrij, minimaal %s", Format(info.Free), Format(MinFreeBytes))
106 }
107 return info, nil
108}
109
110// Format maakt een korte grootte, bijvoorbeeld "1.5 GiB".
111func Format(n uint64) string {
112 const unit = 1024
113 if n < unit {
114 return fmt.Sprintf("%d B", n)
115 }
116 div := uint64(unit)
117 exp := 0
118 for n/div >= unit && exp < 4 {
119 div *= unit
120 exp++
121 }
122 value := float64(n) / float64(div)
123 return fmt.Sprintf("%.1f %ciB", value, "KMGT"[exp])
124}