Merge commit 'release-4-0-0alpha15' into master4-tmp
[kai/samba.git] / source4 / ntvfs / posix / python / pyxattr_tdb.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 "includes.h"
23 #include "tdb_compat.h"
24 #include "lib/util/tdb_wrap.h"
25 #include "librpc/ndr/libndr.h"
26 #include "lib/util/wrap_xattr.h"
27 #include "ntvfs/posix/vfs_posix.h"
28 #include "libcli/util/pyerrors.h"
29
30 void initxattr_tdb(void);
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         int 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(mem_ctx, tdbname, 50000,
53                                 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
54
55         if (eadb == NULL) {
56                 PyErr_SetFromErrno(PyExc_IOError);
57                 talloc_free(mem_ctx);
58                 return NULL;
59         }
60         status = push_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename, -1,
61                                                                          &blob);
62         if (!NT_STATUS_IS_OK(status)) {
63                 PyErr_FromNTSTATUS(status);
64                 talloc_free(mem_ctx);
65                 return NULL;
66         }
67         talloc_free(mem_ctx);
68         Py_RETURN_NONE;
69 }
70
71 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
72 {
73         char *filename, *attribute, *tdbname;
74         TALLOC_CTX *mem_ctx;
75         DATA_BLOB blob;
76         PyObject *ret;
77         NTSTATUS status;
78         struct tdb_wrap *eadb = NULL;
79
80         if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
81                 return NULL;
82
83         mem_ctx = talloc_new(NULL);
84         eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
85                                 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
86         if (eadb == NULL) {
87                 PyErr_SetFromErrno(PyExc_IOError);
88                 talloc_free(mem_ctx);
89                 return NULL;
90         }
91         status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename, 
92                                                                          -1, 100, &blob);
93         if (!NT_STATUS_IS_OK(status) || blob.length < 0) {
94                 PyErr_FromNTSTATUS(status);
95                 talloc_free(mem_ctx);
96                 return NULL;
97         }
98         ret = PyString_FromStringAndSize((char *)blob.data, blob.length);
99         talloc_free(mem_ctx);
100         return ret;
101 }
102
103 static PyMethodDef py_xattr_methods[] = {
104         { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
105                 "wrap_getxattr(filename,attribute) -> blob\n"
106                 "Retreive given attribute on the given file." },
107         { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
108                 "wrap_setxattr(filename,attribute,value)\n"
109                 "Set the given attribute to the given value on the given file." },
110         { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
111                 "Return true if xattr are supported on this system\n"},
112         { NULL }
113 };
114
115 void initxattr_tdb(void)
116 {
117         PyObject *m;
118
119         m = Py_InitModule3("xattr_tdb", py_xattr_methods,
120                            "Python bindings for xattr manipulation.");
121         if (m == NULL)
122                 return;
123 }
124