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