Merge branch 'maint'
[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 struct addatt_event_data {
172         const char *name;
173         unsigned char bitnum;
174 };
175
176 static int com_addatt_callback(struct afs_callback_arg *aca)
177 {
178         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADDATT);
179         int i, ret = 1;
180         size_t len;
181         unsigned num_inputs;
182
183         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
184         assert(ret >= 0);
185         num_inputs = lls_num_inputs(aca->lpr);
186         for (i = 0; i < num_inputs; i++) {
187                 const char *name = lls_input(i, aca->lpr);
188                 struct osl_object objs[NUM_ATT_COLUMNS];
189                 struct osl_row *row;
190                 unsigned char bitnum;
191                 struct addatt_event_data aed;
192
193                 len = strlen(name);
194                 if (len == 0 || name[len - 1] == '-' || name[len - 1] == '+') {
195                         para_printf(&aca->pbout,
196                                 "invalid attribute name: %s\n", name);
197                         continue;
198                 }
199                 ret = get_attribute_bitnum_by_name(name, &bitnum);
200                 if (ret >= 0) {
201                         para_printf(&aca->pbout,
202                                 "attribute \"%s\" already exists\n", name);
203                         continue;
204                 }
205                 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
206                         goto out;
207                 objs[ATTCOL_BITNUM].size = 1;
208                 /* find smallest unused attribute */
209                 for (bitnum = 0; bitnum < 64; bitnum++) {
210                         objs[ATTCOL_BITNUM].data = &bitnum;
211                         ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
212                                 &objs[ATTCOL_BITNUM], &row));
213                         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
214                                 break; /* this bitnum is unused, use it */
215                         if (ret < 0) /* error */
216                                 goto out;
217                         /* this bit is already in use, try next bit */
218                 }
219                 if (bitnum == 64) {
220                         ret = -E_ATT_TABLE_FULL;
221                         goto out;
222                 }
223                 objs[ATTCOL_NAME].data = (char *)name;
224                 objs[ATTCOL_NAME].size = len + 1;
225                 ret = osl(osl_add_row(attribute_table, objs));
226                 if (ret < 0)
227                         goto out;
228                 aed.name = name;
229                 aed.bitnum = bitnum;
230                 ret = afs_event(ATTRIBUTE_ADD, &aca->pbout, &aed);
231                 if (ret < 0)
232                         goto out;
233                 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
234         }
235 out:
236         if (ret < 0)
237                 para_printf(&aca->pbout, "error while adding %s\n",
238                         lls_input(i, aca->lpr));
239         lls_free_parse_result(aca->lpr, cmd);
240         return ret;
241 }
242
243 static int com_addatt(struct command_context *cc, struct lls_parse_result *lpr)
244 {
245         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADDATT);
246         char *errctx;
247         int ret = lls(lls_check_arg_count(lpr, 1, 64, &errctx));
248
249         if (ret < 0) {
250                 send_errctx(cc, errctx);
251                 return ret;
252         }
253         return send_lls_callback_request(com_addatt_callback, cmd, lpr, cc);
254 }
255 EXPORT_SERVER_CMD_HANDLER(addatt);
256
257 static int com_mvatt_callback(struct afs_callback_arg *aca)
258 {
259         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
260         const char *old, *new;
261         struct osl_object obj;
262         struct osl_row *row;
263         int ret;
264
265         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
266         assert(ret >= 0);
267         old = lls_input(0, aca->lpr);
268         new = lls_input(1, aca->lpr);
269         obj.data = (char *)old;
270         obj.size = strlen(old) + 1;
271         ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
272         if (ret < 0)
273                 goto out;
274         obj.data = (char *)new;
275         obj.size = strlen(new) + 1;
276         /* The update fails if the destination attribute exists. */
277         ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
278 out:
279         if (ret < 0)
280                 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
281         else
282                 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
283         lls_free_parse_result(aca->lpr, cmd);
284         return ret;
285 }
286
287 static int com_mvatt(struct command_context *cc, struct lls_parse_result *lpr)
288 {
289         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
290         char *errctx;
291         int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
292         if (ret < 0) {
293                 send_errctx(cc, errctx);
294                 return ret;
295         }
296         return send_lls_callback_request(com_mvatt_callback, cmd, lpr, cc);
297 }
298 EXPORT_SERVER_CMD_HANDLER(mvatt);
299
300 static int remove_attribute(struct osl_table *table, struct osl_row *row,
301                 const char *name, void *data)
302 {
303         struct afs_callback_arg *aca = data;
304         int ret;
305         struct rmatt_event_data red = {.name = name};
306
307         ret = get_attribute_bitnum_by_name(name, &red.bitnum);
308         if (ret < 0) {
309                 para_printf(&aca->pbout, "cannot remove %s\n", name);
310                 return ret;
311         }
312         para_printf(&aca->pbout, "removing attribute %s\n", name);
313         ret = osl(osl_del_row(table, row));
314         if (ret < 0) {
315                 para_printf(&aca->pbout, "cannot remove %s\n", name);
316                 return ret;
317         }
318         return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
319 }
320
321 static int com_rmatt_callback(struct afs_callback_arg *aca)
322 {
323         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
324         int ret;
325         struct pattern_match_data pmd = {
326                 .table = attribute_table,
327                 .loop_col_num = ATTCOL_BITNUM,
328                 .match_col_num = ATTCOL_NAME,
329                 .data = aca,
330                 .action = remove_attribute
331         };
332         ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
333         assert(ret >= 0);
334         pmd.lpr = aca->lpr;
335         ret = for_each_matching_row(&pmd);
336         if (ret < 0)
337                 goto out;
338         if (pmd.num_matches == 0)
339                 ret = -E_NO_MATCH;
340 out:
341         lls_free_parse_result(aca->lpr, cmd);
342         return ret;
343 }
344
345 static int com_rmatt(struct command_context *cc, struct lls_parse_result *lpr)
346 {
347         const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
348         char *errctx;
349         int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
350
351         if (ret < 0) {
352                 send_errctx(cc, errctx);
353                 return ret;
354         }
355         return send_lls_callback_request(com_rmatt_callback, cmd, lpr, cc);
356 }
357 EXPORT_SERVER_CMD_HANDLER(rmatt);
358
359 /**
360  * Return a binary representation of the given attribute value.
361  *
362  * \param atts Pointer to the attribute value.
363  * \param buf Result.
364  *
365  * This function prints a string of at most 64 characters plus the terminating
366  * \p NULL character into \a buf which must be provided by the caller and at
367  * least 65 bytes long. The "x" character is used for set attributes and "-" is
368  * used for unset attributes.
369  *
370  * In practice, not all 64 attributes are defined. In this case, the function
371  * only prints \a N + 1 characters where \a N is the greatest id of a defined
372  * attribute.
373  */
374 void get_attribute_bitmap(const uint64_t *atts, char *buf)
375 {
376         int i;
377         const uint64_t one = 1;
378
379         for (i = 0; i <= greatest_att_bitnum; i++)
380                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
381         buf[i] = '\0';
382 }
383
384 /**
385  * Get a string containing the set attributes in text form.
386  *
387  * \param atts The attribute bitmap.
388  * \param delim The delimiter to separate matching attribute names.
389  * \param text Result pointer.
390  *
391  * \return Positive on success, negative on errors. If no attributes have
392  * been defined, \a *text is NULL.
393  */
394 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
395 {
396         int i, ret;
397         const uint64_t one = 1;
398
399         *text = NULL;
400         if (greatest_att_bitnum < 0) { /* no attributes available */
401                 *text = para_strdup("(no attributes available)");
402                 return 1;
403         }
404         for (i = 0; i <= greatest_att_bitnum; i++) {
405                 unsigned char bn = i;
406                 struct osl_object obj = {.data = &bn, .size = 1};
407                 struct osl_row *row;
408
409                 if (!(*atts & (one << i)))
410                         continue;
411                 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
412                 if (ret < 0)
413                         goto err;
414                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
415                 if (ret < 0)
416                         goto err;
417                 if (*text) {
418                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
419                         free(*text);
420                         *text = tmp;
421                 } else
422                         *text = para_strdup(obj.data);
423         }
424         if (!*text) /* no attributes set */
425                 *text = para_strdup("");
426         return 1;
427 err:
428         free(*text);
429         return ret;
430 }
431
432 static int att_logical_or(struct osl_row *row, void *data)
433 {
434         uint64_t *att_mask = data, one = 1;
435         struct osl_object bitnum_obj;
436         int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
437
438         if (ret < 0)
439                 return ret;
440         *att_mask |= one << *(unsigned char *)bitnum_obj.data;
441         return 0;
442 }
443
444 /**
445  * Compute the attribute bit mask and check each afs info bitmap.
446  *
447  * \param aca The query field of \a aca is ignored.
448  *
449  * This iterates over all attributes in the attribute table and computes the
450  * logical or of 1 << b where b is the bit number of the attribute. The
451  * resulting bit mask is passed to aft_check_attributes() which performs the
452  * actual check.
453  *
454  * \return Standard.
455  *
456  * \sa \ref aft_check_attributes().
457  */
458 int attribute_check_callback(struct afs_callback_arg *aca)
459 {
460         int ret;
461         uint64_t att_mask = 0; /* bits corresponding to a attributes */
462
463         ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
464                 att_logical_or);
465         if (ret < 0) {
466                 PARA_ERROR_LOG("attribute table loop failed: %s\n",
467                         para_strerror(-ret));
468                 return ret;
469         }
470         return aft_check_attributes(att_mask, &aca->pbout);
471 }
472
473 /**
474  * Close the attribute table.
475  *
476  * \sa \ref osl_close_table().
477  */
478 static void attribute_close(void)
479 {
480         osl_close_table(attribute_table, OSL_MARK_CLEAN);
481         attribute_table = NULL;
482 }
483
484 /**
485  * Open the attribute table.
486  *
487  * \param dir The database directory.
488  *
489  * \return Positive on success, negative on errors.
490  *
491  * \sa \ref osl_open_table().
492  */
493 static int attribute_open(const char *dir)
494 {
495         int ret;
496
497         attribute_table_desc.dir = dir;
498         ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
499         greatest_att_bitnum = -1; /* no atts available */
500         if (ret >= 0) {
501                 find_greatest_att_bitnum();
502                 return ret;
503         }
504         attribute_table = NULL;
505         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
506                 return 1;
507         return ret;
508 }
509
510 static int attribute_create(const char *dir)
511 {
512         attribute_table_desc.dir = dir;
513         return osl(osl_create_table(&attribute_table_desc));
514 }
515
516 /**
517  * Initialize the attribute table structure.
518  *
519  * \param t The table structure to initialize.
520  */
521 void attribute_init(struct afs_table *t)
522 {
523         t->open = attribute_open;
524         t->close = attribute_close;
525         t->create = attribute_create;
526 }