2022-05-15 17:13:50 -07:00
|
|
|
import errno
|
2022-01-21 18:11:16 -05:00
|
|
|
|
2025-05-07 19:11:37 -07:00
|
|
|
import xattr
|
|
|
|
|
|
2024-02-25 00:41:23 +00:00
|
|
|
_cached_attributes: dict[tuple, bytes | None] = {}
|
2020-09-22 11:37:24 +02:00
|
|
|
|
2024-02-25 00:41:23 +00:00
|
|
|
def getxattr(path: str, attr_name: str) -> bytes | None:
|
2022-05-15 17:13:50 -07:00
|
|
|
key = (path, attr_name)
|
|
|
|
|
if key not in _cached_attributes:
|
|
|
|
|
try:
|
2025-05-07 19:11:37 -07:00
|
|
|
response = xattr.getxattr(path, attr_name)
|
2022-05-15 17:13:50 -07:00
|
|
|
except OSError as e:
|
2025-05-07 19:11:37 -07: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):
|
2022-05-15 17:13:50 -07:00
|
|
|
response = None
|
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
_cached_attributes[key] = response
|
|
|
|
|
return _cached_attributes[key]
|
2020-09-22 11:37:24 +02:00
|
|
|
|
2022-01-21 18:11:16 -05:00
|
|
|
def setxattr(path: str, attr_name: str, attr_value: bytes) -> None:
|
2022-05-15 17:13:50 -07:00
|
|
|
_cached_attributes.pop((path, attr_name), None)
|
2025-05-07 19:11:37 -07:00
|
|
|
xattr.setxattr(path, attr_name, attr_value)
|