#############################################################################*/
#include <Python.h>
-#include <datetime.h>
+#include <pakfire/digest.h>
#include <pakfire/file.h>
#include "file.h"
#include "pakfire.h"
+#include "util.h"
PyObject* new_file(struct pakfire_file* file) {
PyTypeObject* type = &FileType;
FileObject* self = (FileObject *)type->tp_alloc(type, 0);
if (self) {
- if (file)
- self->file = pakfire_file_ref(file);
- else
- self->file = NULL;
+ self->file = pakfire_file_ref(file);
}
return (PyObject *)self;
}
-static PyObject* File_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
- return new_file(NULL);
-}
-
static void File_dealloc(FileObject* self) {
if (self->file)
pakfire_file_unref(self->file);
Py_TYPE(self)->tp_free((PyObject *)self);
}
-static int File_init(FileObject* self, PyObject* args, PyObject* kwds) {
- PakfireObject* pakfire = NULL;
-
- if (!PyArg_ParseTuple(args, "O!", &PakfireType, &pakfire))
- return -1;
-
- // Create a new File object
- int r = pakfire_file_create(&self->file, pakfire->pakfire);
- if (r)
- return -1;
-
- return 0;
-}
-
static PyObject* File_repr(FileObject* self) {
const char* path = pakfire_file_get_path(self->file);
- return PyUnicode_FromFormat("<_pakfire.Problem %s>", path);
+ return PyUnicode_FromFormat("<_pakfire.File %s>", path);
}
static PyObject* File_get_path(FileObject* self) {
const char* path = pakfire_file_get_path(self->file);
-
- return PyUnicode_FromString(path);
-}
-
-static int File_set_path(FileObject* self, PyObject* value) {
- const char* path = PyUnicode_AsUTF8(value);
if (!path)
- return -1;
+ Py_RETURN_NONE;
- pakfire_file_set_path(self->file, path);
- return 0;
+ return PyUnicode_FromString(path);
}
static PyObject* File_get_size(FileObject* self) {
return PyLong_FromSize_t(size);
}
-static int File_set_size(FileObject* self, PyObject* value) {
- long size = PyLong_AsLong(value);
- if (size < 0)
- return -1;
-
- pakfire_file_set_size(self->file, size);
- return 0;
-}
-
static PyObject* File_get_type(FileObject* self) {
int type = pakfire_file_get_type(self->file);
return PyLong_FromLong(type);
}
-static PyObject* File_get_user(FileObject* self) {
- const char* user = pakfire_file_get_user(self->file);
-
- return PyUnicode_FromString(user);
-}
-
-static int File_set_user(FileObject* self, PyObject* value) {
- const char* user = PyUnicode_AsUTF8(value);
- if (!user)
- return -1;
-
- pakfire_file_set_user(self->file, user);
- return 0;
-}
-
-static PyObject* File_get_group(FileObject* self) {
- const char* group = pakfire_file_get_group(self->file);
+static PyObject* File_get_uname(FileObject* self) {
+ const char* uname = pakfire_file_get_uname(self->file);
+ if (!uname)
+ Py_RETURN_NONE;
- return PyUnicode_FromString(group);
+ return PyUnicode_FromString(uname);
}
-static int File_set_group(FileObject* self, PyObject* value) {
- const char* group = PyUnicode_AsUTF8(value);
- if (!group)
- return -1;
+static PyObject* File_get_gname(FileObject* self) {
+ const char* gname = pakfire_file_get_gname(self->file);
+ if (!gname)
+ Py_RETURN_NONE;
- pakfire_file_set_group(self->file, group);
- return 0;
+ return PyUnicode_FromString(gname);
}
static PyObject* File_get_mode(FileObject* self) {
return PyLong_FromLong(mode);
}
-static int File_set_mode(FileObject* self, PyObject* value) {
- const mode_t mode = PyLong_AsLong(value);
-
- pakfire_file_set_mode(self->file, mode);
- return 0;
-}
-
-static PyObject* PyDateTime_FromTime_t(const time_t* t) {
- struct tm buffer;
-
- struct tm* tm = gmtime_r(t, &buffer);
- if (!tm)
- return NULL;
-
- PyDateTime_IMPORT;
-
- return PyDateTime_FromDateAndTime(
- 1900 + tm->tm_year,
- 1 + tm->tm_mon,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- 0
- );
-}
-
-static time_t PyDateTime_AsTime_t(PyObject* value) {
- if (!PyDateTime_Check(value))
- return -1;
-
- PyDateTime_IMPORT;
-
- // Convert from datetime to struct tm
- struct tm tm = {
- .tm_year = PyDateTime_GET_YEAR(value) - 1900,
- .tm_mon = PyDateTime_GET_MONTH(value) - 1,
- .tm_mday = PyDateTime_GET_DAY(value),
- .tm_hour = PyDateTime_DATE_GET_HOUR(value),
- .tm_min = PyDateTime_DATE_GET_MINUTE(value),
- .tm_sec = PyDateTime_DATE_GET_SECOND(value),
- .tm_isdst = 0,
- };
-
- // Convert into time_t
- const time_t t = mktime(&tm);
- if (t < 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- return -1;
- }
-
- return t;
-}
-
static PyObject* File_get_ctime(FileObject* self) {
const time_t t = pakfire_file_get_ctime(self->file);
return PyDateTime_FromTime_t(&t);
}
-static int File_set_ctime(FileObject* self, PyObject* value) {
- const time_t t = PyDateTime_AsTime_t(value);
- if (t < 0)
- return -1;
-
- pakfire_file_set_ctime(self->file, t);
- return 0;
-}
-
static PyObject* File_get_mtime(FileObject* self) {
const time_t t = pakfire_file_get_mtime(self->file);
return PyDateTime_FromTime_t(&t);
}
-static int File_set_mtime(FileObject* self, PyObject* value) {
- const time_t t = PyDateTime_AsTime_t(value);
- if (t < 0)
- return -1;
+static PyObject* File_digest(FileObject* self, PyObject* args) {
+ const char* name = NULL;
- pakfire_file_set_mtime(self->file, t);
- return 0;
-}
+ if (!PyArg_ParseTuple(args, "s", &name))
+ return NULL;
+
+ // Fetch the type
+ const enum pakfire_digest_types type = pakfire_digest_get_by_name(name);
+
+ // Raise ValueError if we could not find the type
+ if (!type) {
+ PyErr_Format(PyExc_ValueError, "Unknown digest type: %s", name);
+ return NULL;
+ }
-static PyObject* _File_digest(FileObject* self, enum pakfire_digests type) {
size_t length = 0;
// Fetch the digest
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;
+static PyObject* File_get_mimetype(FileObject* self) {
+ const char* mimetype = pakfire_file_get_mimetype(self->file);
+ if (!mimetype)
+ Py_RETURN_NONE;
- return _File_digest(self, type);
+ return PyUnicode_FromString(mimetype);
}
-static PyObject* _File_hexdigest(FileObject* self, enum pakfire_digests type) {
- char* hexdigest = pakfire_file_get_hexdigest(self->file, type);
- if (!hexdigest)
- Py_RETURN_NONE;
+static PyObject* File_get_capabilities(FileObject* self) {
+ PyObject* result = NULL;
+ char* caps = NULL;
- PyObject* obj = PyUnicode_FromString(hexdigest);
- free(hexdigest);
+ // No capabilities
+ if (!pakfire_file_has_caps(self->file))
+ Py_RETURN_NONE;
- return obj;
-}
+ // Fetch capabilities
+ caps = pakfire_file_get_caps(self->file);
+ if (!caps) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ goto ERROR;
+ }
-static PyObject* File_hexdigest(FileObject* self, PyObject* args) {
- int type = 0;
+ // Convert to Python string object
+ result = PyUnicode_FromString(caps);
- if (!PyArg_ParseTuple(args, "i", &type))
- return NULL;
+ERROR:
+ if (caps)
+ free(caps);
- return _File_hexdigest(self, type);
+ return result;
}
static struct PyMethodDef File_methods[] = {
METH_VARARGS,
NULL,
},
- {
- "hexdigest",
- (PyCFunction)File_hexdigest,
- METH_VARARGS,
- NULL,
- },
{ NULL },
};
static struct PyGetSetDef File_getsetters[] = {
+ {
+ "capabilities",
+ (getter)File_get_capabilities,
+ NULL,
+ NULL,
+ NULL,
+ },
{
"ctime",
(getter)File_get_ctime,
- (setter)File_set_ctime,
+ NULL,
NULL,
NULL,
},
{
- "group",
- (getter)File_get_group,
- (setter)File_set_group,
+ "gname",
+ (getter)File_get_gname,
+ NULL,
+ NULL,
+ NULL,
+ },
+ {
+ "mimetype",
+ (getter)File_get_mimetype,
+ NULL,
NULL,
NULL,
},
{
"mode",
(getter)File_get_mode,
- (setter)File_set_mode,
+ NULL,
NULL,
NULL,
},
{
"mtime",
(getter)File_get_mtime,
- (setter)File_set_mtime,
+ NULL,
NULL,
NULL,
},
{
"path",
(getter)File_get_path,
- (setter)File_set_path,
+ NULL,
NULL,
NULL,
},
{
"size",
(getter)File_get_size,
- (setter)File_set_size,
+ NULL,
NULL,
NULL,
},
NULL,
},
{
- "user",
- (getter)File_get_user,
- (setter)File_set_user,
+ "uname",
+ (getter)File_get_uname,
+ NULL,
NULL,
NULL,
},
tp_name: "_pakfire.File",
tp_basicsize: sizeof(FileObject),
tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
- tp_new: File_new,
tp_dealloc: (destructor)File_dealloc,
- tp_init: (initproc)File_init,
tp_doc: "File object",
tp_methods: File_methods,
tp_getset: File_getsetters,