2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
16 static struct osl_table *attribute_table;
17 static int greatest_att_bitnum;
19 /** The columns of the attribute table. */
20 enum attribute_table_columns {
21 /** The bit number (0-63). */
23 /** The name of the attribute. */
25 /** Number of columns in this table. */
29 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
31 const unsigned char *c1 = (const unsigned char*)obj1->data;
32 const unsigned char *c2 = (const unsigned char*)obj2->data;
40 static struct osl_column_description att_cols[] = {
42 .storage_type = OSL_MAPPED_STORAGE,
43 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
45 .compare_function = char_compare,
49 .storage_type = OSL_MAPPED_STORAGE,
50 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
52 .compare_function = string_compare,
56 static struct osl_table_description attribute_table_desc = {
58 .num_columns = NUM_ATT_COLUMNS,
60 .column_descriptions = att_cols
63 static void find_greatest_att_bitnum(void)
68 struct osl_object obj = {.data = &c, .size = 1};
69 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
71 greatest_att_bitnum = c;
75 PARA_INFO_LOG("no attributes\n");
76 greatest_att_bitnum = -E_NO_ATTRIBUTES;
80 * Retrieve the identifier (number) of an attribute.
82 * \param att_name The name of the attribute.
83 * \param bitnum Result pointer.
85 * \return Positive on success, negative on errors.
87 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
89 struct osl_object obj = {.data = (char *)att_name,
90 .size = strlen(att_name) + 1};
92 int ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
96 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj);
99 *bitnum = *(unsigned char *)obj.data;
104 * Flags used by the lsatt command.
106 * \param \sa com_lsatt().
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. */
113 /** Reverse sort order. */
114 LSATT_FLAG_REVERSE = 4
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. */
125 static int print_attribute(struct osl_table *table, struct osl_row *row,
126 const char *name, void *data)
128 struct lsatt_action_data *laad = data;
129 struct osl_object bitnum_obj;
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);
136 ret2 = para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
139 return para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
143 static void com_lsatt_callback(int fd, const struct osl_object *query)
145 struct lsatt_action_data laad = {
146 .flags = *(unsigned *) query->data,
150 .max_size_handler = pass_buffer_as_shm
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,
162 .action = print_attribute
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);
170 pass_buffer_as_shm(laad.pb.buf, laad.pb.offset, &fd);
174 int com_lsatt(int fd, int argc, char * const * const argv)
177 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
180 for (i = 1; i < argc; i++) {
181 const char *arg = argv[i];
184 if (!strcmp(arg, "--")) {
188 if (!strcmp(arg, "-i")) {
189 flags |= LSATT_FLAG_SORT_BY_ID;
192 if (!strcmp(arg, "-l")) {
193 flags |= LSATT_FLAG_LONG;
196 if (!strcmp(arg, "-r")) {
197 flags |= LSATT_FLAG_REVERSE;
201 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
202 com_lsatt_callback, send_result, &fd);
205 ret = send_va_buffer(fd, "no matches\n");
207 send_va_buffer(fd, "%s\n", para_strerror(-ret));
211 static void com_setatt_callback(__a_unused int fd, const struct osl_object *query)
214 uint64_t add_mask = 0, del_mask = 0;
217 struct osl_object obj;
220 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
224 ret = -E_ATTR_SYNTAX;
228 if (c != '+' && c != '-')
233 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
236 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
241 add_mask |= (1UL << *(unsigned char *)obj.data);
243 del_mask |= (1UL << *(unsigned char *)obj.data);
245 ret = -E_ATTR_SYNTAX;
246 if (!add_mask && !del_mask)
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};
255 ret = aft_get_row_of_path(p, &aced.aft_row);
258 ret = get_afsi_object_of_row(aced.aft_row, &obj);
261 ret = load_afsi(&old_afsi, &obj);
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);
272 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
275 int com_setatt(__a_unused int fd, int argc, char * const * const argv)
278 return -E_ATTR_SYNTAX;
279 return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
283 struct addatt_event_data {
285 unsigned char bitnum;
289 static void com_addatt_callback(int fd, const struct osl_object *query)
291 char *p = query->data;
292 int ret = 1, ret2 = 0;
293 struct para_buffer pb = {
296 .max_size_handler = pass_buffer_as_shm
300 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
301 struct osl_object objs[NUM_ATT_COLUMNS];
303 unsigned char bitnum;
305 struct addatt_event_data aed;
307 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
308 ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
313 ret = get_attribute_bitnum_by_name(p, &bitnum);
315 ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
320 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
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 */
332 /* this bit is already in use, try next bit */
335 ret = -E_ATT_TABLE_FULL;
338 objs[ATTCOL_NAME].data = p;
339 objs[ATTCOL_NAME].size = len + 1;
340 ret = osl_add_row(attribute_table, objs);
345 afs_event(ATTRIBUTE_ADD, &pb, &aed);
346 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
349 if (ret < 0 && ret2 >= 0)
350 ret = para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
352 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
356 int com_addatt(int fd, int argc, char * const * const argv)
361 return -E_ATTR_SYNTAX;
362 ret = send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
365 send_va_buffer(fd, "%s\n", para_strerror(-ret));
369 static void com_mvatt_callback(int fd, const struct osl_object *query)
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};
376 struct para_buffer pb = {
379 .max_size_handler = pass_buffer_as_shm
383 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
387 obj.size = strlen(new) + 1;
388 ret = osl_update_object(attribute_table, row, ATTCOL_NAME, &obj);
391 ret = para_printf(&pb, "%s\n", para_strerror(-ret));
393 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
395 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
399 int com_mvatt(int fd, int argc, char * const * const argv)
404 return -E_ATTR_SYNTAX;
405 ret = send_standard_callback_request(argc - 1, argv + 1, com_mvatt_callback,
408 send_va_buffer(fd, "%s\n", para_strerror(-ret));
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. */
418 /** Bitwise "or" of the removed attributes. */
419 uint64_t mask_of_removed_atts;
422 static int remove_attribute(struct osl_table *table, struct osl_row *row,
423 const char *name, void *data)
425 struct remove_attribute_action_data *raad = data;
427 struct rmatt_event_data red = {.name = name};
429 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
431 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
432 ret = osl_del_row(table, row);
434 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
435 ret = para_printf(&raad->pb, "removed attribute %s\n", name);
437 raad->mask_of_removed_atts |= (1 << red.bitnum);
438 afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
442 static void com_rmatt_callback(int fd, const struct osl_object *query)
444 struct remove_attribute_action_data raad = {
449 .max_size_handler = pass_buffer_as_shm
453 struct pattern_match_data pmd = {
454 .table = attribute_table,
456 .loop_col_num = ATTCOL_BITNUM,
457 .match_col_num = ATTCOL_NAME,
459 .action = remove_attribute
461 ret = for_each_matching_row(&pmd);
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);
471 int com_rmatt(int fd, int argc, char * const * const argv)
476 return -E_ATTR_SYNTAX;
477 ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
480 send_va_buffer(fd, "%s\n", para_strerror(-ret));
485 * Return a binary representation of the given attribute value.
487 * \param atts Pointer to the attribute value.
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.
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
499 void get_attribute_bitmap(const uint64_t *atts, char *buf)
502 const uint64_t one = 1;
504 for (i = 0; i <= greatest_att_bitnum; i++)
505 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
510 * Get a string containing the set attributes in text form.
512 * \param atts The attribute bitmap.
513 * \param delim The delimiter to separate matching attribute names.
514 * \param text Result pointer.
516 * \return Positive on success, negative on errors. If no attributes have
517 * been defined, \a *text is NULL.
519 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
522 const uint64_t one = 1;
525 if (greatest_att_bitnum < 0) { /* no attributes available */
526 *text = para_strdup("(no attributes available)");
529 for (i = 0; i <= greatest_att_bitnum; i++) {
530 unsigned char bn = i;
531 struct osl_object obj = {.data = &bn, .size = 1};
534 if (!(*atts & (one << i)))
536 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
539 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
543 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
547 *text = para_strdup(obj.data);
549 if (!*text) /* no attributes set */
550 *text = para_strdup("");
558 * Close the attribute table.
560 * \sa osl_close_table().
562 void attribute_close(void)
564 osl_close_table(attribute_table, OSL_MARK_CLEAN);
565 attribute_table = NULL;
569 * Open the attribute table.
571 * \param ti Gets initialized by this function.
572 * \param db The database directory.
574 * \return Positive on success, negative on errors.
576 * \sa osl_open_table().
578 static int attribute_open(const char *dir)
582 attribute_table_desc.dir = dir;
583 ret = osl_open_table(&attribute_table_desc, &attribute_table);
584 greatest_att_bitnum = -1; /* no atts available */
586 find_greatest_att_bitnum();
589 attribute_table = NULL;
590 if (ret >= 0 || is_errno(-ret, ENOENT))
595 static int attribute_create(const char *dir)
597 attribute_table_desc.dir = dir;
598 return osl_create_table(&attribute_table_desc);
602 void attribute_init(struct afs_table *t)
604 t->name = attribute_table_desc.name;
605 t->open = attribute_open;
606 t->close = attribute_close;
607 t->create = attribute_create;