b8d4a5938beb792e5e5c7ebca62548407a91d12a
[paraslash.git] / attribute.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file attribute.c Attribute handling functions. */
8 #include "para.h"
9 #include "error.h"
10 #include "afh.h"
11 #include "afs.h"
12 #include "string.h"
13 #include "net.h"
14
15 static struct osl_table *attribute_table;
16 static int greatest_att_bitnum;
17
18 /** The columns of the attribute table. */
19 enum attribute_table_columns {
20 /** The bit number (0-63). */
21 ATTCOL_BITNUM,
22 /** The name of the attribute. */
23 ATTCOL_NAME,
24 /** Number of columns in this table. */
25 NUM_ATT_COLUMNS
26 };
27
28 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
29 {
30 const unsigned char *c1 = (const unsigned char*)obj1->data;
31 const unsigned char *c2 = (const unsigned char*)obj2->data;
32 if (*c1 > *c2)
33 return 1;
34 if (*c1 < *c2)
35 return -1;
36 return 0;
37 }
38
39 static struct osl_column_description att_cols[] = {
40 [ATTCOL_BITNUM] = {
41 .storage_type = OSL_MAPPED_STORAGE,
42 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
43 .name = "bitnum",
44 .compare_function = char_compare,
45 .data_size = 1
46 },
47 [ATTCOL_NAME] = {
48 .storage_type = OSL_MAPPED_STORAGE,
49 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
50 .name = "name",
51 .compare_function = string_compare,
52 }
53 };
54
55 static struct osl_table_description attribute_table_desc = {
56 .name = "attributes",
57 .num_columns = NUM_ATT_COLUMNS,
58 .flags = 0,
59 .column_descriptions = att_cols
60 };
61
62 static void find_greatest_att_bitnum(void)
63 {
64 unsigned char c = 63;
65 do {
66 struct osl_row *row;
67 struct osl_object obj = {.data = &c, .size = 1};
68 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
69 &row) >= 0) {
70 greatest_att_bitnum = c;
71 return;
72 }
73 } while (c--);
74 PARA_INFO_LOG("%s\n", "no attributes");
75 greatest_att_bitnum = -E_NO_ATTRIBUTES;
76 }
77
78 /**
79 * Retrieve the identifier (number) of an attribute.
80 *
81 * \param att_name The name of the attribute.
82 * \param bitnum Result pointer.
83 *
84 * \return Positive on success, negative on errors.
85 */
86 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
87 {
88 struct osl_object obj = {.data = (char *)att_name,
89 .size = strlen(att_name) + 1};
90 struct osl_row *row;
91 int ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
92
93 if (ret < 0)
94 return ret;
95 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj);
96 if (ret < 0)
97 return ret;
98 *bitnum = *(unsigned char *)obj.data;
99 return 1;
100 }
101
102 enum lsatt_flags {
103 /** Whether "-a" was given for the lsatt command. */
104 LSATT_FLAG_SORT_BY_ID = 1,
105 /** Whether "-l" was given for the lsatt command. */
106 LSATT_FLAG_LONG = 2,
107 LSATT_FLAG_REVERSE = 4
108 };
109
110 /** Data passed to the action function of lsatt */
111 struct lsatt_action_data {
112 /** The result buffer. */
113 struct para_buffer pb;
114 /** The given flags for the lsatt command. */
115 unsigned flags;
116 };
117
118 static int print_attribute(struct osl_table *table, struct osl_row *row,
119 const char *name, void *data)
120 {
121 struct lsatt_action_data *laad = data;
122 struct osl_object bitnum_obj;
123 int ret;
124
125 if (!(laad->flags & LSATT_FLAG_LONG)) {
126 para_printf(&laad->pb, "%s\n", name);
127 return 1;
128 }
129 ret = osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj);
130 if (ret < 0) {
131 para_printf(&laad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
132 return ret;
133 }
134 para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
135 name);
136 return 1;
137 }
138
139 static int com_lsatt_callback(const struct osl_object *query,
140 struct osl_object *result)
141 {
142 struct lsatt_action_data laad = {.flags = *(unsigned *) query->data};
143 struct pattern_match_data pmd = {
144 .table = attribute_table,
145 .loop_col_num = ATTCOL_BITNUM,
146 .match_col_num = ATTCOL_NAME,
147 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
148 .size = query->size - sizeof(laad.flags)},
149 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
150 .data = &laad,
151 .action = print_attribute
152 };
153 int ret;
154
155 if (laad.flags & LSATT_FLAG_SORT_BY_ID)
156 pmd.loop_col_num = ATTCOL_NAME;
157 if (laad.flags & LSATT_FLAG_REVERSE)
158 pmd.pm_flags |= PM_REVERSE_LOOP;
159 ret = for_each_matching_row(&pmd);
160 if (ret < 0)
161 para_printf(&laad.pb, "%s\n", PARA_STRERROR(-ret));
162 if (!laad.pb.buf)
163 return 0;
164 result->data = laad.pb.buf;
165 result->size = laad.pb.size;
166 return 1;
167 }
168
169 int com_lsatt(int fd, int argc, char * const * const argv)
170 {
171 unsigned flags = 0;
172 struct osl_object options = {.data = &flags, .size = sizeof(flags)},
173 result;
174 int ret, i;
175
176 for (i = 1; i < argc; i++) {
177 const char *arg = argv[i];
178 if (arg[0] != '-')
179 break;
180 if (!strcmp(arg, "--")) {
181 i++;
182 break;
183 }
184 if (!strcmp(arg, "-i")) {
185 flags |= LSATT_FLAG_SORT_BY_ID;
186 continue;
187 }
188 if (!strcmp(arg, "-l")) {
189 flags |= LSATT_FLAG_LONG;
190 continue;
191 }
192 if (!strcmp(arg, "-r")) {
193 flags |= LSATT_FLAG_REVERSE;
194 continue;
195 }
196 }
197 ret = send_option_arg_callback_request(&options, argc -i, argv + i,
198 com_lsatt_callback, &result);
199 if (ret > 0) {
200 ret = send_buffer(fd, (char *)result.data);
201 free(result.data);
202 } else
203 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
204 return ret;
205 }
206
207 static int com_setatt_callback(const struct osl_object *query,
208 __a_unused struct osl_object *result)
209 {
210 char *p;
211 uint64_t add_mask = 0, del_mask = 0;
212 int ret;
213 size_t len;
214 struct osl_object obj;
215 struct osl_row *row;
216
217 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
218 char c;
219
220 len = strlen(p);
221 if (!*p)
222 return -E_ATTR_SYNTAX;
223 c = p[len - 1];
224 if (c != '+' && c != '-')
225 break;
226 p[len - 1] = '\0';
227 obj.data = p;
228 obj.size = len + 1;
229 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
230 if (ret < 0)
231 return ret;
232 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
233 &obj);
234 if (ret < 0)
235 return ret;
236 if (c == '+')
237 add_mask |= (1UL << *(unsigned char *)obj.data);
238 else
239 del_mask |= (1UL << *(unsigned char *)obj.data);
240 }
241 if (!add_mask && !del_mask)
242 return -E_ATTR_SYNTAX;
243 PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask,
244 (long long unsigned)del_mask);
245 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
246 struct afs_info old_afsi, new_afsi;
247 struct osl_row *aft_row;
248
249 len = strlen(p);
250 ret = aft_get_row_of_path(p, &aft_row);
251 if (ret < 0)
252 return ret;
253 ret = get_afsi_object_of_row(aft_row, &obj);
254 if (ret < 0)
255 return ret;
256 ret = load_afsi(&old_afsi, &obj);
257 if (ret < 0)
258 return ret;
259 new_afsi = old_afsi;
260 new_afsi.attributes |= add_mask;
261 new_afsi.attributes &= ~del_mask;
262 save_afsi(&new_afsi, &obj); /* in-place update */
263 // ret = mood_update_audio_file(aft_row, &old_afsi);
264 // if (ret < 0)
265 // return ret;
266 }
267 return 1;
268 }
269
270 int com_setatt(__a_unused int fd, int argc, char * const * const argv)
271 {
272 if (argc < 2)
273 return -E_ATTR_SYNTAX;
274 return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
275 NULL);
276 }
277
278 /* TODO: make it faster by only extracting the attribute member from afsi */
279 static int logical_and_attribute(struct osl_row *aft_row, void *attribute_ptr)
280 {
281 struct afs_info afsi;
282 uint64_t *att = attribute_ptr;
283 struct osl_object obj;
284 int ret = get_afsi_object_of_row(aft_row, &obj);
285 if (ret < 0)
286 return ret;
287 ret = load_afsi(&afsi, &obj);
288 if (ret < 0)
289 return ret;
290 afsi.attributes &= *att;
291 save_afsi(&afsi, &obj);
292 return 1;
293 }
294
295 static int com_addatt_callback(const struct osl_object *query,
296 __a_unused struct osl_object *result)
297 {
298 char *p = query->data;
299 uint64_t atts_added = 0;
300 int ret;
301
302 while (p < (char *)query->data + query->size) {
303 struct osl_object objs[NUM_ATT_COLUMNS];
304 struct osl_row *row;
305 unsigned char bitnum;
306
307 objs[ATTCOL_BITNUM].size = 1;
308 objs[ATTCOL_NAME].data = p;
309 objs[ATTCOL_NAME].size = strlen(p) + 1;
310 ret = osl_get_row(attribute_table, ATTCOL_NAME,
311 &objs[ATTCOL_NAME], &row); /* expected to fail */
312 if (ret >= 0)
313 return -E_ATTR_EXISTS;
314 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
315 return ret;
316 /* find smallest non-used attribute */
317 for (bitnum = 0; bitnum < 64; bitnum++) {
318 objs[ATTCOL_BITNUM].data = &bitnum;
319 ret = osl_get_row(attribute_table, ATTCOL_BITNUM,
320 &objs[ATTCOL_BITNUM], &row);
321 if (ret == -E_RB_KEY_NOT_FOUND)
322 break; /* this bitnum is unused, use it */
323 if (ret < 0) /* error */
324 return ret;
325 /* this bit is already in use, try next bit */
326 }
327 if (bitnum == 64)
328 return -E_ATTR_TABLE_FULL;
329 ret = osl_add_row(attribute_table, objs);
330 if (ret < 0)
331 return ret;
332 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
333 atts_added |= 1 << bitnum;
334 p += strlen(p) + 1;
335 }
336 if (!atts_added)
337 return 1;
338 atts_added = ~atts_added;
339 ret = audio_file_loop(&atts_added, logical_and_attribute);
340 if (ret < 0)
341 return ret;
342 find_greatest_att_bitnum();
343 return reload_current_mood(); /* FIXME: mood_reload() returns an error */
344 }
345
346 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
347 {
348 if (argc < 2)
349 return -E_ATTR_SYNTAX;
350 return send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
351 NULL);
352 }
353 struct remove_attribute_action_data {
354 struct para_buffer pb;
355 int num_removed;
356 };
357
358 static int remove_attribute(struct osl_table *table, struct osl_row *row,
359 const char *name, void *data)
360 {
361 struct remove_attribute_action_data *raad = data;
362 int ret = osl_del_row(table, row);
363 if (ret < 0)
364 para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
365 else {
366 para_printf(&raad->pb, "removed %s\n", name);
367 raad->num_removed++;
368 }
369 return 1;
370 }
371
372 static int com_rmatt_callback(const struct osl_object *query,
373 struct osl_object *result)
374 {
375 struct remove_attribute_action_data raad = {.num_removed = 0};
376 int ret;
377 struct pattern_match_data pmd = {
378 .table = attribute_table,
379 .patterns = *query,
380 .loop_col_num = ATTCOL_BITNUM,
381 .match_col_num = ATTCOL_NAME,
382 .data = &raad,
383 .action = remove_attribute
384 };
385 ret = for_each_matching_row(&pmd);
386 if (ret < 0)
387 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
388 if (raad.num_removed) {
389 find_greatest_att_bitnum();
390 ret = reload_current_mood();
391 if (ret < 0)
392 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
393 }
394 if (!raad.pb.buf)
395 para_printf(&raad.pb, "no match -- nothing removed\n");
396 result->data = raad.pb.buf;
397 result->size = raad.pb.size;
398 return 1;
399 }
400
401 int com_rmatt(int fd, int argc, char * const * const argv)
402 {
403 int ret;
404 struct osl_object result;
405
406 if (argc < 2)
407 return -E_ATTR_SYNTAX;
408 ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
409 &result);
410 if (ret > 0) {
411 send_buffer(fd, (char *)result.data);
412 free(result.data);
413 } else
414 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
415 return ret;
416 }
417
418 /**
419 * Return a binary representation of the geiven attribute value.
420 *
421 * \param atts Pointer to the attribute value.
422 * \param buf Result.
423 *
424 * This function prints a string of at most 64 characters plus the terminating
425 * \p NULL character into \a buf which must be provided by the caller and at
426 * least 65 bytes long. The "x" character is used for set attributes and "-" is
427 * used for unset attributes.
428 *
429 * In practice, not all 64 attributes are defined. In this case, the function
430 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
431 * attribute.
432 */
433 void get_attribute_bitmap(const uint64_t *atts, char *buf)
434 {
435 int i;
436 const uint64_t one = 1;
437
438 for (i = 0; i <= greatest_att_bitnum; i++)
439 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
440 buf[i] = '\0';
441 }
442
443 /**
444 * Get a string containing the set attributes in text form.
445 *
446 * \param atts The attribute bitmap.
447 * \param delim The delimiter to separate matching attribute names.
448 * \param text Result pointer.
449 *
450 * \return Positive on success, negative on errors. If no attributes have
451 * been defined, \a *text is NULL.
452 */
453 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
454 {
455 int i, ret;
456 const uint64_t one = 1;
457
458 *text = NULL;
459 if (greatest_att_bitnum < 0) /* no attributes available */
460 return 1;
461 for (i = 0; i <= greatest_att_bitnum; i++) {
462 unsigned char bn = i;
463 struct osl_object obj = {.data = &bn, .size = 1};
464 struct osl_row *row;
465
466 if (!(*atts & (one << i)))
467 continue;
468 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
469 if (ret < 0)
470 goto err;
471 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
472 if (ret < 0)
473 goto err;
474 if (*text) {
475 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
476 free(*text);
477 *text = tmp;
478 } else
479 *text = para_strdup(obj.data);
480 }
481 if (!*text) /* no attributes set */
482 *text = para_strdup("");
483 return 1;
484 err:
485 free(*text);
486 return ret;
487 }
488
489 /**
490 * Close the attribute table.
491 *
492 * \param flags Ususal flags that are passed to osl_close_table().
493 *
494 * \sa osl_close_table().
495 */
496 void attribute_shutdown(enum osl_close_flags flags)
497 {
498 osl_close_table(attribute_table, flags);
499 attribute_table = NULL;
500 }
501
502 /**
503 * Open the attribute table.
504 *
505 * \param ti Gets initialized by this function.
506 * \param db The database directory.
507 *
508 * \return Positive on success, negative on errors.
509 *
510 * \sa osl_open_table().
511 */
512 int attribute_init(struct table_info *ti, const char *db)
513 {
514 int ret;
515
516 attribute_table_desc.dir = db;
517 ti->desc = &attribute_table_desc;
518 ret = osl_open_table(ti->desc, &attribute_table);
519 greatest_att_bitnum = -1; /* no atts available */
520 if (ret >= 0) {
521 find_greatest_att_bitnum();
522 return ret;
523 }
524 attribute_table = NULL;
525 if (ret == -E_NOENT)
526 return 1;
527 return ret;
528 }