]> git.ipfire.org Git - pakfire.git/commitdiff
files: Add Python functions to access digest/hexdigest
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Jul 2022 15:45:58 +0000 (15:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Jul 2022 15:45:58 +0000 (15:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/file.c

index 72d69cd3e7c0234bc08e29c7e2b88e866c686edb..b1a05ffeaf12f2b1b0cb68cf361a5c3c966eb59c 100644 (file)
@@ -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,