s4-torture: Test for #9058
[Samba/gebeck_regimport.git] / source4 / ntvfs / posix / python / pyxattr_native.c
blob4f610a01f5ffa41f12558e3f0061c81835aa6ca7
1 /*
2 Unix SMB/CIFS implementation. Xattr manipulation bindings.
3 Copyright (C) Matthieu Patou <mat@matws.net> 2009
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 "librpc/ndr/libndr.h"
24 #include "system/filesys.h"
26 void initxattr_native(void);
28 static PyObject *py_is_xattr_supported(PyObject *self)
30 #if !defined(HAVE_XATTR_SUPPORT)
31 return Py_False;
32 #else
33 return Py_True;
34 #endif
37 static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
39 char *filename, *attribute;
40 int ret = 0;
41 int blobsize;
42 DATA_BLOB blob;
44 if (!PyArg_ParseTuple(args, "sss#", &filename, &attribute, &blob.data,
45 &blobsize))
46 return NULL;
48 blob.length = blobsize;
49 ret = setxattr(filename, attribute, blob.data, blob.length, 0);
50 if( ret < 0 ) {
51 if (errno == ENOTSUP) {
52 PyErr_SetFromErrno(PyExc_IOError);
53 } else {
54 PyErr_SetFromErrno(PyExc_TypeError);
56 return NULL;
58 Py_RETURN_NONE;
61 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
63 char *filename, *attribute;
64 int len;
65 TALLOC_CTX *mem_ctx;
66 char *buf;
67 PyObject *ret;
68 if (!PyArg_ParseTuple(args, "ss", &filename, &attribute))
69 return NULL;
70 mem_ctx = talloc_new(NULL);
71 len = getxattr(filename,attribute,NULL,0);
72 if( len < 0 ) {
73 if (errno == ENOTSUP) {
74 PyErr_SetFromErrno(PyExc_IOError);
75 } else {
76 PyErr_SetFromErrno(PyExc_TypeError);
78 talloc_free(mem_ctx);
79 return NULL;
81 /* check length ... */
82 buf = talloc_zero_array(mem_ctx, char, len);
83 len = getxattr(filename, attribute, buf, len);
84 if( len < 0 ) {
85 if (errno == ENOTSUP) {
86 PyErr_SetFromErrno(PyExc_IOError);
87 } else {
88 PyErr_SetFromErrno(PyExc_TypeError);
90 talloc_free(mem_ctx);
91 return NULL;
93 ret = PyString_FromStringAndSize(buf, len);
94 talloc_free(mem_ctx);
95 return ret;
98 static PyMethodDef py_xattr_methods[] = {
99 { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
100 "wrap_getxattr(filename,attribute) -> blob\n"
101 "Retreive given attribute on the given file." },
102 { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
103 "wrap_setxattr(filename,attribute,value)\n"
104 "Set the given attribute to the given value on the given file." },
105 { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
106 "Return true if xattr are supported on this system\n"},
107 { NULL }
110 void initxattr_native(void)
112 PyObject *m;
114 m = Py_InitModule3("xattr_native", py_xattr_methods,
115 "Python bindings for xattr manipulation.");
117 if (m == NULL)
118 return;