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/>.
23 #include "librpc/ndr/libndr.h"
24 #include "system/filesys.h"
25 #include "lib/util/base64.h"
27 void initxattr_native(void);
29 static PyObject *py_is_xattr_supported(PyObject *self)
31 #if !defined(HAVE_XATTR_SUPPORT)
38 static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
40 char *filename, *attribute;
45 if (!PyArg_ParseTuple(args, "sss#", &filename, &attribute, &blob.data,
49 blob.length = blobsize;
50 ret = setxattr(filename, attribute, blob.data, blob.length, 0);
52 if (errno == ENOTSUP) {
53 PyErr_SetFromErrno(PyExc_IOError);
55 PyErr_SetFromErrno(PyExc_TypeError);
62 static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
64 char *filename, *attribute;
69 if (!PyArg_ParseTuple(args, "ss", &filename, &attribute))
71 mem_ctx = talloc_new(NULL);
72 len = getxattr(filename,attribute,NULL,0);
74 if (errno == ENOTSUP) {
75 PyErr_SetFromErrno(PyExc_IOError);
77 PyErr_SetFromErrno(PyExc_TypeError);
82 /* check length ... */
83 buf = talloc_zero_array(mem_ctx, char, len);
84 len = getxattr(filename, attribute, buf, len);
86 if (errno == ENOTSUP) {
87 PyErr_SetFromErrno(PyExc_IOError);
89 PyErr_SetFromErrno(PyExc_TypeError);
94 ret = PyString_FromStringAndSize(buf, len);
99 static PyMethodDef py_xattr_methods[] = {
100 { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
101 "wrap_getxattr(filename,attribute) -> blob\n"
102 "Retrieve given attribute on the given file." },
103 { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
104 "wrap_setxattr(filename,attribute,value)\n"
105 "Set the given attribute to the given value on the given file." },
106 { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
107 "Return true if xattr are supported on this system\n"},
111 void initxattr_native(void)
115 m = Py_InitModule3("xattr_native", py_xattr_methods,
116 "Python bindings for xattr manipulation.");