glusterfs
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
13import sys14import os15import stat16import struct17from syncdutils import umask18from ctypes import create_string_buffer19
20if sys.version_info >= (3,):21def pipe():22(r, w) = os.pipe()23os.set_inheritable(r, True)24os.set_inheritable(w, True)25return (r, w)26
27# Raw conversion of bytearray to string. Used in the cases where28# buffer is created by create_string_buffer which is a 8-bit char29# array and passed to syscalls to fetch results. Using encode/decode30# doesn't work as it converts to string altering the size.31def bytearray_to_str(byte_arr):32return ''.join([chr(b) for b in byte_arr])33
34# Raw conversion of string to bytes. This is required to convert35# back the string into bytearray(c char array) to use in struc36# pack/unpacking. Again encode/decode can't be used as it37# converts it alters size.38def str_to_bytearray(string):39return bytes([ord(c) for c in string])40
41def gr_create_string_buffer(size):42return create_string_buffer(b'\0', size)43
44def gr_query_xattr(cls, path, size, syscall, attr=None):45if attr:46return cls._query_xattr(path.encode(), size, syscall,47attr.encode())48else:49return cls._query_xattr(path.encode(), size, syscall)50
51def gr_lsetxattr(cls, path, attr, val):52return cls.libc.lsetxattr(path.encode(), attr.encode(), val,53len(val), 0)54
55def gr_lremovexattr(cls, path, attr):56return cls.libc.lremovexattr(path.encode(), attr.encode())57
58def gr_cl_register(libgfapi, brick, path, log_file, log_level, retries):59return libgfapi.gf_changelog_register(brick.encode(),60path.encode(),61log_file.encode(),62log_level, retries)63
64def gr_cl_done(libgfapi, clfile):65return libgfapi.gf_changelog_done(clfile.encode())66
67def gr_cl_history_changelog(libgfapi, changelog_path, start, end, num_parallel,68actual_end):69return libgfapi.gf_history_changelog(changelog_path.encode(),70start, end, num_parallel,71actual_end)72
73def gr_cl_history_done(libgfapi, clfile):74return libgfapi.gf_history_changelog_done(clfile.encode())75
76# regular file77
78def entry_pack_reg(cls, gf, bn, mo, uid, gid):79bn_encoded = bn.encode()80blen = len(bn_encoded)81return struct.pack(cls._fmt_mknod(blen),82uid, gid, gf.encode(), mo, bn_encoded,83stat.S_IMODE(mo), 0, umask())84
85def entry_pack_reg_stat(cls, gf, bn, st):86bn_encoded = bn.encode()87blen = len(bn_encoded)88mo = st['mode']89return struct.pack(cls._fmt_mknod(blen),90st['uid'], st['gid'],91gf.encode(), mo, bn_encoded,92stat.S_IMODE(mo), 0, umask())93# mkdir94
95def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):96bn_encoded = bn.encode()97blen = len(bn_encoded)98return struct.pack(cls._fmt_mkdir(blen),99uid, gid, gf.encode(), mo, bn_encoded,100stat.S_IMODE(mo), umask())101# symlink102
103def entry_pack_symlink(cls, gf, bn, lnk, st):104bn_encoded = bn.encode()105blen = len(bn_encoded)106lnk_encoded = lnk.encode()107llen = len(lnk_encoded)108return struct.pack(cls._fmt_symlink(blen, llen),109st['uid'], st['gid'],110gf.encode(), st['mode'], bn_encoded,111lnk_encoded)112else:113def pipe():114(r, w) = os.pipe()115return (r, w)116
117# Raw conversion of bytearray to string118def bytearray_to_str(byte_arr):119return byte_arr120
121# Raw conversion of string to bytearray122def str_to_bytearray(string):123return string124
125def gr_create_string_buffer(size):126return create_string_buffer('\0', size)127
128def gr_query_xattr(cls, path, size, syscall, attr=None):129if attr:130return cls._query_xattr(path, size, syscall, attr)131else:132return cls._query_xattr(path, size, syscall)133
134def gr_lsetxattr(cls, path, attr, val):135return cls.libc.lsetxattr(path, attr, val, len(val), 0)136
137def gr_lremovexattr(cls, path, attr):138return cls.libc.lremovexattr(path, attr)139
140def gr_cl_register(libgfapi, brick, path, log_file, log_level, retries):141return libgfapi.gf_changelog_register(brick, path, log_file,142log_level, retries)143
144def gr_cl_done(libgfapi, clfile):145return libgfapi.gf_changelog_done(clfile)146
147def gr_cl_history_changelog(libgfapi, changelog_path, start, end, num_parallel,148actual_end):149return libgfapi.gf_history_changelog(changelog_path, start, end,150num_parallel, actual_end)151
152def gr_cl_history_done(libgfapi, clfile):153return libgfapi.gf_history_changelog_done(clfile)154
155# regular file156
157def entry_pack_reg(cls, gf, bn, mo, uid, gid):158blen = len(bn)159return struct.pack(cls._fmt_mknod(blen),160uid, gid, gf, mo, bn,161stat.S_IMODE(mo), 0, umask())162
163def entry_pack_reg_stat(cls, gf, bn, st):164blen = len(bn)165mo = st['mode']166return struct.pack(cls._fmt_mknod(blen),167st['uid'], st['gid'],168gf, mo, bn,169stat.S_IMODE(mo), 0, umask())170# mkdir171
172def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):173blen = len(bn)174return struct.pack(cls._fmt_mkdir(blen),175uid, gid, gf, mo, bn,176stat.S_IMODE(mo), umask())177# symlink178
179def entry_pack_symlink(cls, gf, bn, lnk, st):180blen = len(bn)181llen = len(lnk)182return struct.pack(cls._fmt_symlink(blen, llen),183st['uid'], st['gid'],184gf, st['mode'], bn, lnk)185