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