aboutsummaryrefslogtreecommitdiff
path: root/librsync/librsync.go
blob: e154da6d953f9bf44e9541e608acbad866379125 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Package librsync allows you to create binary deltas.
package librsync

/*
#cgo LDFLAGS: -lrsync
#include <stdio.h>
#include <librsync.h>
#include <stdlib.h>

rs_buffers_t* new_rs_buffers() {
	return (rs_buffers_t*) malloc(sizeof(rs_buffers_t));
}
*/
import "C"

import (
	"errors"
	"fmt"
	"io"
	"unsafe"
)

const (
	inbufSize  = 16 * 1024
	outbufSize = 16 * 1024
)

var (
	ErrInputEnded = errors.New("Input ended (possibly unexpected)")
	ErrBadMagic   = errors.New("Bad magic number. Probably not an librsync file.")
	ErrCorrupt    = errors.New("Input stream corrupted")
	ErrInternal   = errors.New("Internal error (library bug?)")
)

// Job holds information about a running librsync operation. The output can be accessed with the Read method.
type Job struct {
	rsbufs *C.rs_buffers_t
	job    *C.rs_job_t

	running bool
	err     error

	inbuf []byte
	in    io.Reader

	outbufTotal []byte
	outbuf      []byte
}

func newJob(input io.Reader) (job *Job, err error) {
	job = new(Job)

	job.in = input
	job.inbuf = make([]byte, inbufSize)
	job.outbufTotal = make([]byte, outbufSize)

	job.rsbufs = C.new_rs_buffers()
	if job.rsbufs == nil {
		return nil, errors.New("Could not allocate memory for rs_buffers_t object")
	}

	job.rsbufs.eof_in = 0
	job.rsbufs.avail_in = 0

	job.running = true

	return
}

// NewDefaultSignatureGen is like NewSignatureGen, but uses default values for blocklen and stronglen.
func NewDefaultSignatureGen(input io.Reader) (job *Job, err error) {
	job, err = NewSignatureGen(C.RS_DEFAULT_BLOCK_LEN, C.RS_DEFAULT_STRONG_LEN, input)
	return
}

// NewSignatureGen creates a signature generation job.
// 
// blocklen is the length of a block.
// stronglen is the length of the stong hash.
// input is an io.Reader that provides the input data.
func NewSignatureGen(blocklen, stronglen uint, input io.Reader) (job *Job, err error) {
	job, err = newJob(input)
	if err != nil {
		return
	}

	job.job = C.rs_sig_begin(C.size_t(blocklen), C.size_t(stronglen))
	if job.job == nil {
		job.Close()
		return nil, errors.New("rs_sig_begin failed")
	}

	return
}

// Close will free memory that Go's garbage collector would not be able to free.
func (job *Job) Close() error {
	if job.rsbufs != nil {
		C.free(unsafe.Pointer(job.rsbufs))
		job.rsbufs = nil
	}

	if job.job != nil {
		C.rs_job_free(job.job)
		job.job = nil
	}

	return nil
}

// For errors in callbacks
type jobInternalPanic struct {
	err error
}

func (jp jobInternalPanic) Error() string { return jp.err.Error() }

func jobIter(job *C.rs_job_t, rsbufs *C.rs_buffers_t) (running bool, err error) {
	defer func() {
		r := recover()
		jp, ok := r.(jobInternalPanic)
		if !ok {
			panic(r)
		}

		running = false
		err = jp.err
	}()

	switch res := C.rs_job_iter(job, rsbufs); res {
	case C.RS_DONE:
	case C.RS_BLOCKED:
		running = true
	case C.RS_INPUT_ENDED:
		err = ErrInputEnded
	case C.RS_BAD_MAGIC:
		err = ErrBadMagic
	case C.RS_CORRUPT:
		err = ErrCorrupt
	case C.RS_INTERNAL_ERROR:
		err = ErrInternal
	default:
		err = fmt.Errorf("Unexpected result from library: %d", res)
	}
	return
}

