LCOV - code coverage report
Current view: top level - source4/ntvfs/posix/python - pyxattr_native.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 31 41 75.6 %
Date: 2021-08-25 13:27:56 Functions: 4 4 100.0 %

          Line data    Source code
       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
       6             : 
       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.
      11             : 
      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.
      16             : 
      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/>.
      19             : */
      20             : 
      21             : #include <Python.h>
      22             : #include "python/py3compat.h"
      23             : #include "includes.h"
      24             : #include "librpc/ndr/libndr.h"
      25             : #include "system/filesys.h"
      26             : #include "lib/util/base64.h"
      27             : 
      28           2 : static PyObject *py_is_xattr_supported(PyObject *self,
      29             :                 PyObject *Py_UNUSED(ignored))
      30             : {
      31             : #if !defined(HAVE_XATTR_SUPPORT)
      32             :         Py_RETURN_FALSE;
      33             : #else
      34           2 :         Py_RETURN_TRUE;
      35             : #endif
      36             : }
      37             : 
      38          57 : static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
      39             : {
      40             :         char *filename, *attribute;
      41          57 :         int ret = 0;
      42             :         Py_ssize_t blobsize;
      43             :         DATA_BLOB blob;
      44             : 
      45          57 :         if (!PyArg_ParseTuple(args, "ss"PYARG_BYTES_LEN, &filename, &attribute, &blob.data,
      46             :         &blobsize))
      47           0 :                 return NULL;
      48             : 
      49          57 :         blob.length = blobsize;
      50          57 :         ret = setxattr(filename, attribute, blob.data, blob.length, 0);
      51          57 :         if( ret < 0 ) {
      52          55 :                 if (errno == ENOTSUP) {
      53           0 :                         PyErr_SetFromErrno(PyExc_IOError);
      54             :                 } else {
      55          55 :                         PyErr_SetFromErrno(PyExc_TypeError);
      56             :                 }
      57          36 :                 return NULL;
      58             :         }
      59           2 :         Py_RETURN_NONE;
      60             : }
      61             : 
      62         113 : static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
      63             : {
      64             :         char *filename, *attribute;
      65             :         int len;
      66             :         TALLOC_CTX *mem_ctx;
      67             :         char *buf;
      68             :         PyObject *ret;
      69         113 :         if (!PyArg_ParseTuple(args, "ss", &filename, &attribute))
      70           0 :                 return NULL;
      71         113 :         mem_ctx = talloc_new(NULL);
      72         113 :         len = getxattr(filename,attribute,NULL,0);
      73         113 :         if( len < 0 ) {
      74         110 :                 if (errno == ENOTSUP) {
      75           0 :                         PyErr_SetFromErrno(PyExc_IOError);
      76             :                 } else {
      77         110 :                         PyErr_SetFromErrno(PyExc_TypeError);
      78             :                 }
      79         110 :                 talloc_free(mem_ctx);
      80         110 :                 return NULL;
      81             :         }
      82             :         /* check length ... */
      83           3 :         buf = talloc_zero_array(mem_ctx, char, len);
      84           3 :         len = getxattr(filename, attribute, buf, len);
      85           3 :         if( len < 0 ) {
      86           0 :                 if (errno == ENOTSUP) {
      87           0 :                         PyErr_SetFromErrno(PyExc_IOError);
      88             :                 } else {
      89           0 :                         PyErr_SetFromErrno(PyExc_TypeError);
      90             :                 }
      91           0 :                 talloc_free(mem_ctx);
      92           0 :                 return NULL;
      93             :         }
      94           3 :         ret = Py_BuildValue(PYARG_BYTES_LEN, buf, len);
      95           3 :         talloc_free(mem_ctx);
      96           3 :         return ret;
      97             : }
      98             : 
      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"},
     108             :         {0}
     109             : };
     110             : 
     111             : static struct PyModuleDef moduledef = {
     112             :     PyModuleDef_HEAD_INIT,
     113             :     .m_name = "xattr_native",
     114             :     .m_doc = "Python bindings for xattr manipulation.",
     115             :     .m_size = -1,
     116             :     .m_methods = py_xattr_methods,
     117             : };
     118             : 
     119        1536 : MODULE_INIT_FUNC(xattr_native)
     120             : {
     121             :         PyObject *m;
     122             : 
     123        1536 :         m = PyModule_Create(&moduledef);
     124             : 
     125        1536 :         if (m == NULL)
     126           0 :                 return NULL;
     127             : 
     128        1536 :         return m;
     129             : }

Generated by: LCOV version 1.13