glusterfs

Форк
0
/
setfattr.py 
76 строк · 2.8 Кб
1

2
import os
3
import sys
4
from optparse import OptionParser
5

6
import xattr
7

8
def convert(string):
9
    tmp_string = string
10
    if (string[0] == '0' and
11
        (string[1] == 's' or
12
         string[1] == 'S')):
13
        tmp_string = string.strip('%s%s' %
14
                                  (string[0],
15
                                   string[1]))
16
        return tmp_string.decode('base64')
17

18
    if (string[0] == '0' and
19
        (string[1] == 'x' or
20
         string[1] == 'X')):
21
        tmp_string = string.split('%s%s' %
22
                                  (string[0],
23
                                   string[1]))
24
        return tmp_string[1].decode('hex')
25

26
    return tmp_string
27

28
if __name__ == '__main__':
29
    usage = "usage: %prog [-n name] [-v value] [-x name]"
30
    parser = OptionParser(usage=usage)
31
    parser.add_option("-n", action="store", dest="name", type="string",
32
                      help="Specifies the name of the extended attribute to set.")
33
    parser.add_option("-v", action="store", dest="value", type="string",
34
                      help="Specifies the new value of the extended attribute."
35
                      " There are three methods available for encoding the value."
36
                      " If the given string is enclosed in double quotes, the"
37
                      " inner string is treated as text. In that case,"
38
                      " backslashes and double quotes have special meanings"
39
                      " and need to be escaped by a preceding backslash. Any"
40
                      " control characters can be encoded as a backslash"
41
                      " followed by three digits as its ASCII code in octal."
42
                      " If the given string begins with 0x or 0X, it expresses"
43
                      " a hexadecimal number. If the given string begins with"
44
                      " 0s or 0S, base64 encoding is expected.")
45
    parser.add_option("-x", action="store", dest="xname", type="string",
46
                      help="Remove the named extended attribute entirely.")
47

48
    (option, args) = parser.parse_args()
49
    if not args:
50
        print ("Usage: setfattr {-n name} [-v value] file...")
51
        print ("       setfattr {-x name} file...")
52
        print ("Try `setfattr --help' for more information.")
53
        sys.exit(1)
54

55
    if option.name and option.xname:
56
        print ("-n and -x are mutually exclusive...")
57
        sys.exit(1)
58

59
    if option.name:
60
        if option.value is None:
61
            print ("-n option requires -v value...")
62

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

65
    if option.name and option.value:
66
        try:
67
            xattr.setxattr(args[0], option.name, convert(option.value))
68
        except Exception as err:
69
            print (err)
70
            sys.exit(1)
71

72
    if option.xname:
73
        try:
74
            xattr.removexattr(args[0], option.xname)
75
        except Exception as err:
76
            print (err)
77
            sys.exit(1)
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.