summaryrefslogtreecommitdiffstats
path: root/gluster/api.py
blob: 3fd9d910a8a2323a6d4bba40bbd7fc8aa5a79679 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#  Copyright (c) 2012-2015 Red Hat, Inc.
#  This file is part of libgfapi-python project
#  (http://review.gluster.org/#/q/project:libgfapi-python)
#  which is a subproject of GlusterFS ( www.gluster.org)
#
#  This file is licensed to you under your choice of the GNU Lesser
#  General Public License, version 3 or any later version (LGPLv3 or
#  later), or the GNU General Public License, version 2 (GPLv2), in all
#  cases as published by the Free Software Foundation.


import ctypes
from ctypes.util import find_library
from ctypes import sizeof


# LD_LIBRARY_PATH is not looked up by ctypes.util.find_library()
so_file_name = find_library("gfapi")

if so_file_name is None:
    for name in ["libgfapi.so.0", "libgfapi.so"]:
        try:
            ctypes.CDLL(name, ctypes.RTLD_GLOBAL, use_errno=True)
        except OSError:
            pass
        else:
            so_file_name = name
            break
    if so_file_name is None:
        # The .so file cannot be found (or loaded)
        # May be you need to run ldconfig command
        raise Exception("libgfapi.so not found")

# Looks like ctypes is having trouble with dependencies, so just force them to
# load with RTLD_GLOBAL until I figure that out.
try:
    client = ctypes.CDLL(so_file_name, ctypes.RTLD_GLOBAL, use_errno=True)
    # The above statement "may" fail with OSError on some systems if
    # libgfapi.so is located in /usr/local/lib/. This happens when glusterfs
    # is installed from source. Refer to: http://bugs.python.org/issue18502
except OSError:
    raise ImportError("ctypes.CDLL() cannot load {0}. You might want to set "
                      "LD_LIBRARY_PATH env variable".format(so_file_name))

# c_ssize_t was only added in py27. Manually backporting it here for py26
try:
    import ctypes.c_ssize_t
except ImportError:
    if sizeof(ctypes.c_uint) == sizeof(ctypes.c_void_p):
        setattr(ctypes, 'c_ssize_t', ctypes.c_int)
    elif sizeof(ctypes.c_ulong) == sizeof(ctypes.c_void_p):
        setattr(ctypes, 'c_ssize_t', ctypes.c_long)
    elif sizeof(ctypes.c_ulonglong) == sizeof(ctypes.c_void_p):
        setattr(ctypes, 'c_ssize_t', ctypes.c_longlong)


# Wow, the Linux kernel folks really play nasty games with this structure.  If
# you look at the man page for stat(2) and then at this definition you'll note
# two discrepancies.  First, we seem to have st_nlink and st_mode reversed.  In
# fact that's exactly how they're defined *for 64-bit systems*; for 32-bit
# they're in the man-page order.  Even uglier, the man page makes no mention of
# the *nsec fields, but they are very much present and if they're not included
# then we get memory corruption because libgfapi has a structure definition
# that's longer than ours and they overwrite some random bit of memory after
# the space we allocated.  Yes, that's all very disgusting, and I'm still not
# sure this will really work on 32-bit because all of the field types are so
# obfuscated behind macros and feature checks.


class Stat (ctypes.Structure):
    _fields_ = [
        ("st_dev", ctypes.c_ulong),
        ("st_ino", ctypes.c_ulong),
        ("st_nlink", ctypes.c_ulong),
        ("st_mode", ctypes.c_uint),
        ("st_uid", ctypes.c_uint),
        ("st_gid", ctypes.c_uint),
        ("st_rdev", ctypes.c_ulong),
        ("st_size", ctypes.c_ulong),
        ("st_blksize", ctypes.c_ulong),
        ("st_blocks", ctypes.c_ulong),
        ("st_atime", ctypes.c_ulong),
        ("st_atimensec", ctypes.c_ulong),
        ("st_mtime", ctypes.c_ulong),
        ("st_mtimensec", ctypes.c_ulong),
        ("st_ctime", ctypes.c_ulong),
        ("st_ctimensec", ctypes.c_ulong),
    ]


class Statvfs (ctypes.Structure):
    _fields_ = [
        ("f_bsize", ctypes.c_ulong),
        ("f_frsize", ctypes.c_ulong),
        ("f_blocks", ctypes.c_ulong),
        ("f_bfree", ctypes.c_ulong),
        ("f_bavail", ctypes.c_ulong),
        ("f_files", ctypes.c_ulong),
        ("f_ffree", ctypes.c_ulong),
        ("f_favail", ctypes.c_ulong),
        ("f_fsid", ctypes.c_ulong),
        ("f_flag", ctypes.c_ulong),
        ("f_namemax", ctypes.c_ulong),
        ("__f_spare", ctypes.c_int * 6),
    ]


