s4-torture: Test for #9058
[Samba/gebeck_regimport.git] / source4 / ntvfs / posix / python / pyposix_eadb.c
blob48310cc0168ea92c7108cb8ccdc50e3a38b2e881
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 "ntvfs/posix/posix_eadb.h"
28 #include "libcli/util/pyerrors.h"
29 #include "param/pyparam.h"
31 void initxattr_tdb(void);
33 static PyObject *py_is_xattr_supported(PyObject *self)
35 return Py_True;
38 static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
40 char *filename, *attribute, *tdbname;
41 DATA_BLOB blob;
42 int blobsize;
43 NTSTATUS status;
44 TALLOC_CTX *mem_ctx;
45 struct tdb_wrap *eadb;
47 if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute,
48 &blob.data, &blobsize))
49 return NULL;
51 blob.length = blobsize;
52 mem_ctx = talloc_new(NULL);
53 eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
54 TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
55 py_default_loadparm_context(mem_ctx));
57 if (eadb == NULL) {
58 PyErr_SetFromErrno(PyExc_IOError);
59 talloc_free(mem_ctx);
60 return NULL;
62 status = push_xattr_blob_tdb_raw(eadb, attribute, filename, -1,
63 &blob);
64 if (!NT_STATUS_IS_OK(status)) {
65 PyErr_SetNTSTATUS(status);
66 talloc_free(mem_ctx);
67 return NULL;
69 talloc_free(mem_ctx);
70 Py_RETURN_NONE;
73 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
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;
82 if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
83 return NULL;
85 mem_ctx = talloc_new(NULL);
86 eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
87 TDB_DEFAULT, O_RDWR|O_CREAT, 0600, py_default_loadparm_context(mem_ctx));
88 if (eadb == NULL) {
89 PyErr_SetFromErrno(PyExc_IOError);
90 talloc_free(mem_ctx);
91 return NULL;
93 status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename,
94 -1, 100, &blob);
95 if (!NT_STATUS_IS_OK(status)) {
96 PyErr_SetNTSTATUS(status);
97 talloc_free(mem_ctx);
98 return NULL;
100 ret = PyString_FromStringAndSize((char *)blob.data, blob.length);
101 talloc_free(mem_ctx);
102 return ret;
105 static PyMethodDef py_posix_eadb_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 }
117 void initposix_eadb(void)
119 PyObject *m;
121 m = Py_InitModule3("posix_eadb", py_posix_eadb_methods,
122 "Python bindings for xattr manipulation.");
123 if (m == NULL)
124 return;