glusterfs

Форк
0
/
py2py3.py 
186 строк · 6.7 Кб
1
#
2
# Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
3
# This file is part of GlusterFS.
4

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

11
# All python2/python3 compatibility routines
12

13
import sys
14
import os
15
import stat
16
import struct
17
from ctypes import create_string_buffer
18

19
def umask():
20
    return os.umask(0)
21

22
if sys.version_info >= (3,):
23
    def pipe():
24
        (r, w) = os.pipe()
25
        os.set_inheritable(r, True)
26
        os.set_inheritable(w, True)
27
        return (r, w)
28

29
    # Raw conversion of bytearray to string. Used in the cases where
30
    # buffer is created by create_string_buffer which is a 8-bit char
31
    # array and passed to syscalls to fetch results. Using encode/decode
32
    # doesn't work as it converts to string altering the size.
33
    def bytearray_to_str(byte_arr):
34
        return ''.join([chr(b) for b in byte_arr])
35

36
    # Raw conversion of string to bytes. This is required to convert
37
    # back the string into bytearray(c char array) to use in struc
38
    # pack/unpacking. Again encode/decode can't be used as it
39
    # converts it alters size.
40
    def str_to_bytearray(string):
41
        return bytes([ord(c) for c in string])
42

43
    def gr_create_string_buffer(size):
44
        return create_string_buffer(b'\0', size)
45

46
    def gr_query_xattr(cls, path, size, syscall, attr=None):
47
        if attr:
48
            return cls._query_xattr(path.encode(), size, syscall,
49
                                    attr.encode())
50
        else:
51
            return cls._query_xattr(path.encode(), size, syscall)
52

53
    def gr_lsetxattr(cls, path, attr, val):
54
        return cls.libc.lsetxattr(path.encode(), attr.encode(), val,
55
                                  len(val), 0)
56

57
    def gr_lremovexattr(cls, path, attr):
58
        return cls.libc.lremovexattr(path.encode(), attr.encode())
59

60
    def gr_cl_register(cls, brick, path, log_file, log_level, retries):
61
        return cls._get_api('gf_changelog_register')(brick.encode(),
62
                                                     path.encode(),
63
                                                     log_file.encode(),
64
                                                     log_level, retries)
65

66
    def gr_cl_done(cls, clfile):
67
        return cls._get_api('gf_changelog_done')(clfile.encode())
68

69
    def gr_cl_history_changelog(cls, changelog_path, start, end, num_parallel,
70
                                actual_end):
71
        return cls._get_api('gf_history_changelog')(changelog_path.encode(),
72
                                                    start, end, num_parallel,
73
                                                    actual_end)
74

75
    def gr_cl_history_done(cls, clfile):
76
        return cls._get_api('gf_history_changelog_done')(clfile.encode())
77

78
    # regular file
79

80
    def entry_pack_reg(cls, gf, bn, mo, uid, gid):
81
        bn_encoded = bn.encode()
82
        blen = len(bn_encoded)
83
        return struct.pack(cls._fmt_mknod(blen),
84
                           uid, gid, gf.encode(), mo, bn_encoded,
85
                           stat.S_IMODE(mo), 0, umask())
86

87
    def entry_pack_reg_stat(cls, gf, bn, st):
88
        bn_encoded = bn.encode()
89
        blen = len(bn_encoded)
90
        mo = st['mode']
91
        return struct.pack(cls._fmt_mknod(blen),
92
                           st['uid'], st['gid'],
93
                           gf.encode(), mo, bn_encoded,
94
                           stat.S_IMODE(mo), 0, umask())
95
    # mkdir
96

97
    def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):
98
        bn_encoded = bn.encode()
99
        blen = len(bn_encoded)
100
        return struct.pack(cls._fmt_mkdir(blen),
101
                           uid, gid, gf.encode(), mo, bn_encoded,
102
                           stat.S_IMODE(mo), umask())
103
    # symlink
104

105
    def entry_pack_symlink(cls, gf, bn, lnk, st):
106
        bn_encoded = bn.encode()
107
        blen = len(bn_encoded)
108
        lnk_encoded = lnk.encode()
109
        llen = len(lnk_encoded)
110
        return struct.pack(cls._fmt_symlink(blen, llen),
111
                           st['uid'], st['gid'],
112
                           gf.encode(), st['mode'], bn_encoded,
113
                           lnk_encoded)
114
else:
115
    def pipe():
116
        (r, w) = os.pipe()
117
        return (r, w)
118

119
    # Raw conversion of bytearray to string
120
    def bytearray_to_str(byte_arr):
121
        return byte_arr
122

123
    # Raw conversion of string to bytearray
124
    def str_to_bytearray(string):
125
        return string
126

127
    def gr_create_string_buffer(size):
128
        return create_string_buffer('\0', size)
129

130
    def gr_query_xattr(cls, path, size, syscall, attr=None):
131
        if attr:
132
            return cls._query_xattr(path, size, syscall, attr)
133
        else:
134
            return cls._query_xattr(path, size, syscall)
135

136
    def gr_lsetxattr(cls, path, attr, val):
137
        return cls.libc.lsetxattr(path, attr, val, len(val), 0)
138

139
    def gr_lremovexattr(cls, path, attr):
140
        return cls.libc.lremovexattr(path, attr)
141

142
    def gr_cl_register(cls, brick, path, log_file, log_level, retries):
143
        return cls._get_api('gf_changelog_register')(brick, path, log_file,
144
                                                     log_level, retries)
145

146
    def gr_cl_done(cls, clfile):
147
        return cls._get_api('gf_changelog_done')(clfile)
148

149
    def gr_cl_history_changelog(cls, changelog_path, start, end, num_parallel,
150
                                actual_end):
151
        return cls._get_api('gf_history_changelog')(changelog_path, start, end,
152
                                                    num_parallel, actual_end)
153

154
    def gr_cl_history_done(cls, clfile):
155
        return cls._get_api('gf_history_changelog_done')(clfile)
156

157
    # regular file
158

159
    def entry_pack_reg(cls, gf, bn, mo, uid, gid):
160
        blen = len(bn)
161
        return struct.pack(cls._fmt_mknod(blen),
162
                           uid, gid, gf, mo, bn,
163
                           stat.S_IMODE(mo), 0, umask())
164

165
    def entry_pack_reg_stat(cls, gf, bn, st):
166
        blen = len(bn)
167
        mo = st['mode']
168
        return struct.pack(cls._fmt_mknod(blen),
169
                           st['uid'], st['gid'],
170
                           gf, mo, bn,
171
                           stat.S_IMODE(mo), 0, umask())
172
    # mkdir
173

174
    def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):
175
        blen = len(bn)
176
        return struct.pack(cls._fmt_mkdir(blen),
177
                           uid, gid, gf, mo, bn,
178
                           stat.S_IMODE(mo), umask())
179
    # symlink
180

181
    def entry_pack_symlink(cls, gf, bn, lnk, st):
182
        blen = len(bn)
183
        llen = len(lnk)
184
        return struct.pack(cls._fmt_symlink(blen, llen),
185
                           st['uid'], st['gid'],
186
                           gf, st['mode'], bn, lnk)
187

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

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

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

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