mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
Simplified inotify API
This commit is contained in:
parent
8569ed406a
commit
3837e1a1c8
@ -142,6 +142,14 @@ class InotifyMask:
|
|||||||
| MOVED_TO
|
| MOVED_TO
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Helper for typicals events when we need to restart watcher
|
||||||
|
ALL_RESTART_EVENTS = (
|
||||||
|
DELETE_SELF
|
||||||
|
| MOVE_SELF
|
||||||
|
| UNMOUNT
|
||||||
|
| ISDIR
|
||||||
|
)
|
||||||
|
|
||||||
# Special flags for watch()
|
# Special flags for watch()
|
||||||
# DONT_FOLLOW = 0x02000000 # Don't follow a symbolic link
|
# DONT_FOLLOW = 0x02000000 # Don't follow a symbolic link
|
||||||
# EXCL_UNLINK = 0x04000000 # Exclude events on unlinked objects
|
# EXCL_UNLINK = 0x04000000 # Exclude events on unlinked objects
|
||||||
@ -172,6 +180,10 @@ class InotifyEvent:
|
|||||||
name: str
|
name: str
|
||||||
path: str
|
path: str
|
||||||
|
|
||||||
|
@property
|
||||||
|
def restart(self) -> bool:
|
||||||
|
return bool(self.mask & InotifyMask.ALL_RESTART_EVENTS)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
return (
|
||||||
f"<InotifyEvent: wd={self.wd}, mask={InotifyMask.to_string(self.mask)},"
|
f"<InotifyEvent: wd={self.wd}, mask={InotifyMask.to_string(self.mask)},"
|
||||||
@ -190,6 +202,9 @@ class Inotify:
|
|||||||
|
|
||||||
self.__events_queue: "asyncio.Queue[InotifyEvent]" = asyncio.Queue()
|
self.__events_queue: "asyncio.Queue[InotifyEvent]" = asyncio.Queue()
|
||||||
|
|
||||||
|
async def watch_all_modify(self, *paths: str) -> None:
|
||||||
|
await self.watch(InotifyMask.ALL_MODIFY_EVENTS, *paths)
|
||||||
|
|
||||||
async def watch(self, mask: int, *paths: str) -> None:
|
async def watch(self, mask: int, *paths: str) -> None:
|
||||||
for path in paths:
|
for path in paths:
|
||||||
path = os.path.normpath(path)
|
path = os.path.normpath(path)
|
||||||
|
|||||||
@ -30,7 +30,6 @@ from typing import AsyncGenerator
|
|||||||
|
|
||||||
from ....logging import get_logger
|
from ....logging import get_logger
|
||||||
|
|
||||||
from ....inotify import InotifyMask
|
|
||||||
from ....inotify import Inotify
|
from ....inotify import Inotify
|
||||||
|
|
||||||
from ....yamlconf import Option
|
from ....yamlconf import Option
|
||||||
@ -415,8 +414,8 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
with Inotify() as inotify:
|
with Inotify() as inotify:
|
||||||
await inotify.watch(InotifyMask.ALL_MODIFY_EVENTS, *self.__storage.get_watchable_paths())
|
await inotify.watch_all_modify(*self.__storage.get_watchable_paths())
|
||||||
await inotify.watch(InotifyMask.ALL_MODIFY_EVENTS, *self.__drive.get_watchable_paths())
|
await inotify.watch_all_modify(*self.__drive.get_watchable_paths())
|
||||||
|
|
||||||
# После установки вотчеров еще раз проверяем стейт, чтобы ничего не потерять
|
# После установки вотчеров еще раз проверяем стейт, чтобы ничего не потерять
|
||||||
await self.__reload_state()
|
await self.__reload_state()
|
||||||
@ -426,8 +425,9 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
need_reload_state = False
|
need_reload_state = False
|
||||||
for event in (await inotify.get_series(timeout=1)):
|
for event in (await inotify.get_series(timeout=1)):
|
||||||
need_reload_state = True
|
need_reload_state = True
|
||||||
if event.mask & (InotifyMask.DELETE_SELF | InotifyMask.MOVE_SELF | InotifyMask.UNMOUNT | InotifyMask.ISDIR):
|
if event.restart:
|
||||||
# Если выгрузили OTG, изменили каталоги, что-то отмонтировали или делают еще какую-то странную фигню
|
# Если выгрузили OTG, изменили каталоги, что-то отмонтировали или делают еще какую-то странную фигню.
|
||||||
|
# Проверяется маска InotifyMask.ALL_RESTART_EVENTS
|
||||||
logger.info("Got a big inotify event: %s; reinitializing MSD ...", event)
|
logger.info("Got a big inotify event: %s; reinitializing MSD ...", event)
|
||||||
need_restart = True
|
need_restart = True
|
||||||
break
|
break
|
||||||
|
|||||||
@ -28,7 +28,6 @@ from typing import Any
|
|||||||
|
|
||||||
from ...logging import get_logger
|
from ...logging import get_logger
|
||||||
|
|
||||||
from ...inotify import InotifyMask
|
|
||||||
from ...inotify import Inotify
|
from ...inotify import Inotify
|
||||||
|
|
||||||
from ... import aiotools
|
from ... import aiotools
|
||||||
@ -82,15 +81,15 @@ class Plugin(BaseUserGpioDriver):
|
|||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
with Inotify() as inotify:
|
with Inotify() as inotify:
|
||||||
await inotify.watch(InotifyMask.ALL_MODIFY_EVENTS, os.path.dirname(self.__udc_path))
|
await inotify.watch_all_modify(os.path.dirname(self.__udc_path))
|
||||||
await inotify.watch(InotifyMask.ALL_MODIFY_EVENTS, self.__profile_path)
|
await inotify.watch_all_modify(self.__profile_path)
|
||||||
self._notifier.notify()
|
self._notifier.notify()
|
||||||
while True:
|
while True:
|
||||||
need_restart = False
|
need_restart = False
|
||||||
need_notify = False
|
need_notify = False
|
||||||
for event in (await inotify.get_series(timeout=1)):
|
for event in (await inotify.get_series(timeout=1)):
|
||||||
need_notify = True
|
need_notify = True
|
||||||
if event.mask & (InotifyMask.DELETE_SELF | InotifyMask.MOVE_SELF | InotifyMask.UNMOUNT):
|
if event.restart:
|
||||||
logger.warning("Got fatal inotify event: %s; reinitializing OTG-bind ...", event)
|
logger.warning("Got fatal inotify event: %s; reinitializing OTG-bind ...", event)
|
||||||
need_restart = True
|
need_restart = True
|
||||||
break
|
break
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user