s4: add python bindings for wrap_(s|g)etxattr
[Samba/cd1.git] / source4 / scripting / python / pyxattr_tdb.c
blob19b4f417a8c0ba359c6960458e13f2a17798900f
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
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.
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.
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/>.
21 #include <Python.h>
22 #include "version.h"
23 #include "includes.h"
24 #include "../tdb/include/tdb.h"
25 #include "tdb_wrap.h"
26 #include "librpc/ndr/libndr.h"
27 #include "lib/util/wrap_xattr.h"
28 #include "ntvfs/posix/vfs_posix.h"
30 #ifndef Py_RETURN_NONE
31 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
32 #endif
34 static PyObject *py_is_xattr_supported(PyObject *self)
36 /*#if !defined(HAVE_XATTR_SUPPORT)
37 return Py_False;
38 #else
39 return Py_True;
40 #endif*/
41 return Py_True;
43 static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
45 char *filename, *attribute, *tdbname;
46 DATA_BLOB blob;
47 int blobsize;
48 NTSTATUS status;
49 TALLOC_CTX *mem_ctx;
50 struct tdb_wrap *eadb;
52 if (!PyArg_ParseTuple(args, "ssss#", &tdbname,&filename,&attribute,&blob.data,&blobsize))
53 return NULL;
55 blob.length = blobsize;
56 mem_ctx = talloc_new(NULL);
57 eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
58 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
60 if (eadb == NULL) {
61 PyErr_SetFromErrno(PyExc_IOError);
62 return NULL;
63 } status = push_xattr_blob_tdb_raw(eadb,mem_ctx,attribute,filename,-1,&blob);
64 if( !NT_STATUS_IS_OK(status) ) {
65 PyErr_SetString(PyExc_TypeError, strerror(errno));
66 return NULL;
68 Py_RETURN_NONE;
71 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
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;
80 if (!PyArg_ParseTuple(args, "sss", &tdbname,&filename,&attribute))
81 return NULL;
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) {
88 PyErr_SetFromErrno(PyExc_IOError);
89 return NULL;
91 status = pull_xattr_blob_tdb_raw(eadb,mem_ctx,attribute,filename,-1,100,&blob);
92 if( !NT_STATUS_IS_OK(status) || blob.length < 0 ) {
93 PyErr_SetString(PyExc_TypeError, get_friendly_nt_error_msg(status));
94 return NULL;
96 ret = PyString_FromStringAndSize(blob.data,blob.length);
97 return ret;
100 static PyMethodDef py_xattr_methods[] = {
101 { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
102 "wrap_getxattr(filename,attribute) -> blob\n"
103 "Retreive given attribute on the given file." },
104 { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
105 "wrap_setxattr(filename,attribute,value)\n"
106 "Set the given attribute to the given value on the given file." },
107 { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
108 "Return true if xattr backed by tdb are supported on this system\n"},
109 { NULL }
112 void initxattr_tdb(void)
114 PyObject *m;
116 m = Py_InitModule3("xattr_tdb", py_xattr_methods,
117 "Python bindings for xattr manipulation.");
118 if (m == NULL)
119 return;
121 PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING));
123 /* one of the most annoying things about python scripts is
124 that they don't die when you hit control-C. This fixes that
125 sillyness. As we do all database operations using
126 transactions, this is also safe. In fact, not dying
127 immediately is unsafe as we could end up treating the
128 control-C exception as a different error and try to modify
129 as database incorrectly
131 signal(SIGINT, SIG_DFL);