2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
22 static struct osl_table *attribute_table;
23 static int greatest_att_bitnum;
25 /** The columns of the attribute table. */
26 enum attribute_table_columns {
27 /** The bit number (0-63). */
29 /** The name of the attribute. */
31 /** Number of columns in this table. */
35 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
37 const unsigned char *c1 = (const unsigned char*)obj1->data;
38 const unsigned char *c2 = (const unsigned char*)obj2->data;
46 static struct osl_column_description att_cols[] = {
48 .storage_type = OSL_MAPPED_STORAGE,
49 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
51 .compare_function = char_compare,
55 .storage_type = OSL_MAPPED_STORAGE,
56 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
58 .compare_function = string_compare,
62 static struct osl_table_description attribute_table_desc = {
64 .num_columns = NUM_ATT_COLUMNS,
66 .column_descriptions = att_cols
69 static void find_greatest_att_bitnum(void)
74 struct osl_object obj = {.data = &c, .size = 1};
75 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
77 greatest_att_bitnum = c;
81 PARA_INFO_LOG("no attributes\n");
82 greatest_att_bitnum = -E_NO_ATTRIBUTES;
86 * Retrieve the identifier (number) of an attribute.
88 * \param att_name The name of the attribute.
89 * \param bitnum Result pointer.
91 * \return Positive on success, negative on errors.
93 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
95 struct osl_object obj = {.data = (char *)att_name,
96 .size = strlen(att_name) + 1};
98 int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
102 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
105 *bitnum = *(unsigned char *)obj.data;
110 * Flags used by the lsatt command.
112 * \param \sa com_lsatt().
115 /** Whether "-a" was given for the lsatt command. */
116 LSATT_FLAG_SORT_BY_ID = 1,
117 /** Whether "-l" was given for the lsatt command. */
119 /** Reverse sort order. */
120 LSATT_FLAG_REVERSE = 4
123 /** Data passed to the action function of lsatt */
124 struct lsatt_action_data {
125 /** The result buffer. */
126 struct para_buffer pb;
127 /** The given flags for the lsatt command. */
131 static int print_attribute(struct osl_table *table, struct osl_row *row,
132 const char *name, void *data)
134 struct lsatt_action_data *laad = data;
135 struct osl_object bitnum_obj;
138 if (!(laad->flags & LSATT_FLAG_LONG)) {
139 para_printf(&laad->pb, "%s\n", name);
142 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
144 para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
147 para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
152 static void com_lsatt_callback(int fd, const struct osl_object *query)
154 struct lsatt_action_data laad = {
155 .flags = *(unsigned *) query->data,
157 .max_size = shm_get_shmmax(),
158 .private_data = &(struct afs_max_size_handler_data) {
162 .max_size_handler = afs_max_size_handler
166 struct pattern_match_data pmd = {
167 .table = attribute_table,
168 .loop_col_num = ATTCOL_BITNUM,
169 .match_col_num = ATTCOL_NAME,
170 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
171 .size = query->size - sizeof(laad.flags)},
172 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
174 .action = print_attribute
176 if (laad.flags & LSATT_FLAG_SORT_BY_ID)
177 pmd.loop_col_num = ATTCOL_NAME;
178 if (laad.flags & LSATT_FLAG_REVERSE)
179 pmd.pm_flags |= PM_REVERSE_LOOP;
180 for_each_matching_row(&pmd);
182 pass_buffer_as_shm(fd, SBD_OUTPUT, laad.pb.buf, laad.pb.offset);
186 int com_lsatt(struct command_context *cc)
189 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
192 for (i = 1; i < cc->argc; i++) {
193 const char *arg = cc->argv[i];
196 if (!strcmp(arg, "--")) {
200 if (!strcmp(arg, "-i")) {
201 flags |= LSATT_FLAG_SORT_BY_ID;
204 if (!strcmp(arg, "-l")) {
205 flags |= LSATT_FLAG_LONG;
208 if (!strcmp(arg, "-r")) {
209 flags |= LSATT_FLAG_REVERSE;
213 ret = send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i,
214 com_lsatt_callback, afs_cb_result_handler, cc);
216 send_strerror(cc, -ret);
217 else if (ret == 0 && cc->argc > 1)
218 ret = send_sb_va(&cc->scc, SBD_ERROR_LOG, "no matches\n");
222 struct addatt_event_data {
224 unsigned char bitnum;
228 static void com_addatt_callback(int fd, const struct osl_object *query)
232 struct para_buffer pb = {
233 .max_size = shm_get_shmmax(),
234 .private_data = &(struct afs_max_size_handler_data) {
238 .max_size_handler = afs_max_size_handler
242 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
243 struct osl_object objs[NUM_ATT_COLUMNS];
245 unsigned char bitnum;
246 struct addatt_event_data aed;
249 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
250 para_printf(&pb, "invalid attribute name: %s\n", p);
253 ret = get_attribute_bitnum_by_name(p, &bitnum);
255 para_printf(&pb, "attribute \"%s\" already exists\n", p);
258 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
260 objs[ATTCOL_BITNUM].size = 1;
261 /* find smallest unused attribute */
262 for (bitnum = 0; bitnum < 64; bitnum++) {
263 objs[ATTCOL_BITNUM].data = &bitnum;
264 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
265 &objs[ATTCOL_BITNUM], &row));
266 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
267 break; /* this bitnum is unused, use it */
268 if (ret < 0) /* error */
270 /* this bit is already in use, try next bit */
273 ret = -E_ATT_TABLE_FULL;
276 objs[ATTCOL_NAME].data = p;
277 objs[ATTCOL_NAME].size = len + 1;
278 ret = osl(osl_add_row(attribute_table, objs));
283 afs_event(ATTRIBUTE_ADD, &pb, &aed);
284 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
288 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
290 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
294 int com_addatt(struct command_context *cc)
299 return -E_ATTR_SYNTAX;
300 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
301 com_addatt_callback, afs_cb_result_handler, cc);
303 send_strerror(cc, -ret);
307 static void com_mvatt_callback(int fd, const struct osl_object *query)
309 char *old = query->data;
310 size_t size = strlen(old) + 1;
311 char *new = old + size;
312 struct osl_object obj = {.data = old, .size = size};
314 struct para_buffer pb = {
315 .max_size = shm_get_shmmax(),
316 .private_data = &(struct afs_max_size_handler_data) {
320 .max_size_handler = afs_max_size_handler,
324 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
328 obj.size = strlen(new) + 1;
329 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
332 para_printf(&pb, "%s\n", para_strerror(-ret));
334 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
336 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
340 int com_mvatt(struct command_context *cc)
345 return -E_ATTR_SYNTAX;
346 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
347 com_mvatt_callback, afs_cb_result_handler, cc);
349 send_strerror(cc, -ret);
353 /** Data passed to the action handler of com_rmatt(). */
354 struct remove_attribute_action_data {
355 /** Message buffer. */
356 struct para_buffer pb;
357 /** Number of attributes removed. */
359 /** Bitwise "or" of the removed attributes. */
360 uint64_t mask_of_removed_atts;
363 /* returns succcess even on errors to keep the loop going */
364 static int remove_attribute(struct osl_table *table, struct osl_row *row,
365 const char *name, void *data)
367 struct remove_attribute_action_data *raad = data;
369 struct rmatt_event_data red = {.name = name};
371 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
373 para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
376 ret = osl(osl_del_row(table, row));
378 para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
381 para_printf(&raad->pb, "removed attribute %s\n", name);
383 raad->mask_of_removed_atts |= (1 << red.bitnum);
384 afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
388 static void com_rmatt_callback(int fd, const struct osl_object *query)
390 struct remove_attribute_action_data raad = {
393 .max_size = shm_get_shmmax(),
394 .private_data = &(struct afs_max_size_handler_data) {
398 .max_size_handler = afs_max_size_handler,
402 struct pattern_match_data pmd = {
403 .table = attribute_table,
405 .loop_col_num = ATTCOL_BITNUM,
406 .match_col_num = ATTCOL_NAME,
408 .action = remove_attribute
410 ret = for_each_matching_row(&pmd);
412 para_printf(&raad.pb, "%s\n", para_strerror(-ret));
413 else if (!raad.num_removed)
414 para_printf(&raad.pb, "no match -- nothing removed\n");
416 pass_buffer_as_shm(fd, SBD_OUTPUT, raad.pb.buf, raad.pb.offset);
420 int com_rmatt(struct command_context *cc)
425 return -E_ATTR_SYNTAX;
426 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
427 com_rmatt_callback, afs_cb_result_handler, cc);
429 send_strerror(cc, -ret);
434 * Return a binary representation of the given attribute value.
436 * \param atts Pointer to the attribute value.
439 * This function prints a string of at most 64 characters plus the terminating
440 * \p NULL character into \a buf which must be provided by the caller and at
441 * least 65 bytes long. The "x" character is used for set attributes and "-" is
442 * used for unset attributes.
444 * In practice, not all 64 attributes are defined. In this case, the function
445 * only prints \a N + 1 characters where \a N is the greatest id of a defined
448 void get_attribute_bitmap(const uint64_t *atts, char *buf)
451 const uint64_t one = 1;
453 for (i = 0; i <= greatest_att_bitnum; i++)
454 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
459 * Get a string containing the set attributes in text form.
461 * \param atts The attribute bitmap.
462 * \param delim The delimiter to separate matching attribute names.
463 * \param text Result pointer.
465 * \return Positive on success, negative on errors. If no attributes have
466 * been defined, \a *text is NULL.
468 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
471 const uint64_t one = 1;
474 if (greatest_att_bitnum < 0) { /* no attributes available */
475 *text = para_strdup("(no attributes available)");
478 for (i = 0; i <= greatest_att_bitnum; i++) {
479 unsigned char bn = i;
480 struct osl_object obj = {.data = &bn, .size = 1};
483 if (!(*atts & (one << i)))
485 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
488 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
492 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
496 *text = para_strdup(obj.data);
498 if (!*text) /* no attributes set */
499 *text = para_strdup("");
507 * Close the attribute table.
509 * \sa osl_close_table().
511 static void attribute_close(void)
513 osl_close_table(attribute_table, OSL_MARK_CLEAN);
514 attribute_table = NULL;
518 * Open the attribute table.
520 * \param dir The database directory.
522 * \return Positive on success, negative on errors.
524 * \sa osl_open_table().
526 static int attribute_open(const char *dir)
530 attribute_table_desc.dir = dir;
531 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
532 greatest_att_bitnum = -1; /* no atts available */
534 find_greatest_att_bitnum();
537 attribute_table = NULL;
538 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
543 static int attribute_create(const char *dir)
545 attribute_table_desc.dir = dir;
546 return osl(osl_create_table(&attribute_table_desc));
550 * Initialize the attribute table structure.
552 * \param t The table structure to initialize.
554 void attribute_init(struct afs_table *t)
556 t->open = attribute_open;
557 t->close = attribute_close;
558 t->create = attribute_create;