-
Notifications
You must be signed in to change notification settings - Fork 8
/
local.go
190 lines (167 loc) · 5.34 KB
/
local.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
//
// s3sync-service - Realtime S3 synchronisation tool
// Copyright (c) 2020 Yevgeniy Valeyev
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package service
import (
"crypto/md5"
"encoding/hex"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// IsExcluded check if a path is excluded
func IsExcluded(path string, exclusions []string, inclusions []string) bool {
var excluded bool
for _, inclusion := range inclusions {
re := regexp.MustCompile(inclusion)
if re.FindAll([]byte(path), -1) != nil {
excluded = false
}
}
for _, exclusion := range exclusions {
re := regexp.MustCompile(exclusion)
if re.FindAll([]byte(path), -1) != nil {
excluded = true
}
}
return excluded
}
// FilePathWalkDir walks through the directory and all subdirectories returning list of files for upload and list of files to be deleted from S3
func FilePathWalkDir(site *Site, awsItems map[string]string, uploadCh chan<- UploadCFG, checksumCh chan<- ChecksumCFG) {
err := filepath.WalkDir(site.LocalPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
// Update errors metric
errorsMetric.WithLabelValues(site.LocalPath, site.Bucket, site.BucketPath, site.Name, "local").Inc()
logger.Error(err)
return err
}
if !d.IsDir() {
// A shorthand workaround for skipping symlinks from processing
if d.Type() == fs.ModeSymlink {
logger.Infof("%s is a symlink, skipping", path)
} else {
excluded := IsExcluded(path, site.Exclusions, site.Inclusions)
s3Key := generateS3Key(site.BucketPath, site.LocalPath, path)
if excluded {
logger.Debugf("skipping without errors: %+v", path)
// Delete the excluded object if already in the cloud
if awsItems[s3Key] != "" && site.RetireDeleted {
uploadCh <- UploadCFG{s3Key, site, "delete"}
}
} else {
checksumCh <- ChecksumCFG{UploadCFG{path, site, "upload"}, path, awsItems[s3Key], site}
}
}
}
return nil
})
// Check for deleted files
if site.RetireDeleted {
for key := range awsItems {
// Generate localPath by removing BucketPath from the key value and addint LocalPath
localPath := filepath.Join(site.LocalPath, strings.Replace(key, site.BucketPath, "", 1))
// Send s3 key for deleteion if generated localPath does not exist
if _, err := os.Stat(localPath); os.IsNotExist(err) {
uploadCh <- UploadCFG{key, site, "delete"}
}
}
}
if err != nil {
// Update errors metric
errorsMetric.WithLabelValues(site.LocalPath, site.Bucket, site.BucketPath, site.Name, "local").Inc()
logger.Error(err)
}
}
// CompareChecksum compares local file checksum with S3 ETag value
func CompareChecksum(filename string, checksumRemote string, site *Site) string {
var sumOfSums []byte
var parts int
var finalSum []byte
chunkSize := int64(5 * 1024 * 1024)
logger.Debugf("%s: comparing checksums", filename)
if checksumRemote == "" {
logger.Infof("%s: remote checksum is unavailable, probably dealing with a new file", filename)
return filename
}
file, err := os.Open(filename)
if err != nil {
// Update errors metric
errorsMetric.WithLabelValues(site.LocalPath, site.Bucket, site.BucketPath, site.Name, "local").Inc()
logger.Error(err)
return ""
}
defer file.Close()
dataSize, err := file.Seek(0, io.SeekEnd)
if err != nil {
// Update errors metric
errorsMetric.WithLabelValues(site.LocalPath, site.Bucket, site.BucketPath, site.Name, "local").Inc()
logger.Error(err)
return ""
}
for start := int64(0); start < dataSize; start += chunkSize {
length := chunkSize
if start+chunkSize > dataSize {
length = dataSize - start
}
sum, err := chunkMd5Sum(file, start, length)
if err != nil {
// Update errors metric
errorsMetric.WithLabelValues(site.LocalPath, site.Bucket, site.BucketPath, site.Name, "local").Inc()
logger.Error(err)
return ""
}
sumOfSums = append(sumOfSums, sum...)
parts++
}
if parts == 1 {
finalSum = sumOfSums
} else {
h := md5.New()
_, err := h.Write(sumOfSums)
if err != nil {
// Update errors metric
errorsMetric.WithLabelValues(site.LocalPath, site.Bucket, site.BucketPath, site.Name, "local").Inc()
logger.Error(err)
return ""
}
finalSum = h.Sum(nil)
}
sumHex := hex.EncodeToString(finalSum)
if parts > 1 {
sumHex += "-" + strconv.Itoa(parts)
}
if sumHex != checksumRemote {
logger.Infof("%s: checksums missmatch, local checksum is %s, remote - %s", filename, sumHex, checksumRemote)
return filename
}
logger.Debugf("%s: checksums matched", filename)
return ""
}
func chunkMd5Sum(file io.ReadSeeker, start int64, length int64) ([]byte, error) {
if _, err := file.Seek(start, io.SeekStart); err != nil {
return nil, err
}
h := md5.New()
if _, err := io.CopyN(h, file, length); err != nil {
return nil, err
}
return h.Sum(nil), nil
}