summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastien Martini <seb@dbzteam.org>2010-05-18 20:53:15 +0200
committerSebastien Martini <seb@dbzteam.org>2010-05-18 20:53:15 +0200
commitfc7f2b79d73b25bbf3f1eb3a677c16e449a24564 (patch)
tree094c8f62a300fd936980fdddad6f03365d88d79c
parente9751bfbaf9a514f76b52ee0290497675e6d67e3 (diff)
downloadpyinotify-fc7f2b79d73b25bbf3f1eb3a677c16e449a24564.tar.gz
Provide errno information from inotify's system calls when available
(Python >=2.6) (reported by Matthew Webber Matthew.Webber@diamond.ac.uk).
-rwxr-xr-xpython2/pyinotify.py25
-rwxr-xr-xpython3/pyinotify.py19
2 files changed, 30 insertions, 14 deletions
diff --git a/python2/pyinotify.py b/python2/pyinotify.py
index 128ed5e..3ff0f05 100755
--- a/python2/pyinotify.py
+++ b/python2/pyinotify.py
@@ -107,7 +107,17 @@ COMPATIBILITY_MODE = False
# Load libc
-LIBC = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
+LIBC = None
+STRERRNO = None
+if sys.version_info[0] >= 2 and sys.version_info[1] >= 6:
+ LIBC = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
+ def _strerrno():
+ code = ctypes.get_errno()
+ return ' Errno=%s (%s)' % (os.strerror(code), errno.errorcode[code])
+ STRERRNO = _strerrno
+else:
+ LIBC = ctypes.CDLL(ctypes.util.find_library('c'))
+ STRERRNO = lambda : ''
# The libc version > 2.4 check.
# XXX: Maybe it is better to check if the libc has the needed functions inside?
@@ -1543,7 +1553,8 @@ class WatchManager:
self._wmd = {} # watch dict key: watch descriptor, value: watch
self._fd = LIBC.inotify_init() # inotify's init, file descriptor
if self._fd < 0:
- raise OSError()
+ err = 'Cannot initialize new instance of inotify%s' % STRERRNO()
+ raise OSError(err)
def get_fd(self):
"""
@@ -1692,8 +1703,8 @@ class WatchManager:
auto_add,
exclude_filter)
if wd < 0:
- err = 'add_watch: cannot watch %s (WD=%d)'
- err = err % (rpath, wd)
+ err = 'add_watch: cannot watch %s WD=%d%s'
+ err = err % (rpath, wd, STRERRNO())
if quiet:
log.error(err)
else:
@@ -1788,8 +1799,8 @@ class WatchManager:
wd_ = addw(self._fd, ctypes.create_string_buffer(apath), mask)
if wd_ < 0:
ret_[awd] = False
- err = 'update_watch: cannot update WD=%d (%s)' % (wd_,
- apath)
+ err = 'update_watch: cannot update %s WD=%d%s'
+ err = err % (apath, wd_, STRERRNO())
if quiet:
log.error(err)
continue
@@ -1898,7 +1909,7 @@ class WatchManager:
wd_ = LIBC.inotify_rm_watch(self._fd, awd)
if wd_ < 0:
ret_[awd] = False
- err = 'rm_watch: cannot remove WD=%d' % awd
+ err = 'rm_watch: cannot remove WD=%d%s' % (awd, STRERRNO())
if quiet:
log.error(err)
continue
diff --git a/python3/pyinotify.py b/python3/pyinotify.py
index b670e15..c3de72a 100755
--- a/python3/pyinotify.py
+++ b/python3/pyinotify.py
@@ -105,7 +105,11 @@ COMPATIBILITY_MODE = False
# Load libc
-LIBC = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
+LIBC = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
+
+def STRERRNO():
+ code = ctypes.get_errno()
+ return '%s (%s)' % (os.strerror(code), errno.errorcode[code])
# The libc version > 2.4 check.
# XXX: Maybe it is better to check if the libc has the needed functions inside?
@@ -1497,7 +1501,8 @@ class WatchManager:
self._wmd = {} # watch dict key: watch descriptor, value: watch
self._fd = LIBC.inotify_init() # inotify's init, file descriptor
if self._fd < 0:
- raise OSError()
+ err = 'Cannot initialize new instance of inotify Errno=%s'
+ raise OSError(err % STRERRNO())
def get_fd(self):
"""
@@ -1649,8 +1654,8 @@ class WatchManager:
auto_add,
exclude_filter)
if wd < 0:
- err = 'add_watch: cannot watch %s (WD=%d)'
- err = err % (rpath, wd)
+ err = 'add_watch: cannot watch %s WD=%d Errno=%s'
+ err = err % (rpath, wd, STRERRNO())
if quiet:
log.error(err)
else:
@@ -1749,8 +1754,8 @@ class WatchManager:
mask)
if wd_ < 0:
ret_[awd] = False
- err = 'update_watch: cannot update WD=%d (%s)' % (wd_,
- apath)
+ err = 'update_watch: cannot update %s WD=%d Errno=%s'
+ err = err % (apath, wd_, STRERRNO())
if quiet:
log.error(err)
continue
@@ -1859,7 +1864,7 @@ class WatchManager:
wd_ = LIBC.inotify_rm_watch(self._fd, awd)
if wd_ < 0:
ret_[awd] = False
- err = 'rm_watch: cannot remove WD=%d' % awd
+ err = 'rm_watch: cannot remove WD=%d Errno=%s' % (awd, STRERRNO())
if quiet:
log.error(err)
continue