summaryrefslogtreecommitdiffstats
path: root/tests/utils/getfattr.py
blob: 7d1f7368e3eb2a6d76da2e2a7ceee03d1edf3ca8 (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
127
128
129
130
131
132
133
134
#!/usr/bin/python2

from __future__ import print_function
import os
import sys
from optparse import OptionParser

import xattr

def handle_textencoding(attr):
    ### required for Python's handling of NULL strings.
    attr_null_replace = (attr.encode('hex').decode('hex')).replace('\x00',
                                                                   '\\000')
    return attr_null_replace

def getfattr(path, option):
    attr = xattr.getxattr(path, option.name)
    encoded_attr = attr

    if option.encoding == "text":
        ## special case handle it.
        encoded_attr = handle_textencoding(attr)
    else:
        encoded_attr = attr.encode(option.encoding)

    if option.onlyvalues:
        print (encoded_attr)
        return

    print_getfattr (path, option, encoded_attr)
    return

def print_getfattr (path, option, encoded_attr=None):
    if encoded_attr:
        if option.encoding == "hex":
            print(("%s=0x%s" % (option.name, encoded_attr)))
        elif option.encoding == "base64":
            print(("%s=0s%s" % (option.name, encoded_attr)))
        else:
            print(("%s=\"%s\"" % (option.name, encoded_attr)))
    else:
        print(option.name)

    return

def print_header (path, absnames):
    if absnames:
        print(("# file: %s" % path))
    else:
        print ("getfattr: Removing leading '/' from absolute path names")
        print(("# file: %s" % path[1:]))

if __name__ == '__main__':
    usage = "usage: %prog [-n name|-d] [-e en] [-m pattern] path...."
    parser = OptionParser(usage=usage)
    parser.add_option("-n", action="store", dest="name", type="string",
                      help="Dump the value of the named extended attribute"
                      " extended attribute.")
    parser.add_option("-d", action="store_true", dest="dump",
                      help="Dump the values of all extended attributes"
                      " associated with pathname.")
    parser.add_option("-e", action="store", dest="encoding", type="string",
                      default="base64",
                      help="Encode values after retrieving"
                      " them. Valid values of [en] are `text`, `hex`,"
                      " and `base64`. Values encoded as text strings are"
                      " enclosed in double quotes (\"), while strings"
                      " encoded as hexidecimal and base64 are prefixed with"
                      " 0x and 0s, respectively.")
    parser.add_option("-m", action="store", dest="pattern", type="string",
                      help="Only include attributes with names matching the"
                      " regular expression pattern. The default value for"
                      " pattern is \"^user\\.\", which includes all the"
                      " attributes in the user namespace. Specify \"-\" for"
                      " including all attributes. Refer to attr(5) for a more"
                      " detailed discussion of namespaces.")
    parser.add_option("--absolute-names", action="store_true", dest="absnames",
                      help="Do not strip leading slash characters ('/')."
                      " The default behaviour is to strip leading slash characters.")
    parser.add_option("--only-values", action="store_true", dest="onlyvalues",
                      help="Dump out the raw extended attribute value(s)"
                      " without encoding them.")

    (option, args) = parser.parse_args()
    if not args:
        print ("Usage: getfattr [-hRLP] [-n name|-d] [-e en] [-m pattern]"
               " path...")
        print ("Try `getfattr --help' for more information.")
        sys.exit(1)

    if option.dump and option.name:
        print ("-d and -n are mutually exclusive...")
        sys.exit(1)

    if option.pattern and option.name:
        print ("-m and -n are mutually exclusive...")
        sys.exit(1)

    if option.encoding:
        if (not (option.encoding.strip() == "hex" or
                 option.encoding.strip() == "base64" or
                 option.encoding.strip() == "text")):
            print(("unrecognized encoding parameter... %s, please use"
                   " `text`, `base64` or `hex`" % option.encoding))
            sys.exit(1)

    args[0] = os.path.abspath(args[0])

    if option.name:
        print_header(args[0], option.absnames)
        try:
            getfattr(args[0], option)
        except KeyError as err:
            print(("Invalid key %s" % err))
            sys.exit(1)
        except IOError as err:
            print (err)
            sys.exit(1)

    if option.pattern:
        print_header(args[0], option.absnames)
        try:
            xattrs = xattr.listxattr(args[0])
            for attr in xattrs:
                if option.dump:
                    option.name = attr.encode('utf-8')
                    getfattr(args[0], option)
                else:
                    option.name = attr.encode('utf-8')
                    print_getfattr(args[0], option, None)

        except IOError as err:
            print (err)
            sys.exit(1)