-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathencoder.go
241 lines (196 loc) · 5.33 KB
/
encoder.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package xdelta
import (
"context"
"fmt"
"io"
"runtime"
"time"
"unsafe"
"github.com/nine-lives-later/go-xdelta/xdelta-lib"
)
type Encoder struct {
io.Closer
handle unsafe.Pointer
inputFile io.Reader
sourceFile io.ReadSeeker
patchFile io.Writer
stats *Stats
inputBuffer []byte
sourceBuffer []byte
outputBuffer []byte
}
type EncoderOptions struct {
BlockSizeKB int
FileID string
FromFile io.ReadSeeker
ToFile io.Reader
PatchFile io.Writer
Header []byte
EnableStats bool
}
func NewEncoder(options EncoderOptions) (*Encoder, error) {
// create the new encoder
handle, err := lib.NewEncoder()
if err != nil {
return nil, err
}
// initialize
if options.BlockSizeKB <= 0 {
options.BlockSizeKB = (8 * 1024) // 8 MB
}
err = lib.EncoderInit(handle, options.BlockSizeKB, options.FileID, options.FromFile != nil)
if err != nil {
lib.FreeEncoder(handle)
return nil, err
}
// set header
if options.Header != nil {
err = lib.EncoderSetHeader(handle, unsafe.Pointer(&options.Header[0]), len(options.Header))
if err != nil {
lib.FreeEncoder(handle)
return nil, err
}
}
// setup encoder object
ret := &Encoder{
handle: handle,
inputFile: options.ToFile,
sourceFile: options.FromFile,
patchFile: options.PatchFile,
inputBuffer: make([]byte, options.BlockSizeKB*1024),
outputBuffer: make([]byte, options.BlockSizeKB*1024),
}
if options.FromFile != nil {
ret.sourceBuffer = make([]byte, options.BlockSizeKB*1024)
}
if options.EnableStats {
ret.stats = newStats()
}
// ensure shutdown
runtime.SetFinalizer(ret, freeEncoder)
return ret, nil
}
func (enc *Encoder) GetStreamError() error {
return lib.EncoderGetStreamError(enc.handle)
}
func (enc *Encoder) DumpStatsToStdout() {
if enc.stats != nil {
enc.stats.DumpToStdout()
}
}
func (enc *Encoder) Process(ctx context.Context) error {
var isFinal bool
var perfStart time.Time
for {
// prepare gathering stats
if enc.stats != nil {
perfStart = time.Now()
}
// retrieve the current state
state, err := lib.EncoderProcess(enc.handle)
if err != nil {
return err
}
switch state {
case lib.XdeltaState_INPUT:
// done?
if isFinal {
return nil
}
// try read input data
n, err := enc.inputFile.Read(enc.inputBuffer)
if err != nil && err != io.EOF {
return fmt.Errorf("Failed to read from TO/input file: %v", err)
}
if n <= 0 { // no more data?
isFinal = true
}
if enc.stats != nil {
enc.stats.addDataBytes(state, n)
}
err = lib.EncoderProvideInputData(enc.handle, unsafe.Pointer(&enc.inputBuffer[0]), n, isFinal)
if err != nil {
return fmt.Errorf("Failed to provide data from TO/input file: %v", err)
}
break
case lib.XdeltaState_OUTPUT:
length, err := lib.EncoderGetOutputRequest(enc.handle)
if err != nil {
return fmt.Errorf("Failed to request data for PATCH/output file: %v", err)
}
if length <= 0 { // nothing to write?
break
}
if length > len(enc.outputBuffer) {
return fmt.Errorf("Failed to consume data for PATCH/output file: output buffer overflow")
}
err = lib.EncoderCopyOutputData(enc.handle, unsafe.Pointer(&enc.outputBuffer[0]))
if err != nil {
return fmt.Errorf("Failed to consume data for PATCH/output file: %v", err)
}
written, err := enc.patchFile.Write(enc.outputBuffer[:length])
if err != nil {
return fmt.Errorf("Failed to write data to PATCH/output file: %v", err)
}
if written < length {
return fmt.Errorf("Failed to write data to PATCH/output file: not enough data written (%v < %v)", written, length)
}
if enc.stats != nil {
enc.stats.addDataBytes(state, written)
}
break
case lib.XdeltaState_GETSRCBLK:
if enc.sourceFile == nil || enc.sourceBuffer == nil {
return fmt.Errorf("Failed to request data for FROM/source file: not available")
}
blockno, blocksize, err := lib.EncoderGetSourceRequest(enc.handle)
if err != nil {
return fmt.Errorf("Failed to request data for FROM/source file: %v", err)
}
if blocksize != len(enc.sourceBuffer) {
return fmt.Errorf("Failed to request data for FROM/source file: source buffer does not match block size (%v != %v)", blocksize, len(enc.sourceBuffer))
}
_, err = enc.sourceFile.Seek(int64(blockno)*int64(blocksize), io.SeekStart)
if err != nil {
return fmt.Errorf("Failed to seek FROM/source file: %v", err)
}
n, err := enc.sourceFile.Read(enc.sourceBuffer)
if err != nil {
return fmt.Errorf("Failed to read from FROM/source file: %v", err)
}
if enc.stats != nil {
enc.stats.addDataBytes(state, n)
}
err = lib.EncoderProvideSourceData(enc.handle, unsafe.Pointer(&enc.sourceBuffer[0]), n)
if err != nil {
return fmt.Errorf("Failed to provide data from FROM/source file: %v", err)
}
break
case lib.XdeltaState_WINSTART, lib.XdeltaState_WINFINISH:
break
default:
return fmt.Errorf("Unknown state: %v", state)
}
// measure time
if enc.stats != nil {
enc.stats.addStateTime(state, time.Since(perfStart))
}
// check if cancelled
err = ctx.Err()
if err != nil {
return err
}
}
}
func (enc *Encoder) Close() error {
return freeEncoder(enc)
}
func freeEncoder(enc *Encoder) (err error) {
// nothing to do?
if enc == nil || enc.handle == nil {
return
}
err = lib.FreeEncoder(enc.handle)
enc.handle = nil
return
}