6 static void *attribute_table
;
7 static int greatest_att_bitnum
;
9 static int char_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
11 const unsigned char *c1
= (const unsigned char*)obj1
->data
;
12 const unsigned char *c2
= (const unsigned char*)obj2
->data
;
20 enum attribute_table_columns
{ATTCOL_BITNUM
, ATTCOL_NAME
, NUM_ATT_COLUMNS
};
22 static struct osl_column_description att_cols
[] = {
24 .storage_type
= OSL_MAPPED_STORAGE
,
25 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
27 .compare_function
= char_compare
,
31 .storage_type
= OSL_MAPPED_STORAGE
,
32 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
34 .compare_function
= string_compare
,
38 static const struct osl_table_description attribute_table_desc
= {
41 .num_columns
= NUM_ATT_COLUMNS
,
43 .column_descriptions
= att_cols
46 static void find_greatest_att_bitnum(void)
51 struct osl_object obj
= {.data
= &c
, .size
= 1};
52 if (osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
,
54 greatest_att_bitnum
= c
;
58 PARA_INFO_LOG("%s\n", "no attributes");
59 greatest_att_bitnum
= -E_NO_ATTRIBUTES
;
62 int get_attribute_bitnum_by_name(const char *att_name
, unsigned char *bitnum
)
64 struct osl_object obj
= {.data
= (char *)att_name
,
65 .size
= strlen(att_name
) + 1};
67 int ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
71 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
, &obj
);
74 *bitnum
= *(unsigned char *)obj
.data
;
78 #define LAA_FLAG_ALPHA 1
79 #define LAA_FLAG_LONG 2
81 struct private_laa_data
{
86 static int log_attribute(struct osl_row
*row
, void *private_data
)
88 struct private_laa_data
*pld
= private_data
;
90 struct osl_object name_obj
, bitnum_obj
;
92 ret
= osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &name_obj
);
95 if (!(pld
->flags
& LAA_FLAG_LONG
)) {
96 printf("%s\n", (char *)name_obj
.data
);
99 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
, &bitnum_obj
);
102 printf("%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
103 (char *)name_obj
.data
);
107 int com_lsatt(int fd
, int argc
, const char **argv
)
109 struct private_laa_data pld
= {.fd
= fd
, .flags
= 0};
112 for (i
= 1; i
< argc
; i
++) {
113 const char *arg
= argv
[i
];
116 if (!strcmp(arg
, "--")) {
120 if (!strcmp(arg
, "-a")) {
121 pld
.flags
|= LAA_FLAG_ALPHA
;
124 if (!strcmp(arg
, "-l")) {
125 pld
.flags
|= LAA_FLAG_LONG
;
130 return -E_ATTR_SYNTAX
;
131 if (pld
.flags
& LAA_FLAG_ALPHA
)
132 return osl_rbtree_loop(attribute_table
, ATTCOL_NAME
,
133 &pld
, log_attribute
);
134 return osl_rbtree_loop(attribute_table
, ATTCOL_BITNUM
,
135 &pld
, log_attribute
);
138 static int com_setatt_callback(const struct osl_object
*query
,
139 __a_unused
struct osl_object
*result
)
142 uint64_t add_mask
= 0, del_mask
= 0;
145 struct osl_object obj
;
148 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
153 return -E_ATTR_SYNTAX
;
155 if (c
!= '+' && c
!= '-')
160 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
163 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
,
168 add_mask
|= (1UL << *(unsigned char *)obj
.data
);
170 del_mask
|= (1UL << *(unsigned char *)obj
.data
);
172 if (!add_mask
&& !del_mask
)
173 return -E_ATTR_SYNTAX
;
174 PARA_DEBUG_LOG("masks: %llx:%llx\n", add_mask
, del_mask
);
175 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) { /* TODO: fnmatch */
176 struct afs_info old_afsi
, new_afsi
;
177 struct osl_row
*aft_row
;
180 ret
= aft_get_row_of_path(p
, &aft_row
);
183 ret
= get_afsi_object_of_row(p
, &obj
);
186 ret
= load_afsi(&old_afsi
, &obj
);
190 new_afsi
.attributes
|= add_mask
;
191 new_afsi
.attributes
&= ~del_mask
;
192 save_afsi(&new_afsi
, &obj
); /* in-place update */
193 ret
= mood_update_audio_file(aft_row
, &old_afsi
);
200 int com_setatt(__a_unused
int fd
, int argc
, const char **argv
)
203 return -E_ATTR_SYNTAX
;
204 return send_standard_callback_request(argc
, argv
, com_setatt_callback
,
208 /* TODO: make it faster by only extracting the attribute member from afsi */
209 static int logical_and_attribute(struct osl_row
*aft_row
, void *attribute_ptr
)
211 struct afs_info afsi
;
212 uint64_t *att
= attribute_ptr
;
213 struct osl_object obj
;
214 int ret
= get_afsi_object_of_row(aft_row
, &obj
);
217 ret
= load_afsi(&afsi
, &obj
);
220 afsi
.attributes
&= *att
;
221 save_afsi(&afsi
, &obj
);
225 static int com_addatt_callback(const struct osl_object
*query
,
226 __a_unused
struct osl_object
*result
)
228 char *p
= query
->data
;
229 uint64_t atts_added
= 0;
232 while (p
< (char *)query
->data
+ query
->size
) {
233 struct osl_object objs
[NUM_ATT_COLUMNS
];
235 unsigned char bitnum
;
237 objs
[ATTCOL_BITNUM
].size
= 1;
238 objs
[ATTCOL_NAME
].data
= p
;
239 objs
[ATTCOL_NAME
].size
= strlen(p
) + 1;
240 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
,
241 &objs
[ATTCOL_NAME
], &row
); /* expected to fail */
243 return -E_ATTR_EXISTS
;
244 if (ret
!= -E_RB_KEY_NOT_FOUND
) /* error */
246 /* find smallest non-used attribute */
247 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
248 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
249 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
,
250 &objs
[ATTCOL_BITNUM
], &row
);
251 if (ret
== -E_RB_KEY_NOT_FOUND
)
252 break; /* this bitnum is unused, use it */
253 if (ret
< 0) /* error */
255 /* this bit is already in use, try next bit */
258 return -E_ATTR_TABLE_FULL
;
259 ret
= osl_add_row(attribute_table
, objs
);
262 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, bitnum
);
263 atts_added
|= 1 << bitnum
;
268 atts_added
= ~atts_added
;
269 ret
= audio_file_loop(&atts_added
, logical_and_attribute
);
272 find_greatest_att_bitnum();
273 return mood_reload();
276 int com_addatt(__a_unused
int fd
, int argc
, const char **argv
)
279 return -E_ATTR_SYNTAX
;
280 return send_standard_callback_request(argc
, argv
, com_addatt_callback
,
284 static int com_rmatt_callback(const struct osl_object
*query
,
285 __a_unused
struct osl_object
*result
)
287 char *p
= query
->data
;
288 int ret
, atts_removed
= 0;
289 while (p
< (char *)query
->data
+ query
->size
) {
290 struct osl_object obj
= {
292 .size
= strlen(p
) + 1
295 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
,
299 ret
= osl_del_row(attribute_table
, row
);
305 find_greatest_att_bitnum();
308 return mood_reload();
311 int com_rmatt(__a_unused
int fd
, int argc
, const char **argv
)
314 return -E_ATTR_SYNTAX
;
315 return send_standard_callback_request(argc
, argv
, com_rmatt_callback
,
319 void get_attribute_bitmap(uint64_t *atts
, char *buf
)
322 const uint64_t one
= 1;
324 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
325 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
329 * Get a string containing the set attributes in text form.
331 * \param atts The attribute bitmap.
332 * \param delim The delimiter to separate matching attribute names.
333 * \param text Result pointer.
335 * \return Positive on success, negative on errors. If no attributes have
336 * been defined, \a *text is NULL.
338 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
341 const uint64_t one
= 1;
344 if (greatest_att_bitnum
< 0) /* no attributes available */
346 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
347 unsigned char bn
= i
;
348 struct osl_object obj
= {.data
= &bn
, .size
= 1};
351 if (!(*atts
& (one
<< i
)))
353 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
);
356 ret
= osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
);
360 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
364 *text
= para_strdup(obj
.data
);
366 if (!*text
) /* no attributes set */
367 *text
= para_strdup("");
374 void attribute_shutdown(enum osl_close_flags flags
)
376 osl_close_table(attribute_table
, flags
);
379 int attribute_init(struct table_info
*ti
)
383 ti
->desc
= &attribute_table_desc
;
384 ret
= osl_open_table(ti
->desc
, &ti
->table
);
385 greatest_att_bitnum
= -1; /* no atts available */
387 attribute_table
= ti
->table
;
388 find_greatest_att_bitnum();
391 attribute_table
= NULL
;