mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-27 18:51:41 +02:00
More work on wrapping librsync
This commit is contained in:
@@ -8,15 +8,20 @@
|
||||
#include "data-types.h"
|
||||
#include <librsync.h>
|
||||
|
||||
#define JOB_CAPSULE "rs_job_t"
|
||||
#define SIGNATURE_CAPSULE "rs_signature_t"
|
||||
#define JOB_WITH_CALLBACK_CAPSULE "rs_callback_job_t"
|
||||
// See whole.c in the librsync source code for estimating IO_BUFFER_SIZE
|
||||
#define IO_BUFFER_SIZE (64u * 1024u)
|
||||
static PyObject *RsyncError = NULL;
|
||||
|
||||
static void
|
||||
free_job_capsule(PyObject *capsule) {
|
||||
rs_job_t *job = PyCapsule_GetPointer(capsule, JOB_CAPSULE);
|
||||
if (job) rs_job_free(job);
|
||||
free_job_with_callback_capsule(PyObject *capsule) {
|
||||
if (PyCapsule_IsValid(capsule, JOB_WITH_CALLBACK_CAPSULE)) {
|
||||
void *job = PyCapsule_GetPointer(capsule, JOB_WITH_CALLBACK_CAPSULE);
|
||||
if (job && job != RsyncError) rs_job_free(job);
|
||||
PyObject *callback = PyCapsule_GetContext(capsule);
|
||||
Py_CLEAR(callback);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -25,6 +30,25 @@ free_sig_capsule(PyObject *capsule) {
|
||||
if (sig) rs_free_sumset(sig);
|
||||
}
|
||||
|
||||
#define CREATE_JOB(func, cb, ...) \
|
||||
PyObject *job_capsule = PyCapsule_New(RsyncError, JOB_WITH_CALLBACK_CAPSULE, free_job_with_callback_capsule); \
|
||||
if (job_capsule) { \
|
||||
rs_job_t *job = func(__VA_ARGS__); \
|
||||
if (job) { \
|
||||
if (PyCapsule_SetPointer(job_capsule, job) == 0) { \
|
||||
if (cb) { \
|
||||
if (PyCapsule_SetContext(job_capsule, cb) == 0) { Py_INCREF(cb); } \
|
||||
else { Py_CLEAR(job_capsule); } \
|
||||
} \
|
||||
} else { \
|
||||
rs_job_free(job); Py_CLEAR(job_capsule); \
|
||||
} \
|
||||
} else { \
|
||||
Py_CLEAR(job_capsule); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
static PyObject*
|
||||
begin_create_signature(PyObject *self UNUSED, PyObject *args) {
|
||||
long long file_size = -1;
|
||||
@@ -39,16 +63,14 @@ begin_create_signature(PyObject *self UNUSED, PyObject *args) {
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
rs_job_t *job = rs_sig_begin(block_len, strong_len, magic_number);
|
||||
if (!job) return PyErr_NoMemory();
|
||||
PyObject *ans = PyCapsule_New(job, JOB_CAPSULE, free_job_capsule);
|
||||
if (!ans) rs_job_free(job);
|
||||
return ans;
|
||||
CREATE_JOB(rs_sig_begin, NULL, block_len, strong_len, magic_number);
|
||||
return job_capsule;
|
||||
}
|
||||
|
||||
#define GET_JOB_FROM_CAPSULE \
|
||||
rs_job_t *job = PyCapsule_GetPointer(job_capsule, JOB_CAPSULE); \
|
||||
if (!job) { PyErr_SetString(PyExc_TypeError, "Not a job capsule"); return NULL; }
|
||||
rs_job_t *job = PyCapsule_GetPointer(job_capsule, JOB_WITH_CALLBACK_CAPSULE); \
|
||||
if (!job) { PyErr_SetString(PyExc_TypeError, "Not a job capsule"); return NULL; } \
|
||||
|
||||
|
||||
static PyObject*
|
||||
iter_job(PyObject *self UNUSED, PyObject *args) {
|
||||
@@ -56,12 +78,12 @@ iter_job(PyObject *self UNUSED, PyObject *args) {
|
||||
char *input_data;
|
||||
int eof = -1, expecting_output = 1;
|
||||
PyObject *job_capsule;
|
||||
if (!PyArg_ParseTuple(args, "O!y#|pp", &PyCapsule_Type, &job_capsule, &input_data, &input_data_size, &eof, expecting_output)) return NULL;
|
||||
if (!PyArg_ParseTuple(args, "O!y#|pp", &PyCapsule_Type, &job_capsule, &input_data, &input_data_size, &eof, &expecting_output)) return NULL;
|
||||
GET_JOB_FROM_CAPSULE;
|
||||
if (eof == -1) eof = input_data_size > 0 ? 0 : 1;
|
||||
rs_buffers_t buffer = {
|
||||
.avail_in=input_data_size, .next_in = input_data, .eof_in=eof,
|
||||
.avail_out=expecting_output ? (MAX(IO_BUFFER_SIZE, 2 * (size_t)input_data_size)) : 64
|
||||
.avail_out=expecting_output ? IO_BUFFER_SIZE : 64
|
||||
};
|
||||
PyObject *ans = PyBytes_FromStringAndSize(NULL, buffer.avail_out);
|
||||
if (!ans) return NULL;
|
||||
@@ -93,13 +115,11 @@ iter_job(PyObject *self UNUSED, PyObject *args) {
|
||||
static PyObject*
|
||||
begin_load_signature(PyObject *self UNUSED, PyObject *args UNUSED) {
|
||||
rs_signature_t *sig = NULL;
|
||||
rs_job_t *job = rs_loadsig_begin(&sig);
|
||||
if (!sig || !job) return PyErr_NoMemory();
|
||||
PyObject *jc = PyCapsule_New(job, JOB_CAPSULE, free_job_capsule);
|
||||
if (!jc) { rs_job_free(job); rs_free_sumset(sig); return NULL; }
|
||||
CREATE_JOB(rs_loadsig_begin, NULL, &sig);
|
||||
if (!job_capsule) { rs_free_sumset(sig); return NULL; }
|
||||
PyObject *sc = PyCapsule_New(sig, SIGNATURE_CAPSULE, free_sig_capsule);
|
||||
if (!sc) { Py_DECREF(jc); rs_free_sumset(sig); return NULL; }
|
||||
return Py_BuildValue("NN", jc, sc);
|
||||
if (!sc) { Py_CLEAR(job_capsule); rs_free_sumset(sig); return NULL; }
|
||||
return Py_BuildValue("NN", job_capsule, sc);
|
||||
}
|
||||
|
||||
#define GET_SIG_FROM_CAPSULE \
|
||||
@@ -125,20 +145,40 @@ begin_create_delta(PyObject *self UNUSED, PyObject *args) {
|
||||
PyObject *sig_capsule;
|
||||
if (!PyArg_ParseTuple(args, "O!", &PyCapsule_Type, &sig_capsule)) return NULL;
|
||||
GET_SIG_FROM_CAPSULE;
|
||||
rs_job_t *job = rs_delta_begin(sig);
|
||||
if (!job) return PyErr_NoMemory();
|
||||
PyObject *ans = PyCapsule_New(job, JOB_CAPSULE, free_job_capsule);
|
||||
if (!ans) rs_job_free(job);
|
||||
return ans;
|
||||
CREATE_JOB(rs_delta_begin, NULL, sig);
|
||||
return job_capsule;
|
||||
}
|
||||
|
||||
static rs_result
|
||||
copy_callback(void *opaque, rs_long_t pos, size_t *len, void **buf) {
|
||||
PyObject *callback = opaque;
|
||||
long long p = pos;
|
||||
PyObject *mem = PyMemoryView_FromMemory(*buf, *len, PyBUF_WRITE);
|
||||
if (!mem) { PyErr_Clear(); return RS_MEM_ERROR; }
|
||||
PyObject *res = PyObject_CallFunction(callback, "OL", mem, p);
|
||||
Py_DECREF(mem);
|
||||
if (res == NULL) { PyErr_Clear(); return RS_IO_ERROR; }
|
||||
rs_result r = RS_DONE;
|
||||
if (PyLong_Check(res)) { *len = PyLong_AsSize_t(res); }
|
||||
else { r = RS_INTERNAL_ERROR; }
|
||||
Py_DECREF(res);
|
||||
return r;
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
begin_patch(PyObject *self UNUSED, PyObject *callback) {
|
||||
if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "callback must be a callable"); return NULL; }
|
||||
CREATE_JOB(rs_patch_begin, callback, copy_callback, callback);
|
||||
return job_capsule;
|
||||
}
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
{"begin_create_signature", (PyCFunction)begin_create_signature, METH_VARARGS, ""},
|
||||
{"begin_load_signature", (PyCFunction)begin_load_signature, METH_NOARGS, ""},
|
||||
{"build_hash_table", (PyCFunction)build_hash_table, METH_VARARGS, ""},
|
||||
{"begin_create_delta", (PyCFunction)begin_create_delta, METH_VARARGS, ""},
|
||||
{"iter_job", (PyCFunction)iter_job, METH_VARARGS, ""},
|
||||
{"begin_create_signature", begin_create_signature, METH_VARARGS, ""},
|
||||
{"begin_load_signature", begin_load_signature, METH_NOARGS, ""},
|
||||
{"build_hash_table", build_hash_table, METH_VARARGS, ""},
|
||||
{"begin_patch", begin_patch, METH_O, ""},
|
||||
{"begin_create_delta", begin_create_delta, METH_VARARGS, ""},
|
||||
{"iter_job", iter_job, METH_VARARGS, ""},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user