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