MCPcopy
hub / github.com/kopia/kopia / copyBuffer

Function copyBuffer

internal/sparsefile/sparsefile.go:23–67  ·  view source on GitHub ↗

Copy copies bits from src to dst, seeking past blocks of zero bits in src. These blocks are omitted, creating a file with holes in dst.

(dst io.WriteSeeker, src io.Reader, buf []byte)

Source from the content-addressed store, hash-verified

21// Copy copies bits from src to dst, seeking past blocks of zero bits in src. These
22// blocks are omitted, creating a file with holes in dst.
23func copyBuffer(dst io.WriteSeeker, src io.Reader, buf []byte) (written int64, err error) {
24 for {
25 nr, er := src.Read(buf)
26 if nr > 0 { //nolint:nestif
27 // If non-zero data is read, write it. Otherwise, skip forwards.
28 if isAllZero(buf) {
29 dst.Seek(int64(nr), io.SeekCurrent) //nolint:errcheck
30 written += int64(nr)
31
32 continue
33 }
34
35 nw, ew := dst.Write(buf[0:nr])
36 if nw < 0 || nr < nw {
37 nw = 0
38
39 if ew == nil {
40 ew = errors.New("invalid write result")
41 }
42 }
43
44 written += int64(nw)
45
46 if ew != nil {
47 err = ew
48 break
49 }
50
51 if nr != nw {
52 err = io.ErrShortWrite
53 break
54 }
55 }
56
57 if er != nil {
58 if er != io.EOF {
59 err = er
60 }
61
62 break
63 }
64 }
65
66 return written, err
67}
68
69func isAllZero(buf []byte) bool {
70 for _, b := range buf {

Callers 1

CopyFunction · 0.85

Calls 4

isAllZeroFunction · 0.85
ReadMethod · 0.45
SeekMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected