{"content":"\"\"\"Synchronous inotify generations cache content hashes, never human revisions.\n\nSourceGuard still probes inventories, file identities and SQLite data_version on\nevery operation. Unsupported/lost/overflowed watches fall back to full hashing.\n\"\"\"\nimport ctypes\nimport os\nfrom pathlib import Path\nimport struct\nimport sys\nimport time\n\nfrom .cluster_targets import digest\nfrom .pallet_description_round4_guard import ContentSourceGuard,binding_guard as content_guard\nfrom .mask_taxonomy_source_guard import SourceGuard\n\n\nclass WatchUnavailable(OSError):pass\n\n\nclass FileGenerations:\n    MODIFY=0x2;ATTRIB=0x4;CLOSE_WRITE=0x8;MOVED_FROM=0x40;MOVED_TO=0x80;CREATE=0x100;DELETE=0x200\n    DELETE_SELF=0x400;MOVE_SELF=0x800;UNMOUNT=0x2000;OVERFLOW=0x4000;IGNORED=0x8000\n    MASK=MODIFY|ATTRIB|CLOSE_WRITE|MOVED_FROM|MOVED_TO|CREATE|DELETE|DELETE_SELF|MOVE_SELF|UNMOUNT\n    HEADER=struct.Struct('iIII')\n\n    @staticmethod\n    def local_filesystem(libc,path):\n        # Remote/FUSE filesystems may change through another host without local\n        # fsnotify events. Only enable this optimization on known local Linux FS.\n        statfs=libc.statfs;statfs.argtypes=[ctypes.c_char_p,ctypes.c_void_p];statfs.restype=ctypes.c_int\n        buffer=ctypes.create_string_buffer(512)\n        if statfs(os.fsencode(path),buffer)!=0:return False\n        kind=ctypes.c_long.from_buffer(buffer).value&0xffffffff\n        return kind in {0xef53,0x58465342,0x9123683e,0x794c7630,0x01021994} # ext4,xfs,btrfs,overlay,tmpfs\n\n    def __init__(self,paths):\n        self.fd=None;self.names={};self.generation=0\n        if sys.platform!='linux':raise WatchUnavailable('Linux inotify unavailable')\n        libc=ctypes.CDLL(None,use_errno=True)\n        init=libc.inotify_init1;init.argtypes=[ctypes.c_int];init.restype=ctypes.c_int\n        add=libc.inotify_add_watch;add.argtypes=[ctypes.c_int,ctypes.c_char_p,ctypes.c_uint32];add.restype=ctypes.c_int\n        fd=init(os.O_NONBLOCK|os.O_CLOEXEC)\n        if fd<0:raise WatchUnavailable(ctypes.get_errno(),'inotify_init1 failed')\n        self.fd=fd\n        try:\n            parents={}\n            for raw in paths:\n                path=Path(raw).absolute()\n                # Parent watches must cover the actual bytes; symlink chains use\n                # the conservative content guard rather than assume coverage.\n                if path.resolve(strict=True)!=path:raise WatchUnavailable('Symlinked source uses full hash fallback')\n                parents.setdefault(path.parent,set()).add(os.fsencode(path.name))\n            devices=set()\n            for parent,names in sorted(parents.items()):\n                device=parent.stat().st_dev\n                if device not in devices:\n                    if not self.local_filesystem(libc,parent):raise WatchUnavailable('Nonlocal/unsupported filesystem uses full hashing')\n                    devices.add(device)\n                wd=add(fd,os.fsencode(parent),self.MASK)\n                if wd<0:raise WatchUnavailable(ctypes.get_errno(),'inotify watch registration failed')\n                self.names.setdefault(wd,set()).update(names)\n        except Exception:self.close();raise\n\n    def feed(self,data):\n        offset=0\n        while offset<len(data):\n            if len(data)-offset<self.HEADER.size:raise WatchUnavailable('Truncated inotify event')\n            wd,mask,cookie,length=self.HEADER.unpack_from(data,offset);offset+=self.HEADER.size\n            if offset+length>len(data):raise WatchUnavailable('Truncated inotify filename')\n            name=data[offset:offset+length].split(b'\\0',1)[0];offset+=length\n            if mask&(self.OVERFLOW|self.IGNORED|self.UNMOUNT|self.DELETE_SELF|self.MOVE_SELF):\n                raise WatchUnavailable('Source watch overflowed or lost coverage')\n            if wd not in self.names:raise WatchUnavailable('Unknown source watch event')\n            if name in self.names[wd] and mask&self.MASK:self.generation+=1\n\n    def drain(self):\n        if self.fd is None:raise WatchUnavailable('Source watch closed')\n        while True:\n            try:data=os.read(self.fd,65536)\n            except BlockingIOError:break\n            except InterruptedError:continue\n            if not data:raise WatchUnavailable('Source watch unexpectedly ended')\n            self.feed(data)\n        return self.generation\n\n    def close(self):\n        if self.fd is not None:\n            fd,self.fd=self.fd,None;os.close(fd)\n\n\nclass GenerationSourceGuard(ContentSourceGuard):\n    def __init__(self,**kwargs):\n        super().__init__(**kwargs)\n        self.watcher=None;self.watch_error=None;self.hash_key=None;self.hash_value=None\n        self.content_hash_passes=0;self.content_hash_seconds=0.\n        try:self.watcher=FileGenerations(self.project/n for n in self.expected_files)\n        except (OSError,AttributeError):self.watch_error='watch_setup_unavailable_using_full_content_hash'\n\n    def _hashes(self):\n        started=time.perf_counter()\n        try:return tuple((n,digest(self.project/n)) for n in sorted(self.expected_files))\n        finally:self.content_hash_passes+=1;self.content_hash_seconds+=time.perf_counter()-started\n\n    def _fallback(self):\n        if self.watcher:self.watcher.close();self.watcher=None\n        self.hash_key=None;self.hash_value=None\n        self.watch_error='watch_coverage_lost_using_full_content_hash'\n\n    def _token(self):\n        # Keep the base guard's live SQLite connections/data_version checks.\n        token=SourceGuard._token(self)\n        if self.watcher is not None:\n            try:\n                generation=self.watcher.drain();key=(token[2],generation)\n                if self.hash_key!=key:\n                    value=self._hashes()\n                    if self.watcher.drain()!=generation:\n                        # Do not associate bytes read during a write with a stable\n                        # generation. Recheck in current()'s bounded retry loop.\n                        self.hash_key=None;self.hash_value=None\n                        return token,('unstable_watch',self.watcher.generation,object())\n                    self.hash_key=key;self.hash_value=value\n                return token,('watched_content',generation,self.hash_value)\n            except OSError:self._fallback()\n        # No TTL or stat-only shortcut when kernel watch coverage is uncertain.\n        return token,('full_content_fallback',self._hashes())\n\n    def stats(self):\n        with self.lock:\n            return dict(super().stats(),content_hash_passes=self.content_hash_passes,content_hash_seconds=self.content_hash_seconds,\n                generation_watch_enabled=self.watcher is not None,watch_error=self.watch_error)\n\n    def close(self):\n        with self.lock:\n            if self.watcher:self.watcher.close();self.watcher=None\n            super().close()\n\n\ndef binding_guard():\n    original=content_guard()\n    try:\n        return GenerationSourceGuard(project=original.project,databases=original.databases,files=original.files,inventories=original.inventories,\n            discover_databases=original.discover_databases,discover_inventories=original.discover_inventories,\n            extra_databases=set(original.databases)-original.expected_databases,pinned_files=original.pinned_files)\n    finally:original.close()\n","path":"pallet/pallet_description_generation_guard.py"}
