python: Port ntvfs posix bindings to Python 3 compatible form
[metze/samba/wip.git] / source4 / ntvfs / posix / python / pyposix_eadb.c
1 /*
2    Unix SMB/CIFS implementation. Xattr manipulation bindings.
3    Copyright (C) Matthieu Patou <mat@matws.net> 2009-2010
4    Base on work of pyglue.c by Jelmer Vernooij <jelmer@samba.org> 2007 and
5     Matthias Dieter Wallnöfer 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <Python.h>
22 #include "python/py3compat.h"
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include <tdb.h>
26 #include "lib/tdb_wrap/tdb_wrap.h"
27 #include "librpc/ndr/libndr.h"
28 #include "ntvfs/posix/posix_eadb.h"
29 #include "libcli/util/pyerrors.h"
30 #include "param/pyparam.h"
31
32 static PyObject *py_is_xattr_supported(PyObject *self)
33 {
34         return Py_True;
35 }
36
37 static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
38 {
39         char *filename, *attribute, *tdbname;
40         DATA_BLOB blob;
41         Py_ssize_t blobsize;
42         NTSTATUS status;
43         TALLOC_CTX *mem_ctx;
44         struct tdb_wrap *eadb;
45
46         if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute,
47                                                   &blob.data, &blobsize))
48                 return NULL;
49
50         blob.length = blobsize;
51         mem_ctx = talloc_new(NULL);
52         eadb = tdb_wrap_open(
53                 mem_ctx, tdbname, 50000,
54                 lpcfg_tdb_flags(py_default_loadparm_context(mem_ctx),
55                                 TDB_DEFAULT),
56                 O_RDWR|O_CREAT, 0600);
57
58         if (eadb == NULL) {
59                 PyErr_SetFromErrno(PyExc_IOError);
60                 talloc_free(mem_ctx);
61                 return NULL;
62         }
63         status = push_xattr_blob_tdb_raw(eadb, attribute, filename, -1,
64                                          &blob);
65         if (!NT_STATUS_IS_OK(status)) {
66                 PyErr_SetNTSTATUS(status);
67                 talloc_free(mem_ctx);
68                 return NULL;
69         }
70         talloc_free(mem_ctx);
71         Py_RETURN_NONE;
72 }
73
74 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
75 {
76         char *filename, *attribute, *tdbname;
77         TALLOC_CTX *mem_ctx;
78         DATA_BLOB blob;
79         PyObject *ret;
80         NTSTATUS status;
81         struct tdb_wrap *eadb = NULL;
82
83         if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
84                 return NULL;
85
86         mem_ctx = talloc_new(NULL);
87         eadb = tdb_wrap_open(
88                 mem_ctx, tdbname, 50000,
89                 lpcfg_tdb_flags(py_default_loadparm_context(mem_ctx),
90                                 TDB_DEFAULT),
91                 O_RDWR|O_CREAT, 0600);
92         if (eadb == NULL) {
93                 PyErr_SetFromErrno(PyExc_IOError);
94                 talloc_free(mem_ctx);
95                 return NULL;
96         }
97         status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename,
98                                                                          -1, 100, &blob);
99         if (!NT_STATUS_IS_OK(status)) {
100                 PyErr_SetNTSTATUS(status);
101                 talloc_free(mem_ctx);
102                 return NULL;
103         }
104         ret = PyStr_FromStringAndSize((char *)blob.data, blob.length);
105         talloc_free(mem_ctx);
106         return ret;
107 }
108
109 static PyMethodDef py_posix_eadb_methods[] = {
110         { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
111                 "wrap_getxattr(filename,attribute) -> blob\n"
112                 "Retrieve given attribute on the given file." },
113         { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
114                 "wrap_setxattr(filename,attribute,value)\n"
115                 "Set the given attribute to the given value on the given file." },
116         { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
117                 "Return true if xattr are supported on this system\n"},
118         { NULL }
119 };
120
121 static struct PyModuleDef moduledef = {
122     PyModuleDef_HEAD_INIT,
123     .m_name = "posix_eadb",
124     .m_doc = "Python bindings for xattr manipulation.",
125     .m_size = -1,
126     .m_methods = py_posix_eadb_methods,
127 };
128
129 MODULE_INIT_FUNC(posix_eadb)
130 {
131         PyObject *m;
132
133         m = PyModule_Create(&moduledef);
134
135         if (m == NULL)
136                 return NULL;
137
138         return m;
139 }