]> git.tuebingen.mpg.de Git - paraslash.git/blob - attribute.c
Rename mood_switch(), mood_close(), playlist_{open/close}.
[paraslash.git] / attribute.c
1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file attribute.c Attribute handling functions. */
4
5 #include <regex.h>
6 #include <osl.h>
7 #include <lopsub.h>
8
9 #include "server_cmd.lsg.h"
10 #include "para.h"
11 #include "error.h"
12 #include "crypt.h"
13 #include "string.h"
14 #include "afh.h"
15 #include "afs.h"
16 #include "ipc.h"
17 #include "sideband.h"
18 #include "command.h"
19
20 static struct osl_table *attribute_table;
21 static int greatest_att_bitnum;
22
23 /** The columns of the attribute table. */
24 enum attribute_table_columns {
25         /** The bit number (0-63). */
26         ATTCOL_BITNUM,
27         /** The name of the attribute. */
28         ATTCOL_NAME,
29         /** Number of columns in this table. */
30         NUM_ATT_COLUMNS
31 };
32
33 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
34 {
35         const unsigned char *c1 = (const unsigned char*)obj1->data;
36         const unsigned char *c2 = (const unsigned char*)obj2->data;
37         if (*c1 > *c2)
38                 return 1;
39         if (*c1 < *c2)
40                 return -1;
41         return 0;
42 }
43
44 static struct osl_column_description att_cols[] = {
45         [ATTCOL_BITNUM] = {
46                 .storage_type = OSL_MAPPED_STORAGE,
47                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
48                 .name = "bitnum",
49                 .compare_function = char_compare,
50                 .data_size = 1
51         },
52         [ATTCOL_NAME] = {
53                 .storage_type = OSL_MAPPED_STORAGE,
54                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
55                 .name = "name",
56                 .compare_function = string_compare,
57         }
58 };
59
60 static struct osl_table_description attribute_table_desc = {
61         .name = "attributes",
62         .num_columns = NUM_ATT_COLUMNS,
63         .flags = 0,
64         .column_descriptions = att_cols
65 };
66
67 static void find_greatest_att_bitnum(void)
68 {
69         unsigned char c = 63;
70         do {
71                 struct osl_row *row;
72                 struct osl_object obj = {.data = &c, .size = 1};
73                 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
74                                 &row) >= 0) {
75                         greatest_att_bitnum = c;
76                         return;
77                 }
78         } while (c--);
79         PARA_INFO_LOG("no attributes\n");
80         greatest_att_bitnum = -E_NO_ATTRIBUTES;
81 }
82
83 /**
84  * Retrieve the identifier (number) of an attribute.
85  *
86  * \param att_name The name of the attribute.
87  * \param bitnum Result pointer.
88  *
89  * \return Positive on success, negative on errors.
90  */
91 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
92 {
93         struct osl_object obj = {.data = (char *)att_name,
94                 .size = strlen(att_name) + 1};
95         struct osl_row *row;
96         int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
97
98         if (ret < 0)
99                 return ret;
100         ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
101         if (ret < 0)
102                 return ret;
103         *bitnum = *(unsigned char *)obj.data;
104         return 1;
105 }
106
107 /* Data passed to the action function of lsatt */
108 static int print_attribute(struct osl_table *table, struct osl_row *row,
109                 const char *name, void *data)
110 {
111         struct afs_callback_arg *aca = data;
112         bool l_given = SERVER_CMD_OPT_GIVEN(LSATT, LONG, aca->lpr);
113         struct osl_object bitnum_obj;
114         int ret;
115
116         if (!l_given) {
117                 para_printf(&aca->pbout, "%s\n", name);
118                 return 1;
119         }
120         ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
121         if (ret < 0) {
122                 para_printf(&aca->pbout, "%s: %s\n", name, para_strerror(-ret));
123                 return ret;
124         }
125         para_printf(&aca->pbout, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
126                 name);
127         return 1;
128 }
129
130 static int com_lsatt_callback(struct afs_callback_arg *aca)
131 {
132         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LSATT);
133         bool i_given, r_given;
134         int ret;
135         struct pattern_match_data pmd = {
136                 .table = attribute_table,
137                 .loop_col_num = ATTCOL_NAME,
138                 .match_col_num = ATTCOL_NAME,
139                 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
140                 .data = aca,
141                 .action = print_attribute
142         };
143
144         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
145         assert(ret >= 0);
146         pmd.lpr = aca->lpr;
147         i_given = SERVER_CMD_OPT_GIVEN(LSATT, ID_SORT, aca->lpr);
148         r_given = SERVER_CMD_OPT_GIVEN(LSATT, REVERSE, aca->lpr);
149
150         if (i_given)
151                 pmd.loop_col_num = ATTCOL_BITNUM;
152         if (r_given)
153                 pmd.pm_flags |= PM_REVERSE_LOOP;
154         ret = for_each_matching_row(&pmd);
155         if (ret < 0)
156                 goto out;
157         if (pmd.num_matches == 0)
158                 ret = -E_NO_MATCH;
159 out:
160         lls_free_parse_result(aca->lpr, cmd);
161         return ret;
162 }
163
164 static int com_lsatt(struct command_context *cc, struct lls_parse_result *lpr)
165 {
166         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LSATT);
167         return send_lls_callback_request(com_lsatt_callback, cmd, lpr, cc);
168 }
169 EXPORT_SERVER_CMD_HANDLER(lsatt);
170
171 static int com_addatt_callback(struct afs_callback_arg *aca)
172 {
173         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADDATT);
174         int i, ret = 1;
175         size_t len;
176         unsigned num_inputs;
177
178         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
179         assert(ret >= 0);
180         num_inputs = lls_num_inputs(aca->lpr);
181         for (i = 0; i < num_inputs; i++) {
182                 const char *name = lls_input(i, aca->lpr);
183                 struct osl_object objs[NUM_ATT_COLUMNS];
184                 struct osl_row *row;
185                 unsigned char bitnum;
186
187                 len = strlen(name);
188                 if (len == 0 || name[len - 1] == '-' || name[len - 1] == '+') {
189                         para_printf(&aca->pbout,
190                                 "invalid attribute name: %s\n", name);
191                         continue;
192                 }
193                 ret = get_attribute_bitnum_by_name(name, &bitnum);
194                 if (ret >= 0) {
195                         para_printf(&aca->pbout,
196                                 "attribute \"%s\" already exists\n", name);
197                         continue;
198                 }
199                 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
200                         goto out;
201                 objs[ATTCOL_BITNUM].size = 1;
202                 /* find smallest unused attribute */
203                 for (bitnum = 0; bitnum < 64; bitnum++) {
204                         objs[ATTCOL_BITNUM].data = &bitnum;
205                         ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
206                                 &objs[ATTCOL_BITNUM], &row));
207                         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
208                                 break; /* this bitnum is unused, use it */
209                         if (ret < 0) /* error */
210                                 goto out;
211                         /* this bit is already in use, try next bit */
212                 }
213                 if (bitnum == 64) {
214                         ret = -E_ATT_TABLE_FULL;
215                         goto out;
216                 }
217                 objs[ATTCOL_NAME].data = (char *)name;
218                 objs[ATTCOL_NAME].size = len + 1;
219                 ret = osl(osl_add_row(attribute_table, objs));
220                 if (ret < 0)
221                         goto out;
222                 ret = afs_event(ATTRIBUTE_ADD, &aca->pbout, NULL);
223                 if (ret < 0)
224                         goto out;
225                 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
226         }
227 out:
228         if (ret < 0)
229                 para_printf(&aca->pbout, "error while adding %s\n",
230                         lls_input(i, aca->lpr));
231         lls_free_parse_result(aca->lpr, cmd);
232         return ret;
233 }
234
235 static int com_addatt(struct command_context *cc, struct lls_parse_result *lpr)
236 {
237         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADDATT);
238         char *errctx;
239         int ret = lls(lls_check_arg_count(lpr, 1, 64, &errctx));
240
241         if (ret < 0) {
242                 send_errctx(cc, errctx);
243                 return ret;
244         }
245         return send_lls_callback_request(com_addatt_callback, cmd, lpr, cc);
246 }
247 EXPORT_SERVER_CMD_HANDLER(addatt);
248
249 static int com_mvatt_callback(struct afs_callback_arg *aca)
250 {
251         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
252         const char *old, *new;
253         struct osl_object obj;
254         struct osl_row *row;
255         int ret;
256
257         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
258         assert(ret >= 0);
259         old = lls_input(0, aca->lpr);
260         new = lls_input(1, aca->lpr);
261         obj.data = (char *)old;
262         obj.size = strlen(old) + 1;
263         ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
264         if (ret < 0)
265                 goto out;
266         obj.data = (char *)new;
267         obj.size = strlen(new) + 1;
268         /* The update fails if the destination attribute exists. */
269         ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
270 out:
271         if (ret < 0)
272                 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
273         else
274                 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
275         lls_free_parse_result(aca->lpr, cmd);
276         return ret;
277 }
278
279 static int com_mvatt(struct command_context *cc, struct lls_parse_result *lpr)
280 {
281         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
282         char *errctx;
283         int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
284         if (ret < 0) {
285                 send_errctx(cc, errctx);
286                 return ret;
287         }
288         return send_lls_callback_request(com_mvatt_callback, cmd, lpr, cc);
289 }
290 EXPORT_SERVER_CMD_HANDLER(mvatt);
291
292 static int remove_attribute(struct osl_table *table, struct osl_row *row,
293                 const char *name, void *data)
294 {
295         struct afs_callback_arg *aca = data;
296         int ret;
297         struct rmatt_event_data red = {.name = name};
298
299         ret = get_attribute_bitnum_by_name(name, &red.bitnum);
300         if (ret < 0) {
301                 para_printf(&aca->pbout, "cannot remove %s\n", name);
302                 return ret;
303         }
304         para_printf(&aca->pbout, "removing attribute %s\n", name);
305         ret = osl(osl_del_row(table, row));
306         if (ret < 0) {
307                 para_printf(&aca->pbout, "cannot remove %s\n", name);
308                 return ret;
309         }
310         return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
311 }
312
313 static int com_rmatt_callback(struct afs_callback_arg *aca)
314 {
315         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
316         int ret;
317         struct pattern_match_data pmd = {
318                 .table = attribute_table,
319                 .loop_col_num = ATTCOL_BITNUM,
320                 .match_col_num = ATTCOL_NAME,
321                 .data = aca,
322                 .action = remove_attribute
323         };
324         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
325         assert(ret >= 0);
326         pmd.lpr = aca->lpr;
327         ret = for_each_matching_row(&pmd);
328         if (ret < 0)
329                 goto out;
330         if (pmd.num_matches == 0)
331                 ret = -E_NO_MATCH;
332 out:
333         lls_free_parse_result(aca->lpr, cmd);
334         return ret;
335 }
336
337 static int com_rmatt(struct command_context *cc, struct lls_parse_result *lpr)
338 {
339         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
340         char *errctx;
341         int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
342
343         if (ret < 0) {
344                 send_errctx(cc, errctx);
345                 return ret;
346         }
347         return send_lls_callback_request(com_rmatt_callback, cmd, lpr, cc);
348 }
349 EXPORT_SERVER_CMD_HANDLER(rmatt);
350
351 /**
352  * Return a binary representation of the given attribute value.
353  *
354  * \param atts Pointer to the attribute value.
355  * \param buf Result.
356  *
357  * This function prints a string of at most 64 characters plus the terminating
358  * \p NULL character into \a buf which must be provided by the caller and at
359  * least 65 bytes long. The "x" character is used for set attributes and "-" is
360  * used for unset attributes.
361  *
362  * In practice, not all 64 attributes are defined. In this case, the function
363  * only prints \a N + 1 characters where \a N is the greatest id of a defined
364  * attribute.
365  */
366 void get_attribute_bitmap(const uint64_t *atts, char *buf)
367 {
368         int i;
369         const uint64_t one = 1;
370
371         for (i = 0; i <= greatest_att_bitnum; i++)
372                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
373         buf[i] = '\0';
374 }
375
376 /**
377  * Get a string containing the set attributes in text form.
378  *
379  * \param atts The attribute bitmap.
380  * \param delim The delimiter to separate matching attribute names.
381  * \param text Result pointer.
382  *
383  * \return Positive on success, negative on errors. If no attributes have
384  * been defined, \a *text is NULL.
385  */
386 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
387 {
388         int i, ret;
389         const uint64_t one = 1;
390
391         *text = NULL;
392         if (greatest_att_bitnum < 0) { /* no attributes available */
393                 *text = para_strdup("(no attributes available)");
394                 return 1;
395         }
396         for (i = 0; i <= greatest_att_bitnum; i++) {
397                 unsigned char bn = i;
398                 struct osl_object obj = {.data = &bn, .size = 1};
399                 struct osl_row *row;
400
401                 if (!(*atts & (one << i)))
402                         continue;
403                 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
404                 if (ret < 0)
405                         goto err;
406                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
407                 if (ret < 0)
408                         goto err;
409                 if (*text) {
410                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
411                         free(*text);
412                         *text = tmp;
413                 } else
414                         *text = para_strdup(obj.data);
415         }
416         if (!*text) /* no attributes set */
417                 *text = para_strdup("");
418         return 1;
419 err:
420         free(*text);
421         return ret;
422 }
423
424 static int att_logical_or(struct osl_row *row, void *data)
425 {
426         uint64_t *att_mask = data, one = 1;
427         struct osl_object bitnum_obj;
428         int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
429
430         if (ret < 0)
431                 return ret;
432         *att_mask |= one << *(unsigned char *)bitnum_obj.data;
433         return 0;
434 }
435
436 /**
437  * Compute the attribute bit mask and check each afs info bitmap.
438  *
439  * \param aca The query field of \a aca is ignored.
440  *
441  * This iterates over all attributes in the attribute table and computes the
442  * logical or of 1 << b where b is the bit number of the attribute. The
443  * resulting bit mask is passed to aft_check_attributes() which performs the
444  * actual check.
445  *
446  * \return Standard.
447  *
448  * \sa \ref aft_check_attributes().
449  */
450 int attribute_check_callback(struct afs_callback_arg *aca)
451 {
452         int ret;
453         uint64_t att_mask = 0; /* bits corresponding to a attributes */
454
455         ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
456                 att_logical_or);
457         if (ret < 0) {
458                 PARA_ERROR_LOG("attribute table loop failed: %s\n",
459                         para_strerror(-ret));
460                 return ret;
461         }
462         return aft_check_attributes(att_mask, &aca->pbout);
463 }
464
465 static void attribute_close(void)
466 {
467         osl_close_table(attribute_table, OSL_MARK_CLEAN);
468         attribute_table = NULL;
469 }
470
471 static int attribute_open(const char *dir)
472 {
473         int ret;
474
475         attribute_table_desc.dir = dir;
476         ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
477         greatest_att_bitnum = -1; /* no atts available */
478         if (ret >= 0) {
479                 find_greatest_att_bitnum();
480                 return ret;
481         }
482         attribute_table = NULL;
483         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
484                 return 1;
485         return ret;
486 }
487
488 static int attribute_create(const char *dir)
489 {
490         attribute_table_desc.dir = dir;
491         return osl(osl_create_table(&attribute_table_desc));
492 }
493
494 /** The attribute table stores name/bitnum pairs. */
495 const struct afs_table_operations attr_ops = { /* no event handler */
496         .open = attribute_open,
497         .close = attribute_close,
498         .create = attribute_create,
499 };