/
file.go
227 lines (206 loc) · 4.69 KB
/
file.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
package u
import (
"archive/zip"
"bufio"
"crypto/sha1"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
)
// PathExists returns true if path exists
func PathExists(path string) bool {
_, err := os.Lstat(path)
return err == nil
}
// FileExists returns true if path exists and is a regular file
func FileExists(path string) bool {
st, err := os.Lstat(path)
return err == nil && st.Mode().IsRegular()
}
// DirExists returns true if path exists and is a directory
func DirExists(path string) bool {
st, err := os.Lstat(path)
return err == nil && st.IsDir()
}
// PathIsDir returns true if a path exists and is a directory
// Returns false, nil if a path exists and is not a directory (e.g. a file)
// Returns undefined, error if there was an error e.g. because a path doesn't exists
func PathIsDir(path string) (isDir bool, err error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.IsDir(), nil
}
// FileSize gets file size, -1 if file doesn't exist
func FileSize(path string) int64 {
st, err := os.Lstat(path)
if err == nil {
return st.Size()
}
return -1
}
// CopyFile copies a file from src to dst
// It'll create destination directory if necessary
func CopyFile(dst string, src string) error {
err := os.MkdirAll(filepath.Dir(dst), 0755)
if err != nil {
return err
}
fin, err := os.Open(src)
if err != nil {
return err
}
defer fin.Close()
fout, err := os.Create(dst)
if err != nil {
return err
}
_, err = io.Copy(fout, fin)
err2 := fout.Close()
if err != nil || err2 != nil {
os.Remove(dst)
}
return err
}
func FileSha1Hex(path string) (string, error) {
sha1OfFile := func(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
//fmt.Printf("os.Open(%s) failed with %s\n", path, err.Error())
return nil, err
}
defer f.Close()
h := sha1.New()
_, err = io.Copy(h, f)
if err != nil {
//fmt.Printf("io.Copy() failed with %s\n", err.Error())
return nil, err
}
return h.Sum(nil), nil
}
sha1, err := sha1OfFile(path)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", sha1), nil
}
func DataSha1Hex(d []byte) string {
sha1 := sha1.Sum(d)
return fmt.Sprintf("%x", sha1[:])
}
// ReadLinesFromReader reads lines from io.Reader
func ReadLinesFromReader(r io.Reader) ([]string, error) {
scanner := bufio.NewScanner(r)
var res []string
for scanner.Scan() {
line := scanner.Bytes()
res = append(res, string(line))
}
return res, scanner.Err()
}
// ReadLines reads file as lines
func ReadLines(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
return ReadLinesFromReader(file)
}
// CloseNoError is like io.Closer Close() but ignores an error
// use as: defer CloseNoError(f)
func CloseNoError(f io.Closer) {
_ = f.Close()
}
func ReadZipFile(path string) (map[string][]byte, error) {
r, err := zip.OpenReader(path)
if err != nil {
return nil, err
}
defer CloseNoError(r)
res := map[string][]byte{}
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return nil, err
}
d, err := io.ReadAll(rc)
err2 := rc.Close()
if err != nil {
return nil, err
}
if err2 != nil {
return nil, err2
}
res[f.Name] = d
}
return res, nil
}
// call cb on each file in a directory
func IterDir(dir string, cb func(path string, de fs.DirEntry) (bool, error)) error {
dirsToVisit := []string{dir}
for len(dirsToVisit) > 0 {
dir = dirsToVisit[0]
dirsToVisit = dirsToVisit[1:]
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, de := range entries {
name := de.Name()
if de.IsDir() {
newDir := filepath.Join(dir, name)
dirsToVisit = append(dirsToVisit, newDir)
continue
}
shouldStop, err := cb(filepath.Join(dir, name), de)
if err != nil {
return err
}
if shouldStop {
return nil
}
}
}
return nil
}
// call cb on each file in fsys
// startDir should be ". for root directory, "" and "/" do not work
func IterReadDirFS(fsys fs.ReadDirFS, startDir string, cb func(string, fs.DirEntry) error) error {
if startDir == "" || startDir == "/" {
return fmt.Errorf("startDir is '%s', should be '.' for ", startDir)
}
dirsToVisit := []string{startDir}
for len(dirsToVisit) > 0 {
dir := dirsToVisit[0]
dirsToVisit = dirsToVisit[1:]
dirs, err := fs.ReadDir(fsys, dir)
if err != nil {
return err
}
for _, d := range dirs {
fpath := path.Join(dir, d.Name())
if d.IsDir() {
dirsToVisit = append(dirsToVisit, fpath)
continue
}
if err := cb(fpath, d); err != nil {
return err
}
}
}
return nil
}
type syncer interface {
Sync() error
}
func MaybeSync(w io.Writer) error {
if f, ok := w.(syncer); ok {
return f.Sync()
}
return nil
}