// Read reads len(p) or less bytes of the generated output.
func (job *Job) Read(p []byte) (readN int, outerr error) {
	if len(job.outbuf) > 0 {
		if len(job.outbuf) > len(p) {
			readN = len(p)
		} else {
			readN = len(job.outbuf)
		}

		copy(p[:readN], job.outbuf[:readN])
		p = p[:readN]
		job.outbuf = job.outbuf[readN:]
		return
	}

	if !job.running {
		if job.err != nil {
			return 0, job.err
		}

		return 0, io.EOF
	}

	// Fill input buffer
	if (job.rsbufs.avail_in == 0) && (job.rsbufs.eof_in == 0) {
		n, err := job.in.Read(job.inbuf[0:inbufSize])

		switch err {
		case nil:
		case io.EOF:
			job.rsbufs.eof_in = 1
		default:
			outerr = err
			job.err = err
			job.running = false
			return
		}

		job.rsbufs.next_in = (*C.char)(unsafe.Pointer(&(job.inbuf[0])))
		job.rsbufs.avail_in = C.size_t(n)
	}

	job.outbuf = job.outbufTotal
	job.rsbufs.next_out = (*C.char)(unsafe.Pointer(&(job.outbuf[0])))
	job.rsbufs.avail_out = C.size_t(len(job.outbuf))

	var err error
	job.running, err = jobIter(job.job, job.rsbufs)

	outN := int(uintptr(unsafe.Pointer(job.rsbufs.next_out)) - uintptr(unsafe.Pointer(&(job.outbuf[0]))))
	job.outbuf = job.outbuf[:outN]

	if err != nil {
		return outN, err
	}
	return
}

// Signature is an in-memory representation of a signature.
type Signature struct {
	sig *C.rs_signature_t
}

// Close will free memory that Go's garbage collector would not be able to free.
func (s Signature) Close() error {
	if s.sig != nil {
		C.rs_free_sumset(s.sig)
		s.sig = nil
	}
	return nil
}

// LoadSignature loads a signature to memory.
func LoadSignature(input io.Reader) (sig Signature, err error) {
	job, err := newJob(input)
	if err != nil {
		return
	}
	defer job.Close()

	job.job = C.rs_loadsig_begin(&(sig.sig))
	if job.job == nil {
		err = errors.New("rs_loadsig_begin failed")
		return
	}

	if _, err = io.Copy(&nirvana{}, job); err != nil {
		return
	}

	rsret := C.rs_build_hash_table(sig.sig)
	if rsret != C.RS_DONE {
		err = fmt.Errorf("rs_build_hash_table returned %d", rsret)
	}

	return
}

// NewDeltaGen creates a delta generation job.
// 
// sig is the signature loaded by LoadSignature.
// input is a reades that provides the new, modified data.
func NewDeltaGen(sig Signature, input io.Reader) (job *Job, err error) {
	job, err = newJob(input)
	if err != nil {
		return
	}

	job.job = C.rs_delta_begin(sig.sig)
	if job.job == nil {
		job.Close()
		return nil, errors.New("rs_delta_begin failed")
	}

	return
}

// Patcher is a job with additional hidden data for patching.
// 
// IMPORTANT: You still need to Close() this!
type Patcher struct {
	*Job
	base io.ReaderAt
	buf  []byte
}

func _patch_callback(_patcher unsafe.Pointer, pos C.rs_long_t, len *C.size_t, _buf *unsafe.Pointer) C.rs_result {
	patcher := (*Patcher)(_patcher)

	patcher.buf = make([]byte, int(*len))
	n, err := patcher.base.ReadAt(patcher.buf, int64(pos))
	if n < int(*len) {
		if err != io.EOF {
			panic(jobInternalPanic{err})
		} else {
			return C.RS_INPUT_ENDED
		}
	}
	*len = C.size_t(n)
	*_buf = unsafe.Pointer(&(patcher.buf[0]))

	return C.RS_DONE
}

var patch_callback = _patch_callback // So we can use the `&` operator in NewPatcher

func NewPatcher(delta io.Reader, base io.ReaderAt) (job *Patcher, err error) {
	_job, e := newJob(delta)
	if e != nil {
		err = e
		return
	}

	job = &Patcher{
		Job:  _job,
		base: base}

	job.job = C.rs_patch_begin((*C.rs_copy_cb)(unsafe.Pointer(&patch_callback)), unsafe.Pointer(job))
	if job.job == nil {
		job.Close()
		return nil, errors.New("rs_patch_begin failed")
	}

	return
}