move the dbwrap library to the top level
[Samba/gebeck_regimport.git] / source4 / ntvfs / posix / python / pyxattr_tdb.c
blob1df9897e9a951a896b7e3c42b67f4b3e1b4a100f
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 "includes.h"
23 #include "system/filesys.h"
24 #include "tdb_compat.h"
25 #include "lib/tdb_wrap/tdb_wrap.h"
26 #include "librpc/ndr/libndr.h"
27 #include "lib/util/wrap_xattr.h"
28 #include "ntvfs/posix/posix_eadb.h"
29 #include "libcli/util/pyerrors.h"
30 #include "param/pyparam.h"
31 #include "lib/dbwrap/dbwrap.h"
32 #include "lib/dbwrap/dbwrap_open.h"
33 #include "lib/dbwrap/dbwrap_tdb.h"
34 #include "source3/lib/xattr_tdb.h"
36 void initxattr_tdb(void);
38 static PyObject *py_is_xattr_supported(PyObject *self)
40 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 int ret;
49 TALLOC_CTX *mem_ctx;
50 struct db_context *eadb = NULL;
51 struct file_id id;
52 struct stat sbuf;
54 if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute,
55 &blob.data, &blobsize))
56 return NULL;
58 blob.length = blobsize;
59 mem_ctx = talloc_new(NULL);
60 eadb = db_open_tdb(mem_ctx, py_default_loadparm_context(mem_ctx), tdbname, 50000,
61 TDB_DEFAULT, O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2);
63 if (eadb == NULL) {
64 PyErr_SetFromErrno(PyExc_IOError);
65 talloc_free(mem_ctx);
66 return NULL;
69 ret = stat(filename, &sbuf);
70 if (ret < 0) {
71 PyErr_SetFromErrno(PyExc_IOError);
72 talloc_free(mem_ctx);
73 return NULL;
76 ZERO_STRUCT(id);
77 id.devid = sbuf.st_dev;
78 id.inode = sbuf.st_ino;
80 ret = xattr_tdb_setattr(eadb, &id, attribute, blob.data, blob.length, 0);
81 if (ret < 0) {
82 PyErr_SetFromErrno(PyExc_TypeError);
83 talloc_free(mem_ctx);
84 return NULL;
86 talloc_free(mem_ctx);
87 Py_RETURN_NONE;
90 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
92 char *filename, *attribute, *tdbname;
93 TALLOC_CTX *mem_ctx;
94 DATA_BLOB blob;
95 PyObject *ret_obj;
96 int ret;
97 ssize_t xattr_size;
98 struct db_context *eadb = NULL;
99 struct file_id id;
100 struct stat sbuf;
102 if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
103 return NULL;
105 mem_ctx = talloc_new(NULL);
107 eadb = db_open_tdb(mem_ctx, py_default_loadparm_context(mem_ctx), tdbname, 50000,
108 TDB_DEFAULT, O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2);
110 if (eadb == NULL) {
111 PyErr_SetFromErrno(PyExc_IOError);
112 talloc_free(mem_ctx);
113 return NULL;
116 ret = stat(filename, &sbuf);
117 if (ret < 0) {
118 PyErr_SetFromErrno(PyExc_IOError);
119 talloc_free(mem_ctx);
120 return NULL;
123 ZERO_STRUCT(id);
124 id.devid = sbuf.st_dev;
125 id.inode = sbuf.st_ino;
127 xattr_size = xattr_tdb_getattr(eadb, mem_ctx, &id, attribute, &blob);
128 if (xattr_size < 0) {
129 PyErr_SetFromErrno(PyExc_TypeError);
130 talloc_free(mem_ctx);
131 return NULL;
133 ret_obj = PyString_FromStringAndSize((char *)blob.data, xattr_size);
134 talloc_free(mem_ctx);
135 return ret_obj;
138 static PyMethodDef py_xattr_methods[] = {
139 { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
140 "wrap_getxattr(filename,attribute) -> blob\n"
141 "Retreive given attribute on the given file." },
142 { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
143 "wrap_setxattr(filename,attribute,value)\n"
144 "Set the given attribute to the given value on the given file." },
145 { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
146 "Return true if xattr are supported on this system\n"},
147 { NULL }
150 void initxattr_tdb(void)
152 PyObject *m;
154 m = Py_InitModule3("xattr_tdb", py_xattr_methods,
155 "Python bindings for xattr manipulation.");
156 if (m == NULL)
157 return;