Skip to content

InotifyTree does not pick up file events if the directories created #110

@oonisim

Description

@oonisim

Environment

  • inotify 0.2.10
  • Ubuntu 22.04 LTS
  • Python 3.10.12

Reproducing the issue

If create the directories after monitoring started, file events e.g CLOSE_WAIT do get picked up.

import os
import pathlib
from inotify.adapters import (
    InotifyTree
)
from inotify.constants import (
    IN_ALL_EVENTS,
)


def test():
    with tempfile.TemporaryDirectory() as directory:
        monitor: InotifyTree = InotifyTree(path=directory, mask=IN_ALL_EVENTS)
        events = monitor.event_gen(yield_nones=False)
        for file in [
            "20231112/0001.txt",
            "20231113/0002.txt",
        ]:
            path_to_file: str = os.path.join(directory, file)
            folder: str = str(pathlib.Path(path_to_file).parent)
            mkdir(folder)
            with open(file=path_to_file, mode="w", encoding='utf-8') as _file:
                _file.write("foobar")

        while True:
            _, event_names, folder, filename = next(events)
            print(f"{folder} {filename} {event_names}")
---
-------------------------------- live log call ---------------------------------
DEBUG    inotify.adapters:adapters.py:58 Inotify handle is (5).
DEBUG    inotify.adapters:adapters.py:343 Adding initial watches on tree: [/tmp/tmphtf2h_ts]
DEBUG    inotify.adapters:adapters.py:82 Adding watch: [/tmp/tmphtf2h_ts]
DEBUG    inotify.adapters:adapters.py:96 Added watch (1): [/tmp/tmphtf2h_ts]
DEBUG    inotify.adapters:adapters.py:228 Events received from epoll: ['IN_ACCESS']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_CREATE', 'IN_ISDIR']
DEBUG    inotify.adapters:adapters.py:295 A directory has been created. We're adding a watch on it (because we're being recursive): [/tmp/tmphtf2h_ts/20231112]
DEBUG    inotify.adapters:adapters.py:82 Adding watch: [/tmp/tmphtf2h_ts/20231112]
DEBUG    inotify.adapters:adapters.py:96 Added watch (2): [/tmp/tmphtf2h_ts/20231112]
/tmp/tmphtf2h_ts 20231112 ['IN_CREATE', 'IN_ISDIR']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_CREATE', 'IN_ISDIR']
DEBUG    inotify.adapters:adapters.py:295 A directory has been created. We're adding a watch on it (because we're being recursive): [/tmp/tmphtf2h_ts/20231113]
DEBUG    inotify.adapters:adapters.py:82 Adding watch: [/tmp/tmphtf2h_ts/20231113]
DEBUG    inotify.adapters:adapters.py:96 Added watch (3): [/tmp/tmphtf2h_ts/20231113]
/tmp/tmphtf2h_ts 20231113 ['IN_CREATE', 'IN_ISDIR']

If the directories created in-advance, file events e.g CLOSE_WAIT get picked up.

def test2():
    with tempfile.TemporaryDirectory() as directory:
        mkdir(os.path.join(directory, "20231112"))
        mkdir(os.path.join(directory, "20231113"))

        monitor: InotifyTree = InotifyTree(path=directory, mask=IN_ALL_EVENTS)
        events = monitor.event_gen(yield_nones=False)
        for file in [
            "20231112/0001.txt",
            "20231113/0002.txt",
        ]:
            path_to_file: str = os.path.join(directory, file)
            folder: str = str(pathlib.Path(path_to_file).parent)
            mkdir(folder)
            with open(file=path_to_file, mode="w", encoding='utf-8') as _file:
                _file.write("foobar")

        while True:
            _, event_names, folder, filename = next(events)
            print(f"{folder} {filename} {event_names}")

-------------------------------- live log call ---------------------------------
DEBUG    inotify.adapters:adapters.py:58 Inotify handle is (5).
DEBUG    inotify.adapters:adapters.py:343 Adding initial watches on tree: [/tmp/tmpdlzodbg_]
DEBUG    inotify.adapters:adapters.py:82 Adding watch: [/tmp/tmpdlzodbg_]
DEBUG    inotify.adapters:adapters.py:96 Added watch (1): [/tmp/tmpdlzodbg_]
DEBUG    inotify.adapters:adapters.py:82 Adding watch: [/tmp/tmpdlzodbg_/20231112]
DEBUG    inotify.adapters:adapters.py:96 Added watch (2): [/tmp/tmpdlzodbg_/20231112]
DEBUG    inotify.adapters:adapters.py:82 Adding watch: [/tmp/tmpdlzodbg_/20231113]
DEBUG    inotify.adapters:adapters.py:96 Added watch (3): [/tmp/tmpdlzodbg_/20231113]
DEBUG    inotify.adapters:adapters.py:228 Events received from epoll: ['IN_ACCESS']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_CREATE']
/tmp/tmpdlzodbg_/20231112 0001.txt ['IN_CREATE']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_OPEN']
/tmp/tmpdlzodbg_/20231112 0001.txt ['IN_OPEN']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_MODIFY']
/tmp/tmpdlzodbg_/20231112 0001.txt ['IN_MODIFY']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_CLOSE_WRITE']
/tmp/tmpdlzodbg_/20231112 0001.txt ['IN_CLOSE_WRITE']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_CREATE']
/tmp/tmpdlzodbg_/20231113 0002.txt ['IN_CREATE']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_OPEN']
/tmp/tmpdlzodbg_/20231113 0002.txt ['IN_OPEN']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_MODIFY']
/tmp/tmpdlzodbg_/20231113 0002.txt ['IN_MODIFY']
DEBUG    inotify.adapters:adapters.py:169 Events received in stream: ['IN_CLOSE_WRITE']
/tmp/tmpdlzodbg_/20231113 0002.txt ['IN_CLOSE_WRITE']

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions