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 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 send_buffer(pld
->fd
, (char *)name_obj
.data
);
100 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
, &bitnum_obj
);
103 send_va_buffer(pld
->fd
, "%u\t%s\n", *(unsigned char*)bitnum_obj
.data
,
104 (char *)name_obj
.data
);
108 /* FIXME: Need callback */
109 int com_lsatt(int fd
, int argc
, char * const * const argv
)
111 struct private_laa_data pld
= {.fd
= fd
, .flags
= 0};
114 for (i
= 1; i
< argc
; i
++) {
115 const char *arg
= argv
[i
];
118 if (!strcmp(arg
, "--")) {
122 if (!strcmp(arg
, "-a")) {
123 pld
.flags
|= LAA_FLAG_ALPHA
;
126 if (!strcmp(arg
, "-l")) {
127 pld
.flags
|= LAA_FLAG_LONG
;
132 return -E_ATTR_SYNTAX
;
133 if (pld
.flags
& LAA_FLAG_ALPHA
)
134 return osl_rbtree_loop(attribute_table
, ATTCOL_NAME
,
135 &pld
, log_attribute
);
136 return osl_rbtree_loop(attribute_table
, ATTCOL_BITNUM
,
137 &pld
, log_attribute
);
140 static int com_setatt_callback(const struct osl_object
*query
,
141 __a_unused
struct osl_object
*result
)
144 uint64_t add_mask
= 0, del_mask
= 0;
147 struct osl_object obj
;
150 for (p
= query
->data
; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
155 return -E_ATTR_SYNTAX
;
157 if (c
!= '+' && c
!= '-')
162 ret
= osl_get_row(attribute_table
, ATTCOL_NAME
, &obj
, &row
);
165 ret
= osl_get_object(attribute_table
, row
, ATTCOL_BITNUM
,
170 add_mask
|= (1UL << *(unsigned char *)obj
.data
);
172 del_mask
|= (1UL << *(unsigned char *)obj
.data
);
174 if (!add_mask
&& !del_mask
)
175 return -E_ATTR_SYNTAX
;
176 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask
,
177 (long long unsigned)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
, char * const * const 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
, char * const * const 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
, char * const * const 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
);
380 attribute_table
= NULL
;
383 int attribute_init(struct table_info
*ti
, const char *db
)
387 attribute_table_desc
.dir
= db
;
388 ti
->desc
= &attribute_table_desc
;
389 ret
= osl_open_table(ti
->desc
, &ti
->table
);
390 greatest_att_bitnum
= -1; /* no atts available */
392 attribute_table
= ti
->table
;
393 find_greatest_att_bitnum();
396 attribute_table
= NULL
;