~ubuntu-branches/ubuntu/wily/libuser/wily

« back to all changes in this revision

Viewing changes to lib/entity.c

  • Committer: Bazaar Package Importer
  • Author(s): Ghe Rivero
  • Date: 2008-05-02 16:01:50 UTC
  • mfrom: (1.2.1 upstream) (7 intrepid)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20080502160150-cq4ayxgq4etkecut
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
17
 */
18
18
 
19
 
#ident "$Id: entity.c,v 1.31 2005/10/09 05:15:47 mitr Exp $"
20
 
 
21
 
#ifdef HAVE_CONFIG_H
22
 
#include "../config.h"
23
 
#endif
 
19
#include <config.h>
24
20
#include <grp.h>
25
21
#include <pwd.h>
26
22
#include <stdio.h>
90
86
        g_free(ent);
91
87
}
92
88
 
 
89
/* Dump a set of attributes */
 
90
static void
 
91
lu_ent_dump_attributes(GArray *attrs, FILE *fp)
 
92
{
 
93
        size_t i;
 
94
 
 
95
        for (i = 0; i < attrs->len; i++) {
 
96
                struct lu_attribute *attribute;
 
97
                size_t j;
 
98
 
 
99
                attribute = &g_array_index(attrs, struct lu_attribute, i);
 
100
                for (j = 0; j < attribute->values->n_values; j++) {
 
101
                        GValue *value;
 
102
 
 
103
                        value = g_value_array_get_nth(attribute->values, j);
 
104
                        fprintf(fp, " %s = ",
 
105
                                g_quark_to_string(attribute->name));
 
106
                        if (G_VALUE_HOLDS_STRING(value))
 
107
                                fprintf(fp, "`%s'\n",
 
108
                                        g_value_get_string(value));
 
109
                        else if (G_VALUE_HOLDS_LONG(value))
 
110
                                fprintf(fp, "%ld\n", g_value_get_long(value));
 
111
                        else if (G_VALUE_HOLDS_INT64(value))
 
112
                                fprintf(fp, "%lld\n",
 
113
                                        (long long)g_value_get_int64(value));
 
114
                        else
 
115
                                fprintf(fp, "???\n");
 
116
                }
 
117
        }
 
118
}
 
119
 
93
120
/* Dump the contents of an entity structure.  This is primarily for
94
121
 * debugging. */
95
122
void
96
123
lu_ent_dump(struct lu_ent *ent, FILE *fp)
97
124
{
98
 
        size_t i, j;
99
 
        struct lu_attribute *attribute;
100
 
        GValue *value;
 
125
        size_t i;
 
126
 
101
127
        g_return_if_fail(ent != NULL);
102
128
        fprintf(fp, "dump of struct lu_ent at %p:\n", ent);
103
129
        fprintf(fp, " magic = %08x\n", ent->magic);
120
146
        /* Print the module list. */
121
147
        fprintf(fp, " modules = (");
122
148
        for (i = 0; i < ent->modules->n_values; i++) {
 
149
                GValue *value;
 
150
 
123
151
                value = g_value_array_get_nth(ent->modules, i);
124
 
                if (i > 0) {
 
152
                if (i > 0)
125
153
                        fprintf(fp, ", ");
126
 
                }
127
154
                if (G_VALUE_HOLDS_STRING(value))
128
155
                        fprintf(fp, "`%s'", g_value_get_string(value));
129
 
                else if (G_VALUE_HOLDS_LONG(value))
130
 
                        fprintf(fp, "%ld", g_value_get_long(value));
131
 
                else if (G_VALUE_HOLDS_INT64(value))
132
 
                        fprintf(fp, "%lld",
133
 
                                (long long)g_value_get_int64(value));
134
156
                else
135
157
                        fprintf(fp, "?");
136
158
        }
137
159
        fprintf(fp, ")\n");
138
160
        /* Print the current data values. */
139
 
        for (i = 0; i < ent->current->len; i++) {
140
 
                attribute = &g_array_index(ent->current,
141
 
                                           struct lu_attribute,
142
 
                                           i);
143
 
                for (j = 0; j < attribute->values->n_values; j++) {
144
 
                        value = g_value_array_get_nth(attribute->values, j);
145
 
                        if (G_VALUE_HOLDS_STRING(value))
146
 
                                fprintf(fp, " %s = `%s'\n",
147
 
                                        g_quark_to_string(attribute->name),
148
 
                                        g_value_get_string(value));
149
 
                        else if (G_VALUE_HOLDS_LONG(value))
150
 
                                fprintf(fp, " %s = %ld\n",
151
 
                                        g_quark_to_string(attribute->name),
152
 
                                        g_value_get_long(value));
153
 
 
154
 
                        else if (G_VALUE_HOLDS_INT64(value))
155
 
                                fprintf(fp, " %s = %lld\n",
156
 
                                        g_quark_to_string(attribute->name),
157
 
                                        (long long)g_value_get_int64(value));
158
 
 
159
 
                }
160
 
        }
 
161
        lu_ent_dump_attributes(ent->current, fp);
161
162
        fprintf(fp, "\n");
162
 
        for (i = 0; i < ent->pending->len; i++) {
163
 
                attribute = &g_array_index(ent->pending,
164
 
                                           struct lu_attribute,
165
 
                                           i);
166
 
                for (j = 0; j < attribute->values->n_values; j++) {
167
 
                        value = g_value_array_get_nth(attribute->values, j);
168
 
                        if (G_VALUE_HOLDS_STRING(value))
169
 
                                fprintf(fp, " %s = `%s'\n",
170
 
                                        g_quark_to_string(attribute->name),
171
 
                                        g_value_get_string(value));
172
 
                        else if (G_VALUE_HOLDS_LONG(value))
173
 
                                fprintf(fp, " %s = %ld\n",
174
 
                                        g_quark_to_string(attribute->name),
175
 
                                        g_value_get_long(value));
176
 
                        else if (G_VALUE_HOLDS_INT64(value))
177
 
                                fprintf(fp, " %s = %lld\n",
178
 
                                        g_quark_to_string(attribute->name),
179
 
                                        (long long)g_value_get_int64(value));
180
 
                }
181
 
        }
 
163
        lu_ent_dump_attributes(ent->pending, fp);
182
164
}
183
165
 
