forked from Foxboron/sbctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
209 lines (188 loc) · 4.61 KB
/
util.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package sbctl
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/foxboron/sbctl/fs"
"github.com/foxboron/sbctl/logging"
"github.com/spf13/afero"
)
func ChecksumFile(file string) (string, error) {
hasher := sha256.New()
s, err := fs.ReadFile(file)
if err != nil {
return "", err
}
hasher.Write(s)
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func CreateDirectory(path string) error {
_, err := fs.Fs.Stat(path)
switch {
case errors.Is(err, os.ErrNotExist):
// Ignore this error
case errors.Is(err, os.ErrExist):
return nil
case err != nil:
return err
}
if err := fs.Fs.MkdirAll(path, os.ModePerm); err != nil {
return err
}
return nil
}
func ReadOrCreateFile(filePath string) ([]byte, error) {
// Try to access or create the file itself
f, err := fs.ReadFile(filePath)
if err != nil {
// Errors will mainly happen due to permissions or non-existing file
if os.IsNotExist(err) {
// First, guarantee the directory's existence
// os.MkdirAll simply returns nil if the directory already exists
fileDir := filepath.Dir(filePath)
if err = fs.Fs.MkdirAll(fileDir, os.ModePerm); err != nil {
return nil, err
}
file, err := fs.Fs.Create(filePath)
if err != nil {
return nil, err
}
file.Close()
// Create zero-length f, which is equivalent to what would be read from empty file
f = make([]byte, 0)
} else {
if os.IsPermission(err) {
return nil, err
}
return nil, err
}
}
return f, nil
}
var EfivarFSFiles = []string{
"/sys/firmware/efi/efivars/PK-8be4df61-93ca-11d2-aa0d-00e098032b8c",
"/sys/firmware/efi/efivars/KEK-8be4df61-93ca-11d2-aa0d-00e098032b8c",
"/sys/firmware/efi/efivars/db-d719b2cb-3d3a-4596-a3bc-dad00e67656f",
}
var ErrImmutable = errors.New("file is immutable")
var ErrNotImmutable = errors.New("file is not immutable")
var Immutable = false
// Check if a given file has the immutable bit set
func IsImmutable(file string) error {
// We can't actually do the syscall check. Implemented a workaround to test stuff instead
if _, ok := fs.Fs.(afero.OsFs); ok {
if Immutable {
return ErrImmutable
}
return ErrNotImmutable
}
f, err := os.Open(file)
// Files in efivarfs might not exist. Ignore them
if errors.Is(err, os.ErrNotExist) {
return nil
} else if err != nil {
return err
}
attr, err := GetAttr(f)
if err != nil {
return err
}
if (attr & FS_IMMUTABLE_FL) != 0 {
return ErrImmutable
}
return ErrNotImmutable
}
// Check if any files in efivarfs has the immutable bit set
func CheckImmutable() error {
var isImmutable bool
for _, file := range EfivarFSFiles {
err := IsImmutable(file)
if errors.Is(err, ErrImmutable) {
isImmutable = true
logging.Warn("File is immutable: %s", file)
} else if errors.Is(err, ErrNotImmutable) {
continue
} else if err != nil {
return fmt.Errorf("couldn't read file: %s", file)
}
}
if isImmutable {
return ErrImmutable
}
return nil
}
func CheckMSDos(r io.Reader) (bool, error) {
// We are looking for MS-DOS executables.
// They contain "MZ" as the two first bytes
var header [2]byte
if _, err := io.ReadFull(r, header[:]); err != nil {
// File is smaller than 2 bytes
if errors.Is(err, io.EOF) {
return false, nil
} else if errors.Is(err, io.ErrUnexpectedEOF) {
return false, nil
}
return false, err
}
if !bytes.Equal(header[:], []byte{0x4d, 0x5a}) {
return false, nil
}
return true, nil
}
var (
checked = make(map[string]bool)
)
func AddChecked(path string) {
normalized := strings.Join(strings.Split(path, "/")[2:], "/")
checked[normalized] = true
}
func InChecked(path string) bool {
normalized := strings.Join(strings.Split(path, "/")[2:], "/")
return checked[normalized]
}
func CopyFile(src, dst string) error {
source, err := fs.Fs.Open(src)
if err != nil {
return err
}
defer source.Close()
f, err := fs.Fs.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(f, source); err != nil {
return err
}
si, err := fs.Fs.Stat(src)
if err != nil {
return err
}
return fs.Fs.Chmod(dst, si.Mode())
}
// CopyDirectory moves files and creates directories
func CopyDirectory(src, dst string) error {
return afero.Walk(fs.Fs, src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath := strings.TrimPrefix(path, src)
newPath := filepath.Join(dst, relPath)
if info.IsDir() {
if err := fs.Fs.MkdirAll(newPath, info.Mode()); err != nil {
return err
}
return nil
}
if err := CopyFile(path, newPath); err != nil {
return err
}
return nil
})
}