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)
182 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADDATT);
187 ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
189 num_inputs = lls_num_inputs(aca->lpr);
190 for (i = 0; i < num_inputs; i++) {
191 const char *name = lls_input(i, aca->lpr);
192 struct osl_object objs[NUM_ATT_COLUMNS];
194 unsigned char bitnum;
195 struct addatt_event_data aed;
198 if (len == 0 || name[len - 1] == '-' || name[len - 1] == '+') {
199 para_printf(&aca->pbout,
200 "invalid attribute name: %s\n", name);
203 ret = get_attribute_bitnum_by_name(name, &bitnum);
205 para_printf(&aca->pbout,
206 "attribute \"%s\" already exists\n", name);
209 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
211 objs[ATTCOL_BITNUM].size = 1;
212 /* find smallest unused attribute */
213 for (bitnum = 0; bitnum < 64; bitnum++) {
214 objs[ATTCOL_BITNUM].data = &bitnum;
215 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
216 &objs[ATTCOL_BITNUM], &row));
217 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
218 break; /* this bitnum is unused, use it */
219 if (ret < 0) /* error */
221 /* this bit is already in use, try next bit */
224 ret = -E_ATT_TABLE_FULL;
227 objs[ATTCOL_NAME].data = (char *)name;
228 objs[ATTCOL_NAME].size = len + 1;
229 ret = osl(osl_add_row(attribute_table, objs));
234 ret = afs_event(ATTRIBUTE_ADD, &aca->pbout, &aed);
237 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
241 para_printf(&aca->pbout, "error while adding %s\n",
242 lls_input(i, aca->lpr));
246 static int com_addatt(struct command_context *cc, struct lls_parse_result *lpr)
248 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADDATT);
250 int ret = lls(lls_check_arg_count(lpr, 1, 64, &errctx));
253 send_errctx(cc, errctx);
256 return send_lls_callback_request(com_addatt_callback, cmd, lpr, cc);
258 EXPORT_SERVER_CMD_HANDLER(addatt);
260 static int com_mvatt_callback(struct afs_callback_arg *aca)
262 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
263 const char *old, *new;
264 struct osl_object obj;
268 ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
270 old = lls_input(0, aca->lpr);
271 new = lls_input(1, aca->lpr);
272 obj.data = (char *)old;
273 obj.size = strlen(old) + 1;
274 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
277 obj.data = (char *)new;
278 obj.size = strlen(new) + 1;
279 /* The update fails if the destination attribute exists. */
280 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
283 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
285 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
286 lls_free_parse_result(aca->lpr, cmd);
290 static int com_mvatt(struct command_context *cc, struct lls_parse_result *lpr)
292 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
294 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
296 send_errctx(cc, errctx);
299 return send_lls_callback_request(com_mvatt_callback, cmd, lpr, cc);
301 EXPORT_SERVER_CMD_HANDLER(mvatt);
303 static int remove_attribute(struct osl_table *table, struct osl_row *row,
304 const char *name, void *data)
306 struct afs_callback_arg *aca = data;
308 struct rmatt_event_data red = {.name = name};
310 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
312 para_printf(&aca->pbout, "cannot remove %s\n", name);
315 para_printf(&aca->pbout, "removing attribute %s\n", name);
316 ret = osl(osl_del_row(table, row));
318 para_printf(&aca->pbout, "cannot remove %s\n", name);
321 return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
324 static int com_rmatt_callback(struct afs_callback_arg *aca)
326 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
328 struct pattern_match_data pmd = {
329 .table = attribute_table,
330 .loop_col_num = ATTCOL_BITNUM,
331 .match_col_num = ATTCOL_NAME,
333 .action = remove_attribute
335 ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
338 ret = for_each_matching_row(&pmd);
341 if (pmd.num_matches == 0)
344 lls_free_parse_result(aca->lpr, cmd);
348 static int com_rmatt(struct command_context *cc, struct lls_parse_result *lpr)
350 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(RMATT);
352 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
355 send_errctx(cc, errctx);
358 return send_lls_callback_request(com_rmatt_callback, cmd, lpr, cc);
360 EXPORT_SERVER_CMD_HANDLER(rmatt);
363 * Return a binary representation of the given attribute value.
365 * \param atts Pointer to the attribute value.
368 * This function prints a string of at most 64 characters plus the terminating
369 * \p NULL character into \a buf which must be provided by the caller and at
370 * least 65 bytes long. The "x" character is used for set attributes and "-" is
371 * used for unset attributes.
373 * In practice, not all 64 attributes are defined. In this case, the function
374 * only prints \a N + 1 characters where \a N is the greatest id of a defined
377 void get_attribute_bitmap(const uint64_t *atts, char *buf)
380 const uint64_t one = 1;
382 for (i = 0; i <= greatest_att_bitnum; i++)
383 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
388 * Get a string containing the set attributes in text form.
390 * \param atts The attribute bitmap.
391 * \param delim The delimiter to separate matching attribute names.
392 * \param text Result pointer.
394 * \return Positive on success, negative on errors. If no attributes have
395 * been defined, \a *text is NULL.
397 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
400 const uint64_t one = 1;
403 if (greatest_att_bitnum < 0) { /* no attributes available */
404 *text = para_strdup("(no attributes available)");
407 for (i = 0; i <= greatest_att_bitnum; i++) {
408 unsigned char bn = i;
409 struct osl_object obj = {.data = &bn, .size = 1};
412 if (!(*atts & (one << i)))
414 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
417 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
421 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
425 *text = para_strdup(obj.data);
427 if (!*text) /* no attributes set */
428 *text = para_strdup("");
435 static int att_logical_or(struct osl_row *row, void *data)
437 uint64_t *att_mask = data, one = 1;
438 struct osl_object bitnum_obj;
439 int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
443 *att_mask |= one << *(unsigned char *)bitnum_obj.data;
448 * Compute the attribute bit mask and check each afs info bitmap.
450 * \param aca The query field of \a aca is ignored.
452 * This iterates over all attributes in the attribute table and computes the
453 * logical or of 1 << b where b is the bit number of the attribute. The
454 * resulting bit mask is passed to aft_check_attributes() which performs the
459 * \sa \ref aft_check_attributes().
461 int attribute_check_callback(struct afs_callback_arg *aca)
464 uint64_t att_mask = 0; /* bits corresponding to a attributes */
466 ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
469 PARA_ERROR_LOG("attribute table loop failed: %s\n",
470 para_strerror(-ret));
473 return aft_check_attributes(att_mask, &aca->pbout);
477 * Close the attribute table.
479 * \sa osl_close_table().
481 static void attribute_close(void)
483 osl_close_table(attribute_table, OSL_MARK_CLEAN);
484 attribute_table = NULL;
488 * Open the attribute table.
490 * \param dir The database directory.
492 * \return Positive on success, negative on errors.
494 * \sa osl_open_table().
496 static int attribute_open(const char *dir)
500 attribute_table_desc.dir = dir;
501 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
502 greatest_att_bitnum = -1; /* no atts available */
504 find_greatest_att_bitnum();
507 attribute_table = NULL;
508 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
513 static int attribute_create(const char *dir)
515 attribute_table_desc.dir = dir;
516 return osl(osl_create_table(&attribute_table_desc));
520 * Initialize the attribute table structure.
522 * \param t The table structure to initialize.
524 void attribute_init(struct afs_table *t)
526 t->open = attribute_open;
527 t->close = attribute_close;
528 t->create = attribute_create;