-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
183 lines (156 loc) · 5.21 KB
/
main.go
File metadata and controls
183 lines (156 loc) · 5.21 KB
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
package main
import (
"os"
"flag"
"fmt"
"path/filepath"
"io/fs"
"io"
"crypto/md5"
"encoding/hex"
"errors"
"strings"
)
func copyFile(src, dest string) error {
in, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open input file at %q; %w", src, err)
}
defer in.Close()
out, err := os.OpenFile(dest, os.O_CREATE | os.O_TRUNC | os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open output file at %q; %w", dest, err)
}
is_closed := false
defer func() {
// Don't unconditionally close it, because we need to check
// whether the close (and thus sync) was successful.
if !is_closed {
out.Close()
}
}()
_, err = io.Copy(out, in)
if err != nil {
return fmt.Errorf("failed to copy %q to %q; %w", src, dest, err)
}
err = out.Close()
is_closed = true
if err != nil {
return fmt.Errorf("failed to close output file at %q; %w", dest, err)
}
instat, err := os.Stat(src)
if err != nil {
return fmt.Errorf("failed to stat %q; %w", dest, err)
}
mtime := instat.ModTime()
err = os.Chtimes(dest, mtime, mtime)
if err != nil {
return fmt.Errorf("failed to reset modtimes for %q; %w", dest, err)
}
return nil
}
func computeChecksum(path string) (string, error) {
in, err_ := os.Open(path)
if err_ != nil {
return "", fmt.Errorf("failed to open '" + path + "'; %w", err_)
}
defer in.Close()
h := md5.New()
_, err := io.Copy(h, in)
if err != nil {
return "", fmt.Errorf("failed to hash '" + path + "'; %w", err_)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func main() {
registry := flag.String("registry", "", "Path to the registry directory (required)")
backup := flag.String("backup", "", "Writeable path to a backup directory (required)")
link := flag.String("link", "", "Path to a backup directory as a read-only link target (default '-backup')")
flag.Parse()
if *registry == "" || *backup == "" {
flag.Usage()
os.Exit(1)
}
if (*link == "") {
link = backup
}
all_errors := []error{}
// We scan the registry, identifying non-symlink files to be backed up.
filepath.WalkDir(*registry, func(path string, d fs.DirEntry, err error) error {
if err != nil {
all_errors = append(all_errors, fmt.Errorf("failed to walk into %q; %w", path, err))
return nil
}
rel, err := filepath.Rel(path, *registry)
if err != nil {
all_errors = append(all_errors, fmt.Errorf("failed to convert %q into a relative path; %w", path, err))
return nil
}
if filepath.IsLocal(rel) {
all_errors = append(all_errors, fmt.Errorf("failed to convert %q into a relative path", path))
return nil
}
final := filepath.Join(*backup, rel)
if d.IsDir() {
err := os.Mkdir(final, 0755)
if err != nil {
all_errors = append(all_errors, fmt.Errorf("failed to create directory %q; %w", final, err))
return fs.SkipDir
}
return nil
}
info, err := d.Info()
if err != nil {
all_errors = append(all_errors, fmt.Errorf("failed to stat %q; %w", path, err))
return nil
}
if info.Mode() & os.ModeSymlink != 0 { // Skipping symlinks.
return nil
}
finfo, err := os.Stat(final)
if err == nil {
if info.ModTime().Equal(finfo.ModTime()) {
return nil
}
} else if !errors.Is(err, os.ErrNotExist) {
all_errors = append(all_errors, fmt.Errorf("failed to stat %q; %w", final, err))
return nil
}
err = copyFile(path, final)
if err != nil {
all_errors = append(all_errors, err)
return nil
}
original_md5, err := computeChecksum(path)
if err != nil {
all_errors = append(all_errors, err)
return nil
}
final_md5, err := computeChecksum(final)
if err != nil {
all_errors = append(all_errors, err)
return nil
}
if original_md5 != final_md5 {
all_errors = append(all_errors, fmt.Errorf("mismatch in MD5 checksums after copying %q to %q", path, final))
return nil
}
// Only replacing actual data files with symlinks, as the gobbler may need to modify its internal files.
if !strings.HasPrefix(filepath.Base(rel), ".") {
err = os.Remove(path)
if err != nil {
all_errors = append(all_errors, fmt.Errorf("failed to remove backed-up file at %q; %w", path, err))
return nil
}
err = os.Symlink(filepath.Join(*link, rel), path)
if err != nil {
all_errors = append(all_errors, fmt.Errorf("failed to replace backed-up file at %q with a symlink; %w", path, err))
return nil
}
}
return nil
})
for _, err := range all_errors {
fmt.Println(err.Error())
}
}