-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync.go
414 lines (364 loc) · 10.7 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
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
package fssync
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/pkg/errors"
iopkg "github.com/Scalingo/go-utils/io"
)
type SyncReport interface {
HasChanged(file string) bool
ChangeCount() int
}
type Syncer interface {
Sync(dst, src string) (SyncReport, error)
}
// Copier is the interface used to copy content from one file to another
// By default it's using iopkg.Copier
type Copier interface {
Copy(dst io.Writer, src io.Reader) (int64, error)
}
type FsSyncer struct {
checkChecksum bool
preserveOwnership bool
ignoreNotFound bool
noCache bool
bufferSize int64
copier Copier
}
type fsSyncReport struct {
fileChanges map[string]bool
}
func (r fsSyncReport) HasChanged(file string) bool {
return r.fileChanges[file]
}
func (r fsSyncReport) ChangeCount() int {
return len(r.fileChanges)
}
func New(opts ...func(*FsSyncer)) *FsSyncer {
s := &FsSyncer{
bufferSize: 512 * 1024,
}
for _, opt := range opts {
opt(s)
}
copierOpts := []iopkg.CopierOpt{}
if s.bufferSize != 0 {
copierOpts = append(copierOpts, iopkg.WithBufferSize(s.bufferSize))
}
if s.noCache {
copierOpts = append(copierOpts, iopkg.WithNoDiskCache)
}
s.copier = iopkg.NewCopier(copierOpts...)
return s
}
// WithChecksum option: Check SHA1 checksum instead of modtime + size
func WithChecksum(s *FsSyncer) {
s.checkChecksum = true
}
// PreserveOwnership option: chown files from source owner instead of copying
// with current owner root required to change the user ownership in most cases
func PreserveOwnership(s *FsSyncer) {
s.preserveOwnership = true
}
// IgnoreNotFound option: if the synced directory is heavily used during the
// sync there might be a file which is walked in but which does not exist
// anymore when Lstat is used
func IgnoreNotFound(s *FsSyncer) {
s.ignoreNotFound = true
}
// NoCache option: Use the system call fadvise to discard kernel cache after
// reading/writing Inspired from
// https://github.com/coreutils/coreutils/blob/master/src/dd.c
func NoCache(s *FsSyncer) {
s.noCache = true
}
// WithBufferSize option: lets you configure the size of the memory buffer used
// to perform the copy from one file to another
// Default is 512kB
func WithBufferSize(n int64) func(*FsSyncer) {
return func(s *FsSyncer) {
s.bufferSize = n
}
}
type syncInfo struct {
base string
path string
fileInfo os.FileInfo
stat *syscall.Stat_t
times statTimes
}
func (s syncInfo) SHA1() ([]byte, error) {
hash := sha1.New()
fd, err := os.Open(s.path)
if err != nil {
return nil, errors.Wrapf(err, "fail to open file")
}
defer fd.Close()
_, err = io.Copy(hash, fd)
if err != nil {
return nil, errors.Wrapf(err, "fail to read file content")
}
return hash.Sum(nil), nil
}
type syncState struct {
timesMap map[string]statTimes
inoMap map[uint64]string
}
type statTimes struct {
atime time.Time
mtime time.Time
}
type existingFileRes struct {
shouldUpdateTimes bool
hasContentChanged bool
}
type unexistingFileRes struct {
shouldUpdateTimes bool
}
func (s *FsSyncer) Sync(dst, src string) (SyncReport, error) {
state := syncState{
timesMap: map[string]statTimes{},
inoMap: map[uint64]string{},
}
report := fsSyncReport{fileChanges: map[string]bool{}}
src = filepath.Clean(src)
dst = filepath.Clean(dst)
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
if os.IsNotExist(err) && s.ignoreNotFound {
return nil
}
return err
}
dstPath := strings.Replace(path, src, dst, 1)
srcSysStat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return errors.Wrapf(err, "fail to get detailed stat info for %s", path)
}
atime := time.Unix(srcSysStat.Atim.Sec, srcSysStat.Atim.Nsec)
mtime := time.Unix(srcSysStat.Mtim.Sec, srcSysStat.Mtim.Nsec)
dstStat, err := os.Lstat(dstPath)
if os.IsNotExist(err) {
report.fileChanges[dstPath] = true
res, err := s.syncUnexistingFile(syncInfo{
base: src,
path: path,
fileInfo: info,
stat: srcSysStat,
}, syncInfo{
base: dst,
path: dstPath,
}, state)
if err != nil {
return errors.Wrapf(err, "fail to handle unexisting file %v", path)
}
if res.shouldUpdateTimes {
state.timesMap[dstPath] = statTimes{atime: atime, mtime: mtime}
}
if s.preserveOwnership {
err = os.Chown(dstPath, int(srcSysStat.Uid), int(srcSysStat.Gid))
if err != nil {
return errors.Wrapf(err, "fail to chown %v", dstPath)
}
}
return nil
} else if err != nil {
return errors.Wrapf(err, "fail to stat %v", dstPath)
}
dstSysStat, ok := dstStat.Sys().(*syscall.Stat_t)
if !ok {
return errors.Wrapf(err, "fail to get detailed stat info for %s", dstPath)
}
dstatime := time.Unix(dstSysStat.Atim.Sec, dstSysStat.Atim.Nsec)
dstmtime := time.Unix(dstSysStat.Mtim.Sec, dstSysStat.Mtim.Nsec)
res, err := s.syncExistingFile(syncInfo{
base: src,
path: path,
fileInfo: info,
stat: srcSysStat,
times: statTimes{atime: atime, mtime: mtime},
}, syncInfo{
base: dst,
path: dstPath,
fileInfo: dstStat,
stat: dstSysStat,
times: statTimes{atime: dstatime, mtime: dstmtime},
}, state)
if err != nil {
return errors.Wrapf(err, "fail to sync existing file %v", path)
}
if res.shouldUpdateTimes {
state.timesMap[dstPath] = statTimes{atime: atime, mtime: mtime}
}
if res.hasContentChanged {
report.fileChanges[dstPath] = true
}
if s.preserveOwnership {
err = os.Chown(dstPath, int(srcSysStat.Uid), int(srcSysStat.Gid))
if err != nil {
return errors.Wrapf(err, "fail to chown %v", dstPath)
}
}
return nil
})
if err != nil {
return report, errors.Wrapf(err, "fail to walk %v", src)
}
dirsToRemove := []string{}
err = filepath.Walk(dst, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
srcPath := strings.Replace(path, dst, src, 1)
_, err = os.Lstat(srcPath)
if os.IsNotExist(err) {
report.fileChanges[path] = true
if info.IsDir() {
// Do not delete directory straight we want to tag all files
// recursively before deleting empty dirs
dirsToRemove = append(dirsToRemove, path)
} else {
err := os.Remove(path)
if err != nil {
return errors.Wrapf(err, "fail to delete %v", path)
}
}
}
return nil
})
if err != nil {
return report, errors.Wrapf(err, "fail to walk %v", dst)
}
for i := len(dirsToRemove) - 1; i >= 0; i-- {
dir := dirsToRemove[i]
err := os.Remove(dir)
if err != nil {
return report, errors.Wrapf(err, "fail to delete %v", dir)
}
}
// Change times after removing entries as removing a file
// changes the mtime at the os level
for file, times := range state.timesMap {
err = os.Chtimes(file, times.atime, times.mtime)
if err != nil && !(os.IsNotExist(err) && s.ignoreNotFound) {
return report, errors.Wrapf(err, "fail to set atime and mtime of %v", file)
}
}
return report, nil
}
func (s *FsSyncer) syncExistingFile(src, dst syncInfo, state syncState) (existingFileRes, error) {
res := existingFileRes{}
if src.fileInfo.IsDir() && dst.fileInfo.IsDir() {
res.shouldUpdateTimes = true
return res, nil
} else if src.fileInfo.IsDir() && !dst.fileInfo.IsDir() ||
!src.fileInfo.IsDir() && dst.fileInfo.IsDir() {
err := os.RemoveAll(dst.path)
if err != nil {
return res, errors.Wrapf(err, "fail to remove destination invalid file %v", dst.path)
}
}
if s.checkChecksum {
srcSHA1, err := src.SHA1()
if err != nil {
return res, errors.Wrapf(err, "fail to compute SHA1 of %v", src.path)
}
dstSHA1, err := dst.SHA1()
if err != nil {
return res, errors.Wrapf(err, "fail to compute SHA1 of %v", dst.path)
}
if bytes.Equal(srcSHA1, dstSHA1) {
res.shouldUpdateTimes = true
return res, nil
}
} else {
if src.fileInfo.Size() == dst.fileInfo.Size() && src.fileInfo.ModTime() == dst.fileInfo.ModTime() {
return res, nil
}
}
res.hasContentChanged = true
dir := filepath.Dir(dst.path)
base := filepath.Base(dst.path)
tmpDst := tmpFileName(dir, base)
newFileRes, err := s.syncUnexistingFile(src, syncInfo{base: dst.base, path: tmpDst}, state)
if err != nil {
return res, errors.Wrapf(err, "fail to sync src to temp file %v -> %v", src.path, tmpDst)
}
res.shouldUpdateTimes = newFileRes.shouldUpdateTimes
// Once the new file is ready, replace the old one
err = os.Rename(tmpDst, dst.path)
if err != nil {
return res, errors.Wrapf(err, "fail to mv tmp file on original file %v -> %v", tmpDst, dst.path)
}
// temp file name has been set to state, restore it to real name
state.inoMap[src.stat.Ino] = dst.path
return res, nil
}
func (s *FsSyncer) syncUnexistingFile(src, dst syncInfo, state syncState) (unexistingFileRes, error) {
res := unexistingFileRes{}
if existingLink, ok := state.inoMap[src.stat.Ino]; ok {
err := os.Link(existingLink, dst.path)
if err != nil {
return res, errors.Wrapf(err, "fail to create link from %v to %v", existingLink, dst.path)
}
return res, nil
}
state.inoMap[src.stat.Ino] = dst.path
if src.fileInfo.IsDir() {
err := os.MkdirAll(dst.path, src.fileInfo.Mode())
if err != nil {
return res, errors.Wrapf(err, "fail to create dst directory %v", dst.path)
}
return unexistingFileRes{shouldUpdateTimes: true}, nil
}
if src.fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
linkDst, err := os.Readlink(src.path)
if err != nil {
return res, errors.Wrapf(err, "fail to get link destination of src %v", src.path)
}
if strings.Contains(linkDst, src.base) {
linkDst = strings.Replace(linkDst, src.base, dst.base, 1)
}
err = os.Symlink(linkDst, dst.path)
if err != nil {
return res, errors.Wrapf(err, "fail to create symlink %v (%v)", dst.path, linkDst)
}
return res, nil
}
_, err := s.copyFileContent(src.path, dst.path, src.fileInfo)
if err != nil {
return res, errors.Wrapf(err, "fail to copy content from %v to %v", src.path, dst.path)
}
return unexistingFileRes{shouldUpdateTimes: true}, nil
}
func (s *FsSyncer) copyFileContent(src, dst string, info os.FileInfo) (int64, error) {
sfd, err := os.Open(src)
if err != nil {
return -1, errors.Wrapf(err, "fail to open src %v", src)
}
defer sfd.Close()
fd, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, info.Mode())
if err != nil {
return -1, errors.Wrapf(err, "fail to open dest %v", dst)
}
defer fd.Close()
n, err := s.copier.Copy(fd, sfd)
if err != nil {
return -1, errors.Wrapf(err, "fail to copy data")
}
return n, nil
}
func tmpFileName(dir, base string) string {
// From io/ioutil.TempFile
r := uint32(time.Now().UnixNano() + int64(os.Getpid()))
r = r*1664525 + 1013904223 // constants from Numerical Recipes
return filepath.Join(dir, fmt.Sprintf(".%s-%s", base, strconv.Itoa(int(1e9 + r%1e9))[1:]))
}