class Dirent (ctypes.Structure):
    _fields_ = [
        ("d_ino", ctypes.c_ulong),
        ("d_off", ctypes.c_ulong),
        ("d_reclen", ctypes.c_ushort),
        ("d_type", ctypes.c_char),
        ("d_name", ctypes.c_char * 256),
    ]


class Timespec (ctypes.Structure):
    _fields_ = [
        ('tv_sec', ctypes.c_long),
        ('tv_nsec', ctypes.c_long)
    ]


# Here is the reference card of libgfapi library exported
# apis with its different versions.
#
#   GFAPI_3.4.0 {
#               glfs_new;
#               glfs_set_volfile;
#               glfs_set_volfile_server;
#               glfs_set_logging;
#               glfs_init;
#               glfs_fini;
#               glfs_open;
#               glfs_creat;
#               glfs_close;
#               glfs_from_glfd;
#               glfs_set_xlator_option;
#               glfs_read;
#               glfs_write;
#               glfs_read_async;
#               glfs_write_async;
#               glfs_readv;
#               glfs_writev;
#               glfs_readv_async;
#               glfs_writev_async;
#               glfs_pread;
#               glfs_pwrite;
#               glfs_pread_async;
#               glfs_pwrite_async;
#               glfs_preadv;
#               glfs_pwritev;
#               glfs_preadv_async;
#               glfs_pwritev_async;
#               glfs_lseek;
#               glfs_truncate;
#               glfs_ftruncate;
#               glfs_ftruncate_async;
#               glfs_lstat;
#               glfs_stat;
#               glfs_fstat;
#               glfs_fsync;
#               glfs_fsync_async;
#               glfs_fdatasync;
#               glfs_fdatasync_async;
#               glfs_access;
#               glfs_symlink;
#               glfs_readlink;
#               glfs_mknod;
#               glfs_mkdir;
#               glfs_unlink;
#               glfs_rmdir;
#               glfs_rename;
#               glfs_link;
#               glfs_opendir;
#               glfs_readdir_r;
#               glfs_readdirplus_r;
#               glfs_telldir;
#               glfs_seekdir;
#               glfs_closedir;
#               glfs_statvfs;
#               glfs_chmod;
#               glfs_fchmod;
#               glfs_chown;
#               glfs_lchown;
#               glfs_fchown;
#               glfs_utimens;
#               glfs_lutimens;
#               glfs_futimens;
#               glfs_getxattr;
#               glfs_lgetxattr;
#               glfs_fgetxattr;
#               glfs_listxattr;
#               glfs_llistxattr;
#               glfs_flistxattr;
#               glfs_setxattr;
#               glfs_lsetxattr;
#               glfs_fsetxattr;
#               glfs_removexattr;
#               glfs_lremovexattr;
#               glfs_fremovexattr;
#               glfs_getcwd;
#               glfs_chdir;
#               glfs_fchdir;
#               glfs_realpath;
#               glfs_posix_lock;
#               glfs_dup;
#
#               }
#
#   GFAPI_3.4.2 {
#               glfs_setfsuid;
#               glfs_setfsgid;
#               glfs_setfsgroups;
#               glfs_h_lookupat;
#               glfs_h_creat;
#               glfs_h_mkdir;
#               glfs_h_mknod;
#               glfs_h_symlink;
#               glfs_h_unlink;
#               glfs_h_close;
#               glfs_h_truncate;
#               glfs_h_stat;
#               glfs_h_getattrs;
#               glfs_h_setattrs;
#               glfs_h_readlink;
#               glfs_h_link;
#               glfs_h_rename;
#               glfs_h_extract_handle;
#               glfs_h_create_from_handle;
#               glfs_h_opendir;
#               glfs_h_open;
#               }
#
#   GFAPI_3.5.0 {
#
#               glfs_get_volumeid;
#               glfs_readdir;
#               glfs_readdirplus;
#               glfs_fallocate;
#               glfs_discard;
#               glfs_discard_async;
#               glfs_zerofill;
#               glfs_zerofill_async;
#               glfs_caller_specific_init;
#               glfs_h_setxattrs;
#
#               }
#
#   GFAPI_3.5.1 {
#
#               glfs_unset_volfile_server;
#               glfs_h_getxattrs;
#               glfs_h_removexattrs;
#
#               }
#
#   GFAPI_3.6.0 {
#
#               glfs_get_volfile;
#               glfs_h_access;
#
#               }
#

