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