summaryrefslogtreecommitdiffstats
path: root/xlators/bindings/python/src/glustertypes.py
blob: e9069d07c723d2f8d5edea3cce50883db5e009e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#   Copyright (c) 2007 Chris AtLee <chris@atlee.ca>
#   This file is part of GlusterFS.
#
#   GlusterFS is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published
#   by the Free Software Foundation; either version 3 of the License,
#   or (at your option) any later version.
#
#   GlusterFS is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see
#   <http://www.gnu.org/licenses/>.
from ctypes import *
import collections

#
# Forward declaration of some gluster types
#
class call_frame_t(Structure):
    pass

class call_ctx_t(Structure):
    pass

class call_pool_t(Structure):
    pass

class xlator_t(Structure):
    def _getFirstChild(self):
        return self.children[0].xlator
    firstChild = property(_getFirstChild)

class xlator_list_t(Structure):
    pass

class xlator_fops(Structure):
    pass

class xlator_mops(Structure):
    pass

class glusterfs_ctx_t(Structure):
    pass

class list_head(Structure):
    pass

class dict_t(Structure):
    pass

class inode_table_t(Structure):
    pass

class fd_t(Structure):
    pass

class iovec(Structure):
    _fields_ = [
            ("iov_base", c_void_p),
            ("iov_len", c_size_t),
            ]

    def __init__(self, s):
        self.iov_base = cast(c_char_p(s), c_void_p)
        self.iov_len = len(s)

    def getBytes(self):
        return string_at(self.iov_base, self.iov_len)

# This is a pthread_spinlock_t
# TODO: what happens to volatile-ness?
gf_lock_t = c_int

uid_t = c_uint32
gid_t = c_uint32
pid_t = c_int32

off_t = c_int64

#
# Function pointer types
#
ret_fn_t = CFUNCTYPE(c_int32, POINTER(call_frame_t), POINTER(call_frame_t),
                              POINTER(xlator_t), c_int32, c_int32)

fini_fn_t = CFUNCTYPE(None, POINTER(xlator_t))
init_fn_t = CFUNCTYPE(c_int32, POINTER(xlator_t))
event_notify_fn_t = CFUNCTYPE(c_int32, POINTER(xlator_t), c_int32, c_void_p)

list_head._fields_ = [
        ("next", POINTER(list_head)),
        ("prev", POINTER(list_head)),
        ]

call_frame_t._fields_ = [
        ("root", POINTER(call_ctx_t)),
        ("parent", POINTER(call_frame_t)),
        ("next", POINTER(call_frame_t)),
        ("prev", POINTER(call_frame_t)),
        ("local", c_void_p),
        ("this", POINTER(xlator_t)),
        ("ret", ret_fn_t),
        ("ref_count", c_int32),
        ("lock", gf_lock_t),
        ("cookie", c_void_p),
        ("op", c_int32),
        ("type", c_int8),
        ]

call_ctx_t._fields_ = [
        ("all_frames", list_head),
        ("trans", c_void_p),
        ("pool", call_pool_t),
        ("unique", c_uint64),
        ("state", c_void_p),
        ("uid", uid_t),
        ("gid", gid_t),
        ("pid", pid_t),
        ("frames", call_frame_t),
        ("req_refs", POINTER(dict_t)),
        ("rsp_refs", POINTER(dict_t)),
        ]

xlator_t._fields_ = [
        ("name", c_char_p),
        ("type", c_char_p),
        ("next", POINTER(xlator_t)),
        ("prev", POINTER(xlator_t)),
        ("parent", POINTER(xlator_t)),
        ("children", POINTER(xlator_list_t)),
        ("fops", POINTER(xlator_fops)),
        ("mops", POINTER(xlator_mops)),
        ("fini", fini_fn_t),
        ("init", init_fn_t),
        ("notify", event_notify_fn_t),
        ("options", POINTER(dict_t)),
        ("ctx", POINTER(glusterfs_ctx_t)),
        ("itable", POINTER(inode_table_t)),
        ("ready", c_char),
        ("private", c_void_p),
        ]

xlator_list_t._fields_ = [
        ("xlator", POINTER(xlator_t)),
        ("next", POINTER(xlator_list_t)),
        ]

fop_functions = collections.defaultdict(lambda: c_void_p)
fop_function_names = ['lookup', 'forget', 'stat', 'fstat', 'chmod', 'fchmod',
        'chown', 'fchown', 'truncate', 'ftruncate', 'utimens', 'access',
        'readlink', 'mknod', 'mkdir', 'unlink', 'rmdir', 'symlink',
        'rename', 'link', 'create', 'open', 'readv', 'writev', 'flush',
        'close', 'fsync', 'opendir', 'readdir', 'closedir', 'fsyncdir',
        'statfs', 'setxattr', 'getxattr', 'removexattr', 'lk', 'writedir',
        # TODO: Call backs?
        ]

fop_writev_t = CFUNCTYPE(c_int32, POINTER(call_frame_t), POINTER(xlator_t),
                                  POINTER(fd_t), POINTER(iovec), c_int32,
                                  off_t)

fop_functions['writev'] = fop_writev_t
xlator_fops._fields_ = [(f, fop_functions[f]) for f in fop_function_names]