diff options
author | Ayke van Laethem <aykevanlaethem@gmail.com> | 2016-04-16 15:24:14 +0200 |
---|---|---|
committer | Ayke van Laethem <aykevanlaethem@gmail.com> | 2016-04-16 15:24:14 +0200 |
commit | dc74089a97453ccb1a1a74078170bd50258a04b1 (patch) | |
tree | 05c15882ffb9e0c8e0f3490d8f5960d0bcc9a46b | |
parent | 698edd704ce126e1112aba8d7e0e9fb80bacc75a (diff) | |
download | golibrsync-dc74089a97453ccb1a1a74078170bd50258a04b1.tar.gz golibrsync-dc74089a97453ccb1a1a74078170bd50258a04b1.tar.bz2 golibrsync-dc74089a97453ccb1a1a74078170bd50258a04b1.zip |
Fix CGo callback
See: https://github.com/kch42/golibrsync/issues/1
It is not allowed to let C directly call Go functions, wrapper functions
must be made. For some reason, it appears to have worked before but it
doesn't work now.
-rw-r--r-- | librsync/librsync.go | 33 | ||||
-rw-r--r-- | librsync/librsync_callback.go | 31 |
2 files changed, 43 insertions, 21 deletions
diff --git a/librsync/librsync.go b/librsync/librsync.go index 57c9cfb..bf60a1d 100644 --- a/librsync/librsync.go +++ b/librsync/librsync.go @@ -7,9 +7,14 @@ package librsync #include <librsync.h> #include <stdlib.h> -rs_buffers_t* new_rs_buffers() { +static inline rs_buffers_t* new_rs_buffers() { return (rs_buffers_t*) malloc(sizeof(rs_buffers_t)); } + +rs_result patchCallback(void* _patcher, rs_long_t pos, size_t* len, void** _buf) { + return patchCallbackGo(_patcher, pos, len, _buf); +} + */ import "C" @@ -118,6 +123,10 @@ 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() + if r == nil { + // there was no panic + return + } jp, ok := r.(jobInternalPanic) if !ok { panic(r) @@ -271,25 +280,7 @@ type Patcher struct { 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.basis.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 +var patchCallback = C.patchCallback // So we can use the `&` operator in NewPatcher // NewPatcher creates a Patcher (which basically is a Job object with some hidden extra data). // @@ -306,7 +297,7 @@ 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)(unsafe.Pointer(&patch_callback)), unsafe.Pointer(job)) + job.job = C.rs_patch_begin((*C.rs_copy_cb)(patchCallback), unsafe.Pointer(job)) if job.job == nil { job.Close() return nil, errors.New("rs_patch_begin failed") diff --git a/librsync/librsync_callback.go b/librsync/librsync_callback.go new file mode 100644 index 0000000..c8b9e4f --- /dev/null +++ b/librsync/librsync_callback.go @@ -0,0 +1,31 @@ +package librsync + +/* +#include <stdio.h> +#include <librsync.h> +*/ +import "C" + +import ( + "io" + "unsafe" +) + +//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) + + patcher.buf = make([]byte, int(*len)) + n, err := patcher.basis.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 +} |