From: Michael Tremer Date: Fri, 15 Jul 2022 15:45:58 +0000 (+0000) Subject: files: Add Python functions to access digest/hexdigest X-Git-Tag: 0.9.28~708 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1bd435b66cb3eb97bf94c6642664f640883549ce;p=pakfire.git files: Add Python functions to access digest/hexdigest Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/file.c b/src/_pakfire/file.c index 72d69cd3e..b1a05ffea 100644 --- a/src/_pakfire/file.c +++ b/src/_pakfire/file.c @@ -227,6 +227,59 @@ static int File_set_mtime(FileObject* self, PyObject* value) { return 0; } +static PyObject* _File_digest(FileObject* self, enum pakfire_digests type) { + size_t length = 0; + + // Fetch the digest + const unsigned char* digest = pakfire_file_get_digest(self->file, type, &length); + if (!digest) + Py_RETURN_NONE; + + return PyBytes_FromStringAndSize((const char*)digest, length); +} + +static PyObject* File_digest(FileObject* self, PyObject* args) { + int type = 0; + + if (!PyArg_ParseTuple(args, "i", &type)) + return NULL; + + return _File_digest(self, type); +} + +static PyObject* _File_hexdigest(FileObject* self, enum pakfire_digests type) { + const char* hexdigest = pakfire_file_get_hexdigest(self->file, type); + if (!hexdigest) + Py_RETURN_NONE; + + return PyUnicode_FromString(hexdigest); +} + +static PyObject* File_hexdigest(FileObject* self, PyObject* args) { + int type = 0; + + if (!PyArg_ParseTuple(args, "i", &type)) + return NULL; + + return _File_hexdigest(self, type); +} + +static struct PyMethodDef File_methods[] = { + { + "digest", + (PyCFunction)File_digest, + METH_VARARGS, + NULL, + }, + { + "hexdigest", + (PyCFunction)File_hexdigest, + METH_VARARGS, + NULL, + }, + { NULL }, +}; + static struct PyGetSetDef File_getsetters[] = { { "ctime", @@ -296,6 +349,7 @@ PyTypeObject FileType = { tp_dealloc: (destructor)File_dealloc, tp_init: (initproc)File_init, tp_doc: "File object", + tp_methods: File_methods, tp_getset: File_getsetters, tp_repr: (reprfunc)File_repr, tp_str: (reprfunc)File_get_path,