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 char *old = aca->query.data;
258 size_t size = strlen(old) + 1;
259 char *new = old + size;
260 struct osl_object obj = {.data = old, .size = size};
264 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
268 obj.size = strlen(new) + 1;
269 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
272 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
274 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
278 int com_mvatt(struct command_context *cc)
281 return -E_ATTR_SYNTAX;
282 return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
283 com_mvatt_callback, afs_cb_result_handler, cc);
286 static int remove_attribute(struct osl_table *table, struct osl_row *row,
287 const char *name, void *data)
289 struct afs_callback_arg *aca = data;
291 struct rmatt_event_data red = {.name = name};
293 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
295 para_printf(&aca->pbout, "cannot remove %s\n", name);
298 para_printf(&aca->pbout, "removing attribute %s\n", name);
299 ret = osl(osl_del_row(table, row));
301 para_printf(&aca->pbout, "cannot remove %s\n", name);
304 return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
307 static int com_rmatt_callback(struct afs_callback_arg *aca)
310 struct pattern_match_data pmd = {
311 .table = attribute_table,
312 .patterns = aca->query,
313 .loop_col_num = ATTCOL_BITNUM,
314 .match_col_num = ATTCOL_NAME,
316 .action = remove_attribute
318 ret = for_each_matching_row(&pmd);
321 if (pmd.num_matches == 0)
327 int com_rmatt(struct command_context *cc)
330 return -E_ATTR_SYNTAX;
331 return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
332 com_rmatt_callback, afs_cb_result_handler, cc);
336 * Return a binary representation of the given attribute value.
338 * \param atts Pointer to the attribute value.
341 * This function prints a string of at most 64 characters plus the terminating
342 * \p NULL character into \a buf which must be provided by the caller and at
343 * least 65 bytes long. The "x" character is used for set attributes and "-" is
344 * used for unset attributes.
346 * In practice, not all 64 attributes are defined. In this case, the function
347 * only prints \a N + 1 characters where \a N is the greatest id of a defined
350 void get_attribute_bitmap(const uint64_t *atts, char *buf)
353 const uint64_t one = 1;
355 for (i = 0; i <= greatest_att_bitnum; i++)
356 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
361 * Get a string containing the set attributes in text form.
363 * \param atts The attribute bitmap.
364 * \param delim The delimiter to separate matching attribute names.
365 * \param text Result pointer.
367 * \return Positive on success, negative on errors. If no attributes have
368 * been defined, \a *text is NULL.
370 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
373 const uint64_t one = 1;
376 if (greatest_att_bitnum < 0) { /* no attributes available */
377 *text = para_strdup("(no attributes available)");
380 for (i = 0; i <= greatest_att_bitnum; i++) {
381 unsigned char bn = i;
382 struct osl_object obj = {.data = &bn, .size = 1};
385 if (!(*atts & (one << i)))
387 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
390 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
394 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
398 *text = para_strdup(obj.data);
400 if (!*text) /* no attributes set */
401 *text = para_strdup("");
408 static int att_logical_or(struct osl_row *row, void *data)
410 uint64_t *att_mask = data, one = 1;
411 struct osl_object bitnum_obj;
412 int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
416 *att_mask |= one << *(unsigned char *)bitnum_obj.data;
421 * Compute the attribute bit mask and check each afs info bitmap.
423 * \param aca The query field of \a aca is ignored.
425 * This iterates over all attributes in the attribute table and computes the
426 * logical or of 1 << b where b is the bit number of the attribute. The
427 * resulting bit mask is passed to aft_check_attributes() which performs the
432 * \sa \ref aft_check_attributes().
434 int attribute_check_callback(struct afs_callback_arg *aca)
437 uint64_t att_mask = 0; /* bits corresponding to a attributes */
439 ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
442 PARA_ERROR_LOG("attribute table loop failed: %s\n",
443 para_strerror(-ret));
446 return aft_check_attributes(att_mask, &aca->pbout);
450 * Close the attribute table.
452 * \sa osl_close_table().
454 static void attribute_close(void)
456 osl_close_table(attribute_table, OSL_MARK_CLEAN);
457 attribute_table = NULL;
461 * Open the attribute table.
463 * \param dir The database directory.
465 * \return Positive on success, negative on errors.
467 * \sa osl_open_table().
469 static int attribute_open(const char *dir)
473 attribute_table_desc.dir = dir;
474 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
475 greatest_att_bitnum = -1; /* no atts available */
477 find_greatest_att_bitnum();
480 attribute_table = NULL;
481 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
486 static int attribute_create(const char *dir)
488 attribute_table_desc.dir = dir;
489 return osl(osl_create_table(&attribute_table_desc));
493 * Initialize the attribute table structure.
495 * \param t The table structure to initialize.
497 void attribute_init(struct afs_table *t)
499 t->open = attribute_open;
500 t->close = attribute_close;
501 t->create = attribute_create;