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 unsigned 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);
338 find_greatest_att_bitnum();
339 return reload_current_mood(); /* FIXME: returns an error */
342 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
345 return -E_ATTR_SYNTAX;
346 return send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
349 struct remove_attribute_action_data {
350 struct para_buffer pb;
352 uint64_t mask_of_removed_atts;
355 static int remove_attribute(struct osl_table *table, struct osl_row *row,
356 const char *name, void *data)
358 struct remove_attribute_action_data *raad = data;
359 int ret = osl_del_row(table, row);
360 unsigned char bitnum;
363 para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
366 ret = get_attribute_bitnum_by_name(name, &bitnum);
368 para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
371 para_printf(&raad->pb, "removed %s\n", name);
373 raad->mask_of_removed_atts |= (1 << bitnum);
377 static int com_rmatt_callback(const struct osl_object *query,
378 struct osl_object *result)
380 struct remove_attribute_action_data raad = {.num_removed = 0};
382 struct pattern_match_data pmd = {
383 .table = attribute_table,
385 .loop_col_num = ATTCOL_BITNUM,
386 .match_col_num = ATTCOL_NAME,
388 .action = remove_attribute
390 ret = for_each_matching_row(&pmd);
392 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
393 if (raad.num_removed) {
394 uint64_t and_mask = ~raad.mask_of_removed_atts;
395 ret = audio_file_loop(&and_mask, logical_and_attribute);
397 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
398 find_greatest_att_bitnum();
399 ret = reload_current_mood();
401 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
404 para_printf(&raad.pb, "no match -- nothing removed\n");
405 result->data = raad.pb.buf;
406 result->size = raad.pb.size;
410 int com_rmatt(int fd, int argc, char * const * const argv)
413 struct osl_object result;
416 return -E_ATTR_SYNTAX;
417 ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
420 send_buffer(fd, (char *)result.data);
423 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
428 * Return a binary representation of the geiven attribute value.
430 * \param atts Pointer to the attribute value.
433 * This function prints a string of at most 64 characters plus the terminating
434 * \p NULL character into \a buf which must be provided by the caller and at
435 * least 65 bytes long. The "x" character is used for set attributes and "-" is
436 * used for unset attributes.
438 * In practice, not all 64 attributes are defined. In this case, the function
439 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
442 void get_attribute_bitmap(const uint64_t *atts, char *buf)
445 const uint64_t one = 1;
447 for (i = 0; i <= greatest_att_bitnum; i++)
448 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
453 * Get a string containing the set attributes in text form.
455 * \param atts The attribute bitmap.
456 * \param delim The delimiter to separate matching attribute names.
457 * \param text Result pointer.
459 * \return Positive on success, negative on errors. If no attributes have
460 * been defined, \a *text is NULL.
462 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
465 const uint64_t one = 1;
468 if (greatest_att_bitnum < 0) /* no attributes available */
470 for (i = 0; i <= greatest_att_bitnum; i++) {
471 unsigned char bn = i;
472 struct osl_object obj = {.data = &bn, .size = 1};
475 if (!(*atts & (one << i)))
477 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
480 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
484 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
488 *text = para_strdup(obj.data);
490 if (!*text) /* no attributes set */
491 *text = para_strdup("");
499 * Close the attribute table.
501 * \param flags Ususal flags that are passed to osl_close_table().
503 * \sa osl_close_table().
505 void attribute_shutdown(enum osl_close_flags flags)
507 osl_close_table(attribute_table, flags);
508 attribute_table = NULL;
512 * Open the attribute table.
514 * \param ti Gets initialized by this function.
515 * \param db The database directory.
517 * \return Positive on success, negative on errors.
519 * \sa osl_open_table().
521 int attribute_init(struct table_info *ti, const char *db)
525 attribute_table_desc.dir = db;
526 ti->desc = &attribute_table_desc;
527 ret = osl_open_table(ti->desc, &attribute_table);
528 greatest_att_bitnum = -1; /* no atts available */
530 find_greatest_att_bitnum();
533 attribute_table = NULL;