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. */
13 #include "server_cmd.lsg.h"
24 static struct osl_table *attribute_table;
25 static int greatest_att_bitnum;
27 /** The columns of the attribute table. */
28 enum attribute_table_columns {
29 /** The bit number (0-63). */
31 /** The name of the attribute. */
33 /** Number of columns in this table. */
37 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
39 const unsigned char *c1 = (const unsigned char*)obj1->data;
40 const unsigned char *c2 = (const unsigned char*)obj2->data;
48 static struct osl_column_description att_cols[] = {
50 .storage_type = OSL_MAPPED_STORAGE,
51 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
53 .compare_function = char_compare,
57 .storage_type = OSL_MAPPED_STORAGE,
58 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
60 .compare_function = string_compare,
64 static struct osl_table_description attribute_table_desc = {
66 .num_columns = NUM_ATT_COLUMNS,
68 .column_descriptions = att_cols
71 static void find_greatest_att_bitnum(void)
76 struct osl_object obj = {.data = &c, .size = 1};
77 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
79 greatest_att_bitnum = c;
83 PARA_INFO_LOG("no attributes\n");
84 greatest_att_bitnum = -E_NO_ATTRIBUTES;
88 * Retrieve the identifier (number) of an attribute.
90 * \param att_name The name of the attribute.
91 * \param bitnum Result pointer.
93 * \return Positive on success, negative on errors.
95 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
97 struct osl_object obj = {.data = (char *)att_name,
98 .size = strlen(att_name) + 1};
100 int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
104 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
107 *bitnum = *(unsigned char *)obj.data;
111 /** Data passed to the action function of lsatt */
112 static int print_attribute(struct osl_table *table, struct osl_row *row,
113 const char *name, void *data)
115 struct afs_callback_arg *aca = data;
116 bool l_given = SERVER_CMD_OPT_GIVEN(LSATT, LONG, aca->lpr);
117 struct osl_object bitnum_obj;
121 para_printf(&aca->pbout, "%s\n", name);
124 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
126 para_printf(&aca->pbout, "%s: %s\n", name, para_strerror(-ret));
129 para_printf(&aca->pbout, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
134 static int com_lsatt_callback(struct afs_callback_arg *aca)
136 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LSATT);
137 bool i_given, r_given;
139 struct pattern_match_data pmd = {
140 .table = attribute_table,
141 .loop_col_num = ATTCOL_NAME,
142 .match_col_num = ATTCOL_NAME,
143 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
145 .action = print_attribute
148 ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
151 i_given = SERVER_CMD_OPT_GIVEN(LSATT, ID_SORT, aca->lpr);
152 r_given = SERVER_CMD_OPT_GIVEN(LSATT, REVERSE, aca->lpr);
155 pmd.loop_col_num = ATTCOL_BITNUM;
157 pmd.pm_flags |= PM_REVERSE_LOOP;
158 ret = for_each_matching_row(&pmd);
161 if (pmd.num_matches == 0)
164 lls_free_parse_result(aca->lpr, cmd);
168 static int com_lsatt(struct command_context *cc, struct lls_parse_result *lpr)
170 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LSATT);
171 return send_lls_callback_request(com_lsatt_callback, cmd, lpr, cc);
173 EXPORT_SERVER_CMD_HANDLER(lsatt);
175 struct addatt_event_data {
177 unsigned char bitnum;
180 static int com_addatt_callback(struct afs_callback_arg *aca)
188 p < (char *)aca->query.data + aca->query.size;
191 struct osl_object objs[NUM_ATT_COLUMNS];
193 unsigned char bitnum;
194 struct addatt_event_data aed;
197 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
198 para_printf(&aca->pbout, "invalid attribute name: %s\n", p);
201 ret = get_attribute_bitnum_by_name(p, &bitnum);
203 para_printf(&aca->pbout, "attribute \"%s\" already exists\n", p);
206 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
208 objs[ATTCOL_BITNUM].size = 1;
209 /* find smallest unused attribute */
210 for (bitnum = 0; bitnum < 64; bitnum++) {
211 objs[ATTCOL_BITNUM].data = &bitnum;
212 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
213 &objs[ATTCOL_BITNUM], &row));
214 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
215 break; /* this bitnum is unused, use it */
216 if (ret < 0) /* error */
218 /* this bit is already in use, try next bit */
221 ret = -E_ATT_TABLE_FULL;
224 objs[ATTCOL_NAME].data = p;
225 objs[ATTCOL_NAME].size = len + 1;
226 ret = osl(osl_add_row(attribute_table, objs));
231 ret = afs_event(ATTRIBUTE_ADD, &aca->pbout, &aed);
234 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
238 para_printf(&aca->pbout, "%s: %s\n", p, para_strerror(-ret));
242 int com_addatt(struct command_context *cc)
247 return -E_ATTR_SYNTAX;
248 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
249 com_addatt_callback, afs_cb_result_handler, cc);
251 send_strerror(cc, -ret);
255 static int com_mvatt_callback(struct afs_callback_arg *aca)
257 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
258 const char *old, *new;
259 struct osl_object obj;
263 ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
265 old = lls_input(0, aca->lpr);
266 new = lls_input(1, aca->lpr);
267 obj.data = (char *)old;
268 obj.size = strlen(old) + 1;
269 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
272 obj.data = (char *)new;
273 obj.size = strlen(new) + 1;
274 /* The update fails if the destination attribute exists. */
275 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
278 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
280 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
281 lls_free_parse_result(aca->lpr, cmd);
285 static int com_mvatt(struct command_context *cc, struct lls_parse_result *lpr)
287 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
289 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
291 send_errctx(cc, errctx);
294 return send_lls_callback_request(com_mvatt_callback, cmd, lpr, cc);
296 EXPORT_SERVER_CMD_HANDLER(mvatt);
298 static int remove_attribute(struct osl_table *table, struct osl_row *row,
299 const char *name, void *data)
301 struct afs_callback_arg *aca = data;
303 struct rmatt_event_data red = {.name = name};
305 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
307 para_printf(&aca->pbout, "cannot remove %s\n", name);
310 para_printf(&aca->pbout, "removing attribute %s\n", name);
311 ret = osl(osl_del_row(table, row));
313 para_printf(&aca->pbout, "cannot remove %s\n", name);
316 return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
319 static int com_rmatt_callback(struct afs_callback_arg *aca)
321 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
323 struct pattern_match_data pmd = {
324 .table = attribute_table,
325 .loop_col_num = ATTCOL_BITNUM,
326 .match_col_num = ATTCOL_NAME,
328 .action = remove_attribute
330 ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
333 ret = for_each_matching_row(&pmd);
336 if (pmd.num_matches == 0)
339 lls_free_parse_result(aca->lpr, cmd);
343 static int com_rmatt(struct command_context *cc, struct lls_parse_result *lpr)
345 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
347 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
350 send_errctx(cc, errctx);
353 return send_lls_callback_request(com_rmatt_callback, cmd, lpr, cc);
355 EXPORT_SERVER_CMD_HANDLER(rmatt);
358 * Return a binary representation of the given attribute value.
360 * \param atts Pointer to the attribute value.
363 * This function prints a string of at most 64 characters plus the terminating
364 * \p NULL character into \a buf which must be provided by the caller and at
365 * least 65 bytes long. The "x" character is used for set attributes and "-" is
366 * used for unset attributes.
368 * In practice, not all 64 attributes are defined. In this case, the function
369 * only prints \a N + 1 characters where \a N is the greatest id of a defined
372 void get_attribute_bitmap(const uint64_t *atts, char *buf)
375 const uint64_t one = 1;
377 for (i = 0; i <= greatest_att_bitnum; i++)
378 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
383 * Get a string containing the set attributes in text form.
385 * \param atts The attribute bitmap.
386 * \param delim The delimiter to separate matching attribute names.
387 * \param text Result pointer.
389 * \return Positive on success, negative on errors. If no attributes have
390 * been defined, \a *text is NULL.
392 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
395 const uint64_t one = 1;
398 if (greatest_att_bitnum < 0) { /* no attributes available */
399 *text = para_strdup("(no attributes available)");
402 for (i = 0; i <= greatest_att_bitnum; i++) {
403 unsigned char bn = i;
404 struct osl_object obj = {.data = &bn, .size = 1};
407 if (!(*atts & (one << i)))
409 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
412 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
416 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
420 *text = para_strdup(obj.data);
422 if (!*text) /* no attributes set */
423 *text = para_strdup("");
430 static int att_logical_or(struct osl_row *row, void *data)
432 uint64_t *att_mask = data, one = 1;
433 struct osl_object bitnum_obj;
434 int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
438 *att_mask |= one << *(unsigned char *)bitnum_obj.data;
443 * Compute the attribute bit mask and check each afs info bitmap.
445 * \param aca The query field of \a aca is ignored.
447 * This iterates over all attributes in the attribute table and computes the
448 * logical or of 1 << b where b is the bit number of the attribute. The
449 * resulting bit mask is passed to aft_check_attributes() which performs the
454 * \sa \ref aft_check_attributes().
456 int attribute_check_callback(struct afs_callback_arg *aca)
459 uint64_t att_mask = 0; /* bits corresponding to a attributes */
461 ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
464 PARA_ERROR_LOG("attribute table loop failed: %s\n",
465 para_strerror(-ret));
468 return aft_check_attributes(att_mask, &aca->pbout);
472 * Close the attribute table.
474 * \sa osl_close_table().
476 static void attribute_close(void)
478 osl_close_table(attribute_table, OSL_MARK_CLEAN);
479 attribute_table = NULL;
483 * Open the attribute table.
485 * \param dir The database directory.
487 * \return Positive on success, negative on errors.
489 * \sa osl_open_table().
491 static int attribute_open(const char *dir)
495 attribute_table_desc.dir = dir;
496 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
497 greatest_att_bitnum = -1; /* no atts available */
499 find_greatest_att_bitnum();
502 attribute_table = NULL;
503 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
508 static int attribute_create(const char *dir)
510 attribute_table_desc.dir = dir;
511 return osl(osl_create_table(&attribute_table_desc));
515 * Initialize the attribute table structure.
517 * \param t The table structure to initialize.
519 void attribute_init(struct afs_table *t)
521 t->open = attribute_open;
522 t->close = attribute_close;
523 t->create = attribute_create;