2 * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
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
,
153 .max_size
= shm_get_shmmax(),
154 .private_data
= &(struct afs_max_size_handler_data
) {
157 .max_size_handler
= afs_max_size_handler
161 struct pattern_match_data pmd
= {
162 .table
= attribute_table
,
163 .loop_col_num
= ATTCOL_BITNUM
,
164 .match_col_num
= ATTCOL_NAME
,
165 .patterns
= {.data
= (char *)query
->data
+ sizeof(laad
.flags
),
166 .size
= query
->size
- sizeof(laad
.flags
)},
167 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
,
169 .action
= print_attribute
171 if (laad
.flags
& LSATT_FLAG_SORT_BY_ID
)
172 pmd
.loop_col_num
= ATTCOL_NAME
;
173 if (laad
.flags
& LSATT_FLAG_REVERSE
)
174 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
175 for_each_matching_row(&pmd
);
177 pass_buffer_as_shm(laad
.pb
.buf
, laad
.pb
.offset
, &fd
);
181 int com_lsatt(struct command_context
*cc
)
184 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
187 for (i
= 1; i
< cc
->argc
; i
++) {
188 const char *arg
= cc
->argv
[i
];
191 if (!strcmp(arg
, "--")) {
195 if (!strcmp(arg
, "-i")) {
196 flags
|= LSATT_FLAG_SORT_BY_ID
;
199 if (!strcmp(arg
, "-l")) {
200 flags
|= LSATT_FLAG_LONG
;
203 if (!strcmp(arg
, "-r")) {
204 flags
|= LSATT_FLAG_REVERSE
;
208 ret
= send_option_arg_callback_request(&options
, cc
->argc
- i
, cc
->argv
+ i
,
209 com_lsatt_callback
, afs_cb_result_handler
, cc
);
212 ret
= sc_send_va_buffer(&cc
->scc
, "no matches\n");
214 sc_send_va_buffer(&cc
->scc
, "%s\n", para_strerror(-ret
));
218 static void com_setatt_callback(__a_unused
int fd
, const struct osl_object
*query
)
221 uint64_t add_mask
= 0, del_mask
= 0;
224 struct osl_object obj
;
227 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
231 ret
= -E_ATTR_SYNTAX
;
235 if (c
!= '+' && c
!= '-')
240 ret
= osl(osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
));
243 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
,
248 add_mask
|= (1UL << *(unsigned char *)obj
.data
);
250 del_mask
|= (1UL << *(unsigned char *)obj
.data
);
252 ret
= -E_ATTR_SYNTAX
;
253 if (!add_mask
&& !del_mask
)
255 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask
,
256 (long long unsigned)del_mask
);
257 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) { /* TODO: fnmatch */
258 struct afs_info old_afsi
, new_afsi
;
259 struct afsi_change_event_data aced
= {.old_afsi
= &old_afsi
};
262 ret
= aft_get_row_of_path(p
, &aced
.aft_row
);
265 ret
= get_afsi_object_of_row(aced
.aft_row
, &obj
);
268 ret
= load_afsi(&old_afsi
, &obj
);
272 new_afsi
.attributes
|= add_mask
;
273 new_afsi
.attributes
&= ~del_mask
;
274 save_afsi(&new_afsi
, &obj
); /* in-place update */
275 afs_event(AFSI_CHANGE
, NULL
, &aced
);
279 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
282 int com_setatt(struct command_context
*cc
)
285 return -E_ATTR_SYNTAX
;
286 return send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
287 com_setatt_callback
, NULL
, NULL
);
290 struct addatt_event_data
{
292 unsigned char bitnum
;
296 static void com_addatt_callback(int fd
, const struct osl_object
*query
)
299 int ret
= 1, ret2
= 0;
300 struct para_buffer pb
= {
301 .max_size
= shm_get_shmmax(),
302 .private_data
= &(struct afs_max_size_handler_data
) {
305 .max_size_handler
= afs_max_size_handler
309 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
310 struct osl_object objs
[NUM_ATT_COLUMNS
];
312 unsigned char bitnum
;
313 struct addatt_event_data aed
;
316 if (!len
|| p
[len
- 1] == '-' || p
[len
- 1] == '+') {
317 ret2
= para_printf(&pb
, "invalid attribute name: %s\n", p
);
322 ret
= get_attribute_bitnum_by_name(p
, &bitnum
);
324 ret2
= para_printf(&pb
, "attribute \"%s\" already exists\n", p
);
329 if (ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
)) /* error */
331 objs
[ATTCOL_BITNUM
].size
= 1;
332 /* find smallest unused attribute */
333 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
334 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
335 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
,
336 &objs
[ATTCOL_BITNUM
], &row
));
337 if (ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
338 break; /* this bitnum is unused, use it */
339 if (ret
< 0) /* error */
341 /* this bit is already in use, try next bit */
344 ret
= -E_ATT_TABLE_FULL
;
347 objs
[ATTCOL_NAME
].data
= p
;
348 objs
[ATTCOL_NAME
].size
= len
+ 1;
349 ret
= osl(osl_add_row(attribute_table
, objs
));
354 afs_event(ATTRIBUTE_ADD
, &pb
, &aed
);
355 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, (int)bitnum
);
358 if (ret
< 0 && ret2
>= 0)
359 para_printf(&pb
, "%s: %s\n", p
, para_strerror(-ret
));
361 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
365 int com_addatt(struct command_context
*cc
)
370 return -E_ATTR_SYNTAX
;
371 ret
= send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
372 com_addatt_callback
, afs_cb_result_handler
, cc
);
374 sc_send_va_buffer(&cc
->scc
, "%s\n", para_strerror(-ret
));
378 static void com_mvatt_callback(int fd
, const struct osl_object
*query
)
380 char *old
= query
->data
;
381 size_t size
= strlen(old
) + 1;
382 char *new = old
+ size
;
383 struct osl_object obj
= {.data
= old
, .size
= size
};
385 struct para_buffer pb
= {
386 .max_size
= shm_get_shmmax(),
387 .private_data
= &(struct afs_max_size_handler_data
) {
390 .max_size_handler
= afs_max_size_handler
,
394 ret
= osl(osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
));
398 obj
.size
= strlen(new) + 1;
399 ret
= osl(osl_update_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
402 para_printf(&pb
, "%s\n", para_strerror(-ret
));
404 afs_event(ATTRIBUTE_RENAME
, &pb
, NULL
);
406 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
410 int com_mvatt(struct command_context
*cc
)
415 return -E_ATTR_SYNTAX
;
416 ret
= send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
417 com_mvatt_callback
, afs_cb_result_handler
, cc
);
419 sc_send_va_buffer(&cc
->scc
, "%s\n", para_strerror(-ret
));
423 /** Data passed to the action handler of com_rmatt(). */
424 struct remove_attribute_action_data
{
425 /** Message buffer. */
426 struct para_buffer pb
;
427 /** Numver of attributes removed. */
429 /** Bitwise "or" of the removed attributes. */
430 uint64_t mask_of_removed_atts
;
433 static int remove_attribute(struct osl_table
*table
, struct osl_row
*row
,
434 const char *name
, void *data
)
436 struct remove_attribute_action_data
*raad
= data
;
438 struct rmatt_event_data red
= {.name
= name
};
440 ret
= get_attribute_bitnum_by_name(name
, &red
.bitnum
);
442 return para_printf(&raad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
443 ret
= osl(osl_del_row(table
, row
));
445 return para_printf(&raad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
446 ret
= para_printf(&raad
->pb
, "removed attribute %s\n", name
);
448 raad
->mask_of_removed_atts
|= (1 << red
.bitnum
);
449 afs_event(ATTRIBUTE_REMOVE
, &raad
->pb
, &red
);
453 static void com_rmatt_callback(int fd
, const struct osl_object
*query
)
455 struct remove_attribute_action_data raad
= {
458 .max_size
= shm_get_shmmax(),
459 .private_data
= &(struct afs_max_size_handler_data
) {
462 .max_size_handler
= afs_max_size_handler
,
466 struct pattern_match_data pmd
= {
467 .table
= attribute_table
,
469 .loop_col_num
= ATTCOL_BITNUM
,
470 .match_col_num
= ATTCOL_NAME
,
472 .action
= remove_attribute
474 ret
= for_each_matching_row(&pmd
);
476 ret2
= para_printf(&raad
.pb
, "%s\n", para_strerror(-ret
));
477 else if (!raad
.num_removed
)
478 ret2
= para_printf(&raad
.pb
, "no match -- nothing removed\n");
479 if (ret2
>= 0 && raad
.pb
.offset
)
480 pass_buffer_as_shm(raad
.pb
.buf
, raad
.pb
.offset
, &fd
);
484 int com_rmatt(struct command_context
*cc
)
489 return -E_ATTR_SYNTAX
;
490 ret
= send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
491 com_rmatt_callback
, afs_cb_result_handler
, cc
);
493 sc_send_va_buffer(&cc
->scc
, "%s\n", para_strerror(-ret
));
498 * Return a binary representation of the given attribute value.
500 * \param atts Pointer to the attribute value.
503 * This function prints a string of at most 64 characters plus the terminating
504 * \p NULL character into \a buf which must be provided by the caller and at
505 * least 65 bytes long. The "x" character is used for set attributes and "-" is
506 * used for unset attributes.
508 * In practice, not all 64 attributes are defined. In this case, the function
509 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
512 void get_attribute_bitmap(const uint64_t *atts
, char *buf
)
515 const uint64_t one
= 1;
517 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
518 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
523 * Get a string containing the set attributes in text form.
525 * \param atts The attribute bitmap.
526 * \param delim The delimiter to separate matching attribute names.
527 * \param text Result pointer.
529 * \return Positive on success, negative on errors. If no attributes have
530 * been defined, \a *text is NULL.
532 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
535 const uint64_t one
= 1;
538 if (greatest_att_bitnum
< 0) { /* no attributes available */
539 *text
= para_strdup("(no attributes available)");
542 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
543 unsigned char bn
= i
;
544 struct osl_object obj
= {.data
= &bn
, .size
= 1};
547 if (!(*atts
& (one
<< i
)))
549 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
));
552 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
556 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
560 *text
= para_strdup(obj
.data
);
562 if (!*text
) /* no attributes set */
563 *text
= para_strdup("");
571 * Close the attribute table.
573 * \sa osl_close_table().
575 static void attribute_close(void)
577 osl_close_table(attribute_table
, OSL_MARK_CLEAN
);
578 attribute_table
= NULL
;
582 * Open the attribute table.
584 * \param dir The database directory.
586 * \return Positive on success, negative on errors.
588 * \sa osl_open_table().
590 static int attribute_open(const char *dir
)
594 attribute_table_desc
.dir
= dir
;
595 ret
= osl(osl_open_table(&attribute_table_desc
, &attribute_table
));
596 greatest_att_bitnum
= -1; /* no atts available */
598 find_greatest_att_bitnum();
601 attribute_table
= NULL
;
602 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
607 static int attribute_create(const char *dir
)
609 attribute_table_desc
.dir
= dir
;
610 return osl(osl_create_table(&attribute_table_desc
));
614 * Initialize the attribute table structure.
616 * \param t The table structure to initialize.
618 void attribute_init(struct afs_table
*t
)
620 t
->open
= attribute_open
;
621 t
->close
= attribute_close
;
622 t
->create
= attribute_create
;