2023-09-27 15:45:31 -07:00
|
|
|
import errno
|
|
|
|
|
2025-06-13 15:59:36 +09:00
|
|
|
import xattr
|
|
|
|
|
2024-06-11 01:36:40 +00:00
|
|
|
_cached_attributes: dict[tuple, bytes | None] = {}
|
2023-09-27 15:45:31 -07:00
|
|
|
|
2024-06-11 01:36:40 +00:00
|
|
|
def getxattr(path: str, attr_name: str) -> bytes | None:
|
2023-09-27 15:45:31 -07:00
|
|
|
key = (path, attr_name)
|
|
|
|
if key not in _cached_attributes:
|
|
|
|
try:
|
2025-06-13 15:59:36 +09:00
|
|
|
response = xattr.getxattr(path, attr_name)
|
2023-09-27 15:45:31 -07:00
|
|
|
except OSError as e:
|
2025-06-13 15:59:36 +09:00
|
|
|
# ENODATA (Linux) or ENOATTR (macOS) means attribute hasn't been set
|
|
|
|
if e.errno == errno.ENODATA or (hasattr(errno, 'ENOATTR') and e.errno == errno.ENOATTR):
|
2023-09-27 15:45:31 -07:00
|
|
|
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)
|
2025-06-13 15:59:36 +09:00
|
|
|
xattr.setxattr(path, attr_name, attr_value)
|