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