2 * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
20 static struct osl_table *attribute_table;
21 static int greatest_att_bitnum;
23 /** The columns of the attribute table. */
24 enum attribute_table_columns {
25 /** The bit number (0-63). */
27 /** The name of the attribute. */
29 /** Number of columns in this table. */
33 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
35 const unsigned char *c1 = (const unsigned char*)obj1->data;
36 const unsigned char *c2 = (const unsigned char*)obj2->data;
44 static struct osl_column_description att_cols[] = {
46 .storage_type = OSL_MAPPED_STORAGE,
47 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
49 .compare_function = char_compare,
53 .storage_type = OSL_MAPPED_STORAGE,
54 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
56 .compare_function = string_compare,
60 static struct osl_table_description attribute_table_desc = {
62 .num_columns = NUM_ATT_COLUMNS,
64 .column_descriptions = att_cols
67 static void find_greatest_att_bitnum(void)
72 struct osl_object obj = {.data = &c, .size = 1};
73 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
75 greatest_att_bitnum = c;
79 PARA_INFO_LOG("no attributes\n");
80 greatest_att_bitnum = -E_NO_ATTRIBUTES;
84 * Retrieve the identifier (number) of an attribute.
86 * \param att_name The name of the attribute.
87 * \param bitnum Result pointer.
89 * \return Positive on success, negative on errors.
91 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
93 struct osl_object obj = {.data = (char *)att_name,
94 .size = strlen(att_name) + 1};
96 int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
100 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
103 *bitnum = *(unsigned char *)obj.data;
108 * Flags used by the lsatt command.
110 * \param \sa com_lsatt().
113 /** Whether "-a" was given for the lsatt command. */
114 LSATT_FLAG_SORT_BY_ID = 1,
115 /** Whether "-l" was given for the lsatt command. */
117 /** Reverse sort order. */
118 LSATT_FLAG_REVERSE = 4
121 /** Data passed to the action function of lsatt */
122 struct lsatt_action_data {
123 /** The result buffer. */
124 struct para_buffer pb;
125 /** The given flags for the lsatt command. */
129 static int print_attribute(struct osl_table *table, struct osl_row *row,
130 const char *name, void *data)
132 struct lsatt_action_data *laad = data;
133 struct osl_object bitnum_obj;
136 if (!(laad->flags & LSATT_FLAG_LONG))
137 return para_printf(&laad->pb, "%s\n", name);
138 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
140 para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
143 return para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
147 static void com_lsatt_callback(int fd, const struct osl_object *query)
149 struct lsatt_action_data laad = {
150 .flags = *(unsigned *) query->data,
154 .max_size_handler = pass_buffer_as_shm
158 struct pattern_match_data pmd = {
159 .table = attribute_table,
160 .loop_col_num = ATTCOL_BITNUM,
161 .match_col_num = ATTCOL_NAME,
162 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
163 .size = query->size - sizeof(laad.flags)},
164 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
166 .action = print_attribute
168 if (laad.flags & LSATT_FLAG_SORT_BY_ID)
169 pmd.loop_col_num = ATTCOL_NAME;
170 if (laad.flags & LSATT_FLAG_REVERSE)
171 pmd.pm_flags |= PM_REVERSE_LOOP;
172 for_each_matching_row(&pmd);
174 pass_buffer_as_shm(laad.pb.buf, laad.pb.offset, &fd);
178 int com_lsatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
181 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
184 for (i = 1; i < argc; i++) {
185 const char *arg = argv[i];
188 if (!strcmp(arg, "--")) {
192 if (!strcmp(arg, "-i")) {
193 flags |= LSATT_FLAG_SORT_BY_ID;
196 if (!strcmp(arg, "-l")) {
197 flags |= LSATT_FLAG_LONG;
200 if (!strcmp(arg, "-r")) {
201 flags |= LSATT_FLAG_REVERSE;
205 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
206 com_lsatt_callback, sc_send_result, scc);
209 ret = sc_send_va_buffer(scc, "no matches\n");
211 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
215 static void com_setatt_callback(__a_unused int fd, const struct osl_object *query)
218 uint64_t add_mask = 0, del_mask = 0;
221 struct osl_object obj;
224 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
228 ret = -E_ATTR_SYNTAX;
232 if (c != '+' && c != '-')
237 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
240 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM,
245 add_mask |= (1UL << *(unsigned char *)obj.data);
247 del_mask |= (1UL << *(unsigned char *)obj.data);
249 ret = -E_ATTR_SYNTAX;
250 if (!add_mask && !del_mask)
252 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask,
253 (long long unsigned)del_mask);
254 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
255 struct afs_info old_afsi, new_afsi;
256 struct afsi_change_event_data aced = {.old_afsi = &old_afsi};
259 ret = aft_get_row_of_path(p, &aced.aft_row);
262 ret = get_afsi_object_of_row(aced.aft_row, &obj);
265 ret = load_afsi(&old_afsi, &obj);
269 new_afsi.attributes |= add_mask;
270 new_afsi.attributes &= ~del_mask;
271 save_afsi(&new_afsi, &obj); /* in-place update */
272 afs_event(AFSI_CHANGE, NULL, &aced);
276 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
279 int com_setatt(__a_unused struct stream_cipher_context *scc, int argc, char * const * const argv)
282 return -E_ATTR_SYNTAX;
283 return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
287 struct addatt_event_data {
289 unsigned char bitnum;
293 static void com_addatt_callback(int fd, const struct osl_object *query)
296 int ret = 1, ret2 = 0;
297 struct para_buffer pb = {
300 .max_size_handler = pass_buffer_as_shm
304 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
305 struct osl_object objs[NUM_ATT_COLUMNS];
307 unsigned char bitnum;
308 struct addatt_event_data aed;
311 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
312 ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
317 ret = get_attribute_bitnum_by_name(p, &bitnum);
319 ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
324 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
326 objs[ATTCOL_BITNUM].size = 1;
327 /* find smallest unused attribute */
328 for (bitnum = 0; bitnum < 64; bitnum++) {
329 objs[ATTCOL_BITNUM].data = &bitnum;
330 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
331 &objs[ATTCOL_BITNUM], &row));
332 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
333 break; /* this bitnum is unused, use it */
334 if (ret < 0) /* error */
336 /* this bit is already in use, try next bit */
339 ret = -E_ATT_TABLE_FULL;
342 objs[ATTCOL_NAME].data = p;
343 objs[ATTCOL_NAME].size = len + 1;
344 ret = osl(osl_add_row(attribute_table, objs));
349 afs_event(ATTRIBUTE_ADD, &pb, &aed);
350 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
353 if (ret < 0 && ret2 >= 0)
354 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
356 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
360 int com_addatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
365 return -E_ATTR_SYNTAX;
366 ret = send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
367 sc_send_result, scc);
369 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
373 static void com_mvatt_callback(int fd, const struct osl_object *query)
375 char *old = query->data;
376 size_t size = strlen(old) + 1;
377 char *new = old + size;
378 struct osl_object obj = {.data = old, .size = size};
380 struct para_buffer pb = {
383 .max_size_handler = pass_buffer_as_shm
387 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
391 obj.size = strlen(new) + 1;
392 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
395 para_printf(&pb, "%s\n", para_strerror(-ret));
397 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
399 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
403 int com_mvatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
408 return -E_ATTR_SYNTAX;
409 ret = send_standard_callback_request(argc - 1, argv + 1, com_mvatt_callback,
410 sc_send_result, scc);
412 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
416 /** Data passed to the action handler of com_rmatt(). */
417 struct remove_attribute_action_data {
418 /** Message buffer. */
419 struct para_buffer pb;
420 /** Numver of attributes removed. */
422 /** Bitwise "or" of the removed attributes. */
423 uint64_t mask_of_removed_atts;
426 static int remove_attribute(struct osl_table *table, struct osl_row *row,
427 const char *name, void *data)
429 struct remove_attribute_action_data *raad = data;
431 struct rmatt_event_data red = {.name = name};
433 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
435 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
436 ret = osl(osl_del_row(table, row));
438 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
439 ret = para_printf(&raad->pb, "removed attribute %s\n", name);
441 raad->mask_of_removed_atts |= (1 << red.bitnum);
442 afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
446 static void com_rmatt_callback(int fd, const struct osl_object *query)
448 struct remove_attribute_action_data raad = {
453 .max_size_handler = pass_buffer_as_shm
457 struct pattern_match_data pmd = {
458 .table = attribute_table,
460 .loop_col_num = ATTCOL_BITNUM,
461 .match_col_num = ATTCOL_NAME,
463 .action = remove_attribute
465 ret = for_each_matching_row(&pmd);
467 ret2 = para_printf(&raad.pb, "%s\n", para_strerror(-ret));
468 else if (!raad.num_removed)
469 ret2 = para_printf(&raad.pb, "no match -- nothing removed\n");
470 if (ret2 >= 0 && raad.pb.offset)
471 pass_buffer_as_shm(raad.pb.buf, raad.pb.offset, &fd);
475 int com_rmatt(struct stream_cipher_context *scc, int argc, char * const * const argv)
480 return -E_ATTR_SYNTAX;
481 ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
482 sc_send_result, scc);
484 sc_send_va_buffer(scc, "%s\n", para_strerror(-ret));
489 * Return a binary representation of the given attribute value.
491 * \param atts Pointer to the attribute value.
494 * This function prints a string of at most 64 characters plus the terminating
495 * \p NULL character into \a buf which must be provided by the caller and at
496 * least 65 bytes long. The "x" character is used for set attributes and "-" is
497 * used for unset attributes.
499 * In practice, not all 64 attributes are defined. In this case, the function
500 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
503 void get_attribute_bitmap(const uint64_t *atts, char *buf)
506 const uint64_t one = 1;
508 for (i = 0; i <= greatest_att_bitnum; i++)
509 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
514 * Get a string containing the set attributes in text form.
516 * \param atts The attribute bitmap.
517 * \param delim The delimiter to separate matching attribute names.
518 * \param text Result pointer.
520 * \return Positive on success, negative on errors. If no attributes have
521 * been defined, \a *text is NULL.
523 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
526 const uint64_t one = 1;
529 if (greatest_att_bitnum < 0) { /* no attributes available */
530 *text = para_strdup("(no attributes available)");
533 for (i = 0; i <= greatest_att_bitnum; i++) {
534 unsigned char bn = i;
535 struct osl_object obj = {.data = &bn, .size = 1};
538 if (!(*atts & (one << i)))
540 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
543 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
547 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
551 *text = para_strdup(obj.data);
553 if (!*text) /* no attributes set */
554 *text = para_strdup("");
562 * Close the attribute table.
564 * \sa osl_close_table().
566 static void attribute_close(void)
568 osl_close_table(attribute_table, OSL_MARK_CLEAN);
569 attribute_table = NULL;
573 * Open the attribute table.
575 * \param dir The database directory.
577 * \return Positive on success, negative on errors.
579 * \sa osl_open_table().
581 static int attribute_open(const char *dir)
585 attribute_table_desc.dir = dir;
586 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
587 greatest_att_bitnum = -1; /* no atts available */
589 find_greatest_att_bitnum();
592 attribute_table = NULL;
593 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
598 static int attribute_create(const char *dir)
600 attribute_table_desc.dir = dir;
601 return osl(osl_create_table(&attribute_table_desc));
605 * Initialize the attribute table structure.
607 * \param t The table structure to initialize.
609 void attribute_init(struct afs_table *t)
611 t->open = attribute_open;
612 t->close = attribute_close;
613 t->create = attribute_create;