2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
9 #include <openssl/rc4.h>
21 static struct osl_table
*attribute_table
;
22 static int greatest_att_bitnum
;
24 /** The columns of the attribute table. */
25 enum attribute_table_columns
{
26 /** The bit number (0-63). */
28 /** The name of the attribute. */
30 /** Number of columns in this table. */
34 static int char_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
36 const unsigned char *c1
= (const unsigned char*)obj1
->data
;
37 const unsigned char *c2
= (const unsigned char*)obj2
->data
;
45 static struct osl_column_description att_cols
[] = {
47 .storage_type
= OSL_MAPPED_STORAGE
,
48 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
50 .compare_function
= char_compare
,
54 .storage_type
= OSL_MAPPED_STORAGE
,
55 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
57 .compare_function
= string_compare
,
61 static struct osl_table_description attribute_table_desc
= {
63 .num_columns
= NUM_ATT_COLUMNS
,
65 .column_descriptions
= att_cols
68 static void find_greatest_att_bitnum(void)
73 struct osl_object obj
= {.data
= &c
, .size
= 1};
74 if (osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
,
76 greatest_att_bitnum
= c
;
80 PARA_INFO_LOG("no attributes\n");
81 greatest_att_bitnum
= -E_NO_ATTRIBUTES
;
85 * Retrieve the identifier (number) of an attribute.
87 * \param att_name The name of the attribute.
88 * \param bitnum Result pointer.
90 * \return Positive on success, negative on errors.
92 int get_attribute_bitnum_by_name(const char *att_name
, unsigned char *bitnum
)
94 struct osl_object obj
= {.data
= (char *)att_name
,
95 .size
= strlen(att_name
) + 1};
97 int ret
= osl(osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
));
101 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
, &obj
));
104 *bitnum
= *(unsigned char *)obj
.data
;
109 * Flags used by the lsatt command.
111 * \param \sa com_lsatt().
114 /** Whether "-a" was given for the lsatt command. */
115 LSATT_FLAG_SORT_BY_ID
= 1,
116 /** Whether "-l" was given for the lsatt command. */
118 /** Reverse sort order. */
119 LSATT_FLAG_REVERSE
= 4
122 /** Data passed to the action function of lsatt */
123 struct lsatt_action_data
{
124 /** The result buffer. */
125 struct para_buffer pb
;
126 /** The given flags for the lsatt command. */
130 static int print_attribute(struct osl_table
*table
, struct osl_row
*row
,
131 const char *name
, void *data
)
133 struct lsatt_action_data
*laad
= data
;
134 struct osl_object bitnum_obj
;
137 if (!(laad
->flags
& LSATT_FLAG_LONG
))
138 return para_printf(&laad
->pb
, "%s\n", name
);
139 ret
= osl(osl_get_object(table
, row
, ATTCOL_BITNUM
, &bitnum_obj
));
141 para_printf(&laad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
144 return para_printf(&laad
->pb
, "%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
148 static void com_lsatt_callback(int fd
, const struct osl_object
*query
)
150 struct lsatt_action_data laad
= {
151 .flags
= *(unsigned *) query
->data
,
155 .max_size_handler
= pass_buffer_as_shm
159 struct pattern_match_data pmd
= {
160 .table
= attribute_table
,
161 .loop_col_num
= ATTCOL_BITNUM
,
162 .match_col_num
= ATTCOL_NAME
,
163 .patterns
= {.data
= (char *)query
->data
+ sizeof(laad
.flags
),
164 .size
= query
->size
- sizeof(laad
.flags
)},
165 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
,
167 .action
= print_attribute
169 if (laad
.flags
& LSATT_FLAG_SORT_BY_ID
)
170 pmd
.loop_col_num
= ATTCOL_NAME
;
171 if (laad
.flags
& LSATT_FLAG_REVERSE
)
172 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
173 for_each_matching_row(&pmd
);
175 pass_buffer_as_shm(laad
.pb
.buf
, laad
.pb
.offset
, &fd
);
179 int com_lsatt(struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
182 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
185 for (i
= 1; i
< argc
; i
++) {
186 const char *arg
= argv
[i
];
189 if (!strcmp(arg
, "--")) {
193 if (!strcmp(arg
, "-i")) {
194 flags
|= LSATT_FLAG_SORT_BY_ID
;
197 if (!strcmp(arg
, "-l")) {
198 flags
|= LSATT_FLAG_LONG
;
201 if (!strcmp(arg
, "-r")) {
202 flags
|= LSATT_FLAG_REVERSE
;
206 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
207 com_lsatt_callback
, rc4_send_result
, rc4c
);
210 ret
= rc4_send_va_buffer(rc4c
, "no matches\n");
212 rc4_send_va_buffer(rc4c
, "%s\n", para_strerror(-ret
));
216 static void com_setatt_callback(__a_unused
int fd
, const struct osl_object
*query
)
219 uint64_t add_mask
= 0, del_mask
= 0;
222 struct osl_object obj
;
225 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
229 ret
= -E_ATTR_SYNTAX
;
233 if (c
!= '+' && c
!= '-')
238 ret
= osl(osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
));
241 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
,
246 add_mask
|= (1UL << *(unsigned char *)obj
.data
);
248 del_mask
|= (1UL << *(unsigned char *)obj
.data
);
250 ret
= -E_ATTR_SYNTAX
;
251 if (!add_mask
&& !del_mask
)
253 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask
,
254 (long long unsigned)del_mask
);
255 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) { /* TODO: fnmatch */
256 struct afs_info old_afsi
, new_afsi
;
257 struct afsi_change_event_data aced
= {.old_afsi
= &old_afsi
};
260 ret
= aft_get_row_of_path(p
, &aced
.aft_row
);
263 ret
= get_afsi_object_of_row(aced
.aft_row
, &obj
);
266 ret
= load_afsi(&old_afsi
, &obj
);
270 new_afsi
.attributes
|= add_mask
;
271 new_afsi
.attributes
&= ~del_mask
;
272 save_afsi(&new_afsi
, &obj
); /* in-place update */
273 afs_event(AFSI_CHANGE
, NULL
, &aced
);
277 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
280 int com_setatt(__a_unused
struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
283 return -E_ATTR_SYNTAX
;
284 return send_standard_callback_request(argc
- 1, argv
+ 1, com_setatt_callback
,
288 struct addatt_event_data
{
290 unsigned char bitnum
;
294 static void com_addatt_callback(int fd
, const struct osl_object
*query
)
297 int ret
= 1, ret2
= 0;
298 struct para_buffer pb
= {
301 .max_size_handler
= pass_buffer_as_shm
305 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
306 struct osl_object objs
[NUM_ATT_COLUMNS
];
308 unsigned char bitnum
;
309 struct addatt_event_data aed
;
312 if (!len
|| p
[len
- 1] == '-' || p
[len
- 1] == '+') {
313 ret2
= para_printf(&pb
, "invalid attribute name: %s\n", p
);
318 ret
= get_attribute_bitnum_by_name(p
, &bitnum
);
320 ret2
= para_printf(&pb
, "attribute \"%s\" already exists\n", p
);
325 if (ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
)) /* error */
327 objs
[ATTCOL_BITNUM
].size
= 1;
328 /* find smallest unused attribute */
329 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
330 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
331 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
,
332 &objs
[ATTCOL_BITNUM
], &row
));
333 if (ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
334 break; /* this bitnum is unused, use it */
335 if (ret
< 0) /* error */
337 /* this bit is already in use, try next bit */
340 ret
= -E_ATT_TABLE_FULL
;
343 objs
[ATTCOL_NAME
].data
= p
;
344 objs
[ATTCOL_NAME
].size
= len
+ 1;
345 ret
= osl(osl_add_row(attribute_table
, objs
));
350 afs_event(ATTRIBUTE_ADD
, &pb
, &aed
);
351 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, (int)bitnum
);
354 if (ret
< 0 && ret2
>= 0)
355 para_printf(&pb
, "%s: %s\n", p
, para_strerror(-ret
));
357 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
361 int com_addatt(struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
366 return -E_ATTR_SYNTAX
;
367 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_addatt_callback
,
368 rc4_send_result
, rc4c
);
370 rc4_send_va_buffer(rc4c
, "%s\n", para_strerror(-ret
));
374 static void com_mvatt_callback(int fd
, const struct osl_object
*query
)
376 char *old
= query
->data
;
377 size_t size
= strlen(old
) + 1;
378 char *new = old
+ size
;
379 struct osl_object obj
= {.data
= old
, .size
= size
};
381 struct para_buffer pb
= {
384 .max_size_handler
= pass_buffer_as_shm
388 ret
= osl(osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
));
392 obj
.size
= strlen(new) + 1;
393 ret
= osl(osl_update_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
396 para_printf(&pb
, "%s\n", para_strerror(-ret
));
398 afs_event(ATTRIBUTE_RENAME
, &pb
, NULL
);
400 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
404 int com_mvatt(struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
409 return -E_ATTR_SYNTAX
;
410 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_mvatt_callback
,
411 rc4_send_result
, rc4c
);
413 rc4_send_va_buffer(rc4c
, "%s\n", para_strerror(-ret
));
417 /** Data passed to the action handler of com_rmatt(). */
418 struct remove_attribute_action_data
{
419 /** Message buffer. */
420 struct para_buffer pb
;
421 /** Numver of attributes removed. */
423 /** Bitwise "or" of the removed attributes. */
424 uint64_t mask_of_removed_atts
;
427 static int remove_attribute(struct osl_table
*table
, struct osl_row
*row
,
428 const char *name
, void *data
)
430 struct remove_attribute_action_data
*raad
= data
;
432 struct rmatt_event_data red
= {.name
= name
};
434 ret
= get_attribute_bitnum_by_name(name
, &red
.bitnum
);
436 return para_printf(&raad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
437 ret
= osl(osl_del_row(table
, row
));
439 return para_printf(&raad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
440 ret
= para_printf(&raad
->pb
, "removed attribute %s\n", name
);
442 raad
->mask_of_removed_atts
|= (1 << red
.bitnum
);
443 afs_event(ATTRIBUTE_REMOVE
, &raad
->pb
, &red
);
447 static void com_rmatt_callback(int fd
, const struct osl_object
*query
)
449 struct remove_attribute_action_data raad
= {
454 .max_size_handler
= pass_buffer_as_shm
458 struct pattern_match_data pmd
= {
459 .table
= attribute_table
,
461 .loop_col_num
= ATTCOL_BITNUM
,
462 .match_col_num
= ATTCOL_NAME
,
464 .action
= remove_attribute
466 ret
= for_each_matching_row(&pmd
);
468 ret2
= para_printf(&raad
.pb
, "%s\n", para_strerror(-ret
));
469 else if (!raad
.num_removed
)
470 ret2
= para_printf(&raad
.pb
, "no match -- nothing removed\n");
471 if (ret2
>= 0 && raad
.pb
.offset
)
472 pass_buffer_as_shm(raad
.pb
.buf
, raad
.pb
.offset
, &fd
);
476 int com_rmatt(struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
481 return -E_ATTR_SYNTAX
;
482 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_rmatt_callback
,
483 rc4_send_result
, rc4c
);
485 rc4_send_va_buffer(rc4c
, "%s\n", para_strerror(-ret
));
490 * Return a binary representation of the given attribute value.
492 * \param atts Pointer to the attribute value.
495 * This function prints a string of at most 64 characters plus the terminating
496 * \p NULL character into \a buf which must be provided by the caller and at
497 * least 65 bytes long. The "x" character is used for set attributes and "-" is
498 * used for unset attributes.
500 * In practice, not all 64 attributes are defined. In this case, the function
501 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
504 void get_attribute_bitmap(const uint64_t *atts
, char *buf
)
507 const uint64_t one
= 1;
509 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
510 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
515 * Get a string containing the set attributes in text form.
517 * \param atts The attribute bitmap.
518 * \param delim The delimiter to separate matching attribute names.
519 * \param text Result pointer.
521 * \return Positive on success, negative on errors. If no attributes have
522 * been defined, \a *text is NULL.
524 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
527 const uint64_t one
= 1;
530 if (greatest_att_bitnum
< 0) { /* no attributes available */
531 *text
= para_strdup("(no attributes available)");
534 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
535 unsigned char bn
= i
;
536 struct osl_object obj
= {.data
= &bn
, .size
= 1};
539 if (!(*atts
& (one
<< i
)))
541 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
));
544 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
548 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
552 *text
= para_strdup(obj
.data
);
554 if (!*text
) /* no attributes set */
555 *text
= para_strdup("");
563 * Close the attribute table.
565 * \sa osl_close_table().
567 void attribute_close(void)
569 osl_close_table(attribute_table
, OSL_MARK_CLEAN
);
570 attribute_table
= NULL
;
574 * Open the attribute table.
576 * \param dir The database directory.
578 * \return Positive on success, negative on errors.
580 * \sa osl_open_table().
582 static int attribute_open(const char *dir
)
586 attribute_table_desc
.dir
= dir
;
587 ret
= osl(osl_open_table(&attribute_table_desc
, &attribute_table
));
588 greatest_att_bitnum
= -1; /* no atts available */
590 find_greatest_att_bitnum();
593 attribute_table
= NULL
;
594 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
599 static int attribute_create(const char *dir
)
601 attribute_table_desc
.dir
= dir
;
602 return osl(osl_create_table(&attribute_table_desc
));
606 * Initialize the attribute table structure.
608 * \param t The table structure to initialize.
610 void attribute_init(struct afs_table
*t
)
612 t
->name
= attribute_table_desc
.name
;
613 t
->open
= attribute_open
;
614 t
->close
= attribute_close
;
615 t
->create
= attribute_create
;