def gfapi_prototype(method_name, restype, *argtypes):
    """
    Create a named foreign function belonging to gfapi

    :param method_name: Name of the foreign function
    :param restype: resulting type
    :param argtypes: arguments that represent argument types
    :returns: foreign function of gfapi library
    """
    # use_errno=True ensures that errno is exposed by ctypes.get_errno()
    return ctypes.CFUNCTYPE(restype, *argtypes, use_errno=True)(
        (method_name, client)
    )

# Define function prototypes for the wrapper functions.

glfs_init = gfapi_prototype('glfs_init', ctypes.c_int, ctypes.c_void_p)

glfs_statvfs = gfapi_prototype('glfs_statvfs', ctypes.c_int,
                               ctypes.c_void_p,
                               ctypes.c_char_p,
                               ctypes.c_void_p)

glfs_new = gfapi_prototype('glfs_new', ctypes.c_void_p, ctypes.c_char_p)

glfs_set_volfile_server = gfapi_prototype(
    'glfs_set_volfile_server', ctypes.c_int,
    ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int)

glfs_set_logging = gfapi_prototype('glfs_set_logging', ctypes.c_int,
                                   ctypes.c_void_p,
                                   ctypes.c_char_p,
                                   ctypes.c_int)

glfs_fini = gfapi_prototype('glfs_fini', ctypes.c_int,
                            ctypes.c_void_p)

glfs_creat = gfapi_prototype('glfs_creat', ctypes.c_void_p, ctypes.c_void_p,
                             ctypes.c_char_p, ctypes.c_int, ctypes.c_uint)

glfs_open = gfapi_prototype('glfs_open', ctypes.c_void_p, ctypes.c_void_p,
                            ctypes.c_char_p, ctypes.c_int)

glfs_close = gfapi_prototype('glfs_close', ctypes.c_int,
                             ctypes.c_void_p)

glfs_lstat = gfapi_prototype('glfs_lstat', ctypes.c_int,
                             ctypes.c_void_p, ctypes.c_char_p,
                             ctypes.POINTER(Stat))

glfs_stat = gfapi_prototype('glfs_stat', ctypes.c_int,
                            ctypes.c_void_p, ctypes.c_char_p,
                            ctypes.POINTER(Stat))

glfs_fstat = gfapi_prototype('glfs_fstat', ctypes.c_int,
                             ctypes.c_void_p, ctypes.POINTER(Stat))

glfs_chmod = gfapi_prototype('glfs_chmod', ctypes.c_int,
                             ctypes.c_void_p,
                             ctypes.c_char_p,
                             ctypes.c_ushort)

glfs_fchmod = gfapi_prototype('glfs_fchmod', ctypes.c_int,
                              ctypes.c_void_p,
                              ctypes.c_ushort)

glfs_chown = gfapi_prototype('glfs_chown', ctypes.c_int,
                             ctypes.c_void_p,
                             ctypes.c_char_p,
                             ctypes.c_uint,
                             ctypes.c_uint)

glfs_lchown = gfapi_prototype('glfs_lchown', ctypes.c_int,
                              ctypes.c_void_p,
                              ctypes.c_char_p,
                              ctypes.c_uint,
                              ctypes.c_uint)

glfs_fchown = gfapi_prototype('glfs_fchown', ctypes.c_int,
                              ctypes.c_void_p, ctypes.c_uint,
                              ctypes.c_uint)

glfs_dup = gfapi_prototype('glfs_dup', ctypes.c_void_p,
                           ctypes.c_void_p)

glfs_fdatasync = gfapi_prototype('glfs_fdatasync', ctypes.c_int,
                                 ctypes.c_void_p)

glfs_fsync = gfapi_prototype('glfs_fsync', ctypes.c_int,
                             ctypes.c_void_p)

glfs_lseek = gfapi_prototype('glfs_lseek', ctypes.c_int,
                             ctypes.c_void_p, ctypes.c_int,
                             ctypes.c_int)

glfs_read = gfapi_prototype('glfs_read', ctypes.c_ssize_t,
                            ctypes.c_void_p,
                            ctypes.c_void_p,
                            ctypes.c_size_t,
                            ctypes.c_int)

glfs_write = gfapi_prototype('glfs_write', ctypes.c_ssize_t,
                             ctypes.c_void_p,
                             ctypes.c_void_p,
                             ctypes.c_size_t,
                             ctypes.c_int)

glfs_getxattr = gfapi_prototype('glfs_getxattr', ctypes.c_ssize_t,
                                ctypes.c_void_p,
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.c_void_p,
                                ctypes.c_size_t)

glfs_listxattr = gfapi_prototype('glfs_listxattr', ctypes.c_ssize_t,
                                 ctypes.c_void_p,
                                 ctypes.c_char_p,
                                 ctypes.c_void_p,
                                 ctypes.c_size_t)

