LCOV - code coverage report
Current view: top level - source4/ntvfs/posix/python - pyxattr_tdb.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 44 61 72.1 %
Date: 2021-09-23 10:06:22 Functions: 3 4 75.0 %

          Line data    Source code
       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
       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 "system/filesys.h"
      25             : #include <tdb.h>
      26             : #include "lib/tdb_wrap/tdb_wrap.h"
      27             : #include "librpc/ndr/libndr.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"
      35             : 
      36           0 : static PyObject *py_is_xattr_supported(PyObject *self,
      37             :                 PyObject *Py_UNUSED(ignored))
      38             : {
      39           0 :         Py_RETURN_TRUE;
      40             : }
      41             : 
      42         216 : static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
      43             : {
      44             :         char *filename, *attribute, *tdbname;
      45             :         DATA_BLOB blob;
      46             :         Py_ssize_t blobsize;
      47             :         int ret;
      48             :         TALLOC_CTX *mem_ctx;
      49             :         struct loadparm_context *lp_ctx;
      50         216 :         struct db_context *eadb = NULL;
      51             :         struct file_id id;
      52             :         struct stat sbuf;
      53             : 
      54         216 :         if (!PyArg_ParseTuple(args, "sss"PYARG_BYTES_LEN, &tdbname, &filename, &attribute,
      55             :                                                   &blob.data, &blobsize))
      56           0 :                 return NULL;
      57             : 
      58         216 :         blob.length = blobsize;
      59         216 :         mem_ctx = talloc_new(NULL);
      60             : 
      61         216 :         lp_ctx = py_default_loadparm_context(mem_ctx);
      62         216 :         eadb = db_open_tdb(mem_ctx, tdbname, 50000,
      63             :                            lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT),
      64             :                            O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2,
      65             :                            DBWRAP_FLAG_NONE);
      66             : 
      67         216 :         if (eadb == NULL) {
      68           1 :                 PyErr_SetFromErrno(PyExc_IOError);
      69           1 :                 talloc_free(mem_ctx);
      70           1 :                 return NULL;
      71             :         }
      72             : 
      73         218 :         ret = stat(filename, &sbuf);
      74         215 :         if (ret < 0) {
      75           0 :                 PyErr_SetFromErrno(PyExc_IOError);
      76           0 :                 talloc_free(mem_ctx);
      77           0 :                 return NULL;
      78             :         }
      79             : 
      80         215 :         ZERO_STRUCT(id);
      81         215 :         id.devid = sbuf.st_dev;
      82         215 :         id.inode = sbuf.st_ino;
      83             : 
      84         215 :         ret = xattr_tdb_setattr(eadb, &id, attribute, blob.data, blob.length, 0);
      85         215 :         if (ret < 0) {
      86           0 :                 PyErr_SetFromErrno(PyExc_TypeError);
      87           0 :                 talloc_free(mem_ctx);
      88           0 :                 return NULL;
      89             :         }
      90         215 :         talloc_free(mem_ctx);
      91         215 :         Py_RETURN_NONE;
      92             : }
      93             : 
      94         197 : static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
      95             : {
      96             :         char *filename, *attribute, *tdbname;
      97             :         TALLOC_CTX *mem_ctx;
      98             :         struct loadparm_context *lp_ctx;
      99             :         DATA_BLOB blob;
     100             :         PyObject *ret_obj;
     101             :         int ret;
     102             :         ssize_t xattr_size;
     103         197 :         struct db_context *eadb = NULL;
     104             :         struct file_id id;
     105             :         struct stat sbuf;
     106             : 
     107         197 :         if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
     108           0 :                 return NULL;
     109             : 
     110         197 :         mem_ctx = talloc_new(NULL);
     111             : 
     112         197 :         lp_ctx = py_default_loadparm_context(mem_ctx);
     113         197 :         eadb = db_open_tdb(mem_ctx, tdbname, 50000,
     114             :                            lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT),
     115             :                            O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2,
     116             :                            DBWRAP_FLAG_NONE);
     117             : 
     118         197 :         if (eadb == NULL) {
     119           0 :                 PyErr_SetFromErrno(PyExc_IOError);
     120           0 :                 talloc_free(mem_ctx);
     121           0 :                 return NULL;
     122             :         }
     123             : 
     124         199 :         ret = stat(filename, &sbuf);
     125         197 :         if (ret < 0) {
     126           0 :                 PyErr_SetFromErrno(PyExc_IOError);
     127           0 :                 talloc_free(mem_ctx);
     128           0 :                 return NULL;
     129             :         }
     130             : 
     131         197 :         ZERO_STRUCT(id);
     132         197 :         id.devid = sbuf.st_dev;
     133         197 :         id.inode = sbuf.st_ino;
     134             : 
     135         197 :         xattr_size = xattr_tdb_getattr(eadb, mem_ctx, &id, attribute, &blob);
     136         197 :         if (xattr_size < 0) {
     137          24 :                 PyErr_SetFromErrno(PyExc_TypeError);
     138          24 :                 talloc_free(mem_ctx);
     139          24 :                 return NULL;
     140             :         }
     141         173 :         ret_obj = Py_BuildValue(PYARG_BYTES_LEN, blob.data, xattr_size);
     142         173 :         talloc_free(mem_ctx);
     143         173 :         return ret_obj;
     144             : }
     145             : 
     146             : static PyMethodDef py_xattr_methods[] = {
     147             :         { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
     148             :                 "wrap_getxattr(filename,attribute) -> blob\n"
     149             :                 "Retrieve given attribute on the given file." },
     150             :         { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
     151             :                 "wrap_setxattr(filename,attribute,value)\n"
     152             :                 "Set the given attribute to the given value on the given file." },
     153             :         { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
     154             :                 "Return true if xattr are supported on this system\n"},
     155             :         {0}
     156             : };
     157             : 
     158             : static struct PyModuleDef moduledef = {
     159             :     PyModuleDef_HEAD_INIT,
     160             :     .m_name = "xattr_tdb",
     161             :     .m_doc = "Python bindings for xattr manipulation.",
     162             :     .m_size = -1,
     163             :     .m_methods = py_xattr_methods,
     164             : };
     165             : 
     166        1539 : MODULE_INIT_FUNC(xattr_tdb)
     167             : {
     168             :         PyObject *m;
     169             : 
     170        1539 :         m = PyModule_Create(&moduledef);
     171             : 
     172        1539 :         if (m == NULL)
     173           0 :                 return NULL;
     174             : 
     175        1539 :         return m;
     176             : }
     177             : 

Generated by: LCOV version 1.13