-
Notifications
You must be signed in to change notification settings - Fork 4
/
files.go
298 lines (245 loc) · 7.45 KB
/
files.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Defense Unicorns
// Package helpers provides common helper functions for Go.
package helpers
import (
"archive/tar"
"crypto/sha256"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/otiai10/copy"
)
const dotCharacter = 46
// CreateDirectory creates a directory for the given path and file mode.
func CreateDirectory(path string, mode os.FileMode) error {
if InvalidPath(path) {
return os.MkdirAll(path, mode)
}
return nil
}
// CreateFile creates an empty file at the given path.
func CreateFile(filepath string) error {
if InvalidPath(filepath) {
f, err := os.Create(filepath)
f.Close()
return err
}
return nil
}
// InvalidPath checks if the given path is valid (if it is a permissions error it is there we just don't have access)
func InvalidPath(path string) bool {
_, err := os.Stat(path)
return !os.IsPermission(err) && err != nil
}
// ListDirectories returns a list of directories in the given directory.
func ListDirectories(directory string) ([]string, error) {
var directories []string
paths, err := os.ReadDir(directory)
if err != nil {
return directories, fmt.Errorf("unable to load the directory %s: %w", directory, err)
}
for _, entry := range paths {
if entry.IsDir() {
directories = append(directories, filepath.Join(directory, entry.Name()))
}
}
return directories, nil
}
// RecursiveFileList walks a path with an optional regex pattern and returns a slice of file paths.
// If skipHidden is true, hidden directories will be skipped.
func RecursiveFileList(dir string, pattern *regexp.Regexp, skipHidden bool) (files []string, err error) {
err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
// Return errors
if err != nil {
return err
}
info, err := d.Info()
if err != nil {
return err
}
if info.Mode().IsRegular() {
if pattern != nil {
if len(pattern.FindStringIndex(path)) > 0 {
files = append(files, path)
}
} else {
files = append(files, path)
}
// Skip hidden directories
} else if skipHidden && IsHidden(d.Name()) {
return filepath.SkipDir
}
return nil
})
return files, err
}
// CreateParentDirectory creates the parent directory for the given file path.
func CreateParentDirectory(destination string) error {
parentDest := filepath.Dir(destination)
return CreateDirectory(parentDest, ReadWriteExecuteUser)
}
// CreatePathAndCopy creates the parent directory for the given file path and copies the source file to the destination.
func CreatePathAndCopy(source string, destination string) error {
if err := CreateParentDirectory(destination); err != nil {
return err
}
// Copy all the source data into the destination location
if err := copy.Copy(source, destination); err != nil {
return err
}
// If the path doesn't exist yet then this is an empty file and we should create it
return CreateFile(destination)
}
// ReadFileByChunks reads a file into multiple chunks by the given size.
func ReadFileByChunks(path string, chunkSizeBytes int) (chunks [][]byte, sha256sum string, err error) {
var file []byte
// Open the created archive for io.Copy
if file, err = os.ReadFile(path); err != nil {
return chunks, sha256sum, err
}
//Calculate the sha256sum of the file before we split it up
sha256sum = fmt.Sprintf("%x", sha256.Sum256(file))
// Loop over the tarball breaking it into chunks based on the payloadChunkSize
for {
if len(file) == 0 {
break
}
// don't bust slice length
if len(file) < chunkSizeBytes {
chunkSizeBytes = len(file)
}
chunks = append(chunks, file[0:chunkSizeBytes])
file = file[chunkSizeBytes:]
}
return chunks, sha256sum, nil
}
// IsTrashBin checks if the given directory path corresponds to an operating system's trash bin.
func IsTrashBin(dirPath string) bool {
dirPath = filepath.Clean(dirPath)
// Check if the directory path matches a Linux trash bin
if strings.HasSuffix(dirPath, "/Trash") || strings.HasSuffix(dirPath, "/.Trash-1000") {
return true
}
// Check if the directory path matches a macOS trash bin
if strings.HasSuffix(dirPath, "./Trash") || strings.HasSuffix(dirPath, "/.Trashes") {
return true
}
// Check if the directory path matches a Windows trash bin
if strings.HasSuffix(dirPath, "\\$RECYCLE.BIN") {
return true
}
return false
}
// IsHidden returns true if the given file name starts with a dot.
func IsHidden(name string) bool {
return name[0] == dotCharacter
}
// GetDirSize walks through all files and directories in the provided path and returns the total size in bytes.
func GetDirSize(path string) (int64, error) {
dirSize := int64(0)
// Walk all files in the path
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
dirSize += info.Size()
}
return nil
})
return dirSize, err
}
// IsDir returns true if the given path is a directory.
func IsDir(path string) bool {
info, err := os.Stat(filepath.Clean(path))
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
// GetSHA256OfFile returns the SHA256 hash of the provided file.
func GetSHA256OfFile(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
return GetSHA256Hash(f)
}
// SHAsMatch returns an error if the SHA256 hash of the provided file does not match the expected hash.
func SHAsMatch(path, expected string) error {
sha, err := GetSHA256OfFile(path)
if err != nil {
return err
}
if sha != expected {
return fmt.Errorf("expected sha256 of %s to be %s, found %s", path, expected, sha)
}
return nil
}
// CreateReproducibleTarballFromDir creates a tarball from a directory with stripped headers
func CreateReproducibleTarballFromDir(dirPath, dirPrefix, tarballPath string) error {
tb, err := os.Create(tarballPath)
if err != nil {
return fmt.Errorf("error creating tarball: %w", err)
}
defer tb.Close()
tw := tar.NewWriter(tb)
defer tw.Close()
// Walk through the directory and process each file
return filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
link := ""
if info.Mode().Type() == os.ModeSymlink {
link, err = os.Readlink(filePath)
if err != nil {
return fmt.Errorf("error reading symlink: %w", err)
}
}
// Create a new header
header, err := tar.FileInfoHeader(info, link)
if err != nil {
return fmt.Errorf("error creating tar header: %w", err)
}
// Strip non-deterministic header data
header.ModTime = time.Time{}
header.AccessTime = time.Time{}
header.ChangeTime = time.Time{}
header.Uid = 0
header.Gid = 0
header.Uname = ""
header.Gname = ""
// Ensure the header's name is correctly set relative to the base directory
name, err := filepath.Rel(dirPath, filePath)
if err != nil {
return fmt.Errorf("error getting relative path: %w", err)
}
name = filepath.Join(dirPrefix, name)
name = filepath.ToSlash(name)
header.Name = name
// Write the header to the tarball
if err := tw.WriteHeader(header); err != nil {
return fmt.Errorf("error writing header: %w", err)
}
// If it's a file, write its content
if info.Mode().IsRegular() {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
defer file.Close()
if _, err := io.Copy(tw, file); err != nil {
return fmt.Errorf("error writing file to tarball: %w", err)
}
}
return nil
})
}