184
166
/* Add a module to the list of modules kept for this entity. */
185
167
void
186
168
lu_ent_add_module(struct lu_ent *ent, const char *source)
187
169
{
188
 
        GValue value;
189
170
        size_t i;
 
171
 
190
172
        g_return_if_fail(ent != NULL);
191
173
        g_return_if_fail(ent->magic == LU_ENT_MAGIC);
192
174
        g_return_if_fail(ent->modules != NULL);
193
 
        /* Initialize a value with the string. */
194
 
        memset(&value, 0, sizeof(value));
195
 
        g_value_init(&value, G_TYPE_STRING);
196
 
        g_value_set_string(&value, source);
197
 
        /* Now scan the array for the value. */
198
175
        for (i = 0; i < ent->modules->n_values; i++) {
199
176
                GValue *val;
200
177
 
202
179
                /* We only add strings, so there had better be only strings
203
180
                 * in this list, otherwise someone is messing with us. */
204
181
                g_assert(G_VALUE_HOLDS_STRING(val));
205
 
                if (strcmp(g_value_get_string(val),
206
 
                           g_value_get_string(&value)) == 0) {
 
182
                if (strcmp(g_value_get_string(val), source) == 0)
207
183
                        break;
208
 
                }
209
184
        }
210
185
        /* If we fell of the end of the array, then the new value is not
211
186
         * in there, so we should add it. */
212
187
        if (i >= ent->modules->n_values) {
 
188
                GValue value;
 
189
 
 
190
                /* Initialize a value with the string. */
 
191
                memset(&value, 0, sizeof(value));
 
192
                g_value_init(&value, G_TYPE_STRING);
 
193
                g_value_set_string(&value, source);
213
194
                g_value_array_append(ent->modules, &value);
 
195
                g_value_unset(&value);
214
196
        }
215
 
        g_value_unset(&value);
216
197
}
217
198
 
218
199
/* Clear the list of modules which affect this module, by freeing the array
443
424
        g_return_if_fail(strlen(attr) > 0);
444
425
        dest = lu_ent_get_int(list, attr);
445
426
        if (dest != NULL) {
446
 
                char *svalue;
447
 
 
448
 
                svalue = g_strdup_value_contents(value);
449
427
                for (i = 0; i < dest->n_values; i++) {
450
428
                        GValue *tvalue;
451
 
                        char *tmp;
452
429
 
453
430
                        tvalue = g_value_array_get_nth(dest, i);
454
 
                        tmp = g_strdup_value_contents(tvalue);
455
 
                        if (strcmp(tmp, svalue) == 0) {
456
 
                                g_free(tmp);
 
431
                        if (G_VALUE_TYPE(value) == G_VALUE_TYPE(tvalue)
 
432
                            && lu_values_equal(value, tvalue))
457
433
                                break;
458
 
                        }
459
 
                        g_free(tmp);
460
434
                }
461
 
                g_free(svalue);
462
435
                if (i < dest->n_values) {
463
436
                        g_value_array_remove(dest, i);
464
437
                        if (dest->n_values == 0)