forked from nine-lives-later/go-xdelta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
224 lines (183 loc) · 5.54 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
package xdelta
import (
"context"
"fmt"
"io"
"runtime"
"unsafe"
"github.com/konsorten/go-xdelta/xdelta-lib"
)
type Encoder struct {
io.Closer
handle uintptr
inputFile io.Reader
sourceFile io.ReadSeeker
patchFile io.Writer
inputBuffer []byte
sourceBuffer []byte
outputBuffer []byte
}
type EncoderOptions struct {
BlockSizeKB int
FileID string
FromFile io.ReadSeeker
ToFile io.Reader
PatchFile io.Writer
Header []byte
}
func NewEncoder(options EncoderOptions) (*Encoder, error) {
// create the new encoder
var handle uintptr
err := lib.CallToError(lib.NewEncoder.Call(uintptr(unsafe.Pointer(&handle))))
if err != nil {
return nil, err
}
// initialize
if options.BlockSizeKB <= 0 {
options.BlockSizeKB = (8 * 1024) // 8 MB
}
var hasSource uintptr
if options.FromFile != nil {
hasSource = 1
}
err = lib.CallToError(lib.EncoderInit.Call(handle, uintptr(options.BlockSizeKB), lib.FromString(options.FileID), hasSource))
if err != nil {
lib.FreeEncoder.Call(uintptr(unsafe.Pointer(&handle)))
return nil, err
}
// set header
if options.Header != nil {
err = lib.CallToError(lib.EncoderSetHeader.Call(handle, uintptr(unsafe.Pointer(&options.Header[0])), uintptr(len(options.Header))))
if err != nil {
lib.FreeEncoder.Call(uintptr(unsafe.Pointer(&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)
}
// ensure shutdown
runtime.SetFinalizer(ret, freeEncoder)
return ret, nil
}
func (enc *Encoder) GetStreamError() error {
var s uintptr
err := lib.CallToError(lib.EncoderGetStreamError.Call(enc.handle, uintptr(unsafe.Pointer(&s))))
if err != nil {
return err
}
if s == 0 {
return nil
}
return fmt.Errorf("%v", lib.ToString(s, true))
}
func (enc *Encoder) Process(ctx context.Context) error {
var isFinal uintptr
for {
// retrieve the current state
var state lib.XdeltaState
err := lib.CallToError(lib.EncoderProcess.Call(enc.handle, uintptr(unsafe.Pointer(&state))))
if err != nil {
return err
}
switch state {
case lib.XdeltaState_INPUT:
// done?
if isFinal != 0 {
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 = 1
}
err = lib.CallToError(lib.EncoderProvideInputData.Call(enc.handle, uintptr(unsafe.Pointer(&enc.inputBuffer[0])), uintptr(n), isFinal))
if err != nil {
return fmt.Errorf("Failed to provide data from TO/input file: %v", err)
}
break
case lib.XdeltaState_OUTPUT:
var length int
err := lib.CallToError(lib.EncoderGetOutputRequest.Call(enc.handle, uintptr(unsafe.Pointer(&length))))
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.CallToError(lib.EncoderCopyOutputData.Call(enc.handle, uintptr(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)
}
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")
}
var blockno, blocksize int
err := lib.CallToError(lib.EncoderGetSourceRequest.Call(enc.handle, uintptr(unsafe.Pointer(&blockno)), uintptr(unsafe.Pointer(&blocksize))))
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 overflow (%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)
}
err = lib.CallToError(lib.EncoderProvideSourceData.Call(enc.handle, uintptr(unsafe.Pointer(&enc.sourceBuffer[0])), uintptr(n)))
if err != nil {
return fmt.Errorf("Failed to provide data from FROM/source file: %v", err)
}
break
case lib.XdeltaState_WINSTART:
case lib.XdeltaState_WINFINISH:
break
default:
return fmt.Errorf("Unknown state: %v", state)
}
// check if cancelled
err = ctx.Err()
if err != nil {
return err
}
}
}
func (enc *Encoder) Close() error {
return freeEncoder(enc)
}
func freeEncoder(enc *Encoder) error {
// nothing to do?
if enc == nil || enc.handle == 0 {
return nil
}
// create the new encoder
return lib.CallToError(lib.FreeEncoder.Call(uintptr(unsafe.Pointer(&enc.handle))))
}