2 * Copyright (C) 1997-2013 Andre Noll <maan@systemlinux.org>
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 struct lsatt_action_data
{
125 /** The result buffer. */
126 struct para_buffer pb
;
127 /** The given flags for the lsatt command. */
131 static int print_attribute(struct osl_table
*table
, struct osl_row
*row
,
132 const char *name
, void *data
)
134 struct lsatt_action_data
*laad
= data
;
135 struct osl_object bitnum_obj
;
138 if (!(laad
->flags
& LSATT_FLAG_LONG
))
139 return para_printf(&laad
->pb
, "%s\n", name
);
140 ret
= osl(osl_get_object(table
, row
, ATTCOL_BITNUM
, &bitnum_obj
));
142 para_printf(&laad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
145 return para_printf(&laad
->pb
, "%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
149 static void com_lsatt_callback(int fd
, const struct osl_object
*query
)
151 struct lsatt_action_data laad
= {
152 .flags
= *(unsigned *) query
->data
,
154 .max_size
= shm_get_shmmax(),
155 .private_data
= &(struct afs_max_size_handler_data
) {
159 .max_size_handler
= afs_max_size_handler
163 struct pattern_match_data pmd
= {
164 .table
= attribute_table
,
165 .loop_col_num
= ATTCOL_BITNUM
,
166 .match_col_num
= ATTCOL_NAME
,
167 .patterns
= {.data
= (char *)query
->data
+ sizeof(laad
.flags
),
168 .size
= query
->size
- sizeof(laad
.flags
)},
169 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
,
171 .action
= print_attribute
173 if (laad
.flags
& LSATT_FLAG_SORT_BY_ID
)
174 pmd
.loop_col_num
= ATTCOL_NAME
;
175 if (laad
.flags
& LSATT_FLAG_REVERSE
)
176 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
177 for_each_matching_row(&pmd
);
179 pass_buffer_as_shm(fd
, SBD_OUTPUT
, laad
.pb
.buf
, laad
.pb
.offset
);
183 int com_lsatt(struct command_context
*cc
)
186 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
189 for (i
= 1; i
< cc
->argc
; i
++) {
190 const char *arg
= cc
->argv
[i
];
193 if (!strcmp(arg
, "--")) {
197 if (!strcmp(arg
, "-i")) {
198 flags
|= LSATT_FLAG_SORT_BY_ID
;
201 if (!strcmp(arg
, "-l")) {
202 flags
|= LSATT_FLAG_LONG
;
205 if (!strcmp(arg
, "-r")) {
206 flags
|= LSATT_FLAG_REVERSE
;
210 ret
= send_option_arg_callback_request(&options
, cc
->argc
- i
, cc
->argv
+ i
,
211 com_lsatt_callback
, afs_cb_result_handler
, cc
);
213 send_strerror(cc
, -ret
);
214 else if (ret
== 0 && cc
->argc
> 1)
215 ret
= cc
->use_sideband
?
216 send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "no matches\n")
218 sc_send_va_buffer(&cc
->scc
, "no matches\n");
222 static void com_setatt_callback(__a_unused
int fd
, const struct osl_object
*query
)
225 uint64_t add_mask
= 0, del_mask
= 0;
228 struct osl_object obj
;
230 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
232 unsigned char bitnum
;
235 ret
= -E_ATTR_SYNTAX
;
239 if (c
!= '+' && c
!= '-')
242 ret
= get_attribute_bitnum_by_name(p
, &bitnum
);
246 add_mask
|= (1UL << bitnum
);
248 del_mask
|= (1UL << bitnum
);
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(struct command_context
*cc
)
283 return -E_ATTR_SYNTAX
;
284 return send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
285 com_setatt_callback
, NULL
, NULL
);
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
= {
299 .max_size
= shm_get_shmmax(),
300 .private_data
= &(struct afs_max_size_handler_data
) {
304 .max_size_handler
= afs_max_size_handler
308 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
309 struct osl_object objs
[NUM_ATT_COLUMNS
];
311 unsigned char bitnum
;
312 struct addatt_event_data aed
;
315 if (!len
|| p
[len
- 1] == '-' || p
[len
- 1] == '+') {
316 ret2
= para_printf(&pb
, "invalid attribute name: %s\n", p
);
321 ret
= get_attribute_bitnum_by_name(p
, &bitnum
);
323 ret2
= para_printf(&pb
, "attribute \"%s\" already exists\n", p
);
328 if (ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
)) /* error */
330 objs
[ATTCOL_BITNUM
].size
= 1;
331 /* find smallest unused attribute */
332 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
333 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
334 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
,
335 &objs
[ATTCOL_BITNUM
], &row
));
336 if (ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
337 break; /* this bitnum is unused, use it */
338 if (ret
< 0) /* error */
340 /* this bit is already in use, try next bit */
343 ret
= -E_ATT_TABLE_FULL
;
346 objs
[ATTCOL_NAME
].data
= p
;
347 objs
[ATTCOL_NAME
].size
= len
+ 1;
348 ret
= osl(osl_add_row(attribute_table
, objs
));
353 afs_event(ATTRIBUTE_ADD
, &pb
, &aed
);
354 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, (int)bitnum
);
357 if (ret
< 0 && ret2
>= 0)
358 para_printf(&pb
, "%s: %s\n", p
, para_strerror(-ret
));
360 pass_buffer_as_shm(fd
, SBD_OUTPUT
, pb
.buf
, pb
.offset
);
364 int com_addatt(struct command_context
*cc
)
369 return -E_ATTR_SYNTAX
;
370 ret
= send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
371 com_addatt_callback
, afs_cb_result_handler
, cc
);
373 send_strerror(cc
, -ret
);
377 static void com_mvatt_callback(int fd
, const struct osl_object
*query
)
379 char *old
= query
->data
;
380 size_t size
= strlen(old
) + 1;
381 char *new = old
+ size
;
382 struct osl_object obj
= {.data
= old
, .size
= size
};
384 struct para_buffer pb
= {
385 .max_size
= shm_get_shmmax(),
386 .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(fd
, SBD_OUTPUT
, pb
.buf
, pb
.offset
);
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 send_strerror(cc
, -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
) {
463 .max_size_handler
= afs_max_size_handler
,
467 struct pattern_match_data pmd
= {
468 .table
= attribute_table
,
470 .loop_col_num
= ATTCOL_BITNUM
,
471 .match_col_num
= ATTCOL_NAME
,
473 .action
= remove_attribute
475 ret
= for_each_matching_row(&pmd
);
477 ret2
= para_printf(&raad
.pb
, "%s\n", para_strerror(-ret
));
478 else if (!raad
.num_removed
)
479 ret2
= para_printf(&raad
.pb
, "no match -- nothing removed\n");
480 if (ret2
>= 0 && raad
.pb
.offset
)
481 pass_buffer_as_shm(fd
, SBD_OUTPUT
, raad
.pb
.buf
, raad
.pb
.offset
);
485 int com_rmatt(struct command_context
*cc
)
490 return -E_ATTR_SYNTAX
;
491 ret
= send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1,
492 com_rmatt_callback
, afs_cb_result_handler
, cc
);
494 send_strerror(cc
, -ret
);
499 * Return a binary representation of the given attribute value.
501 * \param atts Pointer to the attribute value.
504 * This function prints a string of at most 64 characters plus the terminating
505 * \p NULL character into \a buf which must be provided by the caller and at
506 * least 65 bytes long. The "x" character is used for set attributes and "-" is
507 * used for unset attributes.
509 * In practice, not all 64 attributes are defined. In this case, the function
510 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
513 void get_attribute_bitmap(const uint64_t *atts
, char *buf
)
516 const uint64_t one
= 1;
518 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
519 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
524 * Get a string containing the set attributes in text form.
526 * \param atts The attribute bitmap.
527 * \param delim The delimiter to separate matching attribute names.
528 * \param text Result pointer.
530 * \return Positive on success, negative on errors. If no attributes have
531 * been defined, \a *text is NULL.
533 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
536 const uint64_t one
= 1;
539 if (greatest_att_bitnum
< 0) { /* no attributes available */
540 *text
= para_strdup("(no attributes available)");
543 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
544 unsigned char bn
= i
;
545 struct osl_object obj
= {.data
= &bn
, .size
= 1};
548 if (!(*atts
& (one
<< i
)))
550 ret
= osl(osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
));
553 ret
= osl(osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
));
557 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
561 *text
= para_strdup(obj
.data
);
563 if (!*text
) /* no attributes set */
564 *text
= para_strdup("");
572 * Close the attribute table.
574 * \sa osl_close_table().
576 static void attribute_close(void)
578 osl_close_table(attribute_table
, OSL_MARK_CLEAN
);
579 attribute_table
= NULL
;
583 * Open the attribute table.
585 * \param dir The database directory.
587 * \return Positive on success, negative on errors.
589 * \sa osl_open_table().
591 static int attribute_open(const char *dir
)
595 attribute_table_desc
.dir
= dir
;
596 ret
= osl(osl_open_table(&attribute_table_desc
, &attribute_table
));
597 greatest_att_bitnum
= -1; /* no atts available */
599 find_greatest_att_bitnum();
602 attribute_table
= NULL
;
603 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
608 static int attribute_create(const char *dir
)
610 attribute_table_desc
.dir
= dir
;
611 return osl(osl_create_table(&attribute_table_desc
));
615 * Initialize the attribute table structure.
617 * \param t The table structure to initialize.
619 void attribute_init(struct afs_table
*t
)
621 t
->open
= attribute_open
;
622 t
->close
= attribute_close
;
623 t
->create
= attribute_create
;