summaryrefslogtreecommitdiffstats
path: root/xlators/features/changelog/src/changelog-encoders.c
blob: 08626ee2f2269f7937a144f60533a29b695684f9 (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
/*
   Copyright (c) 2013 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.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "changelog-encoders.h"

size_t
entry_fn (void *data, char *buffer, gf_boolean_t encode)
{
        char    *tmpbuf = NULL;
        size_t  bufsz  = 0;
        struct changelog_entry_fields *ce = NULL;

        ce = (struct changelog_entry_fields *) data;

        if (encode) {
                tmpbuf = uuid_utoa (ce->cef_uuid);
                CHANGELOG_FILL_BUFFER (buffer, bufsz, tmpbuf, strlen (tmpbuf));
        } else {
                CHANGELOG_FILL_BUFFER (buffer, bufsz,
                                       ce->cef_uuid, sizeof (uuid_t));
        }

        CHANGELOG_FILL_BUFFER (buffer, bufsz, "/", 1);
        CHANGELOG_FILL_BUFFER (buffer, bufsz,
                               ce->cef_bname, strlen (ce->cef_bname));
        return bufsz;
}

size_t
fop_fn (void *data, char *buffer, gf_boolean_t encode)
{
        char buf[10]          = {0,};
        size_t         bufsz = 0;
        glusterfs_fop_t fop   = 0;

        fop = *(glusterfs_fop_t *) data;

        if (encode) {
                (void) snprintf (buf, sizeof (buf), "%d", fop);
                CHANGELOG_FILL_BUFFER (buffer, bufsz, buf, strlen (buf));
        } else
                CHANGELOG_FILL_BUFFER (buffer, bufsz, &fop, sizeof (fop));

        return bufsz;
}

size_t
number_fn (void *data, char *buffer, gf_boolean_t encode)
{
        size_t       bufsz = 0;
        unsigned int nr    = 0;
        char buf[20]       = {0,};

        nr = *(unsigned int *) data;

        if (encode) {
                (void) snprintf (buf, sizeof (buf), "%u", nr);
                CHANGELOG_FILL_BUFFER (buffer, bufsz, buf, strlen (buf));
        } else
                CHANGELOG_FILL_BUFFER (buffer, bufsz, &nr, sizeof (unsigned int));

        return bufsz;
}

void
entry_free_fn (void *data)
{
        changelog_opt_t *co = data;

        if (!co)
                return;

        GF_FREE (co->co_entry.cef_bname);
}

/**
 * try to write all data in one shot
 */

static inline void
changelog_encode_write_xtra (changelog_log_data_t *cld,
                             char *buffer, size_t *off, gf_boolean_t encode)
{
        int              i      = 0;
        size_t           offset = 0;
        void            *data   = NULL;
        changelog_opt_t *co     = NULL;

        offset = *off;

        co = (changelog_opt_t *) cld->cld_ptr;

        for (; i < cld->cld_xtra_records; i++, co++) {
                CHANGELOG_FILL_BUFFER (buffer, offset, "\0", 1);

                switch (co->co_type) {
                case CHANGELOG_OPT_REC_FOP:
                        data = &co->co_fop;
                        break;
                case CHANGELOG_OPT_REC_ENTRY:
                        data = &co->co_entry;
                        break;
                case CHANGELOG_OPT_REC_UINT32:
                        data = &co->co_uint32;
                        break;
                }

                if (co->co_convert)
                        offset += co->co_convert (data,
                                                  buffer + offset, encode);
                else /* no coversion: write it out as it is */
                        CHANGELOG_FILL_BUFFER (buffer, offset,
                                               data, co->co_len);
        }

        *off = offset;
}

int
changelog_encode_ascii (xlator_t *this, changelog_log_data_t *cld)
{
        size_t            off      = 0;
        size_t            gfid_len = 0;
        char             *gfid_str = NULL;
        char             *buffer   = NULL;
        changelog_priv_t *priv     = NULL;

        priv = this->private;

        gfid_str = uuid_utoa (cld->cld_gfid);
        gfid_len = strlen (gfid_str);

        /* extra bytes for decorations */
        buffer = alloca (gfid_len + cld->cld_ptr_len + 10);
        CHANGELOG_STORE_ASCII (priv, buffer,
                               off, gfid_str, gfid_len, cld);

        if (cld->cld_xtra_records)
                changelog_encode_write_xtra (cld, buffer, &off, _gf_true);

        CHANGELOG_FILL_BUFFER (buffer, off, "\0", 1);

        return changelog_write_change (priv, buffer, off);
}

int
changelog_encode_binary (xlator_t *this, changelog_log_data_t *cld)
{
        size_t            off    = 0;
        char             *buffer = NULL;
        changelog_priv_t *priv   = NULL;

        priv = this->private;

        /* extra bytes for decorations */
        buffer = alloca (sizeof (uuid_t) + cld->cld_ptr_len + 10);
        CHANGELOG_STORE_BINARY (priv, buffer, off, cld->cld_gfid, cld);

        if (cld->cld_xtra_records)
                changelog_encode_write_xtra (cld, buffer, &off, _gf_false);

        CHANGELOG_FILL_BUFFER (buffer, off, "\0", 1);

        return changelog_write_change (priv, buffer, off);
}

static struct changelog_encoder
cb_encoder[] = {
        [CHANGELOG_ENCODE_BINARY] =
        {
                .encoder = CHANGELOG_ENCODE_BINARY,
                .encode = changelog_encode_binary,
        },
        [CHANGELOG_ENCODE_ASCII] =
        {
                .encoder = CHANGELOG_ENCODE_ASCII,
                .encode = changelog_encode_ascii,
        },
};

void
changelog_encode_change( changelog_priv_t * priv)
{
        priv->ce = &cb_encoder[priv->encode_mode];
}