summaryrefslogtreecommitdiffstats
path: root/tools/glusterfind/src/libgfchangelog.py
blob: e54a16a474239a47d1ad330db015b77d1d87c93d (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
#!/usr/bin/env python

# Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com/>
# This file is part of GlusterFS.
#
# 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 os
from ctypes import CDLL, get_errno, create_string_buffer, c_ulong, byref
from ctypes import RTLD_GLOBAL
from ctypes.util import find_library


class ChangelogException(OSError):
    pass


libgfc = CDLL(find_library("gfchangelog"), use_errno=True, mode=RTLD_GLOBAL)


def raise_oserr():
    errn = get_errno()
    raise ChangelogException(errn, os.strerror(errn))


def cl_register(brick, path, log_file, log_level, retries=0):
    ret = libgfc.gf_changelog_register(brick, path, log_file,
                                       log_level, retries)
    if ret == -1:
        raise_oserr()


def cl_history_scan():
    ret = libgfc.gf_history_changelog_scan()
    if ret == -1:
        raise_oserr()

    return ret


def cl_history_changelog(changelog_path, start, end, num_parallel):
    actual_end = c_ulong()
    ret = libgfc.gf_history_changelog(changelog_path, start, end,
                                      num_parallel,
                                      byref(actual_end))
    if ret == -1:
        raise_oserr()

    return actual_end.value


def cl_history_startfresh():
    ret = libgfc.gf_history_changelog_start_fresh()
    if ret == -1:
        raise_oserr()


def cl_history_getchanges():
    """ remove hardcoding for path name length """
    def clsort(f):
        return f.split('.')[-1]

    changes = []
    buf = create_string_buffer('\0', 4096)

    while True:
        ret = libgfc.gf_history_changelog_next_change(buf, 4096)
        if ret in (0, -1):
            break
        changes.append(buf.raw[:ret - 1])
    if ret == -1:
        raise_oserr()

    return sorted(changes, key=clsort)


def cl_history_done(clfile):
    ret = libgfc.gf_history_changelog_done(clfile)
    if ret == -1:
        raise_oserr()