s4/ntvfs/posix/python: python3 (get|set)xattr value should be bytes
[metze/samba/wip.git] / source4 / ntvfs / posix / python / pyposix_eadb.c
index 226160f88d20ba79250a0916fb7b4e9e55552d66..646498225b3960561d1b6447cc371ed349aa4b7c 100644 (file)
 */
 
 #include <Python.h>
+#include "python/py3compat.h"
 #include "includes.h"
 #include "system/filesys.h"
-#include "tdb_compat.h"
+#include <tdb.h>
 #include "lib/tdb_wrap/tdb_wrap.h"
 #include "librpc/ndr/libndr.h"
-#include "lib/util/wrap_xattr.h"
 #include "ntvfs/posix/posix_eadb.h"
 #include "libcli/util/pyerrors.h"
 #include "param/pyparam.h"
 
-void initxattr_tdb(void);
-
 static PyObject *py_is_xattr_supported(PyObject *self)
 {
        return Py_True;
@@ -40,20 +38,22 @@ static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
 {
        char *filename, *attribute, *tdbname;
        DATA_BLOB blob;
-       int blobsize;
+       Py_ssize_t blobsize;
        NTSTATUS status;
        TALLOC_CTX *mem_ctx;
        struct tdb_wrap *eadb;
 
-       if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute,
+       if (!PyArg_ParseTuple(args, "sss"PYARG_BYTES_LEN, &tdbname, &filename, &attribute,
                                                  &blob.data, &blobsize))
                return NULL;
 
        blob.length = blobsize;
        mem_ctx = talloc_new(NULL);
-       eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
-                            TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
-                            py_default_loadparm_context(mem_ctx));
+       eadb = tdb_wrap_open(
+               mem_ctx, tdbname, 50000,
+               lpcfg_tdb_flags(py_default_loadparm_context(mem_ctx),
+                               TDB_DEFAULT),
+               O_RDWR|O_CREAT, 0600);
 
        if (eadb == NULL) {
                PyErr_SetFromErrno(PyExc_IOError);
@@ -84,8 +84,11 @@ static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
                return NULL;
 
        mem_ctx = talloc_new(NULL);
-       eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
-                            TDB_DEFAULT, O_RDWR|O_CREAT, 0600, py_default_loadparm_context(mem_ctx));
+       eadb = tdb_wrap_open(
+               mem_ctx, tdbname, 50000,
+               lpcfg_tdb_flags(py_default_loadparm_context(mem_ctx),
+                               TDB_DEFAULT),
+               O_RDWR|O_CREAT, 0600);
        if (eadb == NULL) {
                PyErr_SetFromErrno(PyExc_IOError);
                talloc_free(mem_ctx);
@@ -98,7 +101,7 @@ static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
                talloc_free(mem_ctx);
                return NULL;
        }
-       ret = PyString_FromStringAndSize((char *)blob.data, blob.length);
+       ret = Py_BuildValue(PYARG_BYTES_LEN, blob.data, blob.length);
        talloc_free(mem_ctx);
        return ret;
 }
@@ -106,7 +109,7 @@ static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
 static PyMethodDef py_posix_eadb_methods[] = {
        { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
                "wrap_getxattr(filename,attribute) -> blob\n"
-               "Retreive given attribute on the given file." },
+               "Retrieve given attribute on the given file." },
        { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
                "wrap_setxattr(filename,attribute,value)\n"
                "Set the given attribute to the given value on the given file." },
@@ -115,12 +118,22 @@ static PyMethodDef py_posix_eadb_methods[] = {
        { NULL }
 };
 
-void initposix_eadb(void)
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "posix_eadb",
+    .m_doc = "Python bindings for xattr manipulation.",
+    .m_size = -1,
+    .m_methods = py_posix_eadb_methods,
+};
+
+MODULE_INIT_FUNC(posix_eadb)
 {
        PyObject *m;
 
-       m = Py_InitModule3("posix_eadb", py_posix_eadb_methods,
-                          "Python bindings for xattr manipulation.");
+       m = PyModule_Create(&moduledef);
+
        if (m == NULL)
-               return;
+               return NULL;
+
+       return m;
 }