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