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
, char * const * const 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",(long long unsigned)add_mask
,
178 (long long unsigned)del_mask
);
179 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) { /* TODO: fnmatch */
180 struct afs_info old_afsi
, new_afsi
;
181 struct osl_row
*aft_row
;
184 ret
= aft_get_row_of_path(p
, &aft_row
);
187 ret
= get_afsi_object_of_row(p
, &obj
);
190 ret
= load_afsi(&old_afsi
, &obj
);
194 new_afsi
.attributes
|= add_mask
;
195 new_afsi
.attributes
&= ~del_mask
;
196 save_afsi(&new_afsi
, &obj
); /* in-place update */
197 ret
= mood_update_audio_file(aft_row
, &old_afsi
);
204 int com_setatt(__a_unused
int fd
, int argc
, char * const * const argv
)
207 return -E_ATTR_SYNTAX
;
208 return send_standard_callback_request(argc
, argv
, com_setatt_callback
,
212 /* TODO: make it faster by only extracting the attribute member from afsi */
213 static int logical_and_attribute(struct osl_row
*aft_row
, void *attribute_ptr
)
215 struct afs_info afsi
;
216 uint64_t *att
= attribute_ptr
;
217 struct osl_object obj
;
218 int ret
= get_afsi_object_of_row(aft_row
, &obj
);
221 ret
= load_afsi(&afsi
, &obj
);
224 afsi
.attributes
&= *att
;
225 save_afsi(&afsi
, &obj
);
229 static int com_addatt_callback(const struct osl_object
*query
,
230 __a_unused
struct osl_object
*result
)
232 char *p
= query
->data
;
233 uint64_t atts_added
= 0;
236 while (p
< (char *)query
->data
+ query
->size
) {
237 struct osl_object objs
[NUM_ATT_COLUMNS
];
239 unsigned char bitnum
;
241 objs
[ATTCOL_BITNUM
].size
= 1;
242 objs
[ATTCOL_NAME
].data
= p
;
243 objs
[ATTCOL_NAME
].size
= strlen(p
) + 1;
244 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
,
245 &objs
[ATTCOL_NAME
], &row
); /* expected to fail */
247 return -E_ATTR_EXISTS
;
248 if (ret
!= -E_RB_KEY_NOT_FOUND
) /* error */
250 /* find smallest non-used attribute */
251 for (bitnum
= 0; bitnum
< 64; bitnum
++) {
252 objs
[ATTCOL_BITNUM
].data
= &bitnum
;
253 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
,
254 &objs
[ATTCOL_BITNUM
], &row
);
255 if (ret
== -E_RB_KEY_NOT_FOUND
)
256 break; /* this bitnum is unused, use it */
257 if (ret
< 0) /* error */
259 /* this bit is already in use, try next bit */
262 return -E_ATTR_TABLE_FULL
;
263 ret
= osl_add_row(attribute_table
, objs
);
266 greatest_att_bitnum
= PARA_MAX(greatest_att_bitnum
, bitnum
);
267 atts_added
|= 1 << bitnum
;
272 atts_added
= ~atts_added
;
273 ret
= audio_file_loop(&atts_added
, logical_and_attribute
);
276 find_greatest_att_bitnum();
277 return mood_reload();
280 int com_addatt(__a_unused
int fd
, int argc
, char * const * const argv
)
283 return -E_ATTR_SYNTAX
;
284 return send_standard_callback_request(argc
, argv
, com_addatt_callback
,
288 static int com_rmatt_callback(const struct osl_object
*query
,
289 __a_unused
struct osl_object
*result
)
291 char *p
= query
->data
;
292 int ret
, atts_removed
= 0;
293 while (p
< (char *)query
->data
+ query
->size
) {
294 struct osl_object obj
= {
296 .size
= strlen(p
) + 1
299 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
,
303 ret
= osl_del_row(attribute_table
, row
);
309 find_greatest_att_bitnum();
312 return mood_reload();
315 int com_rmatt(__a_unused
int fd
, int argc
, char * const * const argv
)
318 return -E_ATTR_SYNTAX
;
319 return send_standard_callback_request(argc
, argv
, com_rmatt_callback
,
323 void get_attribute_bitmap(uint64_t *atts
, char *buf
)
326 const uint64_t one
= 1;
328 for (i
= 0; i
<= greatest_att_bitnum
; i
++)
329 buf
[greatest_att_bitnum
- i
] = (*atts
& (one
<< i
))? 'x' : '-';
333 * Get a string containing the set attributes in text form.
335 * \param atts The attribute bitmap.
336 * \param delim The delimiter to separate matching attribute names.
337 * \param text Result pointer.
339 * \return Positive on success, negative on errors. If no attributes have
340 * been defined, \a *text is NULL.
342 int get_attribute_text(uint64_t *atts
, const char *delim
, char **text
)
345 const uint64_t one
= 1;
348 if (greatest_att_bitnum
< 0) /* no attributes available */
350 for (i
= 0; i
<= greatest_att_bitnum
; i
++) {
351 unsigned char bn
= i
;
352 struct osl_object obj
= {.data
= &bn
, .size
= 1};
355 if (!(*atts
& (one
<< i
)))
357 ret
= osl_get_row(attribute_table
, ATTCOL_BITNUM
, &obj
, &row
);
360 ret
= osl_get_object(attribute_table
, row
, ATTCOL_NAME
, &obj
);
364 char *tmp
= make_message("%s%s%s", *text
, delim
, (char *)obj
.data
);
368 *text
= para_strdup(obj
.data
);
370 if (!*text
) /* no attributes set */
371 *text
= para_strdup("");
378 void attribute_shutdown(enum osl_close_flags flags
)
380 osl_close_table(attribute_table
, flags
);
383 int attribute_init(struct table_info
*ti
)
387 ti
->desc
= &attribute_table_desc
;
388 ret
= osl_open_table(ti
->desc
, &ti
->table
);
389 greatest_att_bitnum
= -1; /* no atts available */
391 attribute_table
= ti
->table
;
392 find_greatest_att_bitnum();
395 attribute_table
= NULL
;