-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathassemble.go
227 lines (211 loc) · 6.01 KB
/
assemble.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
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
package desync
import (
"context"
"crypto/sha512"
"fmt"
"os"
"sync"
"syscall"
"github.com/pkg/errors"
)
// AssembleFile re-assembles a file based on a list of index chunks. It runs n
// goroutines, creating one filehandle for the file "name" per goroutine
// and writes to the file simultaneously. If progress is provided, it'll be
// called when a chunk has been processed.
// If the input file exists and is not empty, the algorithm will first
// confirm if the data matches what is expected and only populate areas that
// differ from the expected content. This can be used to complete partly
// written files.
func AssembleFile(ctx context.Context, name string, idx Index, s Store, seeds []Seed, n int, pb ProgressBar) (*ExtractStats, error) {
type Job struct {
segment indexSegment
source SeedSegment
}
var (
wg sync.WaitGroup
mu sync.Mutex
pErr error
in = make(chan Job)
isBlank bool
)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Setup and start the progressbar if any
if pb != nil {
pb.SetTotal(len(idx.Chunks))
pb.Start()
defer pb.Finish()
}
// Initialize stats to be gathered during extraction
stats := &ExtractStats{
BytesTotal: idx.Length(),
ChunksTotal: len(idx.Chunks),
}
// Helper function to record and deal with any errors in the goroutines
recordError := func(err error) {
mu.Lock()
defer mu.Unlock()
if pErr == nil {
pErr = err
}
cancel()
}
// Determine is the target exists and create it if not
info, err := os.Stat(name)
switch {
case os.IsNotExist(err):
f, err := os.Create(name)
if err != nil {
return stats, err
}
f.Close()
isBlank = true
case info.Size() == 0:
isBlank = true
}
// Truncate the output file to the full expected size. Not only does this
// confirm there's enough disk space, but it allows for an optimization
// when dealing with the Null Chunk
if err := os.Truncate(name, idx.Length()); err != nil {
return stats, err
}
// Determine the blocksize of the target file which is required for reflinking
blocksize := blocksizeOfFile(name)
// Prepend a nullchunk seed to the list of seeds to make sure we read that
// before any large null sections in other seed files
ns, err := newNullChunkSeed(name, blocksize, idx.Index.ChunkSizeMax)
if err != nil {
return stats, err
}
defer ns.close()
// Start a self-seed which will become usable once chunks are written contigously
// beginning at position 0.
ss, err := newSelfSeed(name, idx)
if err != nil {
return stats, err
}
seeds = append([]Seed{ns, ss}, seeds...)
// Record the total number of seeds and blocksize in the stats
stats.Seeds = len(seeds)
stats.Blocksize = blocksize
// Start the workers, each having its own filehandle to write concurrently
for i := 0; i < n; i++ {
wg.Add(1)
f, err := os.OpenFile(name, os.O_RDWR, 0666)
if err != nil {
return stats, fmt.Errorf("unable to open file %s, %s", name, err)
}
defer f.Close()
go func() {
for job := range in {
if pb != nil {
pb.Add(job.segment.lengthChunks())
}
if job.source != nil {
stats.addChunksFromSeed(uint64(job.segment.lengthChunks()))
offset := job.segment.start()
length := job.segment.lengthBytes()
copied, cloned, err := job.source.WriteInto(f, offset, length, blocksize, isBlank)
if err != nil {
recordError(err)
continue
}
stats.addBytesCopied(copied)
stats.addBytesCloned(cloned)
// Record this segment's been written in the self-seed to make it
// available going forward
ss.add(job.segment)
continue
}
c := job.segment.chunks()[0]
// If we operate on an existing file there's a good chance we already
// have the data written for this chunk. Let's read it from disk and
// compare to what is expected.
if !isBlank {
b := make([]byte, c.Size)
if _, err := f.ReadAt(b, int64(c.Start)); err != nil {
recordError(err)
continue
}
sum := sha512.Sum512_256(b)
if sum == c.ID {
// Record this chunk's been written in the self-seed
ss.add(job.segment)
// Record we kept this chunk in the file (when using in-place extract)
stats.incChunksInPlace()
continue
}
}
// Record this chunk having been pulled from the store
stats.incChunksFromStore()
// Pull the (compressed) chunk from the store
b, err := s.GetChunk(c.ID)
if err != nil {
recordError(err)
continue
}
// Since we know how big the chunk is supposed to be, pre-allocate a
// slice to decompress into
var db []byte
db = make([]byte, c.Size)
// The the chunk is compressed. Decompress it here
db, err = Decompress(db, b)
if err != nil {
recordError(errors.Wrap(err, c.ID.String()))
continue
}
// Verify the checksum of the chunk matches the ID
sum := sha512.Sum512_256(db)
if sum != c.ID {
recordError(fmt.Errorf("unexpected sha512/256 %s for chunk id %s", sum, c.ID))
continue
}
// Might as well verify the chunk size while we're at it
if c.Size != uint64(len(db)) {
recordError(fmt.Errorf("unexpected size for chunk %s", c.ID))
continue
}
// Write the decompressed chunk into the file at the right position
if _, err = f.WriteAt(db, int64(c.Start)); err != nil {
recordError(err)
continue
}
// Record this chunk's been written in the self-seed
ss.add(job.segment)
}
wg.Done()
}()
}
// Let the sequencer break up the index into segments, feed the workers, and
// stop if there are any errors
seq := NewSeedSequencer(idx, seeds...)
loop:
for {
// See if we're meant to stop
select {
case <-ctx.Done():
break loop
default:
}
chunks, from, done := seq.Next()
in <- Job{chunks, from}
if done {
break
}
}
close(in)
wg.Wait()
return stats, pErr
}
func blocksizeOfFile(name string) uint64 {
stat, err := os.Stat(name)
if err != nil {
return DefaultBlockSize
}
switch sys := stat.Sys().(type) {
case *syscall.Stat_t:
return uint64(sys.Blksize)
default:
return DefaultBlockSize
}
}