2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
17 static struct osl_table *attribute_table;
18 static int greatest_att_bitnum;
20 /** The columns of the attribute table. */
21 enum attribute_table_columns {
22 /** The bit number (0-63). */
24 /** The name of the attribute. */
26 /** Number of columns in this table. */
30 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
32 const unsigned char *c1 = (const unsigned char*)obj1->data;
33 const unsigned char *c2 = (const unsigned char*)obj2->data;
41 static struct osl_column_description att_cols[] = {
43 .storage_type = OSL_MAPPED_STORAGE,
44 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
46 .compare_function = char_compare,
50 .storage_type = OSL_MAPPED_STORAGE,
51 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
53 .compare_function = string_compare,
57 static struct osl_table_description attribute_table_desc = {
59 .num_columns = NUM_ATT_COLUMNS,
61 .column_descriptions = att_cols
64 static void find_greatest_att_bitnum(void)
69 struct osl_object obj = {.data = &c, .size = 1};
70 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
72 greatest_att_bitnum = c;
76 PARA_INFO_LOG("no attributes\n");
77 greatest_att_bitnum = -E_NO_ATTRIBUTES;
81 * Retrieve the identifier (number) of an attribute.
83 * \param att_name The name of the attribute.
84 * \param bitnum Result pointer.
86 * \return Positive on success, negative on errors.
88 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
90 struct osl_object obj = {.data = (char *)att_name,
91 .size = strlen(att_name) + 1};
93 int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
97 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
100 *bitnum = *(unsigned char *)obj.data;
105 * Flags used by the lsatt command.
107 * \param \sa com_lsatt().
110 /** Whether "-a" was given for the lsatt command. */
111 LSATT_FLAG_SORT_BY_ID = 1,
112 /** Whether "-l" was given for the lsatt command. */
114 /** Reverse sort order. */
115 LSATT_FLAG_REVERSE = 4
118 /** Data passed to the action function of lsatt */
119 struct lsatt_action_data {
120 /** The result buffer. */
121 struct para_buffer pb;
122 /** The given flags for the lsatt command. */
126 static int print_attribute(struct osl_table *table, struct osl_row *row,
127 const char *name, void *data)
129 struct lsatt_action_data *laad = data;
130 struct osl_object bitnum_obj;
133 if (!(laad->flags & LSATT_FLAG_LONG))
134 return para_printf(&laad->pb, "%s\n", name);
135 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
137 para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
140 return para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
144 static void com_lsatt_callback(int fd, const struct osl_object *query)
146 struct lsatt_action_data laad = {
147 .flags = *(unsigned *) query->data,
151 .max_size_handler = pass_buffer_as_shm
155 struct pattern_match_data pmd = {
156 .table = attribute_table,
157 .loop_col_num = ATTCOL_BITNUM,
158 .match_col_num = ATTCOL_NAME,
159 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
160 .size = query->size - sizeof(laad.flags)},
161 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
163 .action = print_attribute
165 if (laad.flags & LSATT_FLAG_SORT_BY_ID)
166 pmd.loop_col_num = ATTCOL_NAME;
167 if (laad.flags & LSATT_FLAG_REVERSE)
168 pmd.pm_flags |= PM_REVERSE_LOOP;
169 for_each_matching_row(&pmd);
171 pass_buffer_as_shm(laad.pb.buf, laad.pb.offset, &fd);
175 int com_lsatt(int fd, int argc, char * const * const argv)
178 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
181 for (i = 1; i < argc; i++) {
182 const char *arg = argv[i];
185 if (!strcmp(arg, "--")) {
189 if (!strcmp(arg, "-i")) {
190 flags |= LSATT_FLAG_SORT_BY_ID;
193 if (!strcmp(arg, "-l")) {
194 flags |= LSATT_FLAG_LONG;
197 if (!strcmp(arg, "-r")) {
198 flags |= LSATT_FLAG_REVERSE;
202 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
203 com_lsatt_callback, send_result, &fd);
206 ret = send_va_buffer(fd, "no matches\n");
208 send_va_buffer(fd, "%s\n", para_strerror(-ret));
212 static void com_setatt_callback(__a_unused int fd, const struct osl_object *query)
215 uint64_t add_mask = 0, del_mask = 0;
218 struct osl_object obj;
221 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
225 ret = -E_ATTR_SYNTAX;
229 if (c != '+' && c != '-')
234 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
237 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM,
242 add_mask |= (1UL << *(unsigned char *)obj.data);
244 del_mask |= (1UL << *(unsigned char *)obj.data);
246 ret = -E_ATTR_SYNTAX;
247 if (!add_mask && !del_mask)
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 afsi_change_event_data aced = {.old_afsi = &old_afsi};
256 ret = aft_get_row_of_path(p, &aced.aft_row);
259 ret = get_afsi_object_of_row(aced.aft_row, &obj);
262 ret = load_afsi(&old_afsi, &obj);
266 new_afsi.attributes |= add_mask;
267 new_afsi.attributes &= ~del_mask;
268 save_afsi(&new_afsi, &obj); /* in-place update */
269 afs_event(AFSI_CHANGE, NULL, &aced);
273 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
276 int com_setatt(__a_unused int fd, int argc, char * const * const argv)
279 return -E_ATTR_SYNTAX;
280 return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
284 struct addatt_event_data {
286 unsigned char bitnum;
290 static void com_addatt_callback(int fd, const struct osl_object *query)
293 int ret = 1, ret2 = 0;
294 struct para_buffer pb = {
297 .max_size_handler = pass_buffer_as_shm
301 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
302 struct osl_object objs[NUM_ATT_COLUMNS];
304 unsigned char bitnum;
305 struct addatt_event_data aed;
308 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
309 ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
314 ret = get_attribute_bitnum_by_name(p, &bitnum);
316 ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
321 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
323 objs[ATTCOL_BITNUM].size = 1;
324 /* find smallest unused attribute */
325 for (bitnum = 0; bitnum < 64; bitnum++) {
326 objs[ATTCOL_BITNUM].data = &bitnum;
327 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
328 &objs[ATTCOL_BITNUM], &row));
329 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
330 break; /* this bitnum is unused, use it */
331 if (ret < 0) /* error */
333 /* this bit is already in use, try next bit */
336 ret = -E_ATT_TABLE_FULL;
339 objs[ATTCOL_NAME].data = p;
340 objs[ATTCOL_NAME].size = len + 1;
341 ret = osl(osl_add_row(attribute_table, objs));
346 afs_event(ATTRIBUTE_ADD, &pb, &aed);
347 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
350 if (ret < 0 && ret2 >= 0)
351 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
353 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
357 int com_addatt(int fd, int argc, char * const * const argv)
362 return -E_ATTR_SYNTAX;
363 ret = send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
366 send_va_buffer(fd, "%s\n", para_strerror(-ret));
370 static void com_mvatt_callback(int fd, const struct osl_object *query)
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};
377 struct para_buffer pb = {
380 .max_size_handler = pass_buffer_as_shm
384 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
388 obj.size = strlen(new) + 1;
389 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
392 para_printf(&pb, "%s\n", para_strerror(-ret));
394 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
396 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
400 int com_mvatt(int fd, int argc, char * const * const argv)
405 return -E_ATTR_SYNTAX;
406 ret = send_standard_callback_request(argc - 1, argv + 1, com_mvatt_callback,
409 send_va_buffer(fd, "%s\n", para_strerror(-ret));
413 /** Data passed to the action handler of com_rmatt(). */
414 struct remove_attribute_action_data {
415 /** Message buffer. */
416 struct para_buffer pb;
417 /** Numver of attributes removed. */
419 /** Bitwise "or" of the removed attributes. */
420 uint64_t mask_of_removed_atts;
423 static int remove_attribute(struct osl_table *table, struct osl_row *row,
424 const char *name, void *data)
426 struct remove_attribute_action_data *raad = data;
428 struct rmatt_event_data red = {.name = name};
430 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
432 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
433 ret = osl(osl_del_row(table, row));
435 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
436 ret = para_printf(&raad->pb, "removed attribute %s\n", name);
438 raad->mask_of_removed_atts |= (1 << red.bitnum);
439 afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
443 static void com_rmatt_callback(int fd, const struct osl_object *query)
445 struct remove_attribute_action_data raad = {
450 .max_size_handler = pass_buffer_as_shm
454 struct pattern_match_data pmd = {
455 .table = attribute_table,
457 .loop_col_num = ATTCOL_BITNUM,
458 .match_col_num = ATTCOL_NAME,
460 .action = remove_attribute
462 ret = for_each_matching_row(&pmd);
464 ret2 = para_printf(&raad.pb, "%s\n", para_strerror(-ret));
465 else if (!raad.num_removed)
466 ret2 = para_printf(&raad.pb, "no match -- nothing removed\n");
467 if (ret2 >= 0 && raad.pb.offset)
468 pass_buffer_as_shm(raad.pb.buf, raad.pb.offset, &fd);
472 int com_rmatt(int fd, int argc, char * const * const argv)
477 return -E_ATTR_SYNTAX;
478 ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
481 send_va_buffer(fd, "%s\n", para_strerror(-ret));
486 * Return a binary representation of the given attribute value.
488 * \param atts Pointer to the attribute value.
491 * This function prints a string of at most 64 characters plus the terminating
492 * \p NULL character into \a buf which must be provided by the caller and at
493 * least 65 bytes long. The "x" character is used for set attributes and "-" is
494 * used for unset attributes.
496 * In practice, not all 64 attributes are defined. In this case, the function
497 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
500 void get_attribute_bitmap(const uint64_t *atts, char *buf)
503 const uint64_t one = 1;
505 for (i = 0; i <= greatest_att_bitnum; i++)
506 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
511 * Get a string containing the set attributes in text form.
513 * \param atts The attribute bitmap.
514 * \param delim The delimiter to separate matching attribute names.
515 * \param text Result pointer.
517 * \return Positive on success, negative on errors. If no attributes have
518 * been defined, \a *text is NULL.
520 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
523 const uint64_t one = 1;
526 if (greatest_att_bitnum < 0) { /* no attributes available */
527 *text = para_strdup("(no attributes available)");
530 for (i = 0; i <= greatest_att_bitnum; i++) {
531 unsigned char bn = i;
532 struct osl_object obj = {.data = &bn, .size = 1};
535 if (!(*atts & (one << i)))
537 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
540 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
544 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
548 *text = para_strdup(obj.data);
550 if (!*text) /* no attributes set */
551 *text = para_strdup("");
559 * Close the attribute table.
561 * \sa osl_close_table().
563 void attribute_close(void)
565 osl_close_table(attribute_table, OSL_MARK_CLEAN);
566 attribute_table = NULL;
570 * Open the attribute table.
572 * \param dir 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(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(osl_create_table(&attribute_table_desc));
602 * Initialize the attribute table structure.
604 * \param t The table structure to initialize.
606 void attribute_init(struct afs_table *t)
608 t->name = attribute_table_desc.name;
609 t->open = attribute_open;
610 t->close = attribute_close;
611 t->create = attribute_create;