Add file path to error message on ACL initialisation
authorIustin Pop <iustin@k1024.org>
Wed, 11 Dec 2019 20:18:46 +0000 (21:18 +0100)
committerIustin Pop <iustin@k1024.org>
Wed, 11 Dec 2019 20:18:46 +0000 (21:18 +0100)
acl.c
tests/test_acls.py

diff --git a/acl.c b/acl.c
index c280967ce29b2169da3aa844cb62b693032259d7..f96b67951553f0c05fef43a026051eb73a939d9b 100644 (file)
--- a/acl.c
+++ b/acl.c
@@ -155,6 +155,7 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
     ACL_Object* thesrc = NULL;
     const void *buf = NULL;
     Py_ssize_t bufsize;
+    int set_err = 0;
 
     if(!PyTuple_Check(args) || PyTuple_Size(args) != 0 ||
        (keywds != NULL && PyDict_Check(keywds) && PyDict_Size(keywds) > 1)) {
@@ -179,6 +180,12 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
         fprintf(stderr, "foobar!\n");
         char *path = PyBytes_AS_STRING(file);
         new = acl_get_file(path, ACL_TYPE_ACCESS);
+        // Set custom exception on this failure path which includes
+        // the filename.
+        if (new == NULL) {
+          PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
+          set_err = 1;
+        }
         Py_DECREF(file);
     } else if(text != NULL)
         new = acl_from_text(text);
@@ -192,6 +199,12 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
     else if(filedef != NULL) {
         char *path = PyBytes_AS_STRING(filedef);
         new = acl_get_file(path, ACL_TYPE_DEFAULT);
+        // Set custom exception on this failure path which includes
+        // the filename.
+        if (new == NULL) {
+          PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
+          set_err = 1;
+        }
         Py_DECREF(path);
     }
 #ifdef HAVE_LINUX
@@ -207,7 +220,9 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
         new = acl_init(0);
 
     if(new == NULL) {
-        PyErr_SetFromErrno(PyExc_IOError);
+        if (!set_err) {
+            PyErr_SetFromErrno(PyExc_IOError);
+        }
         return -1;
     }
 
index cc9a2e290bf2a10cc4327cfcfc8d1e82014f74d2..55ba9e1f276240375b1b9df6517f9c734b4f75b4 100644 (file)
@@ -256,6 +256,8 @@ class TestLoad:
         _, fname = get_file(testdir)
         with pytest.raises(IOError):
             posix1e.ACL(file="fname"+".no-such-file")
+        with pytest.raises(IOError):
+            posix1e.ACL(filedef="fname"+".no-such-file")
 
     def test_from_invalid_fd(self, testdir):
         fd, _ = get_file(testdir)