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