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