summaryrefslogtreecommitdiff
path: root/osinfo/osinfo_os.c
blob: cedeaac36252a18f5dabd5a8e4ff973288b76705 (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
#include <osinfo/osinfo.h>

G_DEFINE_TYPE (OsinfoOs, osinfo_os, OSINFO_TYPE_ENTITY);

#define OSINFO_OS_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), OSINFO_TYPE_OS, OsinfoOsPrivate))

static void osinfo_os_finalize (GObject *object);

static void
osinfo_os_finalize (GObject *object)
{
    OsinfoOs *self = OSINFO_OS (object);

    g_tree_destroy (self->priv->sections);
    g_tree_destroy (self->priv->sectionsAsList);
    g_tree_destroy (self->priv->hypervisors);
    g_tree_destroy (self->priv->relationshipsByOs);
    g_tree_destroy (self->priv->relationshipsByType);

    /* Chain up to the parent class */
    G_OBJECT_CLASS (osinfo_os_parent_class)->finalize (object);
}

/* Init functions */
static void
osinfo_os_class_init (OsinfoOsClass *klass)
{
    GObjectClass *g_klass = G_OBJECT_CLASS (klass);

    g_klass->finalize = osinfo_os_finalize;
    g_type_class_add_private (klass, sizeof (OsinfoOsPrivate));
}

static void
osinfo_os_init (OsinfoOs *self)
{
    OsinfoOsPrivate *priv;
    self->priv = priv = OSINFO_OS_GET_PRIVATE(self);

    self->priv->sections = g_tree_new_full(__osinfoStringCompare,
                                           NULL,
                                           g_free,
                                           __osinfoFreeDeviceSection);

    self->priv->sectionsAsList = g_tree_new_full(__osinfoStringCompare,
                                                 NULL,
                                                 g_free,
                                                 __osinfoFreePtrArray);

    self->priv->relationshipsByOs = g_tree_new_full(__osinfoStringCompare,
                                                NULL,
                                                g_free,
                                                __osinfoFreeRelationship);
    self->priv->relationshipsByType = g_tree_new(__osinfoIntCompareBase);

    self->priv->hypervisors = g_tree_new_full(__osinfoStringCompare,
                                              NULL,
                                              g_free,
                                              __osinfoFreeHvSection);
}

OsinfoOs *osinfo_os_new(const gchar *id)
{
    return g_object_new(OSINFO_TYPE_OS,
			"id", id,
			NULL);
}


static int __osinfoAddOsRelationshipByOs(OsinfoOs *self,
                                         gchar *otherOsId,
                                         osinfoRelationship rel,
                                         struct __osinfoOsLink *osLink)
{
    gboolean found;
    gpointer origKey, foundValue;
    GPtrArray *relationshipsForOs;
    gchar *otherOsIdDup = NULL;

    found = g_tree_lookup_extended(self->priv->relationshipsByOs, otherOsId, &origKey, &foundValue);
    if (!found) {
        otherOsIdDup = g_strdup(otherOsId);
        relationshipsForOs = g_ptr_array_new_with_free_func(__osinfoFreeOsLink);

        g_tree_insert(self->priv->relationshipsByOs, otherOsIdDup, relationshipsForOs);
    }
    else
        relationshipsForOs = (GPtrArray *) foundValue;

    g_ptr_array_add(relationshipsForOs, osLink);
    return 0;
}

static int __osinfoAddOsRelationshipByType(OsinfoOs *self,
                                           osinfoRelationship relshp,
                                           struct __osinfoOsLink *osLink)
{
    gboolean found;
    gpointer origKey, foundValue;
    GPtrArray *relationshipsForType;

    found = g_tree_lookup_extended(self->priv->relationshipsByType, (gpointer) relshp, &origKey, &foundValue);
    if (!found) {
        relationshipsForType = g_ptr_array_new();

        g_tree_insert(self->priv->relationshipsByType, (gpointer) relshp, relationshipsForType);
    }
    else
        relationshipsForType = (GPtrArray *) foundValue;

    g_ptr_array_add(relationshipsForType, osLink);
    return 0;
}

static void __osinfoRemoveOsLink(OsinfoOs *self,
                                 gchar *otherOsId,
                                 osinfoRelationship relshp,
                                 struct __osinfoOsLink *osLink)
{
    gboolean found;
    gpointer origKey, foundValue;
    GPtrArray *relationshipsForOs;
    GPtrArray *relationshipsForType;

    // First from by-os list
    found = g_tree_lookup_extended(self->priv->relationshipsByOs, otherOsId, &origKey, &foundValue);
    if (found) {
        relationshipsForOs = (GPtrArray *) foundValue;
        g_ptr_array_remove(relationshipsForOs, osLink);
    }

    // Now from by-relshp list
    found = g_tree_lookup_extended(self->priv->relationshipsByType, (gpointer) relshp, &origKey, &foundValue);
    if (found) {
        relationshipsForType = (GPtrArray *) foundValue;
        g_ptr_array_remove(relationshipsForType, osLink);
    }
}

int __osinfoAddOsRelationship (OsinfoOs *self, gchar *otherOsId, osinfoRelationship rel)
{
    if ( !OSINFO_IS_OS(self) || !otherOsId)
        return -EINVAL;

    struct __osinfoOsLink *osLink = NULL;
    osLink = g_new0(struct __osinfoOsLink, 1);

    osLink->subjectOs = self;
    osLink->verb = rel;

    int ret;
    ret = __osinfoAddOsRelationshipByOs(self, otherOsId, rel, osLink);
    if (ret != 0)
        goto error_free;

    ret = __osinfoAddOsRelationshipByType(self, rel, osLink);
    if (ret != 0)
        goto error_cleanup;

    return ret;

error_cleanup:
    __osinfoRemoveOsLink(self, otherOsId, rel, osLink);
error_free:
    g_free(osLink);
    return ret;
}

int __osinfoAddDeviceToSectionOs(OsinfoOs *self, gchar *section, gchar *id, gchar *driver)
{
    if( !OSINFO_IS_OS(self) || !section || !id || !driver)
        return -EINVAL;

    return __osinfoAddDeviceToSection(self->priv->sections, self->priv->sectionsAsList, section, id, driver);
}

void __osinfoClearDeviceSectionOs(OsinfoOs *self, gchar *section)
{
    if (!OSINFO_IS_OS(self) || !section)
        return;

    __osinfoClearDeviceSection(self->priv->sections, self->priv->sectionsAsList, section);
}

struct __osinfoHvSection *__osinfoAddHypervisorSectionToOs(OsinfoOs *self, gchar *hvId)
{
    if (!OSINFO_IS_OS(self) || !hvId)
        return NULL;

    gboolean found;
    gpointer origKey, foundValue;
    struct __osinfoHvSection *hvSection = NULL;
    gchar *hvIdDup = NULL;
    GTree *deviceSections;
    GTree *deviceSectionsAsList;

    found = g_tree_lookup_extended(self->priv->hypervisors, hvId, &origKey, &foundValue);
    if (!found) {
        hvSection = g_malloc(sizeof(*hvSection));
        hvIdDup = g_strdup(hvId);
        deviceSections = g_tree_new_full(__osinfoStringCompare,
                                        NULL,
                                        g_free,
                                        __osinfoFreeDeviceSection);


        deviceSectionsAsList = g_tree_new_full(__osinfoStringCompare,
                                               NULL,
                                               g_free,
                                               __osinfoFreePtrArray);

        hvSection->os = self;
        // Will set hv link later
        hvSection->sections = deviceSections;
        hvSection->sectionsAsList = deviceSectionsAsList;

        g_tree_insert(self->priv->hypervisors, hvIdDup, hvSection);
        return hvSection;
    }
    else
        return (struct __osinfoHvSection *) foundValue;
}

void __osinfoRemoveHvSectionFromOs(OsinfoOs *self, gchar *hvId)
{
    g_tree_remove(self->priv->hypervisors, hvId);
}

OsinfoDevice *osinfo_os_get_preferred_device(OsinfoOs *self, OsinfoHypervisor *hv, gchar *devType, OsinfoFilter *filter)
{
    g_return_val_if_fail(OSINFO_IS_OS(self), NULL);
    g_return_val_if_fail(OSINFO_IS_HYPERVISOR(hv), NULL);
    g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);
    g_return_val_if_fail(devType != NULL, NULL);
    // Check if device type info present for <os,hv>, else return NULL.

    GPtrArray *sectionList = NULL;
    if (hv) {
        // Check if hypervisor specific info present for Os, else return NULL.
        struct __osinfoHvSection *hvSection = NULL;
        hvSection = g_tree_lookup(self->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
        if (!hvSection)
            return NULL;

        sectionList = g_tree_lookup(hvSection->sectionsAsList, devType);
        if (!sectionList)
            return NULL;
    }
    else {
        sectionList = g_tree_lookup(self->priv->sectionsAsList, devType);
        if (!sectionList)
            return NULL;
    }

    // For each device in section list, apply filter. If filter passes, return device.
    int i;
    struct __osinfoDeviceLink *deviceLink;
    for (i = 0; i < sectionList->len; i++) {
        deviceLink = g_ptr_array_index(sectionList, i);
        if (osinfo_entity_matches_filter(OSINFO_ENTITY(deviceLink->dev), filter))
	    return deviceLink->dev;
    }

    // If no devices pass filter, return NULL.
    return NULL;
}

OsinfoOsList *osinfo_os_get_related(OsinfoOs *self, osinfoRelationship relshp)
{
    g_return_val_if_fail(OSINFO_IS_OS(self), NULL);

    // Create our list
    OsinfoOsList *newList = osinfo_oslist_new();

    GPtrArray *relatedOses = NULL;
    relatedOses = g_tree_lookup(self->priv->relationshipsByType, (gpointer) relshp);
    if (relatedOses) {
        int i, len;
        len = relatedOses->len;
        for (i = 0; i < len; i++) {
            struct __osinfoOsLink *osLink = g_ptr_array_index(relatedOses, i);
            osinfo_list_add(OSINFO_LIST (newList), OSINFO_ENTITY (osLink->directObjectOs));
        }
    }

    return newList;
}

OsinfoDeviceList *osinfo_os_get_devices(OsinfoOs *self, OsinfoHypervisor *hv, gchar *devType, OsinfoFilter *filter)
{
    g_return_val_if_fail(OSINFO_IS_OS(self), NULL);
    g_return_val_if_fail(OSINFO_IS_HYPERVISOR(hv), NULL);
    g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);
    g_return_val_if_fail(devType != NULL, NULL);

    GPtrArray *sectionList = NULL;

    // Create our device list
    OsinfoDeviceList *newList = osinfo_devicelist_new();

    if (hv) {
        struct __osinfoHvSection *hvSection = NULL;
        hvSection = g_tree_lookup(self->priv->hypervisors, (OSINFO_ENTITY(hv))->priv->id);
        if (!hvSection)
            return newList;

        sectionList = g_tree_lookup(hvSection->sectionsAsList, devType);
        if (!sectionList)
            return newList;
    }
    else {
        sectionList = g_tree_lookup(self->priv->sectionsAsList, devType);
        if (!sectionList)
            return newList;
    }

    // For each device in section list, apply filter. If filter passes, add device to list.
    int i;
    struct __osinfoDeviceLink *deviceLink;
    for (i = 0; i < sectionList->len; i++) {
        deviceLink = g_ptr_array_index(sectionList, i);
        if (osinfo_entity_matches_filter(OSINFO_ENTITY(deviceLink->dev), filter))
	    osinfo_list_add(OSINFO_LIST (newList), OSINFO_ENTITY (deviceLink->dev));
    }

    return NULL;
}