summaryrefslogtreecommitdiff
path: root/src/libnm-glib-aux/nm-keyfile-aux.c
blob: 75abe53848577c63046b666298a629b4ca21a836 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019 Red Hat, Inc.
 */

#include "libnm-glib-aux/nm-default-glib-i18n-lib.h"

#include "nm-keyfile-aux.h"

#include <syslog.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "nm-io-utils.h"

/*****************************************************************************/

struct _NMKeyFileDB {
    NMKeyFileDBLogFcn      log_fcn;
    NMKeyFileDBGotDirtyFcn got_dirty_fcn;
    gpointer               user_data;
    const char *           group_name;
    GKeyFile *             kf;
    guint                  ref_count;

    bool is_started : 1;
    bool dirty : 1;
    bool destroyed : 1;

    char filename[];
};

#define _NMLOG(self, syslog_level, fmt, ...)                                                   \
    G_STMT_START                                                                               \
    {                                                                                          \
        NMKeyFileDB *_self = (self);                                                           \
                                                                                               \
        nm_assert(_self);                                                                      \
        nm_assert(!_self->destroyed);                                                          \
                                                                                               \
        if (_self->log_fcn) {                                                                  \
            _self->log_fcn(_self, (syslog_level), _self->user_data, "" fmt "", ##__VA_ARGS__); \
        };                                                                                     \
    }                                                                                          \
    G_STMT_END

#define _LOGD(...) _NMLOG(self, LOG_DEBUG, __VA_ARGS__)

static gboolean
_IS_KEY_FILE_DB(NMKeyFileDB *self, gboolean require_is_started, gboolean allow_destroyed)
{
    if (self == NULL)
        return FALSE;
    if (self->ref_count <= 0) {
        nm_assert_not_reached();
        return FALSE;
    }
    if (require_is_started && !self->is_started)
        return FALSE;
    if (!allow_destroyed && self->destroyed)
        return FALSE;
    return TRUE;
}

/*****************************************************************************/

NMKeyFileDB *
nm_key_file_db_new(const char *           filename,
                   const char *           group_name,
                   NMKeyFileDBLogFcn      log_fcn,
                   NMKeyFileDBGotDirtyFcn got_dirty_fcn,
                   gpointer               user_data)
{
    NMKeyFileDB *self;
    gsize        l_filename;
    gsize        l_group;

    g_return_val_if_fail(filename && filename[0], NULL);
    g_return_val_if_fail(group_name && group_name[0], NULL);

    l_filename = strlen(filename);
    l_group    = strlen(group_name);

    self                = g_malloc0(sizeof(NMKeyFileDB) + l_filename + 1 + l_group + 1);
    self->ref_count     = 1;
    self->log_fcn       = log_fcn;
    self->got_dirty_fcn = got_dirty_fcn;
    self->user_data     = user_data;
    self->kf            = g_key_file_new();
    g_key_file_set_list_separator(self->kf, ',');
    memcpy(self->filename, filename, l_filename + 1);
    self->group_name = &self->filename[l_filename + 1];
    memcpy((char *) self->group_name, group_name, l_group + 1);

    return self;
}

NMKeyFileDB *
nm_key_file_db_ref(NMKeyFileDB *self)
{
    if (!self)
        return NULL;

    g_return_val_if_fail(_IS_KEY_FILE_DB(self, FALSE, TRUE), NULL);

    nm_assert(self->ref_count < G_MAXUINT);
    self->ref_count++;
    return self;
}

void
nm_key_file_db_unref(NMKeyFileDB *self)
{
    if (!self)
        return;

    g_return_if_fail(_IS_KEY_FILE_DB(self, FALSE, TRUE));

    if (--self->ref_count > 0)
        return;

    g_key_file_unref(self->kf);

    g_free(self);
}

/* destroy() is like unref, but it also makes the instance unusable.
 * All changes afterwards fail with an assertion.
 *
 * The point is that NMKeyFileDB is ref-counted in principle. But there
 * is a primary owner who also provides the log_fcn().
 *
 * When the primary owner goes out of scope and gives up the reference, it does
 * not want to receive any log notifications anymore.
 *
 * The way NMKeyFileDB is intended to be used is in a very strict context:
 * NMSettings owns the NMKeyFileDB instance and receives logging notifications.
 * It's also the last one to persist the data to disk. Afterwards, no other user
 * is supposed to be around and do anything with NMKeyFileDB. But since NMKeyFileDB
 * is ref-counted it's hard to ensure that this is truly honored. So we start
 * asserting at that point.
 */
void
nm_key_file_db_destroy(NMKeyFileDB *self)
{
    if (!self)
        return;

    g_return_if_fail(_IS_KEY_FILE_DB(self, FALSE, FALSE));
    g_return_if_fail(!self->destroyed);

    self->destroyed = TRUE;
    nm_key_file_db_unref(self);
}

/*****************************************************************************/

/* nm_key_file_db_start() is supposed to be called right away, after creating the
 * instance.
 *
 * It's not done as separate step after nm_key_file_db_new(), because we want to log,
 * and the log_fcn returns the self pointer (which we should not expose before
 * nm_key_file_db_new() returns. */
void
nm_key_file_db_start(NMKeyFileDB *self)
{
    gs_free char *contents = NULL;
    gsize         contents_len;
    gs_free_error GError *error = NULL;

    g_return_if_fail(_IS_KEY_FILE_DB(self, FALSE, FALSE));
    g_return_if_fail(!self->is_started);

    self->is_started = TRUE;

    if (!nm_utils_file_get_contents(-1,
                                    self->filename,
                                    20 * 1024 * 1024,
                                    NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
                                    &contents,
                                    &contents_len,
                                    NULL,
                                    &error)) {
        _LOGD("failed to read \"%s\": %s", self->filename, error->message);
        return;
    }

    if (!g_key_file_load_from_data(self->kf,
                                   contents,
                                   contents_len,
                                   G_KEY_FILE_KEEP_COMMENTS,
                                   &error)) {
        _LOGD("failed to load keyfile \"%s\": %s", self->filename, error->message);
        return;
    }

    _LOGD("loaded keyfile-db for \"%s\"", self->filename);
}

/*****************************************************************************/

const char *
nm_key_file_db_get_filename(NMKeyFileDB *self)
{
    g_return_val_if_fail(_IS_KEY_FILE_DB(self, FALSE, TRUE), NULL);

    return self->filename;
}

gboolean
nm_key_file_db_is_dirty(NMKeyFileDB *self)
{
    g_return_val_if_fail(_IS_KEY_FILE_DB(self, FALSE, TRUE), FALSE);

    return self->dirty;
}

/*****************************************************************************/

char *
nm_key_file_db_get_value(NMKeyFileDB *self, const char *key)
{
    g_return_val_if_fail(_IS_KEY_FILE_DB(self, TRUE, TRUE), NULL);

    return g_key_file_get_value(self->kf, self->group_name, key, NULL);
}

char **
nm_key_file_db_get_string_list(NMKeyFileDB *self, const char *key, gsize *out_len)
{
    g_return_val_if_fail(_IS_KEY_FILE_DB(self, TRUE, TRUE), NULL);

    return g_key_file_get_string_list(self->kf, self->group_name, key, out_len, NULL);
}

/*****************************************************************************/

static void
_got_dirty(NMKeyFileDB *self, const char *key)
{
    nm_assert(_IS_KEY_FILE_DB(self, TRUE, FALSE));
    nm_assert(!self->dirty);

    _LOGD("updated entry for %s.%s", self->group_name, key);

    self->dirty = TRUE;
    if (self->got_dirty_fcn)
        self->got_dirty_fcn(self, self->user_data);
}

/*****************************************************************************/

void
nm_key_file_db_remove_key(NMKeyFileDB *self, const char *key)
{
    gboolean got_dirty = FALSE;

    g_return_if_fail(_IS_KEY_FILE_DB(self, TRUE, FALSE));

    if (!key)
        return;

    if (!self->dirty) {
        gs_free_error GError *error = NULL;

        g_key_file_has_key(self->kf, self->group_name, key, &error);
        got_dirty = (error != NULL);
    }
    g_key_file_remove_key(self->kf, self->group_name, key, NULL);

    if (got_dirty)
        _got_dirty(self, key);
}

void
nm_key_file_db_set_value(NMKeyFileDB *self, const char *key, const char *value)
{
    gs_free char *old_value = NULL;
    gboolean      got_dirty = FALSE;

    g_return_if_fail(_IS_KEY_FILE_DB(self, TRUE, FALSE));
    g_return_if_fail(key);

    if (!value) {
        nm_key_file_db_remove_key(self, key);
        return;
    }

    if (!self->dirty) {
        gs_free_error GError *error = NULL;

        old_value = g_key_file_get_value(self->kf, self->group_name, key, &error);
        if (error)
            got_dirty = TRUE;
    }

    g_key_file_set_value(self->kf, self->group_name, key, value);

    if (!self->dirty && !got_dirty) {
        gs_free_error GError *error     = NULL;
        gs_free char *        new_value = NULL;

        new_value = g_key_file_get_value(self->kf, self->group_name, key, &error);
        if (error || !new_value || !nm_streq0(old_value, new_value))
            got_dirty = TRUE;
    }

    if (got_dirty)
        _got_dirty(self, key);
}

void
nm_key_file_db_set_string_list(NMKeyFileDB *      self,
                               const char *       key,
                               const char *const *value,
                               gssize             len)
{
    gs_free char *old_value = NULL;
    gboolean      got_dirty = FALSE;

    g_return_if_fail(_IS_KEY_FILE_DB(self, TRUE, FALSE));
    g_return_if_fail(key);

    if (!value) {
        nm_key_file_db_remove_key(self, key);
        return;
    }

    if (!self->dirty) {
        gs_free_error GError *error = NULL;

        old_value = g_key_file_get_value(self->kf, self->group_name, key, &error);
        if (error)
            got_dirty = TRUE;
    }

    if (len < 0)
        len = NM_PTRARRAY_LEN(value);

    g_key_file_set_string_list(self->kf, self->group_name, key, value, len);

    if (!self->dirty && !got_dirty) {
        gs_free_error GError *error     = NULL;
        gs_free char *        new_value = NULL;

        new_value = g_key_file_get_value(self->kf, self->group_name, key, &error);
        if (error || !new_value || !nm_streq0(old_value, new_value))
            got_dirty = TRUE;
    }

    if (got_dirty)
        _got_dirty(self, key);
}

/*****************************************************************************/

void
nm_key_file_db_to_file(NMKeyFileDB *self, gboolean force)
{
    gs_free_error GError *error = NULL;

    g_return_if_fail(_IS_KEY_FILE_DB(self, TRUE, FALSE));

    if (!force && !self->dirty)
        return;

    self->dirty = FALSE;

    if (!g_key_file_save_to_file(self->kf, self->filename, &error)) {
        _LOGD("failure to write keyfile \"%s\": %s", self->filename, error->message);
    } else
        _LOGD("write keyfile: \"%s\"", self->filename);
}