-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fs.go
531 lines (440 loc) · 12.6 KB
/
fs.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package utils
import (
"bytes"
"context"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/Laisky/errors/v2"
"github.com/Laisky/zap"
"github.com/fsnotify/fsnotify"
"github.com/Laisky/go-utils/v5/log"
)
// ReplaceFile replace file with content atomatically
//
// this function is not goroutine-safe
func ReplaceFile(path string, content []byte, perm os.FileMode) error {
dir, fname := filepath.Split(path)
swapFname := fmt.Sprintf(".%s.swp-%s", fname, RandomStringWithLength(6))
swapFpath, err := JoinFilepath(dir, swapFname)
if err != nil {
return errors.Wrapf(err, "join path %q and %q", dir, swapFname)
}
fp, err := os.OpenFile(swapFpath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm)
if err != nil {
return errors.Wrapf(err, "create swap file %q", swapFpath)
}
defer os.Remove(swapFpath) //nolint: errcheck
defer LogErr(fp.Close, log.Shared)
if _, err = fp.Write(content); err != nil {
return errors.Wrapf(err, "write to file %q", swapFpath)
}
if err = os.Rename(swapFpath, path); err != nil {
return errors.Wrapf(err, "replace %q by %q", path, swapFpath)
}
return nil
}
// JoinFilepath join paths and check if result is escaped basedir
//
// basedir is the first nonempty path in paths.
// this function could be used to prevent path escaping,
// make sure the result is under basedir.
// for example defend zip-slip: https://snyk.io/research/zip-slip-vulnerability#go
//
// Notice: cannot deal with symlink
func JoinFilepath(paths ...string) (result string, err error) {
if len(paths) == 0 {
return "", errors.New("empty paths")
}
if len(paths) == 1 {
return paths[0], nil
}
for i := range paths {
if paths[i] != "" {
paths = paths[i:]
break
}
}
baseDir := strings.TrimRight(paths[0], string(os.PathSeparator))
result = filepath.Clean(filepath.Join(paths...))
if !strings.HasPrefix(result+string(os.PathSeparator), baseDir+string(os.PathSeparator)) {
return result, errors.Errorf("got result %q, escaped basedir %q", result, baseDir)
}
return result, nil
}
// ReplaceFileStream replace file with content atomatically
//
// Deprecated: use ReplaceFileAtomic instead
var ReplaceFileStream = ReplaceFileAtomic
// ReplaceFileAtomic replace file with content atomatically
//
// write content to a tmp file, then rename it to dst file.
//
// Notice: this function is not goroutine-safe
func ReplaceFileAtomic(path string, in io.ReadCloser, perm os.FileMode) error {
dir, fname := filepath.Split(path)
swapFname := fmt.Sprintf(".%s.swp-%s", fname, RandomStringWithLength(6))
swapFpath, err := JoinFilepath(dir, swapFname)
if err != nil {
return errors.Wrapf(err, "join path %q and %q", dir, swapFname)
}
fp, err := os.OpenFile(swapFpath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm)
if err != nil {
return errors.Wrapf(err, "create swap file %q", swapFpath)
}
defer LogErr(func() error { return errors.Wrapf(os.Remove(swapFpath), "remove %q", swapFpath) }, log.Shared)
defer LogErr(fp.Close, log.Shared)
if _, err = io.Copy(fp, in); err != nil {
return errors.Wrapf(err, "write to file %q", swapFpath)
}
if err = os.Rename(swapFpath, path); err != nil {
return errors.Wrapf(err, "replace %q by %q", path, swapFpath)
}
return nil
}
// MoveFile move file from src to dst by copy
//
// sometimes move file by `rename` not work.
// for example, you can not move file between docker volumes by `rename`.
func MoveFile(src, dst string) (err error) {
if err = CopyFile(src, dst); err != nil {
return err
}
if err = os.Remove(src); err != nil {
return errors.Wrapf(err, "remove file `%s`", src)
}
return nil
}
// IsDir is path exists as dir
func IsDir(path string) (bool, error) {
st, err := os.Stat(path)
if err != nil {
return false, err
}
return st.IsDir(), nil
}
// IsDirWritable if dir is writable
func IsDirWritable(dir string) (err error) {
f := filepath.Join(dir, ".touch")
if err = os.WriteFile(f, []byte(""), 0600); err != nil {
return err
}
if err = os.Remove(f); err != nil {
return errors.Wrapf(err, "remove file `%s`", f)
}
return nil
}
// IsFile is path exists as file
func IsFile(path string) (bool, error) {
isdir, err := IsDir(path)
return !isdir, err
}
// FileExists is path a valid file
func FileExists(path string) (bool, error) {
ok, err := IsFile(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, errors.Wrapf(err, "check file %q", path)
}
return ok, nil
}
type copyFileOption struct {
mode fs.FileMode
flag int
overwrite bool
}
func (o *copyFileOption) fillDefault() *copyFileOption {
o.mode = 0640
o.flag = os.O_WRONLY | os.O_CREATE
return o
}
func (o *copyFileOption) applyOpts(optfs ...CopyFileOptionFunc) (*copyFileOption, error) {
for _, f := range optfs {
if err := f(o); err != nil {
return nil, errors.Wrap(err, GetFuncName(f))
}
}
return o, nil
}
// CopyFileOptionFunc set options for copy file
type CopyFileOptionFunc func(o *copyFileOption) error
// WithFileMode if create new dst file, set the file's mode
func WithFileMode(perm fs.FileMode) CopyFileOptionFunc {
return func(o *copyFileOption) error {
o.mode = perm
return nil
}
}
// WithFileFlag how to write dst file
func WithFileFlag(flag int) CopyFileOptionFunc {
return func(o *copyFileOption) error {
o.flag |= flag
return nil
}
}
// Overwrite overwrite file if target existed
func Overwrite() CopyFileOptionFunc {
return func(o *copyFileOption) error {
o.overwrite = true
o.flag |= os.O_TRUNC
return nil
}
}
// CopyFile copy file content from src to dst
func CopyFile(src, dst string, optfs ...CopyFileOptionFunc) (err error) {
opt, err := new(copyFileOption).fillDefault().applyOpts(optfs...)
if err != nil {
return errors.Wrap(err, "apply options")
}
if err = os.MkdirAll(filepath.Dir(dst), 0751); err != nil {
return errors.Wrapf(err, "create dir `%s`", dst)
}
srcFp, err := os.Open(src)
if err != nil {
return errors.Wrapf(err, "open file `%s`", src)
}
defer SilentClose(srcFp)
if !opt.overwrite {
if ok, err := FileExists(dst); err != nil {
return errors.Wrapf(err, "check file %q", dst)
} else if ok {
return errors.Errorf("file %q exists", dst)
}
}
dstFp, err := os.OpenFile(dst, opt.flag, opt.mode)
if err != nil {
return errors.Wrapf(err, "open file `%s`", dst)
}
defer SilentClose(dstFp)
var n int64
if n, err = io.Copy(dstFp, srcFp); err != nil {
return errors.Wrap(err, "copy file")
}
log.Shared.Debug("file copied",
zap.String("src", src),
zap.String("dst", dst),
zap.Int64("len", n))
return nil
}
// IsFileATimeChanged check is file's atime equal to expectATime
func IsFileATimeChanged(path string, expectATime time.Time) (changed bool, newATime time.Time, err error) {
fi, err := os.Stat(path)
if err != nil {
return false, time.Time{}, errors.Wrapf(err, "get stat of file %s", path)
}
return !fi.ModTime().Equal(expectATime), fi.ModTime(), nil
}
// FileMD5 read file and calculate MD5
//
// Deprecated: use Hash instead
func FileMD5(path string) (hashed string, err error) {
hasher := md5.New()
fp, err := os.Open(path)
if err != nil {
return "", errors.Wrapf(err, "open file %s", path)
}
if _, err = io.Copy(hasher, fp); err != nil {
return "", errors.Wrapf(err, "read file %s", path)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// FileSHA1 read file and calculate sha1
//
// return hashed string in 40 bytes
//
// Deprecated: use Hash instead
func FileSHA1(path string) (hashed string, err error) {
hasher := sha1.New()
fp, err := os.Open(path)
if err != nil {
return "", errors.Wrapf(err, "open file %s", path)
}
if _, err = io.Copy(hasher, fp); err != nil {
return "", errors.Wrapf(err, "read file %s", path)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// DirSize calculate directory size
//
// inspired by https://stackoverflow.com/a/32482941/2368737
func DirSize(path string) (size int64, err error) {
err = filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return
}
type listFilesInDirOption struct {
recur bool
filter func(fname string) bool
}
func (o *listFilesInDirOption) applyOpts(opts ...ListFilesInDirOptionFunc) (*listFilesInDirOption, error) {
for _, opt := range opts {
if err := opt(o); err != nil {
return nil, err
}
}
return o, nil
}
// ListFilesInDirOptionFunc options for ListFilesInDir
type ListFilesInDirOptionFunc func(*listFilesInDirOption) error
// Recursive list files recursively
//
// Deprecated: use ListFilesInDirRecursive instead
func Recursive() ListFilesInDirOptionFunc {
return func(o *listFilesInDirOption) error {
o.recur = true
return nil
}
}
// ListFilesInDirRecursive list files in dir recursively
func ListFilesInDirRecursive() ListFilesInDirOptionFunc {
return func(o *listFilesInDirOption) error {
o.recur = true
return nil
}
}
// ListFilesInDirFilter filter files, only return files that filter returns true
func ListFilesInDirFilter(filter func(fname string) bool) ListFilesInDirOptionFunc {
return func(o *listFilesInDirOption) error {
o.filter = filter
return nil
}
}
// ListFilesInDir list files in dir
func ListFilesInDir(dir string, optfs ...ListFilesInDirOptionFunc) (files []string, err error) {
log.Shared.Debug("ListFilesInDir", zap.String("dir", dir))
opt, err := new(listFilesInDirOption).applyOpts(optfs...)
if err != nil {
return nil, errors.Wrap(err, "apply options")
}
fs, err := os.ReadDir(dir)
if err != nil {
return nil, errors.Wrapf(err, "read dir `%s`", dir)
}
for _, f := range fs {
fpath, err := JoinFilepath(dir, f.Name())
if err != nil {
return nil, errors.Wrapf(err, "join path %q and %q", dir, f.Name())
}
if f.IsDir() {
if opt.recur {
fs, err := ListFilesInDir(fpath, optfs...)
if err != nil {
return nil, errors.Wrapf(err, "list files in %q", fpath)
}
files = append(files, fs...)
}
continue
}
if opt.filter != nil && !opt.filter(fpath) {
continue
}
files = append(files, fpath)
}
return
}
// NewTmpFileForContent write content to tmp file and return path
//
// deprecated: use NewTmpFileForReader instead
func NewTmpFileForContent(content []byte) (path string, err error) {
tmpFile, err := os.CreateTemp("", "*")
if err != nil {
return "", errors.Wrap(err, "create tmp file")
}
defer SilentClose(tmpFile)
if _, err = tmpFile.Write(content); err != nil {
return "", errors.Wrap(err, "write to tmp file")
}
return tmpFile.Name(), nil
}
// NewTmpFile write content to tmp file and return path
func NewTmpFile(reader io.Reader) (*os.File, error) {
tmpFile, err := os.CreateTemp("", "NewTmpFileForReader-*")
if err != nil {
return nil, errors.Wrap(err, "create tmp file")
}
if _, err = io.Copy(tmpFile, reader); err != nil {
return nil, errors.Wrapf(err, "write to tmp file %s", tmpFile.Name())
}
if _, err = tmpFile.Seek(0, io.SeekStart); err != nil {
return nil, errors.Wrapf(err, "seek to start of tmp file %s", tmpFile.Name())
}
return tmpFile, nil
}
// WatchFileChanging watch file changing
//
// when file changed, callback will be called,
// callback will only received fsnotify.Write no matter what happened to changing a file.
//
// TODO: only calculate hash when file's folder got fsnotiy
func WatchFileChanging(ctx context.Context, files []string, callback func(fsnotify.Event)) error {
hashes := map[string]string{}
for _, f := range files {
hashed, err := FileSHA1(f)
if err != nil {
return errors.Wrapf(err, "calculate md5 for file %s", f)
}
hashes[f] = hashed
}
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
for f, hashed := range hashes {
newHashed, err := FileSHA1(f)
if err != nil {
continue
}
if newHashed != hashed {
hashes[f] = newHashed
callback(fsnotify.Event{
Name: f,
Op: fsnotify.Write,
})
}
}
select {
case <-ticker.C:
case <-ctx.Done():
return
}
}
}()
return nil
}
// RenderTemplate render template with args
func RenderTemplate(tplContent string, args any) ([]byte, error) {
tpl, err := template.New("gutils").Parse(tplContent)
if err != nil {
return nil, errors.Wrap(err, "parse template")
}
var out bytes.Buffer
if err := tpl.Execute(&out, args); err != nil {
return nil, errors.Wrap(err, "execute with args")
}
return out.Bytes(), nil
}
// RenderTemplateFile render template file with args
func RenderTemplateFile(tplFile string, args any) ([]byte, error) {
cnt, err := os.ReadFile(tplFile)
if err != nil {
return nil, errors.Wrapf(err, "read template file %q", tplFile)
}
return RenderTemplate(string(cnt), args)
}