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