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