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. */
23 static struct osl_table *attribute_table;
24 static int greatest_att_bitnum;
26 /** The columns of the attribute table. */
27 enum attribute_table_columns {
28 /** The bit number (0-63). */
30 /** The name of the attribute. */
32 /** Number of columns in this table. */
36 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
38 const unsigned char *c1 = (const unsigned char*)obj1->data;
39 const unsigned char *c2 = (const unsigned char*)obj2->data;
47 static struct osl_column_description att_cols[] = {
49 .storage_type = OSL_MAPPED_STORAGE,
50 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
52 .compare_function = char_compare,
56 .storage_type = OSL_MAPPED_STORAGE,
57 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
59 .compare_function = string_compare,
63 static struct osl_table_description attribute_table_desc = {
65 .num_columns = NUM_ATT_COLUMNS,
67 .column_descriptions = att_cols
70 static void find_greatest_att_bitnum(void)
75 struct osl_object obj = {.data = &c, .size = 1};
76 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
78 greatest_att_bitnum = c;
82 PARA_INFO_LOG("no attributes\n");
83 greatest_att_bitnum = -E_NO_ATTRIBUTES;
87 * Retrieve the identifier (number) of an attribute.
89 * \param att_name The name of the attribute.
90 * \param bitnum Result pointer.
92 * \return Positive on success, negative on errors.
94 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
96 struct osl_object obj = {.data = (char *)att_name,
97 .size = strlen(att_name) + 1};
99 int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
103 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
106 *bitnum = *(unsigned char *)obj.data;
111 * Flags used by the lsatt command.
113 * \param \sa com_lsatt().
116 /** Whether "-a" was given for the lsatt command. */
117 LSATT_FLAG_SORT_BY_ID = 1,
118 /** Whether "-l" was given for the lsatt command. */
120 /** Reverse sort order. */
121 LSATT_FLAG_REVERSE = 4
124 /** Data passed to the action function of lsatt */
125 static int print_attribute(struct osl_table *table, struct osl_row *row,
126 const char *name, void *data)
128 struct afs_callback_arg *aca = data;
129 unsigned flags = *(unsigned *)aca->query.data;
130 struct osl_object bitnum_obj;
133 if (!(flags & LSATT_FLAG_LONG)) {
134 para_printf(&aca->pbout, "%s\n", name);
137 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
139 para_printf(&aca->pbout, "%s: %s\n", name, para_strerror(-ret));
142 para_printf(&aca->pbout, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
147 static int com_lsatt_callback(struct afs_callback_arg *aca)
149 unsigned flags = *(unsigned *)aca->query.data;
151 struct pattern_match_data pmd = {
152 .table = attribute_table,
153 .loop_col_num = ATTCOL_NAME,
154 .match_col_num = ATTCOL_NAME,
155 .patterns = {.data = (char *)aca->query.data + sizeof(flags),
156 .size = aca->query.size - sizeof(flags)},
157 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
159 .action = print_attribute
161 if (flags & LSATT_FLAG_SORT_BY_ID)
162 pmd.loop_col_num = ATTCOL_BITNUM;
163 if (flags & LSATT_FLAG_REVERSE)
164 pmd.pm_flags |= PM_REVERSE_LOOP;
165 ret = for_each_matching_row(&pmd);
168 if (pmd.num_matches == 0)
174 int com_lsatt(struct command_context *cc)
177 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
180 for (i = 1; i < cc->argc; i++) {
181 const char *arg = cc->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 return send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i,
202 com_lsatt_callback, afs_cb_result_handler, cc);
205 struct addatt_event_data {
207 unsigned char bitnum;
211 static int com_addatt_callback(struct afs_callback_arg *aca)
219 p < (char *)aca->query.data + aca->query.size;
222 struct osl_object objs[NUM_ATT_COLUMNS];
224 unsigned char bitnum;
225 struct addatt_event_data aed;
228 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
229 para_printf(&aca->pbout, "invalid attribute name: %s\n", p);
232 ret = get_attribute_bitnum_by_name(p, &bitnum);
234 para_printf(&aca->pbout, "attribute \"%s\" already exists\n", p);
237 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
239 objs[ATTCOL_BITNUM].size = 1;
240 /* find smallest unused attribute */
241 for (bitnum = 0; bitnum < 64; bitnum++) {
242 objs[ATTCOL_BITNUM].data = &bitnum;
243 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
244 &objs[ATTCOL_BITNUM], &row));
245 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
246 break; /* this bitnum is unused, use it */
247 if (ret < 0) /* error */
249 /* this bit is already in use, try next bit */
252 ret = -E_ATT_TABLE_FULL;
255 objs[ATTCOL_NAME].data = p;
256 objs[ATTCOL_NAME].size = len + 1;
257 ret = osl(osl_add_row(attribute_table, objs));
262 ret = afs_event(ATTRIBUTE_ADD, &aca->pbout, &aed);
265 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
269 para_printf(&aca->pbout, "%s: %s\n", p, para_strerror(-ret));
273 int com_addatt(struct command_context *cc)
278 return -E_ATTR_SYNTAX;
279 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
280 com_addatt_callback, afs_cb_result_handler, cc);
282 send_strerror(cc, -ret);
286 static int com_mvatt_callback(struct afs_callback_arg *aca)
288 char *old = aca->query.data;
289 size_t size = strlen(old) + 1;
290 char *new = old + size;
291 struct osl_object obj = {.data = old, .size = size};
295 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
299 obj.size = strlen(new) + 1;
300 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
303 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
305 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
309 int com_mvatt(struct command_context *cc)
312 return -E_ATTR_SYNTAX;
313 return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
314 com_mvatt_callback, afs_cb_result_handler, cc);
317 static int remove_attribute(struct osl_table *table, struct osl_row *row,
318 const char *name, void *data)
320 struct afs_callback_arg *aca = data;
322 struct rmatt_event_data red = {.name = name};
324 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
326 para_printf(&aca->pbout, "cannot remove %s\n", name);
329 para_printf(&aca->pbout, "removing attribute %s\n", name);
330 ret = osl(osl_del_row(table, row));
332 para_printf(&aca->pbout, "cannot remove %s\n", name);
335 return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
338 static int com_rmatt_callback(struct afs_callback_arg *aca)
341 struct pattern_match_data pmd = {
342 .table = attribute_table,
343 .patterns = aca->query,
344 .loop_col_num = ATTCOL_BITNUM,
345 .match_col_num = ATTCOL_NAME,
347 .action = remove_attribute
349 ret = for_each_matching_row(&pmd);
352 if (pmd.num_matches == 0)
358 int com_rmatt(struct command_context *cc)
361 return -E_ATTR_SYNTAX;
362 return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
363 com_rmatt_callback, afs_cb_result_handler, cc);
367 * Return a binary representation of the given attribute value.
369 * \param atts Pointer to the attribute value.
372 * This function prints a string of at most 64 characters plus the terminating
373 * \p NULL character into \a buf which must be provided by the caller and at
374 * least 65 bytes long. The "x" character is used for set attributes and "-" is
375 * used for unset attributes.
377 * In practice, not all 64 attributes are defined. In this case, the function
378 * only prints \a N + 1 characters where \a N is the greatest id of a defined
381 void get_attribute_bitmap(const uint64_t *atts, char *buf)
384 const uint64_t one = 1;
386 for (i = 0; i <= greatest_att_bitnum; i++)
387 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
392 * Get a string containing the set attributes in text form.
394 * \param atts The attribute bitmap.
395 * \param delim The delimiter to separate matching attribute names.
396 * \param text Result pointer.
398 * \return Positive on success, negative on errors. If no attributes have
399 * been defined, \a *text is NULL.
401 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
404 const uint64_t one = 1;
407 if (greatest_att_bitnum < 0) { /* no attributes available */
408 *text = para_strdup("(no attributes available)");
411 for (i = 0; i <= greatest_att_bitnum; i++) {
412 unsigned char bn = i;
413 struct osl_object obj = {.data = &bn, .size = 1};
416 if (!(*atts & (one << i)))
418 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
421 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
425 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
429 *text = para_strdup(obj.data);
431 if (!*text) /* no attributes set */
432 *text = para_strdup("");
439 static int att_logical_or(struct osl_row *row, void *data)
441 uint64_t *att_mask = data, one = 1;
442 struct osl_object bitnum_obj;
443 int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
447 *att_mask |= one << *(unsigned char *)bitnum_obj.data;
452 * Compute the attribute bit mask and check each afs info bitmap.
454 * \param aca The query field of \a aca is ignored.
456 * This iterates over all attributes in the attribute table and computes the
457 * logical or of 1 << b where b is the bit number of the attribute. The
458 * resulting bit mask is passed to aft_check_attributes() which performs the
463 * \sa \ref aft_check_attributes().
465 int attribute_check_callback(struct afs_callback_arg *aca)
468 uint64_t att_mask = 0; /* bits corresponding to a attributes */
470 ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
473 PARA_ERROR_LOG("attribute table loop failed: %s\n",
474 para_strerror(-ret));
477 return aft_check_attributes(att_mask, &aca->pbout);
481 * Close the attribute table.
483 * \sa osl_close_table().
485 static void attribute_close(void)
487 osl_close_table(attribute_table, OSL_MARK_CLEAN);
488 attribute_table = NULL;
492 * Open the attribute table.
494 * \param dir The database directory.
496 * \return Positive on success, negative on errors.
498 * \sa osl_open_table().
500 static int attribute_open(const char *dir)
504 attribute_table_desc.dir = dir;
505 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
506 greatest_att_bitnum = -1; /* no atts available */
508 find_greatest_att_bitnum();
511 attribute_table = NULL;
512 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
517 static int attribute_create(const char *dir)
519 attribute_table_desc.dir = dir;
520 return osl(osl_create_table(&attribute_table_desc));
524 * Initialize the attribute table structure.
526 * \param t The table structure to initialize.
528 void attribute_init(struct afs_table *t)
530 t->open = attribute_open;
531 t->close = attribute_close;
532 t->create = attribute_create;