glusterfs

Форк
0
184 строки · 6.6 Кб
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 syncdutils import umask
18
from ctypes import create_string_buffer
19

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

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

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

41
    def gr_create_string_buffer(size):
42
        return create_string_buffer(b'\0', size)
43

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

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

55
    def gr_lremovexattr(cls, path, attr):
56
        return cls.libc.lremovexattr(path.encode(), attr.encode())
57

58
    def gr_cl_register(libgfapi, brick, path, log_file, log_level, retries):
59
        return libgfapi.gf_changelog_register(brick.encode(),
60
                                              path.encode(),
61
                                              log_file.encode(),
62
                                              log_level, retries)
63

64
    def gr_cl_done(libgfapi, clfile):
65
        return libgfapi.gf_changelog_done(clfile.encode())
66

67
    def gr_cl_history_changelog(libgfapi, changelog_path, start, end, num_parallel,
68
                                actual_end):
69
        return libgfapi.gf_history_changelog(changelog_path.encode(),
70
                                             start, end, num_parallel,
71
                                             actual_end)
72

73
    def gr_cl_history_done(libgfapi, clfile):
74
        return libgfapi.gf_history_changelog_done(clfile.encode())
75

76
    # regular file
77

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

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

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

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

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

121
    # Raw conversion of string to bytearray
122
    def str_to_bytearray(string):
123
        return string
124

125
    def gr_create_string_buffer(size):
126
        return create_string_buffer('\0', size)
127

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

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

137
    def gr_lremovexattr(cls, path, attr):
138
        return cls.libc.lremovexattr(path, attr)
139

140
    def gr_cl_register(libgfapi, brick, path, log_file, log_level, retries):
141
        return libgfapi.gf_changelog_register(brick, path, log_file,
142
                                              log_level, retries)
143

144
    def gr_cl_done(libgfapi, clfile):
145
        return libgfapi.gf_changelog_done(clfile)
146

147
    def gr_cl_history_changelog(libgfapi, changelog_path, start, end, num_parallel,
148
                                actual_end):
149
        return libgfapi.gf_history_changelog(changelog_path, start, end,
150
                                             num_parallel, actual_end)
151

152
    def gr_cl_history_done(libgfapi, clfile):
153
        return libgfapi.gf_history_changelog_done(clfile)
154

155
    # regular file
156

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

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

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

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

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

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

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

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