summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/xattr.py
blob: 7b634944a401da6c43efbfecefe203cedc32a8b2 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /usr/bin/env python3
#
# Copyright 2023 by Garmin Ltd. or its subsidiaries
#
# SPDX-License-Identifier: MIT

import sys
import ctypes
import os
import errno

libc = ctypes.CDLL("libc.so.6", use_errno=True)
fsencoding = sys.getfilesystemencoding()


libc.listxattr.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t]
libc.llistxattr.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t]


def listxattr(path, follow=True):
    func = libc.listxattr if follow else libc.llistxattr

    os_path = os.fsencode(path)

    while True:
        length = func(os_path, None, 0)

        if length < 0:
            err = ctypes.get_errno()
            raise OSError(err, os.strerror(err), str(path))

        if length == 0:
            return []

        arr = ctypes.create_string_buffer(length)

        read_length = func(os_path, arr, length)
        if read_length != length:
            # Race!
            continue

        return [a.decode(fsencoding) for a in arr.raw.split(b"\x00") if a]


libc.getxattr.argtypes = [
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_size_t,
]
libc.lgetxattr.argtypes = [
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_size_t,
]


def getxattr(path, name, follow=True):
    func = libc.getxattr if follow else libc.lgetxattr

    os_path = os.fsencode(path)
    os_name = os.fsencode(name)

    while True:
        length = func(os_path, os_name, None, 0)

        if length < 0:
            err = ctypes.get_errno()
            if err == errno.ENODATA:
                return None
            raise OSError(err, os.strerror(err), str(path))

        if length == 0:
            return ""

        arr = ctypes.create_string_buffer(length)

        read_length = func(os_path, os_name, arr, length)
        if read_length != length:
            # Race!
            continue

        return arr.raw


def get_all_xattr(path, follow=True):
    attrs = {}

    names = listxattr(path, follow)

    for name in names:
        value = getxattr(path, name, follow)
        if value is None:
            # This can happen if a value is erased after listxattr is called,
            # so ignore it
            continue
        attrs[name] = value

    return attrs


def main():
    import argparse
    from pathlib import Path

    parser = argparse.ArgumentParser()
    parser.add_argument("path", help="File Path", type=Path)

    args = parser.parse_args()

    attrs = get_all_xattr(args.path)

    for name, value in attrs.items():
        try:
            value = value.decode(fsencoding)
        except UnicodeDecodeError:
            pass

        print(f"{name} = {value}")

    return 0


if __name__ == "__main__":
    sys.exit(main())