1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
import os
import sys
from ._lib import lib, ffi
XATTR_NOFOLLOW = lib.XATTR_XATTR_NOFOLLOW
XATTR_CREATE = lib.XATTR_XATTR_CREATE
XATTR_REPLACE = lib.XATTR_XATTR_REPLACE
XATTR_NOSECURITY = lib.XATTR_XATTR_NOSECURITY
XATTR_MAXNAMELEN = lib.XATTR_MAXNAMELEN
XATTR_COMPAT_USER_PREFIX = lib.XATTR_COMPAT_ADD_USER_PREFIX and "user." or ""
XATTR_FINDERINFO_NAME = "com.apple.FinderInfo"
XATTR_RESOURCEFORK_NAME = "com.apple.ResourceFork"
def _check_bytes(val):
if not isinstance(val, bytes):
raise TypeError(
"Value must be bytes, %s was passed." % type(val).__name__
)
def error(path=None):
errno = ffi.errno
strerror = os.strerror(ffi.errno)
if path:
raise IOError(errno, strerror, path)
else:
raise IOError(errno, strerror)
def _getxattr(path, name, size=0, position=0, options=0):
"""
getxattr(path, name, size=0, position=0, options=0) -> str
"""
path = os.fsencode(path)
name = os.fsencode(name)
if size == 0:
res = lib.xattr_getxattr(path, name, ffi.NULL, 0, position, options)
if res == -1:
raise error(path)
size = res
buf = ffi.new("char[]", size)
res = lib.xattr_getxattr(path, name, buf, size, position, options)
if res == -1:
raise error(path)
return ffi.buffer(buf)[:res]
def _fgetxattr(fd, name, size=0, position=0, options=0):
"""
fgetxattr(fd, name, size=0, position=0, options=0) -> str
"""
name = os.fsencode(name)
if size == 0:
res = lib.xattr_fgetxattr(fd, name, ffi.NULL, 0, position, options)
if res == -1:
raise error()
size = res
buf = ffi.new("char[]", size)
res = lib.xattr_fgetxattr(fd, name, buf, size, position, options)
if res == -1:
raise error()
return ffi.buffer(buf)[:res]
def _setxattr(path, name, value, position=0, options=0):
"""
setxattr(path, name, value, position=0, options=0) -> None
"""
_check_bytes(value)
path = os.fsencode(path)
name = os.fsencode(name)
res = lib.xattr_setxattr(path, name, value, len(value), position, options)
if res:
raise error(path)
def _fsetxattr(fd, name, value, position=0, options=0):
"""
fsetxattr(fd, name, value, position=0, options=0) -> None
"""
_check_bytes(value)
name = os.fsencode(name)
res = lib.xattr_fsetxattr(fd, name, value, len(value), position, options)
if res:
raise error()
def _removexattr(path, name, options=0):
"""
removexattr(path, name, options=0) -> None
"""
path = os.fsencode(path)
name = os.fsencode(name)
res = lib.xattr_removexattr(path, name, options)
if res:
raise error(path)
def _fremovexattr(fd, name, options=0):
"""
fremovexattr(fd, name, options=0) -> None
"""
name = os.fsencode(name)
res = lib.xattr_fremovexattr(fd, name, options)
if res:
raise error()
def _listxattr(path, options=0):
"""
listxattr(path, options=0) -> str
"""
path = os.fsencode(path)
res = lib.xattr_listxattr(path, ffi.NULL, 0, options)
if res == -1:
raise error(path)
elif res == 0:
return b""
buf = ffi.new("char[]", res)
res = lib.xattr_listxattr(path, buf, res, options)
if res == -1:
raise error(path)
return ffi.buffer(buf)[:res]
def _flistxattr(fd, options=0):
"""
flistxattr(fd, options=0) -> str
"""
res = lib.xattr_flistxattr(fd, ffi.NULL, 0, options)
if res == -1:
raise error()
buf = ffi.new("char[]", res)
res = lib.xattr_flistxattr(fd, buf, res, options)
if res == -1:
raise error()
return ffi.buffer(buf)[:res]
|