Fix mood reload.
[paraslash.git] / attribute.c
1 /*
2  * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
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                 return para_printf(&laad->pb, "%s\n", name);
140         ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
141         if (ret < 0) {
142                 para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
143                 return ret;
144         }
145         return para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
146                 name);
147 }
148
149 static void com_lsatt_callback(int fd, const struct osl_object *query)
150 {
151         struct lsatt_action_data laad = {
152                 .flags = *(unsigned *) query->data,
153                 .pb = {
154                         .max_size = shm_get_shmmax(),
155                         .private_data = &(struct afs_max_size_handler_data) {
156                                 .fd = fd,
157                                 .band = SBD_OUTPUT
158                         },
159                         .max_size_handler = afs_max_size_handler
160                 }
161
162         };
163         struct pattern_match_data pmd = {
164                 .table = attribute_table,
165                 .loop_col_num = ATTCOL_BITNUM,
166                 .match_col_num = ATTCOL_NAME,
167                 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
168                         .size = query->size - sizeof(laad.flags)},
169                 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
170                 .data = &laad,
171                 .action = print_attribute
172         };
173         if (laad.flags & LSATT_FLAG_SORT_BY_ID)
174                 pmd.loop_col_num = ATTCOL_NAME;
175         if (laad.flags & LSATT_FLAG_REVERSE)
176                 pmd.pm_flags |= PM_REVERSE_LOOP;
177         for_each_matching_row(&pmd);
178         if (laad.pb.offset)
179                 pass_buffer_as_shm(fd, SBD_OUTPUT, laad.pb.buf, laad.pb.offset);
180         free(laad.pb.buf);
181 }
182
183 int com_lsatt(struct command_context *cc)
184 {
185         unsigned flags = 0;
186         struct osl_object options = {.data = &flags, .size = sizeof(flags)};
187         int ret, i;
188
189         for (i = 1; i < cc->argc; i++) {
190                 const char *arg = cc->argv[i];
191                 if (arg[0] != '-')
192                         break;
193                 if (!strcmp(arg, "--")) {
194                         i++;
195                         break;
196                 }
197                 if (!strcmp(arg, "-i")) {
198                         flags |= LSATT_FLAG_SORT_BY_ID;
199                         continue;
200                 }
201                 if (!strcmp(arg, "-l")) {
202                         flags |= LSATT_FLAG_LONG;
203                         continue;
204                 }
205                 if (!strcmp(arg, "-r")) {
206                         flags |= LSATT_FLAG_REVERSE;
207                         continue;
208                 }
209         }
210         ret = send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i,
211                 com_lsatt_callback, afs_cb_result_handler, cc);
212         if (ret < 0)
213                 send_strerror(cc, -ret);
214         else if (ret == 0 && cc->argc > 1)
215                 ret = cc->use_sideband?
216                         send_sb_va(&cc->scc, SBD_ERROR_LOG, "no matches\n")
217                 :
218                         sc_send_va_buffer(&cc->scc, "no matches\n");
219         return ret;
220 }
221
222 static void com_setatt_callback(__a_unused int fd, const struct osl_object *query)
223 {
224         char *p;
225         uint64_t add_mask = 0, del_mask = 0;
226         int ret;
227         size_t len;
228         struct osl_object obj;
229         struct osl_row *row;
230
231         for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
232                 char c;
233
234                 len = strlen(p);
235                 ret = -E_ATTR_SYNTAX;
236                 if (!*p)
237                         goto out;
238                 c = p[len - 1];
239                 if (c != '+' && c != '-')
240                         break;
241                 p[len - 1] = '\0';
242                 obj.data = p;
243                 obj.size = len + 1;
244                 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
245                 if (ret < 0)
246                         goto out;
247                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM,
248                         &obj));
249                 if (ret < 0)
250                         goto out;
251                 if (c == '+')
252                         add_mask |= (1UL << *(unsigned char *)obj.data);
253                 else
254                         del_mask |= (1UL << *(unsigned char *)obj.data);
255         }
256         ret = -E_ATTR_SYNTAX;
257         if (!add_mask && !del_mask)
258                 goto out;
259         PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask,
260                 (long long unsigned)del_mask);
261         for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
262                 struct afs_info old_afsi, new_afsi;
263                 struct afsi_change_event_data aced = {.old_afsi = &old_afsi};
264
265                 len = strlen(p);
266                 ret = aft_get_row_of_path(p, &aced.aft_row);
267                 if (ret < 0)
268                         goto out;
269                 ret = get_afsi_object_of_row(aced.aft_row, &obj);
270                 if (ret < 0)
271                         goto out;
272                 ret = load_afsi(&old_afsi, &obj);
273                 if (ret < 0)
274                         goto out;
275                 new_afsi = old_afsi;
276                 new_afsi.attributes |= add_mask;
277                 new_afsi.attributes &= ~del_mask;
278                 save_afsi(&new_afsi, &obj); /* in-place update */
279                 afs_event(AFSI_CHANGE, NULL, &aced);
280         }
281 out:
282         if (ret < 0)
283                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
284 }
285
286 int com_setatt(struct command_context *cc)
287 {
288         if (cc->argc < 3)
289                 return -E_ATTR_SYNTAX;
290         return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
291                 com_setatt_callback, NULL, NULL);
292 }
293
294 struct addatt_event_data {
295         const char *name;
296         unsigned char bitnum;
297 };
298
299
300 static void com_addatt_callback(int fd, const struct osl_object *query)
301 {
302         char *p;
303         int ret = 1, ret2 = 0;
304         struct para_buffer pb = {
305                 .max_size = shm_get_shmmax(),
306                 .private_data = &(struct afs_max_size_handler_data) {
307                         .fd = fd,
308                         .band = SBD_OUTPUT
309                 },
310                 .max_size_handler = afs_max_size_handler
311         };
312         size_t len;
313
314         for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
315                 struct osl_object objs[NUM_ATT_COLUMNS];
316                 struct osl_row *row;
317                 unsigned char bitnum;
318                 struct addatt_event_data aed;
319
320                 len = strlen(p);
321                 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
322                         ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
323                         if (ret2 < 0)
324                                 goto out;
325                         continue;
326                 }
327                 ret = get_attribute_bitnum_by_name(p, &bitnum);
328                 if (ret >= 0) {
329                         ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
330                         if (ret2 < 0)
331                                 goto out;
332                         continue;
333                 }
334                 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
335                         goto out;
336                 objs[ATTCOL_BITNUM].size = 1;
337                 /* find smallest unused attribute */
338                 for (bitnum = 0; bitnum < 64; bitnum++) {
339                         objs[ATTCOL_BITNUM].data = &bitnum;
340                         ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
341                                 &objs[ATTCOL_BITNUM], &row));
342                         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
343                                 break; /* this bitnum is unused, use it */
344                         if (ret < 0) /* error */
345                                 goto out;
346                         /* this bit is already in use, try next bit */
347                 }
348                 if (bitnum == 64) {
349                         ret = -E_ATT_TABLE_FULL;
350                         goto out;
351                 }
352                 objs[ATTCOL_NAME].data = p;
353                 objs[ATTCOL_NAME].size = len + 1;
354                 ret = osl(osl_add_row(attribute_table, objs));
355                 if (ret < 0)
356                         goto out;
357                 aed.name = p;
358                 aed.bitnum = bitnum;
359                 afs_event(ATTRIBUTE_ADD, &pb, &aed);
360                 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
361         }
362 out:
363         if (ret < 0 && ret2 >= 0)
364                 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
365         if (pb.offset)
366                 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
367         free(pb.buf);
368 }
369
370 int com_addatt(struct command_context *cc)
371 {
372         int ret;
373
374         if (cc->argc < 2)
375                 return -E_ATTR_SYNTAX;
376         ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
377                 com_addatt_callback, afs_cb_result_handler, cc);
378         if (ret < 0)
379                 send_strerror(cc, -ret);
380         return ret;
381 }
382
383 static void com_mvatt_callback(int fd, const struct osl_object *query)
384 {
385         char *old = query->data;
386         size_t size = strlen(old) + 1;
387         char *new = old + size;
388         struct osl_object obj = {.data = old, .size = size};
389         struct osl_row *row;
390         struct para_buffer pb = {
391                 .max_size = shm_get_shmmax(),
392                 .private_data = &(struct afs_max_size_handler_data) {
393                         .fd = fd,
394                         .band = SBD_OUTPUT
395                 },
396                 .max_size_handler = afs_max_size_handler,
397         };
398         int ret;
399
400         ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
401         if (ret < 0)
402                 goto out;
403         obj.data = new;
404         obj.size = strlen(new) + 1;
405         ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
406 out:
407         if (ret < 0)
408                 para_printf(&pb, "%s\n", para_strerror(-ret));
409         else
410                 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
411         if (pb.offset)
412                 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
413         free(pb.buf);
414 }
415
416 int com_mvatt(struct command_context *cc)
417 {
418         int ret;
419
420         if (cc->argc != 3)
421                 return -E_ATTR_SYNTAX;
422         ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
423                 com_mvatt_callback, afs_cb_result_handler, cc);
424         if (ret < 0)
425                 send_strerror(cc, -ret);
426         return ret;
427 }
428
429 /** Data passed to the action handler of com_rmatt(). */
430 struct remove_attribute_action_data {
431         /** Message buffer. */
432         struct para_buffer pb;
433         /** Numver of attributes removed. */
434         int num_removed;
435         /** Bitwise "or" of the removed attributes. */
436         uint64_t mask_of_removed_atts;
437 };
438
439 static int remove_attribute(struct osl_table *table, struct osl_row *row,
440                 const char *name, void *data)
441 {
442         struct remove_attribute_action_data *raad = data;
443         int ret;
444         struct rmatt_event_data red = {.name = name};
445
446         ret = get_attribute_bitnum_by_name(name, &red.bitnum);
447         if (ret < 0)
448                 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
449         ret = osl(osl_del_row(table, row));
450         if (ret < 0)
451                 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
452         ret = para_printf(&raad->pb, "removed attribute %s\n", name);
453         raad->num_removed++;
454         raad->mask_of_removed_atts |= (1 << red.bitnum);
455         afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
456         return ret;
457 }
458
459 static void com_rmatt_callback(int fd, const struct osl_object *query)
460 {
461         struct remove_attribute_action_data raad = {
462                 .num_removed = 0,
463                 .pb = {
464                         .max_size = shm_get_shmmax(),
465                         .private_data = &(struct afs_max_size_handler_data) {
466                                 .fd = fd,
467                                 .band = SBD_OUTPUT
468                         },
469                         .max_size_handler = afs_max_size_handler,
470                 }
471         };
472         int ret, ret2 = 0;
473         struct pattern_match_data pmd = {
474                 .table = attribute_table,
475                 .patterns = *query,
476                 .loop_col_num = ATTCOL_BITNUM,
477                 .match_col_num = ATTCOL_NAME,
478                 .data = &raad,
479                 .action = remove_attribute
480         };
481         ret = for_each_matching_row(&pmd);
482         if (ret < 0)
483                 ret2 = para_printf(&raad.pb, "%s\n", para_strerror(-ret));
484         else if (!raad.num_removed)
485                 ret2 = para_printf(&raad.pb, "no match -- nothing removed\n");
486         if (ret2 >= 0 && raad.pb.offset)
487                 pass_buffer_as_shm(fd, SBD_OUTPUT, raad.pb.buf, raad.pb.offset);
488         free(raad.pb.buf);
489 }
490
491 int com_rmatt(struct command_context *cc)
492 {
493         int ret;
494
495         if (cc->argc < 2)
496                 return -E_ATTR_SYNTAX;
497         ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
498                 com_rmatt_callback, afs_cb_result_handler, cc);
499         if (ret < 0)
500                 send_strerror(cc, -ret);
501         return ret;
502 }
503
504 /**
505  * Return a binary representation of the given attribute value.
506  *
507  * \param atts Pointer to the attribute value.
508  * \param buf Result.
509  *
510  * This function prints a string of at most 64 characters plus the terminating
511  * \p NULL character into \a buf which must be provided by the caller and at
512  * least 65 bytes long. The "x" character is used for set attributes and "-" is
513  * used for unset attributes.
514  *
515  * In practice, not all 64 attributes are defined. In this case, the function
516  * only prints \a N + 1 charaters where \a N is the greatest id of a defined
517  * attribute.
518  */
519 void get_attribute_bitmap(const uint64_t *atts, char *buf)
520 {
521         int i;
522         const uint64_t one = 1;
523
524         for (i = 0; i <= greatest_att_bitnum; i++)
525                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
526         buf[i] = '\0';
527 }
528
529 /**
530  * Get a string containing the set attributes in text form.
531  *
532  * \param atts The attribute bitmap.
533  * \param delim The delimiter to separate matching attribute names.
534  * \param text Result pointer.
535  *
536  * \return Positive on success, negative on errors. If no attributes have
537  * been defined, \a *text is NULL.
538  */
539 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
540 {
541         int i, ret;
542         const uint64_t one = 1;
543
544         *text = NULL;
545         if (greatest_att_bitnum < 0) { /* no attributes available */
546                 *text = para_strdup("(no attributes available)");
547                 return 1;
548         }
549         for (i = 0; i <= greatest_att_bitnum; i++) {
550                 unsigned char bn = i;
551                 struct osl_object obj = {.data = &bn, .size = 1};
552                 struct osl_row *row;
553
554                 if (!(*atts & (one << i)))
555                         continue;
556                 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
557                 if (ret < 0)
558                         goto err;
559                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
560                 if (ret < 0)
561                         goto err;
562                 if (*text) {
563                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
564                         free(*text);
565                         *text = tmp;
566                 } else
567                         *text = para_strdup(obj.data);
568         }
569         if (!*text) /* no attributes set */
570                 *text = para_strdup("");
571         return 1;
572 err:
573         free(*text);
574         return ret;
575 }
576
577 /**
578  * Close the attribute table.
579  *
580  * \sa osl_close_table().
581  */
582 static void attribute_close(void)
583 {
584         osl_close_table(attribute_table, OSL_MARK_CLEAN);
585         attribute_table = NULL;
586 }
587
588 /**
589  * Open the attribute table.
590  *
591  * \param dir The database directory.
592  *
593  * \return Positive on success, negative on errors.
594  *
595  * \sa osl_open_table().
596  */
597 static int attribute_open(const char *dir)
598 {
599         int ret;
600
601         attribute_table_desc.dir = dir;
602         ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
603         greatest_att_bitnum = -1; /* no atts available */
604         if (ret >= 0) {
605                 find_greatest_att_bitnum();
606                 return ret;
607         }
608         attribute_table = NULL;
609         if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
610                 return 1;
611         return ret;
612 }
613
614 static int attribute_create(const char *dir)
615 {
616         attribute_table_desc.dir = dir;
617         return osl(osl_create_table(&attribute_table_desc));
618 }
619
620 /**
621  * Initialize the attribute table structure.
622  *
623  * \param t The table structure to initialize.
624  */
625 void attribute_init(struct afs_table *t)
626 {
627         t->open = attribute_open;
628         t->close = attribute_close;
629         t->create = attribute_create;
630 }