-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
164 lines (135 loc) · 3.26 KB
/
sync.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
package please
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
)
// Sync copies files from the src directory to the dest, deleting files which
// are no longer in the srcs and do not match an exception regex.
func Sync(src string, dest string, exceptionRegexes []string) error {
absSrc, err := filepath.Abs(src)
if err != nil {
return fmt.Errorf("could not get absolute path for '%s': %w", src, err)
}
absDest, err := filepath.Abs(dest)
if err != nil {
return fmt.Errorf("could not get absolute path for '%s': %w", dest, err)
}
srcsSet := map[string]struct{}{}
if err := filepath.WalkDir(absSrc, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == absSrc {
return nil
}
relPath, err := filepath.Rel(absSrc, path)
if err != nil {
return fmt.Errorf("could not determine relPath of '%s': %w", path, err)
}
destPath := filepath.Join(absDest, relPath)
if d.IsDir() {
log.Debug().Str("dest", destPath).Msg("creating directory")
if err := os.MkdirAll(destPath, 0750); err != nil {
return err
}
return nil
}
if d.Type().IsRegular() {
if err := CopyFile(path, destPath); err != nil {
return err
}
srcsSet[relPath] = struct{}{}
}
return nil
}); err != nil {
return err
}
if err := filepath.WalkDir(absDest, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == absDest {
return nil
}
if d.IsDir() {
return nil
}
relPath, err := filepath.Rel(absDest, path)
if err != nil {
return fmt.Errorf("could not determine relPath of '%s': %w", path, err)
}
if !setContains(srcsSet, relPath) && !isFileNameInFilesAndDirsToKeep(
path,
exceptionRegexes,
) {
log.Debug().Str("path", path).Msg("removing path")
if err := os.RemoveAll(path); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}
// BufferSize represents the file copy buffer.
const BufferSize = 32
// CopyFile copies the src file to the destination.
func CopyFile(src string, dest string) error {
log.Debug().Str("src", src).Str("dest", dest).Msg("copying")
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}
if !sourceFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
if err := os.MkdirAll(filepath.Dir(dest), 0750); err != nil {
return err
}
destination, err := os.Create(dest)
if err != nil {
return fmt.Errorf("could not create '%s': %w", dest, err)
}
defer destination.Close()
if err := destination.Chmod(sourceFileStat.Mode()); err != nil {
return err
}
buf := make([]byte, BufferSize)
for {
n, err := source.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := destination.Write(buf[:n]); err != nil {
return err
}
}
return nil
}
func setContains(set map[string]struct{}, elem string) bool {
_, ok := set[elem]
return ok
}
func isFileNameInFilesAndDirsToKeep(fileName string, filesAndDirsToKeepRegexes []string) bool {
for _, keepRegex := range filesAndDirsToKeepRegexes {
re := regexp.MustCompile(keepRegex)
if re.MatchString(fileName) {
return true
}
}
return false
}