summaryrefslogtreecommitdiff
path: root/src/settings/nm-settings-storage.c
blob: bea440cb5a4d2a943b502a7a7aa5852503ea6a9f (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2018 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-settings-storage.h"

#include "nm-utils.h"
#include "nm-settings-plugin.h"

#include "settings/plugins/keyfile/nms-keyfile-storage.h"

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

int
nm_settings_storage_cmp(NMSettingsStorage *a, NMSettingsStorage *b, const GSList *plugin_list)
{
    NMSettingsStorageClass *klass;

    /* Sort by priority.
     *
     * If a > b (by priority), we return a positive number (as one
     * would expect by a cmp() function). */

    nm_assert(NM_IS_SETTINGS_STORAGE(a));
    nm_assert(NM_IS_SETTINGS_STORAGE(b));
    nm_assert(a != b);
    nm_assert(nm_streq(nm_settings_storage_get_uuid(a), nm_settings_storage_get_uuid(b)));

    /* in-memory has always higher priority */
    NM_CMP_DIRECT(nm_settings_storage_is_keyfile_run(a), nm_settings_storage_is_keyfile_run(b));

    NM_CMP_RETURN(nm_settings_plugin_cmp_by_priority(nm_settings_storage_get_plugin(a),
                                                     nm_settings_storage_get_plugin(b),
                                                     plugin_list));

    klass = NM_SETTINGS_STORAGE_GET_CLASS(a);
    if (klass != NM_SETTINGS_STORAGE_GET_CLASS(b)) {
        /* one plugin must return storages of the same type. Otherwise, it's
         * unclear how cmp_fcn() should compare them. */
        nm_assert_not_reached();
        return 0;
    }

    if (klass->cmp_fcn)
        NM_CMP_RETURN(klass->cmp_fcn(a, b));

    return 0;
}

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

NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_PLUGIN, PROP_UUID, PROP_FILENAME, );

G_DEFINE_TYPE(NMSettingsStorage, nm_settings_storage, G_TYPE_OBJECT)

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

static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    NMSettingsStorage *self = NM_SETTINGS_STORAGE(object);

    switch (prop_id) {
    case PROP_PLUGIN:
        /* construct-only */
        self->_plugin = g_object_ref(g_value_get_object(value));
        nm_assert(NM_IS_SETTINGS_PLUGIN(self->_plugin));
        break;
    case PROP_UUID:
        /* construct-only */
        self->_uuid = g_value_dup_string(value);
        nm_assert(!self->_uuid || nm_utils_is_uuid(self->_uuid));
        break;
    case PROP_FILENAME:
        /* construct-only */
        self->_filename = g_value_dup_string(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

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

static void
nm_settings_storage_init(NMSettingsStorage *self)
{
    c_list_init(&self->_storage_lst);
    c_list_init(&self->_storage_by_uuid_lst);
}

NMSettingsStorage *
nm_settings_storage_new(NMSettingsPlugin *plugin, const char *uuid, const char *filename)
{
    nm_assert(NM_IS_SETTINGS_PLUGIN(plugin));
    nm_assert(nm_utils_is_uuid(uuid));

    return g_object_new(NM_TYPE_SETTINGS_STORAGE,
                        NM_SETTINGS_STORAGE_PLUGIN,
                        plugin,
                        NM_SETTINGS_STORAGE_UUID,
                        uuid,
                        NM_SETTINGS_STORAGE_FILENAME,
                        filename,
                        NULL);
}

static void
finalize(GObject *object)
{
    NMSettingsStorage *self = NM_SETTINGS_STORAGE(object);

    c_list_unlink_stale(&self->_storage_lst);
    c_list_unlink_stale(&self->_storage_by_uuid_lst);

    g_object_unref(self->_plugin);
    g_free(self->_uuid);
    g_free(self->_filename);

    G_OBJECT_CLASS(nm_settings_storage_parent_class)->finalize(object);
}

static void
nm_settings_storage_class_init(NMSettingsStorageClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->set_property = set_property;
    object_class->finalize     = finalize;

    obj_properties[PROP_PLUGIN] =
        g_param_spec_object(NM_SETTINGS_STORAGE_PLUGIN,
                            "",
                            "",
                            NM_TYPE_SETTINGS_PLUGIN,
                            G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

    obj_properties[PROP_UUID] =
        g_param_spec_string(NM_SETTINGS_STORAGE_UUID,
                            "",
                            "",
                            NULL,
                            G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

    obj_properties[PROP_FILENAME] =
        g_param_spec_string(NM_SETTINGS_STORAGE_FILENAME,
                            "",
                            "",
                            NULL,
                            G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

    g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
}