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