From 6e1cec477ea3a3afc62aa547b4c8fa05e05c66c7 Mon Sep 17 00:00:00 2001 From: Iustin Pop Date: Sun, 29 Jun 2008 22:33:12 +0200 Subject: [PATCH] Add the new-style get function --- test/test_xattr.py | 8 ++--- xattr.c | 73 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/test/test_xattr.py b/test/test_xattr.py index 12eed6c..8e3c9a6 100644 --- a/test/test_xattr.py +++ b/test/test_xattr.py @@ -91,7 +91,7 @@ class xattrTest(unittest.TestCase): self.USER_ATTR, self.USER_VAL, flags=xattr.XATTR_CREATE) self.failUnlessEqual(xattr.listxattr(item, symlink), [self.USER_ATTR]) - self.failUnlessEqual(xattr.getxattr(item, self.USER_ATTR, symlink), + self.failUnlessEqual(xattr.get(item, self.USER_ATTR, nofollow=symlink), self.USER_VAL) self.failUnlessEqual(xattr.get_all(item, nofollow=symlink), [(self.USER_ATTR, self.USER_VAL)]) @@ -172,7 +172,7 @@ class xattrTest(unittest.TestCase): self.failUnlessEqual(xattr.listxattr(fname), []) xattr.set(fname, self.USER_ATTR, self.USER_VAL) self.failUnlessEqual(xattr.listxattr(fh), [self.USER_ATTR]) - self.failUnlessEqual(xattr.getxattr(fo, self.USER_ATTR), + self.failUnlessEqual(xattr.get(fo, self.USER_ATTR), self.USER_VAL) self.failUnlessEqual(xattr.get_all(fo), [(self.USER_ATTR, self.USER_VAL)]) @@ -219,7 +219,7 @@ class xattrTest(unittest.TestCase): BINVAL = "abc" + '\0' + "def" xattr.set(fname, self.USER_ATTR, BINVAL) self.failUnlessEqual(xattr.listxattr(fname), [self.USER_ATTR]) - self.failUnlessEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL) + self.failUnlessEqual(xattr.get(fname, self.USER_ATTR), BINVAL) self.failUnlessEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)]) xattr.removexattr(fname, self.USER_ATTR) @@ -245,7 +245,7 @@ class xattrTest(unittest.TestCase): for i in range(self.MANYOPS_COUNT): self.failUnlessEqual(xattr.listxattr(fh), VL) for i in range(self.MANYOPS_COUNT): - self.failUnlessEqual(xattr.getxattr(fh, self.USER_ATTR), + self.failUnlessEqual(xattr.get(fh, self.USER_ATTR), self.USER_VAL) for i in range(self.MANYOPS_COUNT): self.failUnlessEqual(xattr.get_all(fh), diff --git a/xattr.c b/xattr.c index e0c38d7..250284f 100644 --- a/xattr.c +++ b/xattr.c @@ -122,7 +122,7 @@ static char __pygetxattr_doc__[] = " the file name given is a symbolic link, makes the\n" " function operate on the symbolic link itself instead\n" " of its target;\n" - "@deprecated: this function has been replace with the L{get_all} function" + "@deprecated: this function has been replace with the L{get} function" " which replaces the positional parameters with keyword ones\n" ; @@ -170,6 +170,75 @@ pygetxattr(PyObject *self, PyObject *args) return res; } +/* Wrapper for getxattr */ +static char __get_doc__[] = + "Get the value of a given extended attribute.\n" + "\n" + "Parameters:\n" + " - a string representing filename, or a file-like object,\n" + " or a file descriptor; this represents the file on \n" + " which to act\n" + " - a string, representing the attribute whose value to retrieve;\n" + " usually in form of system.posix_acl or user.mime_type\n" + " - (optional) a boolean value (defaults to false), which, if\n" + " the file name given is a symbolic link, makes the\n" + " function operate on the symbolic link itself instead\n" + " of its target;\n" + ; + +static PyObject * +xattr_get(PyObject *self, PyObject *args, PyObject *keywds) +{ + PyObject *myarg; + target_t tgt; + int nofollow=0; + char *attrname, *namebuf; + const char *fullname; + char *buf; + char *ns = NULL; + int nalloc, nret; + PyObject *res; + static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL}; + + /* Parse the arguments */ + if (!PyArg_ParseTupleAndKeywords(args, keywds, "Os|iz", kwlist, + &myarg, &attrname, &nofollow, &ns)) + return NULL; + if(!convertObj(myarg, &tgt, nofollow)) + return NULL; + + fullname = merge_ns(ns, attrname, &namebuf); + + /* Find out the needed size of the buffer */ + if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) { + return PyErr_SetFromErrno(PyExc_IOError); + } + + /* Try to allocate the memory, using Python's allocator */ + if((buf = PyMem_Malloc(nalloc)) == NULL) { + PyMem_Free(namebuf); + PyErr_NoMemory(); + return NULL; + } + + /* Now retrieve the attribute value */ + if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) { + PyMem_Free(buf); + PyMem_Free(namebuf); + return PyErr_SetFromErrno(PyExc_IOError); + } + + /* Create the string which will hold the result */ + res = PyString_FromStringAndSize(buf, nret); + + /* Free the buffers, they are no longer needed */ + PyMem_Free(namebuf); + PyMem_Free(buf); + + /* Return the result */ + return res; +} + /* Wrapper for getxattr */ static char __get_all_doc__[] = "Get all the extended attributes of an item.\n" @@ -538,6 +607,8 @@ pylistxattr(PyObject *self, PyObject *args) static PyMethodDef xattr_methods[] = { {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ }, + {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS, + __get_doc__ }, {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS, __get_all_doc__ }, {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ }, -- 2.39.5