xattr.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. def is_enabled():
  8. """Determine if xattr is enabled on the filesystem
  9. """
  10. with tempfile.NamedTemporaryFile() as fd:
  11. try:
  12. setxattr(fd.fileno(), 'user.name', b'value')
  13. except OSError:
  14. return False
  15. return getxattr(fd.fileno(), 'user.name') == b'value'
  16. def get_all(path, follow_symlinks=True):
  17. return dict((name, getxattr(path, name, follow_symlinks=follow_symlinks))
  18. for name in listxattr(path, follow_symlinks=follow_symlinks))
  19. try:
  20. # Currently only available on Python 3.3+ on Linux
  21. from os import getxattr, setxattr, listxattr2
  22. except ImportError:
  23. from ctypes import CDLL, create_string_buffer, c_ssize_t, c_size_t, c_char_p, c_int, c_uint32, get_errno
  24. from ctypes.util import find_library
  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]]
  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 = 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), 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 = 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), 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. try:
  182. n = _check(func(path, ns, None, 0), path)
  183. except OSError as e:
  184. if e.errno == errno.ENOTSUP:
  185. return []
  186. raise
  187. if n == 0:
  188. return []
  189. namebuf = create_string_buffer(n)
  190. n2 = _check(func(path, ns, namebuf, n), path)
  191. if n2 != n:
  192. raise Exception('listxattr failed')
  193. names = []
  194. mv = memoryview(namebuf.raw)
  195. while mv:
  196. length = mv[0]
  197. # Python < 3.3 returns bytes instead of int
  198. if isinstance(length, bytes):
  199. length = ord(length)
  200. names.append(os.fsdecode(bytes(mv[1:1+length])))
  201. mv = mv[1+length:]
  202. return names
  203. def getxattr(path, name, *, follow_symlinks=True):
  204. name = os.fsencode(name)
  205. if isinstance(path, str):
  206. path = os.fsencode(path)
  207. if isinstance(path, int):
  208. func = libc.extattr_get_fd
  209. elif follow_symlinks:
  210. func = libc.extattr_get_file
  211. else:
  212. func = libc.extattr_get_link
  213. n = _check(func(path, EXTATTR_NAMESPACE_USER, name, None, 0))
  214. if n == 0:
  215. return
  216. valuebuf = create_string_buffer(n)
  217. n2 = _check(func(path, EXTATTR_NAMESPACE_USER, name, valuebuf, n), path)
  218. if n2 != n:
  219. raise Exception('getxattr failed')
  220. return valuebuf.raw
  221. def setxattr(path, name, value, *, follow_symlinks=True):
  222. name = os.fsencode(name)
  223. value = os.fsencode(value)
  224. if isinstance(path, str):
  225. path = os.fsencode(path)
  226. if isinstance(path, int):
  227. func = libc.extattr_set_fd
  228. elif follow_symlinks:
  229. func = libc.extattr_set_file
  230. else:
  231. func = libc.extattr_set_link
  232. _check(func(path, EXTATTR_NAMESPACE_USER, name, value, len(value)), path)
  233. else:
  234. raise Exception('Unsupported platform: %s' % sys.platform)