summaryrefslogtreecommitdiffstats
path: root/geo-replication/syncdaemon/libgfchangelog.py
blob: 34beadb35521343f5e27683326fe8fc201fa1fb2 (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
#
# Copyright (c) 2011-2014 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, RTLD_GLOBAL, get_errno, byref, c_ulong
from ctypes.util import find_library
from syncdutils import ChangelogException, ChangelogHistoryNotAvailable
from py2py3 import gr_cl_history_changelog, gr_cl_done, gr_create_string_buffer
from py2py3 import gr_cl_register, gr_cl_history_done, bytearray_to_str


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


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


def _init():
    if libgfc.gf_changelog_init(None) == -1:
        _raise_changelog_err()


def register(brick, path, log_file, log_level, retries=0):
    _init()

    ret = gr_cl_register(libgfc, brick, path, log_file, log_level, retries)

    if ret == -1:
        _raise_changelog_err()


def scan():
    ret = libgfc.gf_changelog_scan()
    if ret == -1:
        _raise_changelog_err()


def startfresh():
    ret = libgfc.gf_changelog_start_fresh()
    if ret == -1:
        _raise_changelog_err()


def getchanges():
    def clsort(cfile):
        return cfile.split('.')[-1]

    changes = []
    buf = gr_create_string_buffer(4096)
    call = libgfc.gf_changelog_next_change

    while True:
        ret = call(buf, 4096)
        if ret in (0, -1):
            break

        # py2 and py3 compatibility
        result = bytearray_to_str(buf.raw[:ret - 1])
        changes.append(result)

    if ret == -1:
        _raise_changelog_err()

    # cleanup tracker
    startfresh()

    return sorted(changes, key=clsort)


def done(clfile):
    ret = gr_cl_done(libgfc, clfile)
    if ret == -1:
        _raise_changelog_err()


def history_scan():
    ret = libgfc.gf_history_changelog_scan()
    if ret == -1:
        _raise_changelog_err()

    return ret


def history_changelog(changelog_path, start, end, num_parallel):
    actual_end = c_ulong()
    ret = gr_cl_history_changelog(libgfc, changelog_path, start, end,
                                  num_parallel, byref(actual_end))
    if ret == -1:
        _raise_changelog_err()

    if ret == -2:
        raise ChangelogHistoryNotAvailable()

    return (ret, actual_end.value)


def history_startfresh():
    ret = libgfc.gf_history_changelog_start_fresh()
    if ret == -1:
        _raise_changelog_err()


def history_getchanges():
    def clsort(cfile):
        return cfile.split('.')[-1]

    changes = []
    buf = gr_create_string_buffer(4096)
    call = libgfc.gf_history_changelog_next_change

    while True:
        ret = call(buf, 4096)
        if ret in (0, -1):
            break

        # py2 and py3 compatibility
        result = bytearray_to_str(buf.raw[:ret - 1])
        changes.append(result)

    if ret == -1:
        _raise_changelog_err()

    return sorted(changes, key=clsort)


def history_done(clfile):
    ret = gr_cl_history_done(libgfc, clfile)
    if ret == -1:
        _raise_changelog_err()