summaryrefslogtreecommitdiffstats
path: root/geo-replication/syncdaemon/libcxattr.py
blob: 7f3f6ce453af9acbd49bfa52dff1ae551059948a (plain)
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
#
# Copyright (c) 2011-2014 Red Hat, Inc. <http://www.redhat.com>
# This file is part of GlusterFS.

# This file is licensed to you under your choice of the GNU Lesser
# General Public License, version 3 or any later version (LGPLv3 or
# later), or the GNU General Public License, version 2 (GPLv2), in all
# cases as published by the Free Software Foundation.
#

import os
from ctypes import CDLL, create_string_buffer, get_errno
import py2py3
from py2py3 import bytearray_to_str


class Xattr(object):

    """singleton that wraps the extended attributes system
       interface for python using ctypes

       Just implement it to the degree we need it, in particular
       - we need just the l*xattr variants, ie. we never want symlinks to be
         followed
       - don't need size discovery for getxattr, as we always know the exact
         sizes we expect
    """

    libc = CDLL("libc.so.6", use_errno=True)

    @classmethod
    def geterrno(cls):
        return get_errno()

    @classmethod
    def raise_oserr(cls):
        errn = cls.geterrno()
        raise OSError(errn, os.strerror(errn))

    @classmethod
    def _query_xattr(cls, path, siz, syscall, *a):
        if siz:
            buf = create_string_buffer(b'\0' * siz)
        else:
            buf = None
        ret = getattr(cls.libc, syscall)(*((path,) + a + (buf, siz)))
        if ret == -1:
            cls.raise_oserr()
        if siz:
            # py2 and py3 compatibility. Convert bytes array
            # to string
            result = bytearray_to_str(buf.raw)
            return result[:ret]
        else:
            return ret

    @classmethod
    def lgetxattr(cls, path, attr, siz=0):
        return cls._query_xattr(path.encode(), siz, 'lgetxattr', attr.encode())

    @classmethod
    def lgetxattr_buf(cls, path, attr):
        """lgetxattr variant with size discovery"""
        size = cls.lgetxattr(path, attr)
        if size == -1:
            cls.raise_oserr()
        if size == 0:
            return ''
        return cls.lgetxattr(path, attr, size)

    @classmethod
    def llistxattr(cls, path, siz=0):
        ret = cls._query_xattr(path.encode(), siz, 'llistxattr')
        if isinstance(ret, str):
            ret = ret.strip('\0')
            ret = ret.split('\0') if ret else []
        return ret

    @classmethod
    def lsetxattr(cls, path, attr, val):
        ret = cls.libc.lsetxattr(path.encode(), attr.encode(), val, len(val), 0)
        if ret == -1:
            cls.raise_oserr()

    @classmethod
    def lremovexattr(cls, path, attr):
        ret = cls.libc.lremovexattr(path.encode(), attr.encode())
        if ret == -1:
            cls.raise_oserr()

    @classmethod
    def llistxattr_buf(cls, path):
        """listxattr variant with size discovery"""
        try:
            # Assuming no more than 100 xattrs in a file/directory and
            # each xattr key length will be less than 256 bytes
            # llistxattr will be called with bigger size so that
            # listxattr will not fail with ERANGE. OSError will be
            # raised if fails even with the large size specified.
            size = 256 * 100
            return cls.llistxattr(path, size)
        except OSError:
            # If fixed length failed for getting list of xattrs then
            # use the llistxattr call to get the size and use that
            # size to get the list of xattrs.
            size = cls.llistxattr(path)
            if size == -1:
                cls.raise_oserr()
            if size == 0:
                return []

            return cls.llistxattr(path, size)