/
Sync.go
163 lines (149 loc) · 4.5 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
// ==================================================================================
//
// Work of the U.S. Department of the Navy, Naval Information Warfare Center Pacific.
// Released as open source under the MIT License. See LICENSE file.
//
// ==================================================================================
package fs
import (
"context"
"fmt"
"strings"
)
func Sync(ctx context.Context, input *SyncInput) (int, error) {
if !strings.HasPrefix(input.Source, "/") {
return 0, fmt.Errorf("source %q does not start with /", input.Source)
}
if !strings.HasPrefix(input.Destination, "/") {
return 0, fmt.Errorf("destination %q does not start with /", input.Destination)
}
if input.Logger != nil {
_ = input.Logger.Log("Synchronizing", map[string]interface{}{
"delete": input.Delete,
"dst": input.Destination,
"exclude": input.Exclude,
"threads": input.MaxThreads,
"src": input.Source,
})
}
skip := false
if len(input.Exclude) > 0 {
for _, exclude := range input.Exclude {
if strings.HasPrefix(exclude, "*") {
if strings.HasSuffix(exclude, "*") {
if strings.Contains(input.Source, exclude[1:len(exclude)-1]) {
skip = true
break
}
} else {
if strings.HasSuffix(input.Source, exclude[1:]) {
skip = true
break
}
}
} else {
if strings.HasSuffix(exclude, "*") {
if strings.HasPrefix(input.Source, exclude[0:len(exclude)-1]) {
skip = true
break
}
} else {
if input.Source == exclude {
skip = true
break
}
}
}
}
}
if skip {
if input.Logger != nil {
_ = input.Logger.Log("Skipping source", map[string]interface{}{
"src": input.Source,
})
}
return 0, nil
}
sourceFileInfo, err := input.SourceFileSystem.Stat(ctx, input.Source)
if err != nil {
if input.SourceFileSystem.IsNotExist(err) {
return 0, fmt.Errorf("source does not exist %q: %w", input.Source, err)
}
return 0, fmt.Errorf("error stating source %q: %w", input.Source, err)
}
// if source is a directory
if sourceFileInfo.IsDir() {
if _, statError := input.DestinationFileSystem.Stat(ctx, input.Destination); statError != nil {
if input.DestinationFileSystem.IsNotExist(statError) {
if !input.Parents {
return 0, fmt.Errorf("destination directory does not exist and parents is false: %q", input.Destination)
}
}
}
count, syncDirectoryError := SyncDirectory(ctx, &SyncDirectoryInput{
Delete: input.Delete,
CheckTimestamps: input.CheckTimestamps,
SourceDirectory: input.Source,
SourceFileSystem: input.SourceFileSystem,
DestinationDirectory: input.Destination,
DestinationFileSystem: input.DestinationFileSystem,
Exclude: input.Exclude,
Limit: input.Limit,
Logger: input.Logger,
MaxThreads: input.MaxThreads,
TimestampPrecision: input.TimestampPrecision,
})
if syncDirectoryError != nil {
return 0, fmt.Errorf(
"error syncing source directory %q (base %q) to destination directory %q (base %q): %w",
input.Source,
input.SourceFileSystem.Root(),
input.Destination,
input.DestinationFileSystem.Root(),
syncDirectoryError,
)
}
return count, nil
}
// if source is a file
copyFile := false
destinationFileInfo, err := input.DestinationFileSystem.Stat(ctx, input.Destination)
if err != nil {
if input.DestinationFileSystem.IsNotExist(err) {
copyFile = true
} else {
return 0, fmt.Errorf("error stating destination %q: %w", input.Destination, err)
}
} else {
if sourceFileInfo.Size() != destinationFileInfo.Size() {
copyFile = true
}
if input.CheckTimestamps {
if !EqualTimestamp(sourceFileInfo.ModTime(), destinationFileInfo.ModTime(), input.TimestampPrecision) {
copyFile = true
}
}
}
if copyFile {
err = Copy(ctx, &CopyInput{
CheckParents: true, // check if parent for destination exists
SourceName: input.Source,
SourceFileSystem: input.SourceFileSystem,
DestinationName: input.Destination,
DestinationFileSystem: input.DestinationFileSystem,
Logger: input.Logger,
MakeParents: input.Parents, // create destination parents if not exist
})
if err != nil {
return 0, fmt.Errorf("error copying %q to %q: %w", input.Source, input.Destination, err)
}
return 1, nil
} else {
if input.Logger != nil {
_ = input.Logger.Log("Skipping file", map[string]interface{}{
"src": input.Source,
})
}
}
return 0, nil
}