Files
sunnypilot/system/loggerd/xattr_cache.py
T
DevTekVE a79a044a25 Remove unnecessary os module import and file xattr removal code
The import of the 'os' module is eliminated to clean up the code, as it is no longer needed. Furthermore, the code for removing the extended attribute from all files in the current directory has been removed as well. This streamlines the setxattr function and focuses only on setting the attributes without additional file operations.
2024-05-28 19:59:36 +02:00

29 lines
890 B
Python

import errno
import platform
if platform.system() == 'Darwin': # macOS
from xattr import getxattr as _getxattr
from xattr import setxattr as _setxattr
else:
from os import getxattr as _getxattr
from os import setxattr as _setxattr
_cached_attributes: dict[tuple, bytes | None] = {}
def getxattr(path: str, attr_name: str) -> bytes | None:
key = (path, attr_name)
if key not in _cached_attributes:
try:
response = _getxattr(path, attr_name)
except OSError as e:
# ENODATA means attribute hasn't been set
if e.errno == errno.ENODATA or e.errno == errno.ENOATTR:
response = None
else:
raise
_cached_attributes[key] = response
return _cached_attributes[key]
def setxattr(path: str, attr_name: str, attr_value: bytes) -> None:
_cached_attributes.pop((path, attr_name), None)
return _setxattr(path, attr_name, attr_value)