platform_linux.pyx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import os
  2. import re
  3. from stat import S_ISLNK
  4. from attic.helpers import posix_acl_use_stored_uid_gid, user2uid, group2gid
  5. API_VERSION = 2
  6. cdef extern from "sys/types.h":
  7. int ACL_TYPE_ACCESS
  8. int ACL_TYPE_DEFAULT
  9. cdef extern from "sys/acl.h":
  10. ctypedef struct _acl_t:
  11. pass
  12. ctypedef _acl_t *acl_t
  13. int acl_free(void *obj)
  14. acl_t acl_get_file(const char *path, int type)
  15. acl_t acl_set_file(const char *path, int type, acl_t acl)
  16. acl_t acl_from_text(const char *buf)
  17. char *acl_to_text(acl_t acl, ssize_t *len)
  18. cdef extern from "acl/libacl.h":
  19. int acl_extended_file(const char *path)
  20. _comment_re = re.compile(' *#.*', re.M)
  21. def acl_use_local_uid_gid(acl):
  22. """Replace the user/group field with the local uid/gid if possible
  23. """
  24. entries = []
  25. for entry in acl.decode('ascii').split('\n'):
  26. if entry:
  27. fields = entry.split(':')
  28. if fields[0] == 'user' and fields[1]:
  29. fields[1] = user2uid(fields[1], fields[3])
  30. elif fields[0] == 'group' and fields[1]:
  31. fields[1] = group2gid(fields[1], fields[3])
  32. entries.append(':'.join(entry.split(':')[:3]))
  33. return ('\n'.join(entries)).encode('ascii')
  34. cdef acl_append_numeric_ids(acl):
  35. """Extend the "POSIX 1003.1e draft standard 17" format with an additional uid/gid field
  36. """
  37. entries = []
  38. for entry in _comment_re.sub('', acl.decode('ascii')).split('\n'):
  39. if entry:
  40. type, name, permission = entry.split(':')
  41. if name and type == 'user':
  42. entries.append(':'.join([type, name, permission, str(user2uid(name, name))]))
  43. elif name and type == 'group':
  44. entries.append(':'.join([type, name, permission, str(group2gid(name, name))]))
  45. else:
  46. entries.append(entry)
  47. return ('\n'.join(entries)).encode('ascii')
  48. cdef acl_numeric_ids(acl):
  49. """Replace the "POSIX 1003.1e draft standard 17" user/group field with uid/gid
  50. """
  51. entries = []
  52. for entry in _comment_re.sub('', acl.decode('ascii')).split('\n'):
  53. if entry:
  54. type, name, permission = entry.split(':')
  55. if name and type == 'user':
  56. uid = str(user2uid(name, name))
  57. entries.append(':'.join([type, uid, permission, uid]))
  58. elif name and type == 'group':
  59. gid = str(group2gid(name, name))
  60. entries.append(':'.join([type, gid, permission, gid]))
  61. else:
  62. entries.append(entry)
  63. return ('\n'.join(entries)).encode('ascii')
  64. def acl_get(path, item, st, numeric_owner=False):
  65. """Saves ACL Entries
  66. If `numeric_owner` is True the user/group field is not preserved only uid/gid
  67. """
  68. cdef acl_t default_acl = NULL
  69. cdef acl_t access_acl = NULL
  70. cdef char *default_text = NULL
  71. cdef char *access_text = NULL
  72. p = <bytes>os.fsencode(path)
  73. if S_ISLNK(st.st_mode) or acl_extended_file(p) <= 0:
  74. return
  75. if numeric_owner:
  76. converter = acl_numeric_ids
  77. else:
  78. converter = acl_append_numeric_ids
  79. try:
  80. access_acl = acl_get_file(p, ACL_TYPE_ACCESS)
  81. if access_acl:
  82. access_text = acl_to_text(access_acl, NULL)
  83. if access_text:
  84. item[b'acl_access'] = converter(access_text)
  85. default_acl = acl_get_file(p, ACL_TYPE_DEFAULT)
  86. if default_acl:
  87. default_text = acl_to_text(default_acl, NULL)
  88. if default_text:
  89. item[b'acl_default'] = converter(default_text)
  90. finally:
  91. acl_free(default_text)
  92. acl_free(default_acl)
  93. acl_free(access_text)
  94. acl_free(access_acl)
  95. def acl_set(path, item, numeric_owner=False):
  96. """Restore ACL Entries
  97. If `numeric_owner` is True the stored uid/gid is used instead
  98. of the user/group names
  99. """
  100. cdef acl_t access_acl = NULL
  101. cdef acl_t default_acl = NULL
  102. p = <bytes>os.fsencode(path)
  103. if numeric_owner:
  104. converter = posix_acl_use_stored_uid_gid
  105. else:
  106. converter = acl_use_local_uid_gid
  107. access_text = item.get(b'acl_access')
  108. default_text = item.get(b'acl_default')
  109. if access_text:
  110. try:
  111. access_acl = acl_from_text(<bytes>converter(access_text))
  112. if access_acl:
  113. acl_set_file(p, ACL_TYPE_ACCESS, access_acl)
  114. finally:
  115. acl_free(access_acl)
  116. if default_text:
  117. try:
  118. default_acl = acl_from_text(<bytes>converter(default_text))
  119. if default_acl:
  120. acl_set_file(p, ACL_TYPE_DEFAULT, default_acl)
  121. finally:
  122. acl_free(default_acl)