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 /** Whether "-a" was given for the lsatt command. */
104 LSATT_FLAG_SORT_BY_ID
= 1,
105 /** Whether "-l" was given for the lsatt command. */
107 LSATT_FLAG_REVERSE
= 4
110 /** Data passed to the action function of lsatt */
111 struct lsatt_action_data
{
112 /** The result buffer. */
113 struct para_buffer pb
;
114 /** The given flags for the lsatt command. */
118 static int print_attribute(struct osl_table
*table
, struct osl_row
*row
,
119 const char *name
, void *data
)
121 struct lsatt_action_data
*laad
= data
;
122 struct osl_object bitnum_obj
;
125 if (!(laad
->flags
& LSATT_FLAG_LONG
)) {
126 para_printf(&laad
->pb
, "%s\n", name
);
129 ret
= osl_get_object(table
, row
, ATTCOL_BITNUM
, &bitnum_obj
);
131 para_printf(&laad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
134 para_printf(&laad
->pb
, "%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
139 static int com_lsatt_callback(const struct osl_object
*query
,
140 struct osl_object
*result
)
142 struct lsatt_action_data laad
= {.flags
= *(unsigned *) query
->data
};
143 struct pattern_match_data pmd
= {
144 .table
= attribute_table
,
145 .loop_col_num
= ATTCOL_BITNUM
,
146 .match_col_num
= ATTCOL_NAME
,
147 .patterns
= {.data
= (char *)query
->data
+ sizeof(laad
.flags
),
148 .size
= query
->size
- sizeof(laad
.flags
)},
149 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
,
151 .action
= print_attribute
155 if (laad
.flags
& LSATT_FLAG_SORT_BY_ID
)
156 pmd
.loop_col_num
= ATTCOL_NAME
;
157 if (laad
.flags
& LSATT_FLAG_REVERSE
)
158 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
159 ret
= for_each_matching_row(&pmd
);
161 para_printf(&laad
.pb
, "%s\n", PARA_STRERROR(-ret
));
164 result
->data
= laad
.pb
.buf
;
165 result
->size
= laad
.pb
.size
;
169 int com_lsatt(int fd
, int argc
, char * const * const argv
)
172 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
176 for (i
= 1; i
< argc
; i
++) {
177 const char *arg
= argv
[i
];
180 if (!strcmp(arg
, "--")) {
184 if (!strcmp(arg
, "-i")) {
185 flags
|= LSATT_FLAG_SORT_BY_ID
;
188 if (!strcmp(arg
, "-l")) {
189 flags
|= LSATT_FLAG_LONG
;
192 if (!strcmp(arg
, "-r")) {
193 flags
|= LSATT_FLAG_REVERSE
;
197 ret
= send_option_arg_callback_request(&options
, argc
-i
, argv
+ i
,
198 com_lsatt_callback
, &result
);
200 ret
= send_buffer(fd
, (char *)result
.data
);
203 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
207 static int com_setatt_callback(const struct osl_object
*query
,
208 __a_unused
struct osl_object
*result
)
211 uint64_t add_mask
= 0, del_mask
= 0;
214 struct osl_object obj
;
217 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
222 return -E_ATTR_SYNTAX
;
224 if (c
!= '+' && c
!= '-')
229 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
232 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
,
237 add_mask
|= (1UL << *(unsigned char *)obj
.data
);
239 del_mask
|= (1UL << *(unsigned char *)obj
.data
);
241 if (!add_mask
&& !del_mask
)
242 return -E_ATTR_SYNTAX
;
243 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask
,
244 (long long unsigned)del_mask
);
245 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) { /* TODO: fnmatch */
246 struct afs_info old_afsi
, new_afsi
;
247 struct osl_row
*aft_row
;
250 ret
= aft_get_row_of_path(p
, &aft_row
);
253 ret
= get_afsi_object_of_row(aft_row
, &obj
);
256 ret
= load_afsi(&old_afsi
, &obj
);
260 new_afsi
.attributes
|= add_mask
;
261 new_afsi
.attributes
&= ~del_mask
;
262 save_afsi(&new_afsi
, &obj
); /* in-place update */
263 // ret = mood_update_audio_file(aft_row, &old_afsi);
270 int com_setatt(__a_unused
int fd
, int argc
, char * const * const argv
)
273 return -E_ATTR_SYNTAX
;
274 return send_standard_callback_request(argc
- 1, argv
+ 1, com_setatt_callback
,
278 /* TODO: make it faster by only extracting the attribute member from afsi */
279 static int logical_and_attribute(struct osl_row
*aft_row
, void *attribute_ptr
)
281 struct afs_info afsi
;
282 uint64_t *att
= attribute_ptr
;
283 struct osl_object obj
;
284 int ret
= get_afsi_object_of_row(aft_row
, &obj
);
287 ret
= load_afsi(&afsi
, &obj
);
290 afsi
.attributes
&= *att
;
291 save_afsi(&afsi
, &obj
);
295 static int com_addatt_callback(const struct osl_object
*query
,
296 __a_unused
struct osl_object
*result
)
298 char *p
= query
->data
;
299 uint64_t atts_added
= 0;
302 while (p
< (char *)query
->data
+ query
->size
) {
303 struct osl_object objs
[NUM_ATT_COLUMNS
];
305 unsigned char bitnum
;
307 objs
[ATTCOL_BITNUM
].size
= 1;
308 objs
[ATTCOL_NAME
].data
= p
;
309 objs
[ATTCOL_NAME
].size
= strlen(p
) + 1;
310 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
,
311 &objs
[ATTCOL_NAME
], &row
); /* expected to fail */
313 return -E_ATTR_EXISTS
;
314 if (ret
!= -E_RB_KEY_NOT_FOUND
) /* error */
316 /* find smallest non-used attribute */
317 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
318 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
319 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
,
320 &objs
[ATTCOL_BITNUM
], &row
);
321 if (ret
== -E_RB_KEY_NOT_FOUND
)
322 break; /* this bitnum is unused, use it */
323 if (ret
< 0) /* error */
325 /* this bit is already in use, try next bit */
328 return -E_ATTR_TABLE_FULL
;
329 ret
= osl_add_row(attribute_table
, objs
);
332 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, bitnum
);
333 atts_added
|= 1 << bitnum
;
338 atts_added
= ~atts_added
;
339 ret
= audio_file_loop(&atts_added
, logical_and_attribute
);
342 find_greatest_att_bitnum();
343 return reload_current_mood(); /* FIXME: mood_reload() returns an error */
346 int com_addatt(__a_unused
int fd
, int argc
, char * const * const argv
)
349 return -E_ATTR_SYNTAX
;
350 return send_standard_callback_request(argc
- 1, argv
+ 1, com_addatt_callback
,
353 struct remove_attribute_action_data
{
354 struct para_buffer pb
;
358 static int remove_attribute(struct osl_table
*table
, struct osl_row
*row
,
359 const char *name
, void *data
)
361 struct remove_attribute_action_data
*raad
= data
;
362 int ret
= osl_del_row(table
, row
);
364 para_printf(&raad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
366 para_printf(&raad
->pb
, "removed %s\n", name
);
372 static int com_rmatt_callback(const struct osl_object
*query
,
373 struct osl_object
*result
)
375 struct remove_attribute_action_data raad
= {.num_removed
= 0};
377 struct pattern_match_data pmd
= {
378 .table
= attribute_table
,
380 .loop_col_num
= ATTCOL_BITNUM
,
381 .match_col_num
= ATTCOL_NAME
,
383 .action
= remove_attribute
385 ret
= for_each_matching_row(&pmd
);
387 para_printf(&raad
.pb
, "%s\n", PARA_STRERROR(-ret
));
388 if (raad
.num_removed
) {
389 find_greatest_att_bitnum();
390 ret
= reload_current_mood();
392 para_printf(&raad
.pb
, "%s\n", PARA_STRERROR(-ret
));
395 para_printf(&raad
.pb
, "no match -- nothing removed\n");
396 result
->data
= raad
.pb
.buf
;
397 result
->size
= raad
.pb
.size
;
401 int com_rmatt(int fd
, int argc
, char * const * const argv
)
404 struct osl_object result
;
407 return -E_ATTR_SYNTAX
;
408 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, com_rmatt_callback
,
411 send_buffer(fd
, (char *)result
.data
);
414 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
419 * Return a binary representation of the geiven attribute value.
421 * \param atts Pointer to the attribute value.
424 * This function prints a string of at most 64 characters plus the terminating
425 * \p NULL character into \a buf which must be provided by the caller and at
426 * least 65 bytes long. The "x" character is used for set attributes and "-" is
427 * used for unset attributes.
429 * In practice, not all 64 attributes are defined. In this case, the function
430 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
433 void get_attribute_bitmap(const uint64_t *atts
, char *buf
)
436 const uint64_t one
= 1;
438 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
439 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
444 * Get a string containing the set attributes in text form.
446 * \param atts The attribute bitmap.
447 * \param delim The delimiter to separate matching attribute names.
448 * \param text Result pointer.
450 * \return Positive on success, negative on errors. If no attributes have
451 * been defined, \a *text is NULL.
453 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
456 const uint64_t one
= 1;
459 if (greatest_att_bitnum
< 0) /* no attributes available */
461 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
462 unsigned char bn
= i
;
463 struct osl_object obj
= {.data
= &bn
, .size
= 1};
466 if (!(*atts
& (one
<< i
)))
468 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
);
471 ret
= osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
);
475 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
479 *text
= para_strdup(obj
.data
);
481 if (!*text
) /* no attributes set */
482 *text
= para_strdup("");
490 * Close the attribute table.
492 * \param flags Ususal flags that are passed to osl_close_table().
494 * \sa osl_close_table().
496 void attribute_shutdown(enum osl_close_flags flags
)
498 osl_close_table(attribute_table
, flags
);
499 attribute_table
= NULL
;
503 * Open the attribute table.
505 * \param ti Gets initialized by this function.
506 * \param db The database directory.
508 * \return Positive on success, negative on errors.
510 * \sa osl_open_table().
512 int attribute_init(struct table_info
*ti
, const char *db
)
516 attribute_table_desc
.dir
= db
;
517 ti
->desc
= &attribute_table_desc
;
518 ret
= osl_open_table(ti
->desc
, &attribute_table
);
519 greatest_att_bitnum
= -1; /* no atts available */
521 find_greatest_att_bitnum();
524 attribute_table
= NULL
;