pyxattr: Move to the same directory as the xattr code.
[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.h>
24 #include "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 #ifndef Py_RETURN_NONE
31 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
32 #endif
33
34 static PyObject *py_is_xattr_supported(PyObject *self)
35 {
36         return Py_True;
37 }
38
39 static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
40 {
41         char *filename, *attribute, *tdbname;
42         DATA_BLOB blob;
43         int blobsize;
44         NTSTATUS status;
45         TALLOC_CTX *mem_ctx;
46         struct tdb_wrap *eadb;
47
48         if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute, 
49                                                   &blob.data, &blobsize))
50                 return NULL;
51
52         blob.length = blobsize;
53         mem_ctx = talloc_new(NULL);
54         eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
55                                 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
56
57         if (eadb == NULL) {
58                 PyErr_SetFromErrno(PyExc_IOError);
59                 talloc_free(mem_ctx);
60                 return NULL;
61         }
62         status = push_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename, -1,
63                                                                          &blob);
64         if (!NT_STATUS_IS_OK(status)) {
65                 PyErr_FromNTSTATUS(status);
66                 talloc_free(mem_ctx);
67                 return NULL;
68         }
69         talloc_free(mem_ctx);
70         Py_RETURN_NONE;
71 }
72
73 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
74 {
75         char *filename, *attribute, *tdbname;
76         TALLOC_CTX *mem_ctx;
77         DATA_BLOB blob;
78         PyObject *ret;
79         NTSTATUS status;
80         struct tdb_wrap *eadb = NULL;
81
82         if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
83                 return NULL;
84
85         mem_ctx = talloc_new(NULL);
86         eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
87                                 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
88         if (eadb == NULL) {
89                 PyErr_SetFromErrno(PyExc_IOError);
90                 talloc_free(mem_ctx);
91                 return NULL;
92         }
93         status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename, 
94                                                                          -1, 100, &blob);
95         if (!NT_STATUS_IS_OK(status) || blob.length < 0) {
96                 PyErr_FromNTSTATUS(status);
97                 talloc_free(mem_ctx);
98                 return NULL;
99         }
100         ret = PyString_FromStringAndSize((char *)blob.data, blob.length);
101         talloc_free(mem_ctx);
102         return ret;
103 }
104
105 static PyMethodDef py_xattr_methods[] = {
106         { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
107                 "wrap_getxattr(filename,attribute) -> blob\n"
108                 "Retreive given attribute on the given file." },
109         { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
110                 "wrap_setxattr(filename,attribute,value)\n"
111                 "Set the given attribute to the given value on the given file." },
112         { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
113                 "Return true if xattr are supported on this system\n"},
114         { NULL }
115 };
116
117 void initxattr_tdb(void)
118 {
119         PyObject *m;
120
121         m = Py_InitModule3("xattr_tdb", py_xattr_methods,
122                            "Python bindings for xattr manipulation.");
123         if (m == NULL)
124                 return;
125 }
126