7 static void *attribute_table;
8 static int greatest_att_bitnum;
10 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
12 const unsigned char *c1 = (const unsigned char*)obj1->data;
13 const unsigned char *c2 = (const unsigned char*)obj2->data;
21 enum attribute_table_columns {ATTCOL_BITNUM, ATTCOL_NAME, NUM_ATT_COLUMNS};
23 static struct osl_column_description att_cols[] = {
25 .storage_type = OSL_MAPPED_STORAGE,
26 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
28 .compare_function = char_compare,
32 .storage_type = OSL_MAPPED_STORAGE,
33 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
35 .compare_function = string_compare,
39 static const struct osl_table_description attribute_table_desc = {
42 .num_columns = NUM_ATT_COLUMNS,
44 .column_descriptions = att_cols
47 static void find_greatest_att_bitnum(void)
52 struct osl_object obj = {.data = &c, .size = 1};
53 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
55 greatest_att_bitnum = c;
59 PARA_INFO_LOG("%s\n", "no attributes");
60 greatest_att_bitnum = -E_NO_ATTRIBUTES;
63 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
65 struct osl_object obj = {.data = (char *)att_name,
66 .size = strlen(att_name) + 1};
68 int ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
72 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj);
75 *bitnum = *(unsigned char *)obj.data;
79 #define LAA_FLAG_ALPHA 1
80 #define LAA_FLAG_LONG 2
82 struct private_laa_data {
87 static int log_attribute(struct osl_row *row, void *private_data)
89 struct private_laa_data *pld = private_data;
91 struct osl_object name_obj, bitnum_obj;
93 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &name_obj);
96 if (!(pld->flags & LAA_FLAG_LONG)) {
97 printf("%s\n", (char *)name_obj.data);
100 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
103 printf("%u\t%s\n", *(unsigned char*)bitnum_obj.data,
104 (char *)name_obj.data);
108 int com_lsatt(int fd, int argc, const char **argv)
110 struct private_laa_data pld = {.fd = fd, .flags = 0};
113 for (i = 1; i < argc; i++) {
114 const char *arg = argv[i];
117 if (!strcmp(arg, "--")) {
121 if (!strcmp(arg, "-a")) {
122 pld.flags |= LAA_FLAG_ALPHA;
125 if (!strcmp(arg, "-l")) {
126 pld.flags |= LAA_FLAG_LONG;
131 return -E_ATTR_SYNTAX;
132 if (pld.flags & LAA_FLAG_ALPHA)
133 return osl_rbtree_loop(attribute_table, ATTCOL_NAME,
134 &pld, log_attribute);
135 return osl_rbtree_loop(attribute_table, ATTCOL_BITNUM,
136 &pld, log_attribute);
139 static int com_setatt_callback(const struct osl_object *query,
140 __a_unused struct osl_object *result)
143 uint64_t add_mask = 0, del_mask = 0;
146 struct osl_object obj;
149 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
154 return -E_ATTR_SYNTAX;
156 if (c != '+' && c != '-')
161 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
164 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
169 add_mask |= (1UL << *(unsigned char *)obj.data);
171 del_mask |= (1UL << *(unsigned char *)obj.data);
173 if (!add_mask && !del_mask)
174 return -E_ATTR_SYNTAX;
175 PARA_DEBUG_LOG("masks: %llx:%llx\n", add_mask, del_mask);
176 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
177 struct afs_info old_afsi, new_afsi;
178 struct osl_row *aft_row;
181 ret = aft_get_row_of_path(p, &aft_row);
184 ret = get_afsi_object_of_row(p, &obj);
187 ret = load_afsi(&old_afsi, &obj);
191 new_afsi.attributes |= add_mask;
192 new_afsi.attributes &= ~del_mask;
193 save_afsi(&new_afsi, &obj); /* in-place update */
194 ret = mood_update_audio_file(aft_row, &old_afsi);
201 int com_setatt(__a_unused int fd, int argc, const char **argv)
204 return -E_ATTR_SYNTAX;
205 return send_standard_callback_request(argc, argv, com_setatt_callback,
209 /* TODO: make it faster by only extracting the attribute member from afsi */
210 static int logical_and_attribute(struct osl_row *aft_row, void *attribute_ptr)
212 struct afs_info afsi;
213 uint64_t *att = attribute_ptr;
214 struct osl_object obj;
215 int ret = get_afsi_object_of_row(aft_row, &obj);
218 ret = load_afsi(&afsi, &obj);
221 afsi.attributes &= *att;
222 save_afsi(&afsi, &obj);
226 static int com_addatt_callback(const struct osl_object *query,
227 __a_unused struct osl_object *result)
229 char *p = query->data;
230 uint64_t atts_added = 0;
233 while (p < (char *)query->data + query->size) {
234 struct osl_object objs[NUM_ATT_COLUMNS];
236 unsigned char bitnum;
238 objs[ATTCOL_BITNUM].size = 1;
239 objs[ATTCOL_NAME].data = p;
240 objs[ATTCOL_NAME].size = strlen(p) + 1;
241 ret = osl_get_row(attribute_table, ATTCOL_NAME,
242 &objs[ATTCOL_NAME], &row); /* expected to fail */
244 return -E_ATTR_EXISTS;
245 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
247 /* find smallest non-used attribute */
248 for (bitnum = 0; bitnum < 64; bitnum++) {
249 objs[ATTCOL_BITNUM].data = &bitnum;
250 ret = osl_get_row(attribute_table, ATTCOL_BITNUM,
251 &objs[ATTCOL_BITNUM], &row);
252 if (ret == -E_RB_KEY_NOT_FOUND)
253 break; /* this bitnum is unused, use it */
254 if (ret < 0) /* error */
256 /* this bit is already in use, try next bit */
259 return -E_ATTR_TABLE_FULL;
260 ret = osl_add_row(attribute_table, objs);
263 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
264 atts_added |= 1 << bitnum;
269 atts_added = ~atts_added;
270 ret = audio_file_loop(&atts_added, logical_and_attribute);
273 find_greatest_att_bitnum();
274 return mood_reload();
277 int com_addatt(__a_unused int fd, int argc, const char **argv)
280 return -E_ATTR_SYNTAX;
281 return send_standard_callback_request(argc, argv, com_addatt_callback,
285 static int com_rmatt_callback(const struct osl_object *query,
286 __a_unused struct osl_object *result)
288 char *p = query->data;
289 int ret, atts_removed = 0;
290 while (p < (char *)query->data + query->size) {
291 struct osl_object obj = {
293 .size = strlen(p) + 1
296 ret = osl_get_row(attribute_table, ATTCOL_NAME,
300 ret = osl_del_row(attribute_table, row);
306 find_greatest_att_bitnum();
309 return mood_reload();
312 int com_rmatt(__a_unused int fd, int argc, const char **argv)
315 return -E_ATTR_SYNTAX;
316 return send_standard_callback_request(argc, argv, com_rmatt_callback,
320 void get_attribute_bitmap(uint64_t *atts, char *buf)
323 const uint64_t one = 1;
325 for (i = 0; i <= greatest_att_bitnum; i++)
326 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
330 * Get a string containing the set attributes in text form.
332 * \param atts The attribute bitmap.
333 * \param delim The delimiter to separate matching attribute names.
334 * \param text Result pointer.
336 * \return Positive on success, negative on errors. If no attributes have
337 * been defined, \a *text is NULL.
339 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
342 const uint64_t one = 1;
345 if (greatest_att_bitnum < 0) /* no attributes available */
347 for (i = 0; i <= greatest_att_bitnum; i++) {
348 unsigned char bn = i;
349 struct osl_object obj = {.data = &bn, .size = 1};
352 if (!(*atts & (one << i)))
354 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
357 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
361 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
365 *text = para_strdup(obj.data);
367 if (!*text) /* no attributes set */
368 *text = para_strdup("");
375 void attribute_shutdown(enum osl_close_flags flags)
377 osl_close_table(attribute_table, flags);
380 int attribute_init(struct table_info *ti)
384 ti->desc = &attribute_table_desc;
385 ret = osl_open_table(ti->desc, &ti->table);
386 greatest_att_bitnum = -1; /* no atts available */
388 attribute_table = ti->table;
389 find_greatest_att_bitnum();
392 attribute_table = NULL;