para_server: Initialize the new afs.
[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
7 static void *attribute_table;
8 static int greatest_att_bitnum;
9
10 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
11 {
12 const unsigned char *c1 = (const unsigned char*)obj1->data;
13 const unsigned char *c2 = (const unsigned char*)obj2->data;
14 if (*c1 > *c2)
15 return 1;
16 if (*c1 < *c2)
17 return -1;
18 return 0;
19 }
20
21 enum attribute_table_columns {ATTCOL_BITNUM, ATTCOL_NAME, NUM_ATT_COLUMNS};
22
23 static struct osl_column_description att_cols[] = {
24 [ATTCOL_BITNUM] = {
25 .storage_type = OSL_MAPPED_STORAGE,
26 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
27 .name = "bitnum",
28 .compare_function = char_compare,
29 .data_size = 1
30 },
31 [ATTCOL_NAME] = {
32 .storage_type = OSL_MAPPED_STORAGE,
33 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
34 .name = "name",
35 .compare_function = string_compare,
36 }
37 };
38
39 static const struct osl_table_description attribute_table_desc = {
40 .dir = DATABASE_DIR,
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 printf("%s\n", (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 printf("%u\t%s\n", *(unsigned char*)bitnum_obj.data,
104 (char *)name_obj.data);
105 return 1;
106 }
107
108 int com_lsatt(int fd, int argc, const char **argv)
109 {
110 struct private_laa_data pld = {.fd = fd, .flags = 0};
111 int i;
112
113 for (i = 1; i < argc; i++) {
114 const char *arg = argv[i];
115 if (arg[0] != '-')
116 break;
117 if (!strcmp(arg, "--")) {
118 i++;
119 break;
120 }
121 if (!strcmp(arg, "-a")) {
122 pld.flags |= LAA_FLAG_ALPHA;
123 continue;
124 }
125 if (!strcmp(arg, "-l")) {
126 pld.flags |= LAA_FLAG_LONG;
127 continue;
128 }
129 }
130 if (argc > i)
131 return -E_ATTR_SYNTAX;
132 if (pld.flags & LAA_FLAG_ALPHA)
133 return osl_rbtree_loop(attribute_table, ATTCOL_NAME,
134 &pld, log_attribute);
135 return osl_rbtree_loop(attribute_table, ATTCOL_BITNUM,
136 &pld, log_attribute);
137 }
138
139 static int com_setatt_callback(const struct osl_object *query,
140 __a_unused struct osl_object *result)
141 {
142 char *p;
143 uint64_t add_mask = 0, del_mask = 0;
144 int ret;
145 size_t len;
146 struct osl_object obj;
147 struct osl_row *row;
148
149 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
150 char c;
151
152 len = strlen(p);
153 if (!*p)
154 return -E_ATTR_SYNTAX;
155 c = p[len - 1];
156 if (c != '+' && c != '-')
157 break;
158 p[len - 1] = '\0';
159 obj.data = p;
160 obj.size = len + 1;
161 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
162 if (ret < 0)
163 return ret;
164 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
165 &obj);
166 if (ret < 0)
167 return ret;
168 if (c == '+')
169 add_mask |= (1UL << *(unsigned char *)obj.data);
170 else
171 del_mask |= (1UL << *(unsigned char *)obj.data);
172 }
173 if (!add_mask && !del_mask)
174 return -E_ATTR_SYNTAX;
175 PARA_DEBUG_LOG("masks: %llx:%llx\n", add_mask, del_mask);
176 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
177 struct afs_info old_afsi, new_afsi;
178 struct osl_row *aft_row;
179
180 len = strlen(p);
181 ret = aft_get_row_of_path(p, &aft_row);
182 if (ret < 0)
183 return ret;
184 ret = get_afsi_object_of_row(p, &obj);
185 if (ret < 0)
186 return ret;
187 ret = load_afsi(&old_afsi, &obj);
188 if (ret < 0)
189 return ret;
190 new_afsi = old_afsi;
191 new_afsi.attributes |= add_mask;
192 new_afsi.attributes &= ~del_mask;
193 save_afsi(&new_afsi, &obj); /* in-place update */
194 ret = mood_update_audio_file(aft_row, &old_afsi);
195 if (ret < 0)
196 return ret;
197 }
198 return 1;
199 }
200
201 int com_setatt(__a_unused int fd, int argc, const char **argv)
202 {
203 if (argc < 2)
204 return -E_ATTR_SYNTAX;
205 return send_standard_callback_request(argc, argv, com_setatt_callback,
206 NULL);
207 }
208
209 /* TODO: make it faster by only extracting the attribute member from afsi */
210 static int logical_and_attribute(struct osl_row *aft_row, void *attribute_ptr)
211 {
212 struct afs_info afsi;
213 uint64_t *att = attribute_ptr;
214 struct osl_object obj;
215 int ret = get_afsi_object_of_row(aft_row, &obj);
216 if (ret < 0)
217 return ret;
218 ret = load_afsi(&afsi, &obj);
219 if (ret < 0)
220 return ret;
221 afsi.attributes &= *att;
222 save_afsi(&afsi, &obj);
223 return 1;
224 }
225
226 static int com_addatt_callback(const struct osl_object *query,
227 __a_unused struct osl_object *result)
228 {
229 char *p = query->data;
230 uint64_t atts_added = 0;
231 int ret;
232
233 while (p < (char *)query->data + query->size) {
234 struct osl_object objs[NUM_ATT_COLUMNS];
235 struct osl_row *row;
236 unsigned char bitnum;
237
238 objs[ATTCOL_BITNUM].size = 1;
239 objs[ATTCOL_NAME].data = p;
240 objs[ATTCOL_NAME].size = strlen(p) + 1;
241 ret = osl_get_row(attribute_table, ATTCOL_NAME,
242 &objs[ATTCOL_NAME], &row); /* expected to fail */
243 if (ret >= 0)
244 return -E_ATTR_EXISTS;
245 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
246 return ret;
247 /* find smallest non-used attribute */
248 for (bitnum = 0; bitnum < 64; bitnum++) {
249 objs[ATTCOL_BITNUM].data = &bitnum;
250 ret = osl_get_row(attribute_table, ATTCOL_BITNUM,
251 &objs[ATTCOL_BITNUM], &row);
252 if (ret == -E_RB_KEY_NOT_FOUND)
253 break; /* this bitnum is unused, use it */
254 if (ret < 0) /* error */
255 return ret;
256 /* this bit is already in use, try next bit */
257 }
258 if (bitnum == 64)
259 return -E_ATTR_TABLE_FULL;
260 ret = osl_add_row(attribute_table, objs);
261 if (ret < 0)
262 return ret;
263 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
264 atts_added |= 1 << bitnum;
265 p += strlen(p) + 1;
266 }
267 if (!atts_added)
268 return 1;
269 atts_added = ~atts_added;
270 ret = audio_file_loop(&atts_added, logical_and_attribute);
271 if (ret < 0)
272 return ret;
273 find_greatest_att_bitnum();
274 return mood_reload();
275 }
276
277 int com_addatt(__a_unused int fd, int argc, const char **argv)
278 {
279 if (argc < 2)
280 return -E_ATTR_SYNTAX;
281 return send_standard_callback_request(argc, argv, com_addatt_callback,
282 NULL);
283 }
284
285 static int com_rmatt_callback(const struct osl_object *query,
286 __a_unused struct osl_object *result)
287 {
288 char *p = query->data;
289 int ret, atts_removed = 0;
290 while (p < (char *)query->data + query->size) {
291 struct osl_object obj = {
292 .data = p,
293 .size = strlen(p) + 1
294 };
295 struct osl_row *row;
296 ret = osl_get_row(attribute_table, ATTCOL_NAME,
297 &obj, &row);
298 if (ret < 0)
299 return ret;
300 ret = osl_del_row(attribute_table, row);
301 if (ret < 0)
302 return ret;
303 atts_removed++;
304 p += strlen(p) + 1;
305 }
306 find_greatest_att_bitnum();
307 if (!atts_removed)
308 return 1;
309 return mood_reload();
310 }
311
312 int com_rmatt(__a_unused int fd, int argc, const char **argv)
313 {
314 if (argc < 2)
315 return -E_ATTR_SYNTAX;
316 return send_standard_callback_request(argc, argv, com_rmatt_callback,
317 NULL);
318 }
319
320 void get_attribute_bitmap(uint64_t *atts, char *buf)
321 {
322 int i;
323 const uint64_t one = 1;
324
325 for (i = 0; i <= greatest_att_bitnum; i++)
326 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
327 buf[i] = '\0';
328 }
329 /**
330 * Get a string containing the set attributes in text form.
331 *
332 * \param atts The attribute bitmap.
333 * \param delim The delimiter to separate matching attribute names.
334 * \param text Result pointer.
335 *
336 * \return Positive on success, negative on errors. If no attributes have
337 * been defined, \a *text is NULL.
338 */
339 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
340 {
341 int i, ret;
342 const uint64_t one = 1;
343
344 *text = NULL;
345 if (greatest_att_bitnum < 0) /* no attributes available */
346 return 1;
347 for (i = 0; i <= greatest_att_bitnum; i++) {
348 unsigned char bn = i;
349 struct osl_object obj = {.data = &bn, .size = 1};
350 struct osl_row *row;
351
352 if (!(*atts & (one << i)))
353 continue;
354 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
355 if (ret < 0)
356 goto err;
357 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
358 if (ret < 0)
359 goto err;
360 if (*text) {
361 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
362 free(*text);
363 *text = tmp;
364 } else
365 *text = para_strdup(obj.data);
366 }
367 if (!*text) /* no attributes set */
368 *text = para_strdup("");
369 return 1;
370 err:
371 free(*text);
372 return ret;
373 }
374
375 void attribute_shutdown(enum osl_close_flags flags)
376 {
377 osl_close_table(attribute_table, flags);
378 }
379
380 int attribute_init(struct table_info *ti)
381 {
382 int ret;
383
384 ti->desc = &attribute_table_desc;
385 ret = osl_open_table(ti->desc, &ti->table);
386 greatest_att_bitnum = -1; /* no atts available */
387 if (ret >= 0) {
388 attribute_table = ti->table;
389 find_greatest_att_bitnum();
390 return ret;
391 }
392 attribute_table = NULL;
393 if (ret == -E_NOENT)
394 return 1;
395 return ret;
396 }