|
| 1 | +from .xattr import _listxattr_inner, _getxattr_inner, _setxattr_inner, split_lstring |
| 2 | + |
| 3 | +API_VERSION = '1.2_05' |
| 4 | + |
| 5 | +cdef extern from "sys/extattr.h": |
| 6 | + ssize_t c_extattr_list_file "extattr_list_file" (const char *path, int attrnamespace, void *data, size_t nbytes) |
| 7 | + ssize_t c_extattr_list_link "extattr_list_link" (const char *path, int attrnamespace, void *data, size_t nbytes) |
| 8 | + ssize_t c_extattr_list_fd "extattr_list_fd" (int fd, int attrnamespace, void *data, size_t nbytes) |
| 9 | + |
| 10 | + ssize_t c_extattr_get_file "extattr_get_file" (const char *path, int attrnamespace, const char *attrname, void *data, size_t nbytes) |
| 11 | + ssize_t c_extattr_get_link "extattr_get_link" (const char *path, int attrnamespace, const char *attrname, void *data, size_t nbytes) |
| 12 | + ssize_t c_extattr_get_fd "extattr_get_fd" (int fd, int attrnamespace, const char *attrname, void *data, size_t nbytes) |
| 13 | + |
| 14 | + int c_extattr_set_file "extattr_set_file" (const char *path, int attrnamespace, const char *attrname, const void *data, size_t nbytes) |
| 15 | + int c_extattr_set_link "extattr_set_link" (const char *path, int attrnamespace, const char *attrname, const void *data, size_t nbytes) |
| 16 | + int c_extattr_set_fd "extattr_set_fd" (int fd, int attrnamespace, const char *attrname, const void *data, size_t nbytes) |
| 17 | + |
| 18 | + int EXTATTR_NAMESPACE_USER |
| 19 | + |
| 20 | + |
| 21 | +# On NetBSD, Borg currently only deals with the USER namespace, as it is unclear |
| 22 | +# whether (and, if so, how exactly) it should deal with the SYSTEM namespace. |
| 23 | +NS_ID_MAP = {b"user": EXTATTR_NAMESPACE_USER, } |
| 24 | + |
| 25 | + |
| 26 | +def split_ns(ns_name, default_ns): |
| 27 | + # Split ns_name (which is in the form b"namespace.name") into namespace and name. |
| 28 | + # If there is no namespace given in ns_name, default to default_ns. |
| 29 | + # We also need to deal with "unexpected" namespaces here — they could come |
| 30 | + # from Borg archives made on other operating systems. |
| 31 | + ns_name_tuple = ns_name.split(b".", 1) |
| 32 | + if len(ns_name_tuple) == 2: |
| 33 | + # We have a namespace prefix in the given name. |
| 34 | + ns, name = ns_name_tuple |
| 35 | + else: |
| 36 | + # No namespace given in ns_name (no dot found); maybe data from an old Borg archive. |
| 37 | + ns, name = default_ns, ns_name |
| 38 | + return ns, name |
| 39 | + |
| 40 | + |
| 41 | +def listxattr(path, *, follow_symlinks=False): |
| 42 | + def func(path, buf, size): |
| 43 | + if isinstance(path, int): |
| 44 | + return c_extattr_list_fd(path, ns_id, <char *> buf, size) |
| 45 | + else: |
| 46 | + if follow_symlinks: |
| 47 | + return c_extattr_list_file(path, ns_id, <char *> buf, size) |
| 48 | + else: |
| 49 | + return c_extattr_list_link(path, ns_id, <char *> buf, size) |
| 50 | + |
| 51 | + ns = b"user" |
| 52 | + ns_id = NS_ID_MAP[ns] |
| 53 | + n, buf = _listxattr_inner(func, path) |
| 54 | + return [ns + b"." + name for name in split_lstring(buf[:n]) if name] |
| 55 | + |
| 56 | + |
| 57 | +def getxattr(path, name, *, follow_symlinks=False): |
| 58 | + def func(path, name, buf, size): |
| 59 | + if isinstance(path, int): |
| 60 | + return c_extattr_get_fd(path, ns_id, name, <char *> buf, size) |
| 61 | + else: |
| 62 | + if follow_symlinks: |
| 63 | + return c_extattr_get_file(path, ns_id, name, <char *> buf, size) |
| 64 | + else: |
| 65 | + return c_extattr_get_link(path, ns_id, name, <char *> buf, size) |
| 66 | + |
| 67 | + ns, name = split_ns(name, b"user") |
| 68 | + ns_id = NS_ID_MAP[ns] # this will raise a KeyError it the namespace is unsupported |
| 69 | + n, buf = _getxattr_inner(func, path, name) |
| 70 | + return bytes(buf[:n]) |
| 71 | + |
| 72 | + |
| 73 | +def setxattr(path, name, value, *, follow_symlinks=False): |
| 74 | + def func(path, name, value, size): |
| 75 | + if isinstance(path, int): |
| 76 | + return c_extattr_set_fd(path, ns_id, name, <char *> value, size) |
| 77 | + else: |
| 78 | + if follow_symlinks: |
| 79 | + return c_extattr_set_file(path, ns_id, name, <char *> value, size) |
| 80 | + else: |
| 81 | + return c_extattr_set_link(path, ns_id, name, <char *> value, size) |
| 82 | + |
| 83 | + ns, name = split_ns(name, b"user") |
| 84 | + try: |
| 85 | + ns_id = NS_ID_MAP[ns] # this will raise a KeyError it the namespace is unsupported |
| 86 | + except KeyError: |
| 87 | + pass |
| 88 | + else: |
| 89 | + _setxattr_inner(func, path, name, value) |
0 commit comments