Rename osl index entry stuff.
[paraslash.git] / attribute.c
1 #include "para.h"
2 #include "error.h"
3 #include "afh.h"
4 #include "afs.h"
5 #include "string.h"
6 #include "net.h"
7
8 static void *attribute_table;
9 static int greatest_att_bitnum;
10
11 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
12 {
13 const unsigned char *c1 = (const unsigned char*)obj1->data;
14 const unsigned char *c2 = (const unsigned char*)obj2->data;
15 if (*c1 > *c2)
16 return 1;
17 if (*c1 < *c2)
18 return -1;
19 return 0;
20 }
21
22 enum attribute_table_columns {ATTCOL_BITNUM, ATTCOL_NAME, NUM_ATT_COLUMNS};
23
24 static struct osl_column_description att_cols[] = {
25 [ATTCOL_BITNUM] = {
26 .storage_type = OSL_MAPPED_STORAGE,
27 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
28 .name = "bitnum",
29 .compare_function = char_compare,
30 .data_size = 1
31 },
32 [ATTCOL_NAME] = {
33 .storage_type = OSL_MAPPED_STORAGE,
34 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
35 .name = "name",
36 .compare_function = string_compare,
37 }
38 };
39
40 static struct osl_table_description attribute_table_desc = {
41 .name = "attributes",
42 .num_columns = NUM_ATT_COLUMNS,
43 .flags = 0,
44 .column_descriptions = att_cols
45 };
46
47 static void find_greatest_att_bitnum(void)
48 {
49 unsigned char c = 63;
50 do {
51 struct osl_row *row;
52 struct osl_object obj = {.data = &c, .size = 1};
53 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
54 &row) >= 0) {
55 greatest_att_bitnum = c;
56 return;
57 }
58 } while (c--);
59 PARA_INFO_LOG("%s\n", "no attributes");
60 greatest_att_bitnum = -E_NO_ATTRIBUTES;
61 }
62
63 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
64 {
65 struct osl_object obj = {.data = (char *)att_name,
66 .size = strlen(att_name) + 1};
67 struct osl_row *row;
68 int ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
69
70 if (ret < 0)
71 return ret;
72 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj);
73 if (ret < 0)
74 return ret;
75 *bitnum = *(unsigned char *)obj.data;
76 return 1;
77 }
78
79 #define LAA_FLAG_ALPHA 1
80 #define LAA_FLAG_LONG 2
81
82 struct private_laa_data {
83 int fd;
84 unsigned flags;
85 };
86
87 static int log_attribute(struct osl_row *row, void *private_data)
88 {
89 struct private_laa_data *pld = private_data;
90 int ret;
91 struct osl_object name_obj, bitnum_obj;
92
93 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &name_obj);
94 if (ret < 0)
95 return ret;
96 if (!(pld->flags & LAA_FLAG_LONG)) {
97 send_buffer(pld->fd, (char *)name_obj.data);
98 return 1;
99 }
100 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
101 if (ret < 0)
102 return ret;
103 send_va_buffer(pld->fd, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
104 (char *)name_obj.data);
105 return 1;
106 }
107
108 /* FIXME: Need callback */
109 int com_lsatt(int fd, int argc, char * const * const argv)
110 {
111 struct private_laa_data pld = {.fd = fd, .flags = 0};
112 int i;
113
114 for (i = 1; i < argc; i++) {
115 const char *arg = argv[i];
116 if (arg[0] != '-')
117 break;
118 if (!strcmp(arg, "--")) {
119 i++;
120 break;
121 }
122 if (!strcmp(arg, "-a")) {
123 pld.flags |= LAA_FLAG_ALPHA;
124 continue;
125 }
126 if (!strcmp(arg, "-l")) {
127 pld.flags |= LAA_FLAG_LONG;
128 continue;
129 }
130 }
131 if (argc > i)
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);
138 }
139
140 static int com_setatt_callback(const struct osl_object *query,
141 __a_unused struct osl_object *result)
142 {
143 char *p;
144 uint64_t add_mask = 0, del_mask = 0;
145 int ret;
146 size_t len;
147 struct osl_object obj;
148 struct osl_row *row;
149
150 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
151 char c;
152
153 len = strlen(p);
154 if (!*p)
155 return -E_ATTR_SYNTAX;
156 c = p[len - 1];
157 if (c != '+' && c != '-')
158 break;
159 p[len - 1] = '\0';
160 obj.data = p;
161 obj.size = len + 1;
162 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
163 if (ret < 0)
164 return ret;
165 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
166 &obj);
167 if (ret < 0)
168 return ret;
169 if (c == '+')
170 add_mask |= (1UL << *(unsigned char *)obj.data);
171 else
172 del_mask |= (1UL << *(unsigned char *)obj.data);
173 }
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;
181
182 len = strlen(p);
183 ret = aft_get_row_of_path(p, &aft_row);
184 if (ret < 0)
185 return ret;
186 ret = get_afsi_object_of_row(p, &obj);
187 if (ret < 0)
188 return ret;
189 ret = load_afsi(&old_afsi, &obj);
190 if (ret < 0)
191 return ret;
192 new_afsi = old_afsi;
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);
197 if (ret < 0)
198 return ret;
199 }
200 return 1;
201 }
202
203 int com_setatt(__a_unused int fd, int argc, char * const * const argv)
204 {
205 if (argc < 2)
206 return -E_ATTR_SYNTAX;
207 return send_standard_callback_request(argc, argv, com_setatt_callback,
208 NULL);
209 }
210
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)
213 {
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);
218 if (ret < 0)
219 return ret;
220 ret = load_afsi(&afsi, &obj);
221 if (ret < 0)
222 return ret;
223 afsi.attributes &= *att;
224 save_afsi(&afsi, &obj);
225 return 1;
226 }
227
228 static int com_addatt_callback(const struct osl_object *query,
229 __a_unused struct osl_object *result)
230 {
231 char *p = query->data;
232 uint64_t atts_added = 0;
233 int ret;
234
235 while (p < (char *)query->data + query->size) {
236 struct osl_object objs[NUM_ATT_COLUMNS];
237 struct osl_row *row;
238 unsigned char bitnum;
239
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 */
245 if (ret >= 0)
246 return -E_ATTR_EXISTS;
247 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
248 return ret;
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 */
257 return ret;
258 /* this bit is already in use, try next bit */
259 }
260 if (bitnum == 64)
261 return -E_ATTR_TABLE_FULL;
262 ret = osl_add_row(attribute_table, objs);
263 if (ret < 0)
264 return ret;
265 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
266 atts_added |= 1 << bitnum;
267 p += strlen(p) + 1;
268 }
269 if (!atts_added)
270 return 1;
271 atts_added = ~atts_added;
272 ret = audio_file_loop(&atts_added, logical_and_attribute);
273 if (ret < 0)
274 return ret;
275 find_greatest_att_bitnum();
276 return mood_reload();
277 }
278
279 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
280 {
281 if (argc < 2)
282 return -E_ATTR_SYNTAX;
283 return send_standard_callback_request(argc, argv, com_addatt_callback,
284 NULL);
285 }
286
287 static int com_rmatt_callback(const struct osl_object *query,
288 __a_unused struct osl_object *result)
289 {
290 char *p = query->data;
291 int ret, atts_removed = 0;
292 while (p < (char *)query->data + query->size) {
293 struct osl_object obj = {
294 .data = p,
295 .size = strlen(p) + 1
296 };
297 struct osl_row *row;
298 ret = osl_get_row(attribute_table, ATTCOL_NAME,
299 &obj, &row);
300 if (ret < 0)
301 return ret;
302 ret = osl_del_row(attribute_table, row);
303 if (ret < 0)
304 return ret;
305 atts_removed++;
306 p += strlen(p) + 1;
307 }
308 find_greatest_att_bitnum();
309 if (!atts_removed)
310 return 1;
311 return mood_reload();
312 }
313
314 int com_rmatt(__a_unused int fd, int argc, char * const * const argv)
315 {
316 if (argc < 2)
317 return -E_ATTR_SYNTAX;
318 return send_standard_callback_request(argc, argv, com_rmatt_callback,
319 NULL);
320 }
321
322 void get_attribute_bitmap(uint64_t *atts, char *buf)
323 {
324 int i;
325 const uint64_t one = 1;
326
327 for (i = 0; i <= greatest_att_bitnum; i++)
328 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
329 buf[i] = '\0';
330 }
331 /**
332 * Get a string containing the set attributes in text form.
333 *
334 * \param atts The attribute bitmap.
335 * \param delim The delimiter to separate matching attribute names.
336 * \param text Result pointer.
337 *
338 * \return Positive on success, negative on errors. If no attributes have
339 * been defined, \a *text is NULL.
340 */
341 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
342 {
343 int i, ret;
344 const uint64_t one = 1;
345
346 *text = NULL;
347 if (greatest_att_bitnum < 0) /* no attributes available */
348 return 1;
349 for (i = 0; i <= greatest_att_bitnum; i++) {
350 unsigned char bn = i;
351 struct osl_object obj = {.data = &bn, .size = 1};
352 struct osl_row *row;
353
354 if (!(*atts & (one << i)))
355 continue;
356 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
357 if (ret < 0)
358 goto err;
359 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
360 if (ret < 0)
361 goto err;
362 if (*text) {
363 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
364 free(*text);
365 *text = tmp;
366 } else
367 *text = para_strdup(obj.data);
368 }
369 if (!*text) /* no attributes set */
370 *text = para_strdup("");
371 return 1;
372 err:
373 free(*text);
374 return ret;
375 }
376
377 void attribute_shutdown(enum osl_close_flags flags)
378 {
379 osl_close_table(attribute_table, flags);
380 }
381
382 int attribute_init(struct table_info *ti, const char *db)
383 {
384 int ret;
385
386 attribute_table_desc.dir = db;
387 ti->desc = &attribute_table_desc;
388 ret = osl_open_table(ti->desc, &ti->table);
389 greatest_att_bitnum = -1; /* no atts available */
390 if (ret >= 0) {
391 attribute_table = ti->table;
392 find_greatest_att_bitnum();
393 return ret;
394 }
395 attribute_table = NULL;
396 if (ret == -E_NOENT)
397 return 1;
398 return ret;
399 }