aboutsummaryrefslogtreecommitdiff
path: root/librsync
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2016-04-30 23:41:03 +0200
committerLaria Carolin Chabowski <laria@laria.me>2016-04-30 23:41:03 +0200
commit4d3dc3d585a7232477d8fcf717113217578afa17 (patch)
treef38824251e6eb4ff64919ea89bcb1729ed49656f /librsync
parent3a9208ec940a902e1c00580dc17ca422f3e4f0c3 (diff)
parent181d4286ad8dbca39bb4d43475b31ab9b3c626ee (diff)
downloadgolibrsync-4d3dc3d585a7232477d8fcf717113217578afa17.tar.gz
golibrsync-4d3dc3d585a7232477d8fcf717113217578afa17.tar.bz2
golibrsync-4d3dc3d585a7232477d8fcf717113217578afa17.zip
Merge branch 'fix-newer-versions'
Diffstat (limited to 'librsync')
-rw-r--r--librsync/librsync.go74
-rw-r--r--librsync/librsync_callback.go4
-rw-r--r--librsync/librsync_test.go9
-rw-r--r--librsync/pointers.go51
-rw-r--r--librsync/testdata/random_data.sig.go30
-rw-r--r--librsync/testdata/random_data.sig2bin0 -> 156 bytes
6 files changed, 150 insertions, 18 deletions
diff --git a/librsync/librsync.go b/librsync/librsync.go
index a3a4e87..1ffc7ec 100644
--- a/librsync/librsync.go
+++ b/librsync/librsync.go
@@ -6,15 +6,39 @@ package librsync
#include <stdio.h>
#include <librsync.h>
#include <stdlib.h>
+#include <stdbool.h>
static inline rs_buffers_t* new_rs_buffers() {
return (rs_buffers_t*) malloc(sizeof(rs_buffers_t));
}
+rs_result patchCallbackGo(void *_patcher, rs_long_t pos, size_t *len, void *_buf);
+
rs_result patchCallback(void* _patcher, rs_long_t pos, size_t* len, void** _buf) {
return patchCallbackGo(_patcher, pos, len, _buf);
}
+#ifndef RS_DEFAULT_STRONG_LEN
+// librsync >= 1.0.0, 0 is the full size (32 bytes)
+#define DEFAULT_STRONG_LEN 0
+#else
+// librsync < 1.0.0, using md4 (8 bytes)
+#define DEFAULT_STRONG_LEN RS_DEFAULT_STRONG_LEN
+#endif
+
+static inline rs_job_t* sig_begin(size_t new_block_len, size_t strong_sum_len, bool compat) {
+#ifndef RS_DEFAULT_STRONG_LEN
+ // librsync >= 1.0.0, supporting the newer hash function (blake2b)
+ if (compat) {
+ return rs_sig_begin(new_block_len, strong_sum_len, RS_MD4_SIG_MAGIC);
+ }
+ return rs_sig_begin(new_block_len, strong_sum_len, RS_BLAKE2_SIG_MAGIC);
+#else
+ // not supporting the newer hash function, fall back to the md4 hash
+ return rs_sig_begin(new_block_len, strong_sum_len);
+#endif
+}
+
*/
import "C"
@@ -30,6 +54,11 @@ const (
outbufSize = 16 * 1024
)
+const (
+ DefaultBlockLen = C.RS_DEFAULT_BLOCK_LEN
+ DefaultStrongLen = C.DEFAULT_STRONG_LEN
+)
+
var (
ErrInputEnded = errors.New("Input ended (possibly unexpected)")
ErrBadMagic = errors.New("Bad magic number. Probably not an librsync file.")
@@ -72,24 +101,43 @@ func newJob(input io.Reader) (job *Job, err error) {
return
}
-// NewDefaultSignatureGen is like NewSignatureGen, but uses default values for blocklen and stronglen.
+// Config sets parameters for NewSignatureGen. May be the zero value for default
+// values.
+type Config struct {
+ BlockLen uint // length of a block, e.g. 2048
+ StrongLen uint // length of a strong hash, e.g. 32 or 0
+ CompatMD4 bool // enable for compatibility with librsync < 1.0.0
+}
+
+func (c *Config) setup() {
+ if c.BlockLen == 0 {
+ c.BlockLen = DefaultBlockLen
+ }
+ if c.StrongLen == 0 {
+ c.StrongLen = DefaultStrongLen
+ }
+}
+
+// NewDefaultSignatureGen is like NewSignatureGen, but uses the default
+// configuration.
func NewDefaultSignatureGen(basis io.Reader) (job *Job, err error) {
- job, err = NewSignatureGen(C.RS_DEFAULT_BLOCK_LEN, C.RS_DEFAULT_STRONG_LEN, basis)
+ job, err = NewSignatureGen(Config{}, basis)
return
}
// NewSignatureGen creates a signature generation job.
//
-// blocklen is the length of a block.
-// stronglen is the length of the stong hash.
+// config is a Config object for more options.
// basis is an io.Reader that provides data of the basis file.
-func NewSignatureGen(blocklen, stronglen uint, basis io.Reader) (job *Job, err error) {
+func NewSignatureGen(config Config, basis io.Reader) (job *Job, err error) {
job, err = newJob(basis)
if err != nil {
return
}
- job.job = C.rs_sig_begin(C.size_t(blocklen), C.size_t(stronglen))
+ config.setup()
+
+ job.job = C.sig_begin(C.size_t(config.BlockLen), C.size_t(config.StrongLen), C.bool(config.CompatMD4))
if job.job == nil {
job.Close()
return nil, errors.New("rs_sig_begin failed")
@@ -273,7 +321,7 @@ func NewDeltaGen(sig Signature, newfile io.Reader) (job *Job, err error) {
// Patcher is a job with additional hidden data for patching.
//
-// IMPORTANT: You still need to Close() this!
+// This patcher must be closed after use to free memory.
type Patcher struct {
*Job
basis io.ReaderAt
@@ -297,11 +345,21 @@ func NewPatcher(delta io.Reader, basis io.ReaderAt) (job *Patcher, err error) {
Job: _job,
basis: basis}
- job.job = C.rs_patch_begin((*C.rs_copy_cb)(patchCallback), unsafe.Pointer(job))
+ id := uintptr(unsafe.Pointer(_job.rsbufs)) // this is a unique, unchanging number (C doesn't change pointers under the hood)
+ storePatcher(job, id)
+ job.job = C.rs_patch_begin((*C.rs_copy_cb)(patchCallback), unsafe.Pointer(id))
if job.job == nil {
+ dropPatcher(id)
job.Close()
return nil, errors.New("rs_patch_begin failed")
}
return
}
+
+// Close unreferences memory that the garbage collector would not otherwise be
+// able to free.
+func (patch *Patcher) Close() error {
+ dropPatcher(uintptr(unsafe.Pointer(patch.Job.rsbufs)))
+ return patch.Job.Close()
+}
diff --git a/librsync/librsync_callback.go b/librsync/librsync_callback.go
index c8b9e4f..ace9779 100644
--- a/librsync/librsync_callback.go
+++ b/librsync/librsync_callback.go
@@ -12,8 +12,8 @@ import (
)
//export patchCallbackGo
-func patchCallbackGo(_patcher unsafe.Pointer, pos C.rs_long_t, len *C.size_t, _buf *unsafe.Pointer) C.rs_result {
- patcher := (*Patcher)(_patcher)
+func patchCallbackGo(_patcher uintptr, pos C.rs_long_t, len *C.size_t, _buf *unsafe.Pointer) C.rs_result {
+ patcher := getPatcher(_patcher)
patcher.buf = make([]byte, int(*len))
n, err := patcher.basis.ReadAt(patcher.buf, int64(pos))
diff --git a/librsync/librsync_test.go b/librsync/librsync_test.go
index 7c6a278..c291fac 100644
--- a/librsync/librsync_test.go
+++ b/librsync/librsync_test.go
@@ -22,7 +22,14 @@ func TestSignatureDeltaPatch(t *testing.T) {
t.Fatalf("Creating the signature failed: %s", err)
}
- if !bytes.Equal(sigbuf.Bytes(), testdata.RandomDataSig()) {
+ matches := false
+ // Check both possible signatures.
+ for _, sigcheck := range testdata.RandomDataSig() {
+ if bytes.Equal(sigbuf.Bytes(), sigcheck) {
+ matches = true
+ }
+ }
+ if !matches {
if path, err := dump(sigbuf); err == nil {
t.Fatalf("Signatures do not match. Generated signature dumped to %s", path)
} else {
diff --git a/librsync/pointers.go b/librsync/pointers.go
new file mode 100644
index 0000000..d14f41f
--- /dev/null
+++ b/librsync/pointers.go
@@ -0,0 +1,51 @@
+package librsync
+
+import (
+ "sync"
+)
+
+// pointerMap holds Go *Patcher objects to pass to C.
+// Don't touch this data structure, instead use the storePatcher, getPatcher,
+// and dropPatcher functions.
+var patcherStore = struct {
+ lock sync.Mutex
+ store map[uintptr]*Patcher
+}{
+ store: make(map[uintptr]*Patcher),
+}
+
+// storePatcher stores the value and returns a reference to it, for use in a CGo
+// call. Use the same reference for dropPatcher. C callbacks can use getPatcher
+// to get the original value.
+func storePatcher(patcher *Patcher, id uintptr) {
+ patcherStore.lock.Lock()
+ defer patcherStore.lock.Unlock()
+
+ if _, ok := patcherStore.store[id]; ok {
+ // Just to be on the safe side.
+ panic("pointer already stored")
+ }
+ patcherStore.store[id] = patcher
+}
+
+// getPatcher returns the value for the reference id. It returns nil if there
+// is no such reference.
+func getPatcher(id uintptr) *Patcher {
+ patcherStore.lock.Lock()
+ defer patcherStore.lock.Unlock()
+
+ return patcherStore.store[id]
+}
+
+// dropPatcher unreferences the value so the garbage collector can free it's
+// memory.
+func dropPatcher(id uintptr) {
+ patcherStore.lock.Lock()
+ defer patcherStore.lock.Unlock()
+
+ if _, ok := patcherStore.store[id]; !ok {
+ panic("pointer not stored")
+ }
+
+ delete(patcherStore.store, id)
+}
diff --git a/librsync/testdata/random_data.sig.go b/librsync/testdata/random_data.sig.go
index 8822edf..e5e57a3 100644
--- a/librsync/testdata/random_data.sig.go
+++ b/librsync/testdata/random_data.sig.go
@@ -1,12 +1,28 @@
package testdata
-func RandomDataSig() []byte {
+func RandomDataSig() [][]byte {
// Was generated with `rdiff signature random_data random_data.sig`
- return []byte{
- 0x72, 0x73, 0x01, 0x36, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08,
- 0x60, 0xcb, 0xf7, 0x64, 0x2e, 0xbe, 0x39, 0x5e, 0x1a, 0xf2, 0x77, 0x07,
- 0xf5, 0x8f, 0xf3, 0xdc, 0x1d, 0xcc, 0xe0, 0x3b, 0x3b, 0xc9, 0x7e, 0x0d,
- 0x2e, 0xcf, 0xed, 0xc5, 0x09, 0x1f, 0xbb, 0x75, 0x27, 0xbb, 0x46, 0xa9,
- 0x86, 0x26, 0x04, 0xa1, 0x9d, 0x4d, 0xa3, 0x7a, 0xfa, 0xdd, 0x5d, 0xc5,
+ return [][]byte{
+ {
+ 0x72, 0x73, 0x01, 0x36, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08,
+ 0x60, 0xcb, 0xf7, 0x64, 0x2e, 0xbe, 0x39, 0x5e, 0x1a, 0xf2, 0x77, 0x07,
+ 0xf5, 0x8f, 0xf3, 0xdc, 0x1d, 0xcc, 0xe0, 0x3b, 0x3b, 0xc9, 0x7e, 0x0d,
+ 0x2e, 0xcf, 0xed, 0xc5, 0x09, 0x1f, 0xbb, 0x75, 0x27, 0xbb, 0x46, 0xa9,
+ 0x86, 0x26, 0x04, 0xa1, 0x9d, 0x4d, 0xa3, 0x7a, 0xfa, 0xdd, 0x5d, 0xc5,
+ }, {
+ 0x72, 0x73, 0x01, 0x37, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x20,
+ 0x60, 0xcb, 0xf7, 0x64, 0x3a, 0x2b, 0x95, 0x58, 0xad, 0xd8, 0xa9, 0x71,
+ 0x70, 0x7d, 0x38, 0x64, 0xd5, 0xff, 0xa2, 0xc1, 0xae, 0xdf, 0x62, 0x76,
+ 0x7a, 0x84, 0xf8, 0xa5, 0xc7, 0x68, 0xd9, 0xbd, 0x04, 0xea, 0x09, 0x2b,
+ 0xf5, 0x8f, 0xf3, 0xdc, 0x13, 0x67, 0x5e, 0x50, 0xd9, 0xd3, 0x38, 0x47,
+ 0x1c, 0xb9, 0x27, 0x60, 0x26, 0x8d, 0x23, 0x39, 0x47, 0x0e, 0x57, 0x2c,
+ 0xfd, 0x2a, 0x27, 0xf1, 0xab, 0xee, 0x88, 0xe6, 0x74, 0x60, 0x78, 0xf8,
+ 0x2e, 0xcf, 0xed, 0xc5, 0x3b, 0xef, 0xab, 0x41, 0x13, 0x93, 0x0e, 0x26,
+ 0x38, 0x79, 0xc6, 0xed, 0x4d, 0xce, 0x65, 0xcd, 0x02, 0x5f, 0x2f, 0x19,
+ 0x42, 0x4a, 0x2d, 0xac, 0x8f, 0xb1, 0x40, 0xa2, 0x3c, 0xfc, 0x90, 0x66,
+ 0x86, 0x26, 0x04, 0xa1, 0x08, 0x25, 0xe4, 0x2c, 0xf7, 0x77, 0xa8, 0x7e,
+ 0x8f, 0xbf, 0xe9, 0xdf, 0x7a, 0x2d, 0x89, 0xc3, 0xe8, 0x15, 0x65, 0x46,
+ 0xa2, 0xe0, 0x33, 0x38, 0xc9, 0x73, 0xf0, 0xc3, 0x20, 0x79, 0x5a, 0x11,
+ },
}
}
diff --git a/librsync/testdata/random_data.sig2 b/librsync/testdata/random_data.sig2
new file mode 100644
index 0000000..684eacc
--- /dev/null
+++ b/librsync/testdata/random_data.sig2
Binary files differ