-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.go
More file actions
301 lines (283 loc) · 7.45 KB
/
Copy pathapply.go
File metadata and controls
301 lines (283 loc) · 7.45 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
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
package cowdiff
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
)
// zeroChunk is a read-only source of zeros for hole-filling fallbacks.
var zeroChunk = make([]byte, 1<<20)
type applyConfig struct {
reflink *bool
}
// ApplyOption configures Apply.
type ApplyOption func(*applyConfig)
// WithReflink forces reflink-accelerated apply on or off (default: auto-detect).
func WithReflink(enabled bool) ApplyOption {
return func(c *applyConfig) { c.reflink = &enabled }
}
// Apply reconstructs targetPath = base + diffs applied in order (oldest first).
// It materializes and patches a temporary sibling (reflink-cloning base on a
// CoW filesystem, else copying), verifies every diff's checksum, then atomically
// renames it over targetPath. On any error the target is left untouched and the
// temporary is removed. Apply refuses to run if targetPath is the same file as
// base.
func Apply(base *os.File, diffs []io.Reader, targetPath string, opts ...ApplyOption) error {
var cfg applyConfig
for _, o := range opts {
o(&cfg)
}
if len(diffs) == 0 {
return fmt.Errorf("cowdiff: no diffs")
}
if err := checkNotSameFile(base, targetPath); err != nil {
return err
}
st, err := base.Stat()
if err != nil {
return err
}
return atomicWriteFile(targetPath, st.Mode().Perm(), func(out *os.File) error {
if err := materializeInto(out, base, st, cfg.reflink); err != nil {
return err
}
for _, d := range diffs {
if err := applyOne(d, out); err != nil {
return err
}
}
return nil
})
}
// atomicWriteFile creates a temporary sibling of path, lets fn populate it, then
// fsyncs and atomically renames it over path (and fsyncs the directory). On any
// error the temporary is removed and path is left untouched. The temporary
// keeps mode, so the final file does.
func atomicWriteFile(path string, mode os.FileMode, fn func(f *os.File) error) error {
tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".cowdiff-*")
if err != nil {
return err
}
name := tmp.Name()
committed := false
defer func() {
if !committed {
tmp.Close()
os.Remove(name)
}
}()
if err := tmp.Chmod(mode); err != nil {
return err
}
if err := fn(tmp); err != nil {
return err
}
if err := tmp.Sync(); err != nil {
return err
}
if err := tmp.Close(); err != nil {
return err
}
if err := os.Rename(name, path); err != nil {
return err
}
committed = true
syncDir(filepath.Dir(path))
return nil
}
// syncDir best-effort fsyncs a directory so a rename survives a crash. Failures
// (e.g. platforms that disallow directory fsync) are non-fatal.
func syncDir(dir string) {
if d, err := os.Open(dir); err == nil {
d.Sync()
d.Close()
}
}
// checkNotSameFile rejects a targetPath that resolves to the same file as base
// (same path, or a hard link), which would otherwise be truncated as an input.
func checkNotSameFile(base *os.File, targetPath string) error {
bi, err := base.Stat()
if err != nil {
return err
}
ti, err := os.Stat(targetPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if os.SameFile(bi, ti) {
return fmt.Errorf("cowdiff: output %q is the same file as the base input", targetPath)
}
return nil
}
// ApplyTo reconstructs base + diffs into out over the range [0, final target
// size). It is storage- and filesystem-agnostic (no reflink optimization) and
// does not truncate out; the caller sizes out to the final target size. out is
// assumed to read back as zeros where nothing is written.
func ApplyTo(base io.ReaderAt, diffs []io.Reader, out io.WriterAt) error {
parsed, err := parseAll(diffs)
if err != nil {
return err
}
if len(parsed) == 0 {
return fmt.Errorf("cowdiff: no diffs")
}
_, pieces := resolveChain(parsed)
for _, p := range pieces {
switch p.typ {
case pieceData:
if _, err := out.WriteAt(p.data, int64(p.offset)); err != nil {
return err
}
case pieceZero:
if err := zeroWriterAt(out, int64(p.offset), int64(p.length)); err != nil {
return err
}
case pieceBase:
if err := copyBaseRangeAt(base, out, int64(p.offset), int64(p.length)); err != nil {
return err
}
}
}
return nil
}
// Reconstruct writes base + diffs applied in order (oldest first) to out
// sequentially over [0, final target size). Unlike ApplyTo it needs no seekable
// sink, so it suits streaming to a pipe or stdout.
func Reconstruct(base io.ReaderAt, diffs []io.Reader, out io.Writer) error {
parsed, err := parseAll(diffs)
if err != nil {
return err
}
if len(parsed) == 0 {
return fmt.Errorf("cowdiff: no diffs")
}
return reconstruct(base, parsed, out)
}
// materializeInto fills the (empty) out file with base's contents, by reflink
// clone when possible/requested, else by copy.
func materializeInto(out, base *os.File, st os.FileInfo, wantReflink *bool) error {
if wantReflink == nil || *wantReflink {
if err := tryReflinkClone(out, base); err == nil {
return nil
} else if wantReflink != nil && *wantReflink {
return fmt.Errorf("cowdiff: reflink apply requested but clone failed: %w", err)
}
}
_, err := io.Copy(out, io.NewSectionReader(base, 0, st.Size()))
return err
}
// applyOne patches a single diff into out, streaming its data section and
// verifying its checksum.
func applyOne(r io.Reader, out *os.File) error {
h := sha256.New()
tr := io.TeeReader(r, h)
ts, _, segs, err := readHeader(tr)
if err != nil {
return err
}
if err := out.Truncate(int64(ts)); err != nil {
return err
}
buf := make([]byte, 1<<20)
for _, s := range segs {
if s.length == 0 {
continue
}
if s.typ == SegData {
if err := copyStreamAt(out, tr, int64(s.offset), int64(s.length), buf); err != nil {
return err
}
} else {
if err := zeroFileRange(out, int64(s.offset), int64(s.length)); err != nil {
return err
}
}
}
sum := make([]byte, checksumSize)
if _, err := io.ReadFull(r, sum); err != nil {
return err
}
if !bytes.Equal(sum, h.Sum(nil)) {
return errChecksum
}
return expectEOF(r)
}
func copyStreamAt(out *os.File, r io.Reader, off, n int64, buf []byte) error {
for n > 0 {
c := int64(len(buf))
if c > n {
c = n
}
if _, err := io.ReadFull(r, buf[:c]); err != nil {
return err
}
if _, err := out.WriteAt(buf[:c], off); err != nil {
return err
}
off += c
n -= c
}
return nil
}
// zeroFileRange punches a hole when the platform/filesystem supports it, else
// writes zeros.
func zeroFileRange(out *os.File, off, n int64) error {
if err := punchHole(out, off, n); err == nil {
return nil
}
return zeroWriterAt(out, off, n)
}
func zeroWriterAt(out io.WriterAt, off, n int64) error {
for n > 0 {
c := int64(len(zeroChunk))
if c > n {
c = n
}
if _, err := out.WriteAt(zeroChunk[:c], off); err != nil {
return err
}
off += c
n -= c
}
return nil
}
func copyBaseRangeAt(base io.ReaderAt, out io.WriterAt, off, n int64) error {
buf := make([]byte, 1<<20)
for n > 0 {
c := int64(len(buf))
if c > n {
c = n
}
m, err := base.ReadAt(buf[:c], off)
if m > 0 {
if _, werr := out.WriteAt(buf[:m], off); werr != nil {
return werr
}
}
if err != nil {
if err != io.EOF {
return err // a real read error must not be masked as zeros
}
return zeroWriterAt(out, off+int64(m), n-int64(m))
}
off += c
n -= c
}
return nil
}
func parseAll(diffs []io.Reader) ([]*parsedDiff, error) {
parsed := make([]*parsedDiff, 0, len(diffs))
for _, r := range diffs {
p, err := parseDiff(r)
if err != nil {
return nil, err
}
parsed = append(parsed, p)
}
return parsed, nil
}