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