glfs_removexattr = gfapi_prototype('glfs_removexattr', ctypes.c_int,
                                   ctypes.c_void_p,
                                   ctypes.c_char_p,
                                   ctypes.c_char_p)

glfs_setxattr = gfapi_prototype('glfs_setxattr', ctypes.c_int,
                                ctypes.c_void_p,
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.c_void_p,
                                ctypes.c_size_t,
                                ctypes.c_int)

glfs_rename = gfapi_prototype('glfs_rename', ctypes.c_int,
                              ctypes.c_void_p,
                              ctypes.c_char_p,
                              ctypes.c_char_p)

glfs_symlink = gfapi_prototype('glfs_symlink', ctypes.c_int,
                               ctypes.c_void_p,
                               ctypes.c_char_p,
                               ctypes.c_char_p)

glfs_unlink = gfapi_prototype('glfs_unlink', ctypes.c_int,
                              ctypes.c_void_p, ctypes.c_char_p)

glfs_readdir_r = gfapi_prototype('glfs_readdir_r', ctypes.c_int,
                                 ctypes.c_void_p,
                                 ctypes.POINTER(Dirent),
                                 ctypes.POINTER(ctypes.POINTER(Dirent)))

glfs_closedir = gfapi_prototype('glfs_closedir', ctypes.c_int,
                                ctypes.c_void_p)

glfs_mkdir = gfapi_prototype('glfs_mkdir', ctypes.c_int,
                             ctypes.c_void_p, ctypes.c_char_p,
                             ctypes.c_ushort)

glfs_opendir = gfapi_prototype('glfs_opendir', ctypes.c_void_p,
                               ctypes.c_void_p,
                               ctypes.c_char_p)

glfs_rmdir = gfapi_prototype('glfs_rmdir', ctypes.c_int,
                             ctypes.c_void_p,
                             ctypes.c_char_p)

glfs_setfsuid = gfapi_prototype('glfs_setfsuid', ctypes.c_int,
                                ctypes.c_uint)

glfs_setfsgid = gfapi_prototype('glfs_setfsgid', ctypes.c_int,
                                ctypes.c_uint)

glfs_ftruncate = gfapi_prototype('glfs_ftruncate', ctypes.c_int,
                                 ctypes.c_void_p, ctypes.c_int)

glfs_fgetxattr = gfapi_prototype('glfs_fgetxattr', ctypes.c_ssize_t,
                                 ctypes.c_void_p,
                                 ctypes.c_char_p,
                                 ctypes.c_void_p,
                                 ctypes.c_size_t)

glfs_fremovexattr = gfapi_prototype('glfs_fremovexattr', ctypes.c_int,
                                    ctypes.c_void_p,
                                    ctypes.c_char_p)

glfs_fsetxattr = gfapi_prototype('glfs_fsetxattr', ctypes.c_int,
                                 ctypes.c_void_p,
                                 ctypes.c_char_p,
                                 ctypes.c_void_p,
                                 ctypes.c_size_t,
                                 ctypes.c_int)

glfs_flistxattr = gfapi_prototype('glfs_flistxattr', ctypes.c_ssize_t,
                                  ctypes.c_void_p,
                                  ctypes.c_void_p,
                                  ctypes.c_size_t)

glfs_access = gfapi_prototype('glfs_access', ctypes.c_int,
                              ctypes.c_void_p,
                              ctypes.c_char_p,
                              ctypes.c_int)

glfs_readlink = gfapi_prototype('glfs_readlink', ctypes.c_int,
                                ctypes.c_void_p,
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.c_size_t)

glfs_chdir = gfapi_prototype('glfs_chdir', ctypes.c_int,
                             ctypes.c_void_p,
                             ctypes.c_char_p)

glfs_getcwd = gfapi_prototype('glfs_getcwd', ctypes.c_char_p,
                              ctypes.c_void_p,
                              ctypes.c_char_p,
                              ctypes.c_size_t)

glfs_fallocate = gfapi_prototype('glfs_fallocate', ctypes.c_int,
                                 ctypes.c_void_p,
                                 ctypes.c_int,
                                 ctypes.c_size_t)

glfs_discard = gfapi_prototype('glfs_discard', ctypes.c_int,
                               ctypes.c_void_p,
                               ctypes.c_int,
                               ctypes.c_size_t)

glfs_zerofill = gfapi_prototype('glfs_zerofill', ctypes.c_int,
                                ctypes.c_void_p,
                                ctypes.c_int,
                                ctypes.c_size_t)

glfs_utimens = gfapi_prototype('glfs_utimens', ctypes.c_int,
                               ctypes.c_void_p,
                               ctypes.c_char_p,
                               ctypes.POINTER(Timespec))