aboutsummaryrefslogtreecommitdiff
path: root/internal/ioutil/ioutil.go
blob: 8e98c65cda8df123c80a531d695f84dd64c409a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package ioutil

import (
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"os"
)

func CommitData(path string, data []byte) error {
	return os.WriteFile(path, data, 0644) // FIXME: use safefile package for atomic file writes
}

func ReadData(path string) ([]byte, error) {
	return os.ReadFile(path)
}

func CommitJSON(path string, obj any) error {
	b, err := json.MarshalIndent(obj, "", "  ")
	if err != nil {
		return err
	}
	return CommitData(path, b)
}

func ReadJSON(path string, obj any) error {
	b, err := os.ReadFile(path)
	if err != nil {
		return fmt.Errorf("%s: %v", path, err)
	}
	return json.Unmarshal(b, obj)
}

func CreateDirectories(paths []string) error {
	for _, path := range paths {
		if err := os.Mkdir(path, 0755); err != nil {
			return err
		}
	}
	return nil
}

func DirectoriesExist(paths []string) error {
	for _, path := range paths {
		info, err := os.Stat(path)
		if os.IsNotExist(err) {
			return fmt.Errorf("directory does not exist: %s", path)
		}
		if err != nil {
			return err
		}
		if !info.IsDir() {
			return fmt.Errorf("%s: is not a directory", path)
		}
	}
	return nil
}

func CopyHashes(hashes [][sha256.Size]byte) (ret [][sha256.Size]byte) {
	for _, hash := range hashes {
		var dst [sha256.Size]byte
		copy(dst[:], hash[:])
		ret = append(ret, dst)
	}
	return
}

func SliceHashes(hashes [][sha256.Size]byte) (ret [][]byte) {
	for _, hash := range hashes {
		dst := hash
		ret = append(ret, dst[:])
	}
	return
}

// UnsliceHashes panics unless all hashes are 32 bytes
func UnsliceHashes(hashes [][]byte) (ret [][sha256.Size]byte) {
	for _, hash := range hashes {
		if got, want := len(hash), sha256.Size; got != want {
			panic(fmt.Sprintf("bug: invalid hash: size %d", got))
		}

		var dst [sha256.Size]byte
		copy(dst[:], hash)
		ret = append(ret, dst)
	}
	return
}