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 static int print_attribute(struct osl_table
*table
, struct osl_row
*row
,
125 const char *name
, void *data
)
127 struct afs_callback_arg
*aca
= data
;
128 unsigned flags
= *(unsigned *)aca
->query
.data
;
129 struct osl_object bitnum_obj
;
132 if (!(flags
& LSATT_FLAG_LONG
)) {
133 para_printf(&aca
->pbout
, "%s\n", name
);
136 ret
= osl(osl_get_object(table
, row
, ATTCOL_BITNUM
, &bitnum_obj
));
138 para_printf(&aca
->pbout
, "%s: %s\n", name
, para_strerror(-ret
));
141 para_printf(&aca
->pbout
, "%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
146 static int com_lsatt_callback(struct afs_callback_arg
*aca
)
148 unsigned flags
= *(unsigned *)aca
->query
.data
;
150 struct pattern_match_data pmd
= {
151 .table
= attribute_table
,
152 .loop_col_num
= ATTCOL_NAME
,
153 .match_col_num
= ATTCOL_NAME
,
154 .patterns
= {.data
= (char *)aca
->query
.data
+ sizeof(flags
),
155 .size
= aca
->query
.size
- sizeof(flags
)},
156 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
,
158 .action
= print_attribute
160 if (flags
& LSATT_FLAG_SORT_BY_ID
)
161 pmd
.loop_col_num
= ATTCOL_BITNUM
;
162 if (flags
& LSATT_FLAG_REVERSE
)
163 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
164 ret
= for_each_matching_row(&pmd
);
167 if (pmd
.num_matches
== 0)
173 int com_lsatt(struct command_context
*cc
)
176 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
179 for (i
= 1; i
< cc
->argc
; i
++) {
180 const char *arg
= cc
->argv
[i
];
183 if (!strcmp(arg
, "--")) {
187 if (!strcmp(arg
, "-i")) {
188 flags
|= LSATT_FLAG_SORT_BY_ID
;
191 if (!strcmp(arg
, "-l")) {
192 flags
|= LSATT_FLAG_LONG
;
195 if (!strcmp(arg
, "-r")) {
196 flags
|= LSATT_FLAG_REVERSE
;
200 return send_option_arg_callback_request(&options
, cc
->argc
- i
, cc
->argv
+ i
,
201 com_lsatt_callback
, afs_cb_result_handler
, cc
);
204 struct addatt_event_data
{
206 unsigned char bitnum
;
210 static int com_addatt_callback(struct afs_callback_arg
*aca
)
218 p
< (char *)aca
->query
.data
+ aca
->query
.size
;
221 struct osl_object objs
[NUM_ATT_COLUMNS
];
223 unsigned char bitnum
;
224 struct addatt_event_data aed
;
227 if (!len
|| p
[len
- 1] == '-' || p
[len
- 1] == '+') {
228 para_printf(&aca
->pbout
, "invalid attribute name: %s\n", p
);
231 ret
= get_attribute_bitnum_by_name(p
, &bitnum
);
233 para_printf(&aca
->pbout
, "attribute \"%s\" already exists\n", p
);
236 if (ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
)) /* error */
238 objs
[ATTCOL_BITNUM
].size
= 1;
239 /* find smallest unused attribute */
240 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
241 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
242 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
,
243 &objs
[ATTCOL_BITNUM
], &row
));
244 if (ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
245 break; /* this bitnum is unused, use it */
246 if (ret
< 0) /* error */
248 /* this bit is already in use, try next bit */
251 ret
= -E_ATT_TABLE_FULL
;
254 objs
[ATTCOL_NAME
].data
= p
;
255 objs
[ATTCOL_NAME
].size
= len
+ 1;
256 ret
= osl(osl_add_row(attribute_table
, objs
));
261 ret
= afs_event(ATTRIBUTE_ADD
, &aca
->pbout
, &aed
);
264 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, (int)bitnum
);
268 para_printf(&aca
->pbout
, "%s: %s\n", p
, para_strerror(-ret
));
272 int com_addatt(struct command_context
*cc
)
277 return -E_ATTR_SYNTAX
;
278 ret
= send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
279 com_addatt_callback
, afs_cb_result_handler
, cc
);
281 send_strerror(cc
, -ret
);
285 static int com_mvatt_callback(struct afs_callback_arg
*aca
)
287 char *old
= aca
->query
.data
;
288 size_t size
= strlen(old
) + 1;
289 char *new = old
+ size
;
290 struct osl_object obj
= {.data
= old
, .size
= size
};
294 ret
= osl(osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
));
298 obj
.size
= strlen(new) + 1;
299 ret
= osl(osl_update_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
302 para_printf(&aca
->pbout
, "cannot rename %s to %s\n", old
, new);
304 ret
= afs_event(ATTRIBUTE_RENAME
, &aca
->pbout
, NULL
);
308 int com_mvatt(struct command_context
*cc
)
311 return -E_ATTR_SYNTAX
;
312 return send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
313 com_mvatt_callback
, afs_cb_result_handler
, cc
);
316 static int remove_attribute(struct osl_table
*table
, struct osl_row
*row
,
317 const char *name
, void *data
)
319 struct afs_callback_arg
*aca
= data
;
321 struct rmatt_event_data red
= {.name
= name
};
323 ret
= get_attribute_bitnum_by_name(name
, &red
.bitnum
);
325 para_printf(&aca
->pbout
, "cannot remove %s\n", name
);
328 para_printf(&aca
->pbout
, "removing attribute %s\n", name
);
329 ret
= osl(osl_del_row(table
, row
));
331 para_printf(&aca
->pbout
, "cannot remove %s\n", name
);
334 return afs_event(ATTRIBUTE_REMOVE
, &aca
->pbout
, &red
);
337 static int com_rmatt_callback(struct afs_callback_arg
*aca
)
340 struct pattern_match_data pmd
= {
341 .table
= attribute_table
,
342 .patterns
= aca
->query
,
343 .loop_col_num
= ATTCOL_BITNUM
,
344 .match_col_num
= ATTCOL_NAME
,
346 .action
= remove_attribute
348 ret
= for_each_matching_row(&pmd
);
351 if (pmd
.num_matches
== 0)
357 int com_rmatt(struct command_context
*cc
)
360 return -E_ATTR_SYNTAX
;
361 return send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
362 com_rmatt_callback
, afs_cb_result_handler
, cc
);
366 * Return a binary representation of the given attribute value.
368 * \param atts Pointer to the attribute value.
371 * This function prints a string of at most 64 characters plus the terminating
372 * \p NULL character into \a buf which must be provided by the caller and at
373 * least 65 bytes long. The "x" character is used for set attributes and "-" is
374 * used for unset attributes.
376 * In practice, not all 64 attributes are defined. In this case, the function
377 * only prints \a N + 1 characters where \a N is the greatest id of a defined
380 void get_attribute_bitmap(const uint64_t *atts
, char *buf
)
383 const uint64_t one
= 1;
385 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
386 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
391 * Get a string containing the set attributes in text form.
393 * \param atts The attribute bitmap.
394 * \param delim The delimiter to separate matching attribute names.
395 * \param text Result pointer.
397 * \return Positive on success, negative on errors. If no attributes have
398 * been defined, \a *text is NULL.
400 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
403 const uint64_t one
= 1;
406 if (greatest_att_bitnum
< 0) { /* no attributes available */
407 *text
= para_strdup("(no attributes available)");
410 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
411 unsigned char bn
= i
;
412 struct osl_object obj
= {.data
= &bn
, .size
= 1};
415 if (!(*atts
& (one
<< i
)))
417 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
));
420 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
424 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
428 *text
= para_strdup(obj
.data
);
430 if (!*text
) /* no attributes set */
431 *text
= para_strdup("");
438 static int att_logical_or(struct osl_row
*row
, void *data
)
440 uint64_t *att_mask
= data
, one
= 1;
441 struct osl_object bitnum_obj
;
442 int ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
, &bitnum_obj
);
446 *att_mask
|= one
<< *(unsigned char *)bitnum_obj
.data
;
451 * Compute the attribute bit mask and check each afs info bitmap.
453 * \param aca The query field of \a aca is ignored.
455 * This iterates over all attributes in the attribute table and computes the
456 * logical or of 1 << b where b is the bit number of the attribute. The
457 * resulting bit mask is passed to aft_check_attributes() which performs the
462 * \sa \ref aft_check_attributes().
464 int attribute_check_callback(struct afs_callback_arg
*aca
)
467 uint64_t att_mask
= 0; /* bits corresponding to a attributes */
469 ret
= osl_rbtree_loop(attribute_table
, ATTCOL_BITNUM
, &att_mask
,
472 PARA_ERROR_LOG("attribute table loop failed: %s\n",
473 para_strerror(-ret
));
476 return aft_check_attributes(att_mask
, &aca
->pbout
);
480 * Close the attribute table.
482 * \sa osl_close_table().
484 static void attribute_close(void)
486 osl_close_table(attribute_table
, OSL_MARK_CLEAN
);
487 attribute_table
= NULL
;
491 * Open the attribute table.
493 * \param dir The database directory.
495 * \return Positive on success, negative on errors.
497 * \sa osl_open_table().
499 static int attribute_open(const char *dir
)
503 attribute_table_desc
.dir
= dir
;
504 ret
= osl(osl_open_table(&attribute_table_desc
, &attribute_table
));
505 greatest_att_bitnum
= -1; /* no atts available */
507 find_greatest_att_bitnum();
510 attribute_table
= NULL
;
511 if (ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
516 static int attribute_create(const char *dir
)
518 attribute_table_desc
.dir
= dir
;
519 return osl(osl_create_table(&attribute_table_desc
));
523 * Initialize the attribute table structure.
525 * \param t The table structure to initialize.
527 void attribute_init(struct afs_table
*t
)
529 t
->open
= attribute_open
;
530 t
->close
= attribute_close
;
531 t
->create
= attribute_create
;