diff --git a/main.py b/main.py index c8f3dab..cc3ffeb 100644 --- a/main.py +++ b/main.py @@ -31,16 +31,14 @@ def trigger_scan(): def add_watch_recursive(inotify, path, watch_flags, wd_to_path): """Walk a directory path and add watches recursively.""" - try: - for root, _, _ in os.walk(path): - try: - logging.info(f"Watching {root}") - wd = inotify.add_watch(root, watch_flags) - wd_to_path[wd] = root - except OSError as e: - logging.error(f"Error adding watch for {root}: {e}") - except FileNotFoundError: - logging.warning(f"Could not scan {path} as it was not found.") + logging.info(f"Watching {path} and its subdirectories recursively.") + for root, _, _ in os.walk(path): + try: + logging.info(f"Watching {root}") + wd = inotify.add_watch(root, watch_flags) + wd_to_path[wd] = root + except OSError as e: + logging.error(f"Error adding watch for {root}: {e}") def main(): @@ -49,12 +47,16 @@ def main(): wd_to_path = {} with INotify() as inotify: - for path in secrets.WATCH_PATHS: - logging.info(f"Watching {path} and its subdirectories recursively.") - add_watch_recursive(inotify, path, watch_flags, wd_to_path) - try: while True: + watched_dirs = set(wd_to_path.values()) + for path in secrets.WATCH_PATHS: + if path not in watched_dirs: + if os.path.exists(path): + add_watch_recursive(inotify, path, watch_flags, wd_to_path) + else: + logging.warning(f"Watch path {path} does not exist. Will retry.") + events = inotify.read(timeout=1000) if events: scan_needed = False