From 485fa1c558566c08d2ec8c4946fa053b5b1509a2 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 24 Sep 2016 15:41:03 -0500 Subject: [PATCH] inotify: Correctly parse multiple structs at once When multiple events arrive together, the `Inotify.read()` method parsed them incorrectly and emitted the same `Event` for each one. This commit corrects this behavior by appropriately parsing each struct. --- src/linuxapi/inotify.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/linuxapi/inotify.py b/src/linuxapi/inotify.py index 1fc7704..485837b 100644 --- a/src/linuxapi/inotify.py +++ b/src/linuxapi/inotify.py @@ -173,16 +173,21 @@ class Inotify(object): buf = memoryview(os.read(self.__fd, self.BUFSIZE)) nread = len(buf) - i = 0 + i = begin = 0 + end = self.STRUCT_SIZE while i < nread: - wd, mask, cookie, sz = self._unpack(buf) - end = self.STRUCT_SIZE - while end < nread and buf[end:end + 1] != b'\x00': - end += 1 - name = buf[self.STRUCT_SIZE:end].tobytes() + wd, mask, cookie, sz = self._unpack(buf[begin:end]) + begin = end + end += sz + x = begin + while buf[x:x + 1] != b'\x00': + x += 1 + name = buf[begin:x].tobytes() pathname = self.__watches[wd] - i += self.STRUCT_SIZE + sz + i += end yield Event(wd, mask, cookie, name, pathname) + begin = end + end += self.STRUCT_SIZE def close(self): '''Close all watch descriptors and the inotify descriptor'''