platform_linux.pyx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if S_ISLNK(st.st_mode) or acl_extended_file(<bytes>os.fsencode(path)) <= 0:
  73. return
  74. if numeric_owner:
  75. converter = acl_numeric_ids
  76. else:
  77. converter = acl_append_numeric_ids
  78. try:
  79. access_acl = acl_get_file(<bytes>os.fsencode(path), ACL_TYPE_ACCESS)
  80. if access_acl:
  81. access_text = acl_to_text(access_acl, NULL)
  82. if access_text:
  83. item[b'acl_access'] = converter(access_text)
  84. default_acl = acl_get_file(<bytes>os.fsencode(path), ACL_TYPE_DEFAULT)
  85. if default_acl:
  86. default_text = acl_to_text(default_acl, NULL)
  87. if default_text:
  88. item[b'acl_default'] = converter(default_text)
  89. finally:
  90. acl_free(default_text)
  91. acl_free(default_acl)
  92. acl_free(access_text)
  93. acl_free(access_acl)
  94. def acl_set(path, item, numeric_owner=False):
  95. """Restore ACL Entries
  96. If `numeric_owner` is True the stored uid/gid is used instead
  97. of the user/group names
  98. """
  99. cdef acl_t access_acl = NULL
  100. cdef acl_t default_acl = NULL
  101. if numeric_owner:
  102. converter = posix_acl_use_stored_uid_gid
  103. else:
  104. converter = acl_use_local_uid_gid
  105. access_text = item.get(b'acl_access')
  106. default_text = item.get(b'acl_default')
  107. if access_text:
  108. try:
  109. access_acl = acl_from_text(<bytes>converter(access_text))
  110. if access_acl:
  111. acl_set_file(<bytes>os.fsencode(path), ACL_TYPE_ACCESS, access_acl)
  112. finally:
  113. acl_free(access_acl)
  114. if default_text:
  115. try:
  116. default_acl = acl_from_text(<bytes>converter(default_text))
  117. if default_acl:
  118. acl_set_file(<bytes>os.fsencode(path), ACL_TYPE_DEFAULT, default_acl)
  119. finally:
  120. acl_free(default_acl)