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