8 static void *attribute_table;
9 static int greatest_att_bitnum;
11 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
13 const unsigned char *c1 = (const unsigned char*)obj1->data;
14 const unsigned char *c2 = (const unsigned char*)obj2->data;
22 enum attribute_table_columns {ATTCOL_BITNUM, ATTCOL_NAME, NUM_ATT_COLUMNS};
24 static struct osl_column_description att_cols[] = {
26 .storage_type = OSL_MAPPED_STORAGE,
27 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
29 .compare_function = char_compare,
33 .storage_type = OSL_MAPPED_STORAGE,
34 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
36 .compare_function = string_compare,
40 static const struct osl_table_description attribute_table_desc = {
43 .num_columns = NUM_ATT_COLUMNS,
45 .column_descriptions = att_cols
48 static void find_greatest_att_bitnum(void)
53 struct osl_object obj = {.data = &c, .size = 1};
54 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
56 greatest_att_bitnum = c;
60 PARA_INFO_LOG("%s\n", "no attributes");
61 greatest_att_bitnum = -E_NO_ATTRIBUTES;
64 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
66 struct osl_object obj = {.data = (char *)att_name,
67 .size = strlen(att_name) + 1};
69 int ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
73 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj);
76 *bitnum = *(unsigned char *)obj.data;
80 #define LAA_FLAG_ALPHA 1
81 #define LAA_FLAG_LONG 2
83 struct private_laa_data {
88 static int log_attribute(struct osl_row *row, void *private_data)
90 struct private_laa_data *pld = private_data;
92 struct osl_object name_obj, bitnum_obj;
94 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &name_obj);
97 if (!(pld->flags & LAA_FLAG_LONG)) {
98 send_buffer(pld->fd, (char *)name_obj.data);
101 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
104 send_va_buffer(pld->fd, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
105 (char *)name_obj.data);
109 /* FIXME: Need callback */
110 int com_lsatt(int fd, int argc, const char **argv)
112 struct private_laa_data pld = {.fd = fd, .flags = 0};
115 for (i = 1; i < argc; i++) {
116 const char *arg = argv[i];
119 if (!strcmp(arg, "--")) {
123 if (!strcmp(arg, "-a")) {
124 pld.flags |= LAA_FLAG_ALPHA;
127 if (!strcmp(arg, "-l")) {
128 pld.flags |= LAA_FLAG_LONG;
133 return -E_ATTR_SYNTAX;
134 if (pld.flags & LAA_FLAG_ALPHA)
135 return osl_rbtree_loop(attribute_table, ATTCOL_NAME,
136 &pld, log_attribute);
137 return osl_rbtree_loop(attribute_table, ATTCOL_BITNUM,
138 &pld, log_attribute);
141 static int com_setatt_callback(const struct osl_object *query,
142 __a_unused struct osl_object *result)
145 uint64_t add_mask = 0, del_mask = 0;
148 struct osl_object obj;
151 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
156 return -E_ATTR_SYNTAX;
158 if (c != '+' && c != '-')
163 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
166 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
171 add_mask |= (1UL << *(unsigned char *)obj.data);
173 del_mask |= (1UL << *(unsigned char *)obj.data);
175 if (!add_mask && !del_mask)
176 return -E_ATTR_SYNTAX;
177 PARA_DEBUG_LOG("masks: %llx:%llx\n", add_mask, del_mask);
178 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
179 struct afs_info old_afsi, new_afsi;
180 struct osl_row *aft_row;
183 ret = aft_get_row_of_path(p, &aft_row);
186 ret = get_afsi_object_of_row(p, &obj);
189 ret = load_afsi(&old_afsi, &obj);
193 new_afsi.attributes |= add_mask;
194 new_afsi.attributes &= ~del_mask;
195 save_afsi(&new_afsi, &obj); /* in-place update */
196 ret = mood_update_audio_file(aft_row, &old_afsi);
203 int com_setatt(__a_unused int fd, int argc, const char **argv)
206 return -E_ATTR_SYNTAX;
207 return send_standard_callback_request(argc, argv, com_setatt_callback,
211 /* TODO: make it faster by only extracting the attribute member from afsi */
212 static int logical_and_attribute(struct osl_row *aft_row, void *attribute_ptr)
214 struct afs_info afsi;
215 uint64_t *att = attribute_ptr;
216 struct osl_object obj;
217 int ret = get_afsi_object_of_row(aft_row, &obj);
220 ret = load_afsi(&afsi, &obj);
223 afsi.attributes &= *att;
224 save_afsi(&afsi, &obj);
228 static int com_addatt_callback(const struct osl_object *query,
229 __a_unused struct osl_object *result)
231 char *p = query->data;
232 uint64_t atts_added = 0;
235 while (p < (char *)query->data + query->size) {
236 struct osl_object objs[NUM_ATT_COLUMNS];
238 unsigned char bitnum;
240 objs[ATTCOL_BITNUM].size = 1;
241 objs[ATTCOL_NAME].data = p;
242 objs[ATTCOL_NAME].size = strlen(p) + 1;
243 ret = osl_get_row(attribute_table, ATTCOL_NAME,
244 &objs[ATTCOL_NAME], &row); /* expected to fail */
246 return -E_ATTR_EXISTS;
247 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
249 /* find smallest non-used attribute */
250 for (bitnum = 0; bitnum < 64; bitnum++) {
251 objs[ATTCOL_BITNUM].data = &bitnum;
252 ret = osl_get_row(attribute_table, ATTCOL_BITNUM,
253 &objs[ATTCOL_BITNUM], &row);
254 if (ret == -E_RB_KEY_NOT_FOUND)
255 break; /* this bitnum is unused, use it */
256 if (ret < 0) /* error */
258 /* this bit is already in use, try next bit */
261 return -E_ATTR_TABLE_FULL;
262 ret = osl_add_row(attribute_table, objs);
265 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
266 atts_added |= 1 << bitnum;
271 atts_added = ~atts_added;
272 ret = audio_file_loop(&atts_added, logical_and_attribute);
275 find_greatest_att_bitnum();
276 return mood_reload();
279 int com_addatt(__a_unused int fd, int argc, const char **argv)
282 return -E_ATTR_SYNTAX;
283 return send_standard_callback_request(argc, argv, com_addatt_callback,
287 static int com_rmatt_callback(const struct osl_object *query,
288 __a_unused struct osl_object *result)
290 char *p = query->data;
291 int ret, atts_removed = 0;
292 while (p < (char *)query->data + query->size) {
293 struct osl_object obj = {
295 .size = strlen(p) + 1
298 ret = osl_get_row(attribute_table, ATTCOL_NAME,
302 ret = osl_del_row(attribute_table, row);
308 find_greatest_att_bitnum();
311 return mood_reload();
314 int com_rmatt(__a_unused int fd, int argc, const char **argv)
317 return -E_ATTR_SYNTAX;
318 return send_standard_callback_request(argc, argv, com_rmatt_callback,
322 void get_attribute_bitmap(uint64_t *atts, char *buf)
325 const uint64_t one = 1;
327 for (i = 0; i <= greatest_att_bitnum; i++)
328 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
332 * Get a string containing the set attributes in text form.
334 * \param atts The attribute bitmap.
335 * \param delim The delimiter to separate matching attribute names.
336 * \param text Result pointer.
338 * \return Positive on success, negative on errors. If no attributes have
339 * been defined, \a *text is NULL.
341 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
344 const uint64_t one = 1;
347 if (greatest_att_bitnum < 0) /* no attributes available */
349 for (i = 0; i <= greatest_att_bitnum; i++) {
350 unsigned char bn = i;
351 struct osl_object obj = {.data = &bn, .size = 1};
354 if (!(*atts & (one << i)))
356 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
359 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
363 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
367 *text = para_strdup(obj.data);
369 if (!*text) /* no attributes set */
370 *text = para_strdup("");
377 void attribute_shutdown(enum osl_close_flags flags)
379 osl_close_table(attribute_table, flags);
382 int attribute_init(struct table_info *ti)
386 ti->desc = &attribute_table_desc;
387 ret = osl_open_table(ti->desc, &ti->table);
388 greatest_att_bitnum = -1; /* no atts available */
390 attribute_table = ti->table;
391 find_greatest_att_bitnum();
394 attribute_table = NULL;