| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
) |
| 10 |
|
| 11 |
type nullChunkSeed struct { |
| 12 |
id ChunkID |
| 13 |
blockfile *os.File |
| 14 |
canReflink bool |
| 15 |
} |
| 16 |
|
| 17 |
func newNullChunkSeed(dstFile string, blocksize uint64, max uint64) (*nullChunkSeed, error) { |
| 18 |
blockfile, err := os.CreateTemp(filepath.Dir(dstFile), ".tmp-block") |
| 19 |
if err != nil { |
| 20 |
return nil, err |
| 21 |
} |
| 22 |
var canReflink bool |
| 23 |
if CanClone(dstFile, blockfile.Name()) { |
| 24 |
canReflink = true |
| 25 |
b := make([]byte, blocksize) |
| 26 |
if _, err := blockfile.Write(b); err != nil { |
| 27 |
return nil, err |
| 28 |
} |
| 29 |
} |
| 30 |
return &nullChunkSeed{ |
| 31 |
id: NewNullChunk(max).ID, |
| 32 |
canReflink: canReflink, |
| 33 |
blockfile: blockfile, |
| 34 |
}, nil |
| 35 |
} |
| 36 |
|
| 37 |
func (s *nullChunkSeed) close() error { |
| 38 |
if s.blockfile != nil { |
| 39 |
s.blockfile.Close() |
| 40 |
return os.Remove(s.blockfile.Name()) |
| 41 |
} |
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
func (s *nullChunkSeed) LongestMatchWith(chunks []IndexChunk) (int, SeedSegment) { |
| 46 |
if len(chunks) == 0 { |
| 47 |
return 0, nil |
| 48 |
} |
| 49 |
var ( |
| 50 |
n int |
| 51 |
limit int |
| 52 |
) |
| 53 |
if !s.canReflink { |
| 54 |
limit = 100 |
| 55 |
} |
| 56 |
for _, c := range chunks { |
| 57 |
if limit != 0 && limit == n { |
| 58 |
break |
| 59 |
} |
| 60 |
if c.ID != s.id { |
| 61 |
break |
| 62 |
} |
| 63 |
n++ |
| 64 |
} |
| 65 |
if n == 0 { |
| 66 |
return 0, nil |
| 67 |
} |
| 68 |
return n, &nullChunkSection{ |
| 69 |
from: chunks[0].Start, |
| 70 |
to: chunks[n-1].Start + chunks[n-1].Size, |
| 71 |
blockfile: s.blockfile, |
| 72 |
canReflink: s.canReflink, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func (s *nullChunkSeed) RegenerateIndex(ctx context.Context, n int, attempt int, seedNumber int) error { |
| 77 |
panic("A nullseed can't be regenerated") |
| 78 |
} |
| 79 |
|
| 80 |
func (s *nullChunkSeed) SetInvalid(value bool) { |
| 81 |
panic("A nullseed is never expected to be invalid") |
| 82 |
} |
| 83 |
|
| 84 |
func (s *nullChunkSeed) IsInvalid() bool { |
| 85 |
// A nullseed is never expected to be invalid |
| 86 |
return false |
| 87 |
} |
| 88 |
|
| 89 |
type nullChunkSection struct { |
| 90 |
from, to uint64 |
| 91 |
blockfile *os.File |
| 92 |
canReflink bool |
| 93 |
} |
| 94 |
|
| 95 |
func (s *nullChunkSection) Validate(file *os.File) error { |
| 96 |
// We always assume a nullseed to be valid |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
func (s *nullChunkSection) FileName() string { |
| 101 |
return "" |
| 102 |
} |
| 103 |
|
| 104 |
func (s *nullChunkSection) Size() uint64 { return s.to - s.from } |
| 105 |
|
| 106 |
func (s *nullChunkSection) WriteInto(dst *os.File, offset, length, blocksize uint64, isBlank bool) (uint64, uint64, error) { |
| 107 |
if length != s.Size() { |
| 108 |
return 0, 0, fmt.Errorf("unable to copy %d bytes to %s : wrong size", length, dst.Name()) |
| 109 |
} |
| 110 |
|
| 111 |
// When cloning isn't available we'd normally have to copy the 0 bytes into |
| 112 |
// the target range. But if that's already blank (because it's a new/truncated |
| 113 |
// file) there's no need to copy 0 bytes. |
| 114 |
if !s.canReflink { |
| 115 |
if isBlank { |
| 116 |
return 0, 0, nil |
| 117 |
} |
| 118 |
return s.copy(dst, offset, s.Size()) |
| 119 |
} |
| 120 |
return s.clone(dst, offset, length, blocksize, isBlank) |
| 121 |
} |
| 122 |
|
| 123 |
func (s *nullChunkSection) copy(dst *os.File, offset, length uint64) (uint64, uint64, error) { |
| 124 |
if _, err := dst.Seek(int64(offset), io.SeekStart); err != nil { |
| 125 |
return 0, 0, err |
| 126 |
} |
| 127 |
// Copy using a fixed buffer. Using io.Copy() with a LimitReader will make it |
| 128 |
// create a buffer matching N of the LimitReader which can be too large |
| 129 |
copied, err := io.CopyBuffer(dst, io.LimitReader(nullReader{}, int64(length)), make([]byte, 64*1024)) |
| 130 |
return uint64(copied), 0, err |
| 131 |
} |
| 132 |
|
| 133 |
func (s *nullChunkSection) clone(dst *os.File, offset, length, blocksize uint64, isBlank bool) (uint64, uint64, error) { |
| 134 |
dstAlignStart := (offset/blocksize + 1) * blocksize |
| 135 |
dstAlignEnd := (offset + length) / blocksize * blocksize |
| 136 |
|
| 137 |
// If the range is too small to contain a full aligned block, there is |
| 138 |
// nothing that can be cloned, and the copies below would write outside |
| 139 |
// the range. Write zeros over the whole range instead, or nothing if |
| 140 |
// it's still blank. |
| 141 |
if dstAlignEnd <= dstAlignStart { |
| 142 |
if isBlank { |
| 143 |
return 0, 0, nil |
| 144 |
} |
| 145 |
return s.copy(dst, offset, length) |
| 146 |
} |
| 147 |
|
| 148 |
// fill the area before the first aligned block |
| 149 |
var copied, cloned uint64 |
| 150 |
c1, _, err := s.copy(dst, offset, dstAlignStart-offset) |
| 151 |
if err != nil { |
| 152 |
return c1, 0, err |
| 153 |
} |
| 154 |
copied += c1 |
| 155 |
// fill the area after the last aligned block |
| 156 |
c2, _, err := s.copy(dst, dstAlignEnd, offset+length-dstAlignEnd) |
| 157 |
if err != nil { |
| 158 |
return copied + c2, 0, err |
| 159 |
} |
| 160 |
copied += c2 |
| 161 |
|
| 162 |
for blkOffset := dstAlignStart; blkOffset < dstAlignEnd; blkOffset += blocksize { |
| 163 |
if err := cloneRange(dst, s.blockfile, 0, blocksize, blkOffset); err != nil { |
| 164 |
// Not every filesystem that passes the CanClone probe can clone |
| 165 |
// every range. ZFS for example refuses to clone from the blockfile |
| 166 |
// before it has been committed to disk. Fall back to writing zeros, |
| 167 |
// or to doing nothing if the target range is still blank. |
| 168 |
if isBlank { |
| 169 |
return copied, cloned, nil |
| 170 |
} |
| 171 |
c3, _, err := s.copy(dst, blkOffset, dstAlignEnd-blkOffset) |
| 172 |
return copied + c3, cloned, err |
| 173 |
} |
| 174 |
cloned += blocksize |
| 175 |
} |
| 176 |
return copied, cloned, nil |
| 177 |
} |
| 178 |
|
| 179 |
type nullReader struct{} |
| 180 |
|
| 181 |
func (r nullReader) Read(b []byte) (n int, err error) { |
| 182 |
for i := range b { |
| 183 |
b[i] = 0 |
| 184 |
} |
| 185 |
return len(b), nil |
| 186 |
} |