-
Notifications
You must be signed in to change notification settings - Fork 698
/
files.go
74 lines (68 loc) · 1.63 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
package dependencies
import (
"io"
"os"
"path"
"runtime"
"strings"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/spf13/afero"
)
func updateFiles(fs afero.Fs, sourceDir, targetDir string) error {
const bufferSize = 20
err := afero.Walk(fs, targetDir, func(wpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info == nil || info.IsDir() {
return nil
}
relativePath := strings.Replace(wpath, targetDir, "", -1)
sourcePath := path.Join(sourceDir, relativePath)
sourceFile, err := fs.Open(sourcePath)
if os.IsNotExist(err) {
// we allow source files to not exist, for instance, test vectors that we
// have in zkevm-node but are not present in the upstream repo
return nil
}
if err != nil {
return err
}
defer func() {
if err := sourceFile.Close(); err != nil {
log.Errorf("Could not close %s: %v", sourceFile.Name(), err)
}
}()
targetFile, err := fs.OpenFile(wpath, os.O_RDWR|os.O_TRUNC, 0644) //nolint:gomnd
if err != nil {
return err
}
defer func() {
if err := targetFile.Close(); err != nil {
log.Errorf("Could not close %s: %v", targetFile.Name(), err)
}
}()
buf := make([]byte, bufferSize)
for {
n, err := sourceFile.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := targetFile.Write(buf[:n]); err != nil {
return err
}
}
return nil
})
return err
}
func getTargetPath(targetPath string) string {
if strings.HasPrefix(targetPath, "/") {
return targetPath
}
_, filename, _, _ := runtime.Caller(1)
return path.Join(path.Dir(filename), targetPath)
}