2
# Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
3
# This file is part of GlusterFS.
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.
11
# All python2/python3 compatibility routines
17
from ctypes import create_string_buffer
22
if sys.version_info >= (3,):
25
os.set_inheritable(r, True)
26
os.set_inheritable(w, True)
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])
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])
43
def gr_create_string_buffer(size):
44
return create_string_buffer(b'\0', size)
46
def gr_query_xattr(cls, path, size, syscall, attr=None):
48
return cls._query_xattr(path.encode(), size, syscall,
51
return cls._query_xattr(path.encode(), size, syscall)
53
def gr_lsetxattr(cls, path, attr, val):
54
return cls.libc.lsetxattr(path.encode(), attr.encode(), val,
57
def gr_lremovexattr(cls, path, attr):
58
return cls.libc.lremovexattr(path.encode(), attr.encode())
60
def gr_cl_register(cls, brick, path, log_file, log_level, retries):
61
return cls._get_api('gf_changelog_register')(brick.encode(),
66
def gr_cl_done(cls, clfile):
67
return cls._get_api('gf_changelog_done')(clfile.encode())
69
def gr_cl_history_changelog(cls, changelog_path, start, end, num_parallel,
71
return cls._get_api('gf_history_changelog')(changelog_path.encode(),
72
start, end, num_parallel,
75
def gr_cl_history_done(cls, clfile):
76
return cls._get_api('gf_history_changelog_done')(clfile.encode())
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())
87
def entry_pack_reg_stat(cls, gf, bn, st):
88
bn_encoded = bn.encode()
89
blen = len(bn_encoded)
91
return struct.pack(cls._fmt_mknod(blen),
93
gf.encode(), mo, bn_encoded,
94
stat.S_IMODE(mo), 0, umask())
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())
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,
119
# Raw conversion of bytearray to string
120
def bytearray_to_str(byte_arr):
123
# Raw conversion of string to bytearray
124
def str_to_bytearray(string):
127
def gr_create_string_buffer(size):
128
return create_string_buffer('\0', size)
130
def gr_query_xattr(cls, path, size, syscall, attr=None):
132
return cls._query_xattr(path, size, syscall, attr)
134
return cls._query_xattr(path, size, syscall)
136
def gr_lsetxattr(cls, path, attr, val):
137
return cls.libc.lsetxattr(path, attr, val, len(val), 0)
139
def gr_lremovexattr(cls, path, attr):
140
return cls.libc.lremovexattr(path, attr)
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,
146
def gr_cl_done(cls, clfile):
147
return cls._get_api('gf_changelog_done')(clfile)
149
def gr_cl_history_changelog(cls, changelog_path, start, end, num_parallel,
151
return cls._get_api('gf_history_changelog')(changelog_path, start, end,
152
num_parallel, actual_end)
154
def gr_cl_history_done(cls, clfile):
155
return cls._get_api('gf_history_changelog_done')(clfile)
159
def entry_pack_reg(cls, gf, bn, mo, uid, gid):
161
return struct.pack(cls._fmt_mknod(blen),
162
uid, gid, gf, mo, bn,
163
stat.S_IMODE(mo), 0, umask())
165
def entry_pack_reg_stat(cls, gf, bn, st):
168
return struct.pack(cls._fmt_mknod(blen),
169
st['uid'], st['gid'],
171
stat.S_IMODE(mo), 0, umask())
174
def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):
176
return struct.pack(cls._fmt_mkdir(blen),
177
uid, gid, gf, mo, bn,
178
stat.S_IMODE(mo), umask())
181
def entry_pack_symlink(cls, gf, bn, lnk, st):
184
return struct.pack(cls._fmt_symlink(blen, llen),
185
st['uid'], st['gid'],
186
gf, st['mode'], bn, lnk)