2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file attribute.c Attribute handling functions. */
15 static struct osl_table
*attribute_table
;
16 static int greatest_att_bitnum
;
18 /** The columns of the attribute table. */
19 enum attribute_table_columns
{
20 /** The bit number (0-63). */
22 /** The name of the attribute. */
24 /** Number of columns in this table. */
28 static int char_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
30 const unsigned char *c1
= (const unsigned char*)obj1
->data
;
31 const unsigned char *c2
= (const unsigned char*)obj2
->data
;
39 static struct osl_column_description att_cols
[] = {
41 .storage_type
= OSL_MAPPED_STORAGE
,
42 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
44 .compare_function
= char_compare
,
48 .storage_type
= OSL_MAPPED_STORAGE
,
49 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
51 .compare_function
= string_compare
,
55 static struct osl_table_description attribute_table_desc
= {
57 .num_columns
= NUM_ATT_COLUMNS
,
59 .column_descriptions
= att_cols
62 static void find_greatest_att_bitnum(void)
67 struct osl_object obj
= {.data
= &c
, .size
= 1};
68 if (osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
,
70 greatest_att_bitnum
= c
;
74 PARA_INFO_LOG("%s\n", "no attributes");
75 greatest_att_bitnum
= -E_NO_ATTRIBUTES
;
79 * Retrieve the identifier (number) of an attribute.
81 * \param att_name The name of the attribute.
82 * \param bitnum Result pointer.
84 * \return Positive on success, negative on errors.
86 int get_attribute_bitnum_by_name(const char *att_name
, unsigned char *bitnum
)
88 struct osl_object obj
= {.data
= (char *)att_name
,
89 .size
= strlen(att_name
) + 1};
91 int ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
95 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
, &obj
);
98 *bitnum
= *(unsigned char *)obj
.data
;
103 * Flags used by the lsatt command.
105 * \param \sa com_lsatt().
108 /** Whether "-a" was given for the lsatt command. */
109 LSATT_FLAG_SORT_BY_ID
= 1,
110 /** Whether "-l" was given for the lsatt command. */
112 /** Reverse sort order. */
113 LSATT_FLAG_REVERSE
= 4
116 /** Data passed to the action function of lsatt */
117 struct lsatt_action_data
{
118 /** The result buffer. */
119 struct para_buffer pb
;
120 /** The given flags for the lsatt command. */
124 static int print_attribute(struct osl_table
*table
, struct osl_row
*row
,
125 const char *name
, void *data
)
127 struct lsatt_action_data
*laad
= data
;
128 struct osl_object bitnum_obj
;
131 if (!(laad
->flags
& LSATT_FLAG_LONG
)) {
132 para_printf(&laad
->pb
, "%s\n", name
);
135 ret
= osl_get_object(table
, row
, ATTCOL_BITNUM
, &bitnum_obj
);
137 para_printf(&laad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
140 para_printf(&laad
->pb
, "%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
145 static int com_lsatt_callback(const struct osl_object
*query
,
146 struct osl_object
*result
)
148 struct lsatt_action_data laad
= {.flags
= *(unsigned *) query
->data
};
149 struct pattern_match_data pmd
= {
150 .table
= attribute_table
,
151 .loop_col_num
= ATTCOL_BITNUM
,
152 .match_col_num
= ATTCOL_NAME
,
153 .patterns
= {.data
= (char *)query
->data
+ sizeof(laad
.flags
),
154 .size
= query
->size
- sizeof(laad
.flags
)},
155 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
,
157 .action
= print_attribute
161 if (laad
.flags
& LSATT_FLAG_SORT_BY_ID
)
162 pmd
.loop_col_num
= ATTCOL_NAME
;
163 if (laad
.flags
& LSATT_FLAG_REVERSE
)
164 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
165 ret
= for_each_matching_row(&pmd
);
167 para_printf(&laad
.pb
, "%s\n", PARA_STRERROR(-ret
));
170 result
->data
= laad
.pb
.buf
;
171 result
->size
= laad
.pb
.size
;
175 int com_lsatt(int fd
, int argc
, char * const * const argv
)
178 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
182 for (i
= 1; i
< argc
; i
++) {
183 const char *arg
= argv
[i
];
186 if (!strcmp(arg
, "--")) {
190 if (!strcmp(arg
, "-i")) {
191 flags
|= LSATT_FLAG_SORT_BY_ID
;
194 if (!strcmp(arg
, "-l")) {
195 flags
|= LSATT_FLAG_LONG
;
198 if (!strcmp(arg
, "-r")) {
199 flags
|= LSATT_FLAG_REVERSE
;
203 ret
= send_option_arg_callback_request(&options
, argc
-i
, argv
+ i
,
204 com_lsatt_callback
, &result
);
206 ret
= send_buffer(fd
, (char *)result
.data
);
209 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
213 static int com_setatt_callback(const struct osl_object
*query
,
214 __a_unused
struct osl_object
*result
)
217 uint64_t add_mask
= 0, del_mask
= 0;
220 struct osl_object obj
;
223 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
228 return -E_ATTR_SYNTAX
;
230 if (c
!= '+' && c
!= '-')
235 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
238 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
,
243 add_mask
|= (1UL << *(unsigned char *)obj
.data
);
245 del_mask
|= (1UL << *(unsigned char *)obj
.data
);
247 if (!add_mask
&& !del_mask
)
248 return -E_ATTR_SYNTAX
;
249 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask
,
250 (long long unsigned)del_mask
);
251 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) { /* TODO: fnmatch */
252 struct afs_info old_afsi
, new_afsi
;
253 struct osl_row
*aft_row
;
256 ret
= aft_get_row_of_path(p
, &aft_row
);
259 ret
= get_afsi_object_of_row(aft_row
, &obj
);
262 ret
= load_afsi(&old_afsi
, &obj
);
266 new_afsi
.attributes
|= add_mask
;
267 new_afsi
.attributes
&= ~del_mask
;
268 save_afsi(&new_afsi
, &obj
); /* in-place update */
269 // ret = mood_update_audio_file(aft_row, &old_afsi);
276 int com_setatt(__a_unused
int fd
, int argc
, char * const * const argv
)
279 return -E_ATTR_SYNTAX
;
280 return send_standard_callback_request(argc
- 1, argv
+ 1, com_setatt_callback
,
284 static int com_addatt_callback(const struct osl_object
*query
,
285 struct osl_object
*result
)
287 char *p
= query
->data
;
289 struct para_buffer pb
= {.size
= 0};
292 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
293 struct osl_object objs
[NUM_ATT_COLUMNS
];
295 unsigned char bitnum
;
297 struct addatt_event_data aed
;
299 if (!len
|| p
[len
- 1] == '-' || p
[len
- 1] == '+') {
300 para_printf(&pb
, "invalid attribute name: %s\n", p
);
303 objs
[ATTCOL_BITNUM
].size
= 1;
304 objs
[ATTCOL_NAME
].data
= p
;
305 objs
[ATTCOL_NAME
].size
= len
+ 1;
306 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
,
307 &objs
[ATTCOL_NAME
], &row
); /* expected to fail FIXME: Use get_attribute_bitnum_by_name() */
309 para_printf(&pb
, "attribute %s already exists\n", p
);
312 if (ret
!= -E_RB_KEY_NOT_FOUND
) /* error */
314 /* find smallest non-used attribute FIXME: Use find_greatest_att_bitnum() */
315 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
316 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
317 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
,
318 &objs
[ATTCOL_BITNUM
], &row
);
319 if (ret
== -E_RB_KEY_NOT_FOUND
)
320 break; /* this bitnum is unused, use it */
321 if (ret
< 0) /* error */
323 /* this bit is already in use, try next bit */
326 para_printf(&pb
, "attribute table full\n");
329 ret
= osl_add_row(attribute_table
, objs
);
334 afs_event(ATTRIBUTE_ADD
, &pb
, &aed
);
335 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, bitnum
);
339 para_printf(&pb
, "%s: %s\n", p
, PARA_STRERROR(-ret
));
340 result
->data
= pb
.buf
;
341 result
->size
= pb
.size
;
342 return result
->data
? 0 : 1;
345 int com_addatt(int fd
, int argc
, char * const * const argv
)
347 struct osl_object result
;
351 return -E_ATTR_SYNTAX
;
352 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_addatt_callback
,
358 if (!result
.data
|| !result
.size
)
360 ret
= send_va_buffer(fd
, "%s", (char *) result
.data
);
365 static int com_mvatt_callback(const struct osl_object
*query
,
366 struct osl_object
*result
)
368 char *old
= query
->data
;
369 size_t size
= strlen(old
) + 1;
370 char *new = old
+ size
;
371 struct osl_object obj
= {.data
= old
, .size
= size
};
373 struct para_buffer pb
= {.size
= 0};
376 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
380 obj
.size
= strlen(new) + 1;
381 ret
= osl_update_object(attribute_table
, row
, ATTCOL_NAME
, &obj
);
384 para_printf(&pb
, "%s\n", PARA_STRERROR(-ret
));
386 afs_event(ATTRIBUTE_RENAME
, &pb
, NULL
);
389 result
->data
= pb
.buf
;
390 result
->size
= pb
.size
;
394 int com_mvatt(int fd
, int argc
, char * const * const argv
)
396 struct osl_object result
;
400 return -E_ATTR_SYNTAX
;
401 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_mvatt_callback
,
407 if (!result
.data
|| !result
.size
)
409 ret
= send_va_buffer(fd
, "%s", (char *) result
.data
);
415 struct remove_attribute_action_data
{
416 struct para_buffer pb
;
418 uint64_t mask_of_removed_atts
;
421 static int remove_attribute(struct osl_table
*table
, struct osl_row
*row
,
422 const char *name
, void *data
)
424 struct remove_attribute_action_data
*raad
= data
;
426 struct rmatt_event_data red
= {.name
= name
};
428 ret
= get_attribute_bitnum_by_name(name
, &red
.bitnum
);
430 para_printf(&raad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
433 ret
= osl_del_row(table
, row
);
435 para_printf(&raad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
438 para_printf(&raad
->pb
, "removed attribute %s\n", name
);
440 raad
->mask_of_removed_atts
|= (1 << red
.bitnum
);
441 afs_event(ATTRIBUTE_REMOVE
, &raad
->pb
, &red
);
445 static int com_rmatt_callback(const struct osl_object
*query
,
446 struct osl_object
*result
)
448 struct remove_attribute_action_data raad
= {.num_removed
= 0};
450 struct pattern_match_data pmd
= {
451 .table
= attribute_table
,
453 .loop_col_num
= ATTCOL_BITNUM
,
454 .match_col_num
= ATTCOL_NAME
,
456 .action
= remove_attribute
458 ret
= for_each_matching_row(&pmd
);
460 para_printf(&raad
.pb
, "%s\n", PARA_STRERROR(-ret
));
461 if (!raad
.num_removed
)
462 para_printf(&raad
.pb
, "no match -- nothing removed\n");
463 result
->data
= raad
.pb
.buf
;
464 result
->size
= raad
.pb
.size
;
468 int com_rmatt(int fd
, int argc
, char * const * const argv
)
471 struct osl_object result
;
474 return -E_ATTR_SYNTAX
;
475 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_rmatt_callback
,
478 send_buffer(fd
, (char *)result
.data
);
481 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
486 * Return a binary representation of the given attribute value.
488 * \param atts Pointer to the attribute value.
491 * This function prints a string of at most 64 characters plus the terminating
492 * \p NULL character into \a buf which must be provided by the caller and at
493 * least 65 bytes long. The "x" character is used for set attributes and "-" is
494 * used for unset attributes.
496 * In practice, not all 64 attributes are defined. In this case, the function
497 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
500 void get_attribute_bitmap(const uint64_t *atts
, char *buf
)
503 const uint64_t one
= 1;
505 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
506 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
511 * Get a string containing the set attributes in text form.
513 * \param atts The attribute bitmap.
514 * \param delim The delimiter to separate matching attribute names.
515 * \param text Result pointer.
517 * \return Positive on success, negative on errors. If no attributes have
518 * been defined, \a *text is NULL.
520 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
523 const uint64_t one
= 1;
526 if (greatest_att_bitnum
< 0) /* no attributes available */
528 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
529 unsigned char bn
= i
;
530 struct osl_object obj
= {.data
= &bn
, .size
= 1};
533 if (!(*atts
& (one
<< i
)))
535 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
);
538 ret
= osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
);
542 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
546 *text
= para_strdup(obj
.data
);
548 if (!*text
) /* no attributes set */
549 *text
= para_strdup("");
557 * Close the attribute table.
559 * \param flags Ususal flags that are passed to osl_close_table().
561 * \sa osl_close_table().
563 void attribute_close(void)
565 osl_close_table(attribute_table
, OSL_MARK_CLEAN
);
566 attribute_table
= NULL
;
570 * Open the attribute table.
572 * \param ti Gets initialized by this function.
573 * \param db The database directory.
575 * \return Positive on success, negative on errors.
577 * \sa osl_open_table().
579 static int attribute_open(const char *dir
)
583 attribute_table_desc
.dir
= dir
;
584 ret
= osl_open_table(&attribute_table_desc
, &attribute_table
);
585 greatest_att_bitnum
= -1; /* no atts available */
587 find_greatest_att_bitnum();
590 attribute_table
= NULL
;
591 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
596 static int attribute_create(const char *dir
)
598 attribute_table_desc
.dir
= dir
;
599 return osl_create_table(&attribute_table_desc
);
603 void attribute_init(struct afs_table
*t
)
605 t
->name
= attribute_table_desc
.name
;
606 t
->open
= attribute_open
;
607 t
->close
= attribute_close
;
608 t
->create
= attribute_create
;