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