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.
master
Dustin C. Hatch 2016-09-24 15:41:03 -05:00
parent 533071b981
commit 485fa1c558
1 changed files with 12 additions and 7 deletions

View File

@ -173,16 +173,21 @@ class Inotify(object):
buf = memoryview(os.read(self.__fd, self.BUFSIZE)) buf = memoryview(os.read(self.__fd, self.BUFSIZE))
nread = len(buf) nread = len(buf)
i = 0 i = begin = 0
while i < nread:
wd, mask, cookie, sz = self._unpack(buf)
end = self.STRUCT_SIZE end = self.STRUCT_SIZE
while end < nread and buf[end:end + 1] != b'\x00': while i < nread:
end += 1 wd, mask, cookie, sz = self._unpack(buf[begin:end])
name = buf[self.STRUCT_SIZE:end].tobytes() begin = end
end += sz
x = begin
while buf[x:x + 1] != b'\x00':
x += 1
name = buf[begin:x].tobytes()
pathname = self.__watches[wd] pathname = self.__watches[wd]
i += self.STRUCT_SIZE + sz i += end
yield Event(wd, mask, cookie, name, pathname) yield Event(wd, mask, cookie, name, pathname)
begin = end
end += self.STRUCT_SIZE
def close(self): def close(self):
'''Close all watch descriptors and the inotify descriptor''' '''Close all watch descriptors and the inotify descriptor'''