xattr.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """A basic extended attributes (xattr) implementation for Linux and MacOS X
  2. """
  3. import errno
  4. import os
  5. import sys
  6. import tempfile
  7. from ctypes import CDLL, create_string_buffer, c_ssize_t, c_size_t, c_char_p, c_int, c_uint32, get_errno
  8. from ctypes.util import find_library
  9. def is_enabled(path=None):
  10. """Determine if xattr is enabled on the filesystem
  11. """
  12. with tempfile.NamedTemporaryFile(dir=path) as fd:
  13. try:
  14. setxattr(fd.fileno(), 'user.name', b'value')
  15. except OSError:
  16. return False
  17. return getxattr(fd.fileno(), 'user.name') == b'value'
  18. def get_all(path, follow_symlinks=True):
  19. try:
  20. return dict((name, getxattr(path, name, follow_symlinks=follow_symlinks))
  21. for name in listxattr(path, follow_symlinks=follow_symlinks))
  22. except OSError as e:
  23. if e.errno in (errno.ENOTSUP, errno.EPERM):
  24. return {}
  25. libc = CDLL(find_library('c'), use_errno=True)
  26. def _check(rv, path=None):
  27. if rv < 0:
  28. raise OSError(get_errno(), path)
  29. return rv
  30. if sys.platform.startswith('linux'):
  31. libc.llistxattr.argtypes = (c_char_p, c_char_p, c_size_t)
  32. libc.llistxattr.restype = c_ssize_t
  33. libc.flistxattr.argtypes = (c_int, c_char_p, c_size_t)
  34. libc.flistxattr.restype = c_ssize_t
  35. libc.lsetxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t, c_int)
  36. libc.lsetxattr.restype = c_int
  37. libc.fsetxattr.argtypes = (c_int, c_char_p, c_char_p, c_size_t, c_int)
  38. libc.fsetxattr.restype = c_int
  39. libc.lgetxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t)
  40. libc.lgetxattr.restype = c_ssize_t
  41. libc.fgetxattr.argtypes = (c_int, c_char_p, c_char_p, c_size_t)
  42. libc.fgetxattr.restype = c_ssize_t
  43. def listxattr(path, *, follow_symlinks=True):
  44. if isinstance(path, str):
  45. path = os.fsencode(path)
  46. if isinstance(path, int):
  47. func = libc.flistxattr
  48. elif follow_symlinks:
  49. func = libc.listxattr
  50. else:
  51. func = libc.llistxattr
  52. n = _check(func(path, None, 0), path)
  53. if n == 0:
  54. return []
  55. namebuf = create_string_buffer(n)
  56. n2 = _check(func(path, namebuf, n), path)
  57. if n2 != n:
  58. raise Exception('listxattr failed')
  59. return [os.fsdecode(name) for name in namebuf.raw.split(b'\0')[:-1] if not name.startswith(b'system.posix_acl_')]
  60. def getxattr(path, name, *, follow_symlinks=True):
  61. name = os.fsencode(name)
  62. if isinstance(path, str):
  63. path = os.fsencode(path)
  64. if isinstance(path, int):
  65. func = libc.fgetxattr
  66. elif follow_symlinks:
  67. func = libc.getxattr
  68. else:
  69. func = libc.lgetxattr
  70. n = _check(func(path, name, None, 0))
  71. if n == 0:
  72. return
  73. valuebuf = create_string_buffer(n)
  74. n2 = _check(func(path, name, valuebuf, n), path)
  75. if n2 != n:
  76. raise Exception('getxattr failed')
  77. return valuebuf.raw
  78. def setxattr(path, name, value, *, follow_symlinks=True):
  79. name = os.fsencode(name)
  80. value = value and os.fsencode(value)
  81. if isinstance(path, str):
  82. path = os.fsencode(path)
  83. if isinstance(path, int):
  84. func = libc.fsetxattr
  85. elif follow_symlinks:
  86. func = libc.setxattr
  87. else:
  88. func = libc.lsetxattr
  89. _check(func(path, name, value, len(value) if value else 0, 0), path)
  90. elif sys.platform == 'darwin':
  91. libc.listxattr.argtypes = (c_char_p, c_char_p, c_size_t, c_int)
  92. libc.listxattr.restype = c_ssize_t
  93. libc.flistxattr.argtypes = (c_int, c_char_p, c_size_t)
  94. libc.flistxattr.restype = c_ssize_t
  95. libc.setxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t, c_uint32, c_int)
  96. libc.setxattr.restype = c_int
  97. libc.fsetxattr.argtypes = (c_int, c_char_p, c_char_p, c_size_t, c_uint32, c_int)
  98. libc.fsetxattr.restype = c_int
  99. libc.getxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t, c_uint32, c_int)
  100. libc.getxattr.restype = c_ssize_t
  101. libc.fgetxattr.argtypes = (c_int, c_char_p, c_char_p, c_size_t, c_uint32, c_int)
  102. libc.fgetxattr.restype = c_ssize_t
  103. XATTR_NOFOLLOW = 0x0001
  104. def listxattr(path, *, follow_symlinks=True):
  105. func = libc.listxattr
  106. flags = 0
  107. if isinstance(path, str):
  108. path = os.fsencode(path)
  109. if isinstance(path, int):
  110. func = libc.flistxattr
  111. elif not follow_symlinks:
  112. flags = XATTR_NOFOLLOW
  113. n = _check(func(path, None, 0, flags), path)
  114. if n == 0:
  115. return []
  116. namebuf = create_string_buffer(n)
  117. n2 = _check(func(path, namebuf, n, flags), path)
  118. if n2 != n:
  119. raise Exception('listxattr failed')
  120. return [os.fsdecode(name) for name in namebuf.raw.split(b'\0')[:-1]]
  121. def getxattr(path, name, *, follow_symlinks=True):
  122. name = os.fsencode(name)
  123. func = libc.getxattr
  124. flags = 0
  125. if isinstance(path, str):
  126. path = os.fsencode(path)
  127. if isinstance(path, int):
  128. func = libc.fgetxattr
  129. elif not follow_symlinks:
  130. flags = XATTR_NOFOLLOW
  131. n = _check(func(path, name, None, 0, 0, flags))
  132. if n == 0:
  133. return
  134. valuebuf = create_string_buffer(n)
  135. n2 = _check(func(path, name, valuebuf, n, 0, flags), path)
  136. if n2 != n:
  137. raise Exception('getxattr failed')
  138. return valuebuf.raw
  139. def setxattr(path, name, value, *, follow_symlinks=True):
  140. name = os.fsencode(name)
  141. value = value and os.fsencode(value)
  142. func = libc.setxattr
  143. flags = 0
  144. if isinstance(path, str):
  145. path = os.fsencode(path)
  146. if isinstance(path, int):
  147. func = libc.fsetxattr
  148. elif not follow_symlinks:
  149. flags = XATTR_NOFOLLOW
  150. _check(func(path, name, value, len(value) if value else 0, 0, flags), path)
  151. elif sys.platform.startswith('freebsd'):
  152. EXTATTR_NAMESPACE_USER = 0x0001
  153. libc.extattr_list_fd.argtypes = (c_int, c_int, c_char_p, c_size_t)
  154. libc.extattr_list_fd.restype = c_ssize_t
  155. libc.extattr_list_link.argtypes = (c_char_p, c_int, c_char_p, c_size_t)
  156. libc.extattr_list_link.restype = c_ssize_t
  157. libc.extattr_list_file.argtypes = (c_char_p, c_int, c_char_p, c_size_t)
  158. libc.extattr_list_file.restype = c_ssize_t
  159. libc.extattr_get_fd.argtypes = (c_int, c_int, c_char_p, c_char_p, c_size_t)
  160. libc.extattr_get_fd.restype = c_ssize_t
  161. libc.extattr_get_link.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_size_t)
  162. libc.extattr_get_link.restype = c_ssize_t
  163. libc.extattr_get_file.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_size_t)
  164. libc.extattr_get_file.restype = c_ssize_t
  165. libc.extattr_set_fd.argtypes = (c_int, c_int, c_char_p, c_char_p, c_size_t)
  166. libc.extattr_set_fd.restype = c_int
  167. libc.extattr_set_link.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_size_t)
  168. libc.extattr_set_link.restype = c_int
  169. libc.extattr_set_file.argtypes = (c_char_p, c_int, c_char_p, c_char_p, c_size_t)
  170. libc.extattr_set_file.restype = c_int
  171. def listxattr(path, *, follow_symlinks=True):
  172. ns = EXTATTR_NAMESPACE_USER
  173. if isinstance(path, str):
  174. path = os.fsencode(path)
  175. if isinstance(path, int):
  176. func = libc.extattr_list_fd
  177. elif follow_symlinks:
  178. func = libc.extattr_list_file
  179. else:
  180. func = libc.extattr_list_link
  181. n = _check(func(path, ns, None, 0), path)
  182. if n == 0:
  183. return []
  184. namebuf = create_string_buffer(n)
  185. n2 = _check(func(path, ns, namebuf, n), path)
  186. if n2 != n:
  187. raise Exception('listxattr failed')
  188. names = []
  189. mv = memoryview(namebuf.raw)
  190. while mv:
  191. length = mv[0]
  192. # Python < 3.3 returns bytes instead of int
  193. if isinstance(length, bytes):
  194. length = ord(length)
  195. names.append(os.fsdecode(bytes(mv[1:1+length])))
  196. mv = mv[1+length:]
  197. return names
  198. def getxattr(path, name, *, follow_symlinks=True):
  199. name = os.fsencode(name)
  200. if isinstance(path, str):
  201. path = os.fsencode(path)
  202. if isinstance(path, int):
  203. func = libc.extattr_get_fd
  204. elif follow_symlinks:
  205. func = libc.extattr_get_file
  206. else:
  207. func = libc.extattr_get_link
  208. n = _check(func(path, EXTATTR_NAMESPACE_USER, name, None, 0))
  209. if n == 0:
  210. return
  211. valuebuf = create_string_buffer(n)
  212. n2 = _check(func(path, EXTATTR_NAMESPACE_USER, name, valuebuf, n), path)
  213. if n2 != n:
  214. raise Exception('getxattr failed')
  215. return valuebuf.raw
  216. def setxattr(path, name, value, *, follow_symlinks=True):
  217. name = os.fsencode(name)
  218. value = value and os.fsencode(value)
  219. if isinstance(path, str):
  220. path = os.fsencode(path)
  221. if isinstance(path, int):
  222. func = libc.extattr_set_fd
  223. elif follow_symlinks:
  224. func = libc.extattr_set_file
  225. else:
  226. func = libc.extattr_set_link
  227. _check(func(path, EXTATTR_NAMESPACE_USER, name, value, len(value) if value else 0), path)
  228. else:
  229. def listxattr(path, *, follow_symlinks=True):
  230. return []
  231. def getxattr(path, name, *, follow_symlinks=True):
  232. pass
  233. def setxattr(path, name, value, *, follow_symlinks=True):
  234. pass