560e758272dfa3c4b59c78cabc6a1a8a77dade68
[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 /** Whether "-a" was given for the lsatt command. */
103 #define LSATT_FLAG_ALPHA 1
104 /** Whether "-l" was given for the lsatt command. */
105 #define LSATT_FLAG_LONG 2
106
107 /** Data passed via osl_rbtree_loop(). */
108 struct private_lsatt_data {
109         /** The given flags for the lsatt command. */
110         unsigned flags;
111         /** The result buffer. */
112         struct para_buffer b;
113 };
114
115 static int print_attribute(struct osl_row *row, void *private_data)
116 {
117         struct private_lsatt_data *pld = private_data;
118         int ret;
119         struct osl_object name_obj, bitnum_obj;
120
121         ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &name_obj);
122         if (ret < 0)
123                 return ret;
124         if (!(pld->flags & LSATT_FLAG_LONG)) {
125                 para_printf(&pld->b, "%s\n", (char *)name_obj.data);
126                 return 1;
127         }
128         ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
129         if (ret < 0)
130                 return ret;
131         para_printf(&pld->b, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
132                 (char *)name_obj.data);
133         return 1;
134 }
135
136 static int com_lsatt_callback(const struct osl_object *query,
137                 struct osl_object *result)
138 {
139         struct private_lsatt_data pld = {.flags = *(uint32_t *) query->data};
140         int ret;
141
142         if (pld.flags & LSATT_FLAG_ALPHA)
143                 ret = osl_rbtree_loop(attribute_table, ATTCOL_NAME,
144                         &pld, print_attribute);
145         else
146                 ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM,
147                         &pld, print_attribute);
148         result->data = pld.b.buf;
149         result->size = pld.b.size;
150         return ret;
151 }
152
153
154 int com_lsatt(int fd, int argc, char * const * const argv)
155 {
156         int ret, i;
157         uint32_t flags = 0;
158         struct osl_object query, result;
159
160         for (i = 1; i < argc; i++) {
161                 const char *arg = argv[i];
162                 if (arg[0] != '-')
163                         break;
164                 if (!strcmp(arg, "--")) {
165                         i++;
166                         break;
167                 }
168                 if (!strcmp(arg, "-a")) {
169                         flags |= LSATT_FLAG_ALPHA;
170                         continue;
171                 }
172                 if (!strcmp(arg, "-l")) {
173                         flags |= LSATT_FLAG_LONG;
174                         continue;
175                 }
176         }
177         if (argc > i)
178                 return -E_ATTR_SYNTAX;
179         query.data = &flags;
180         query.size = sizeof(flags);
181         ret = send_callback_request(com_lsatt_callback, &query, &result);
182         if (ret > 0) {
183                 ret = send_buffer(fd, (char *)result.data);
184                 free(result.data);
185         }
186         return ret;
187 }
188
189 static int com_setatt_callback(const struct osl_object *query,
190                 __a_unused struct osl_object *result)
191 {
192         char *p;
193         uint64_t add_mask = 0, del_mask = 0;
194         int ret;
195         size_t len;
196         struct osl_object obj;
197         struct osl_row *row;
198
199         for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
200                 char c;
201
202                 len = strlen(p);
203                 if (!*p)
204                         return -E_ATTR_SYNTAX;
205                 c = p[len - 1];
206                 if (c != '+' && c != '-')
207                         break;
208                 p[len - 1] = '\0';
209                 obj.data = p;
210                 obj.size = len + 1;
211                 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
212                 if (ret < 0)
213                         return ret;
214                 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
215                         &obj);
216                 if (ret < 0)
217                         return ret;
218                 if (c == '+')
219                         add_mask |= (1UL << *(unsigned char *)obj.data);
220                 else
221                         del_mask |= (1UL << *(unsigned char *)obj.data);
222         }
223         if (!add_mask && !del_mask)
224                 return -E_ATTR_SYNTAX;
225         PARA_DEBUG_LOG("masks: %llx:%llx\n",(long long unsigned)add_mask,
226                 (long long unsigned)del_mask);
227         for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
228                 struct afs_info old_afsi, new_afsi;
229                 struct osl_row *aft_row;
230
231                 len = strlen(p);
232                 ret = aft_get_row_of_path(p, &aft_row);
233                 if (ret < 0)
234                         return ret;
235                 ret = get_afsi_object_of_row(aft_row, &obj);
236                 if (ret < 0)
237                         return ret;
238                 ret = load_afsi(&old_afsi, &obj);
239                 if (ret < 0)
240                         return ret;
241                 new_afsi = old_afsi;
242                 new_afsi.attributes |= add_mask;
243                 new_afsi.attributes &= ~del_mask;
244                 save_afsi(&new_afsi, &obj); /* in-place update */
245 //              ret = mood_update_audio_file(aft_row, &old_afsi);
246 //              if (ret < 0)
247 //                      return ret;
248         }
249         return 1;
250 }
251
252 int com_setatt(__a_unused int fd, int argc, char * const * const argv)
253 {
254         if (argc < 2)
255                 return -E_ATTR_SYNTAX;
256         return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
257                 NULL);
258 }
259
260 /* TODO: make it faster by only extracting the attribute member from afsi */
261 static int logical_and_attribute(struct osl_row *aft_row, void *attribute_ptr)
262 {
263         struct afs_info afsi;
264         uint64_t *att = attribute_ptr;
265         struct osl_object obj;
266         int ret = get_afsi_object_of_row(aft_row, &obj);
267         if (ret < 0)
268                 return ret;
269         ret = load_afsi(&afsi, &obj);
270         if (ret < 0)
271                 return ret;
272         afsi.attributes &= *att;
273         save_afsi(&afsi, &obj);
274         return 1;
275 }
276
277 static int com_addatt_callback(const struct osl_object *query,
278                 __a_unused struct osl_object *result)
279 {
280         char *p = query->data;
281         uint64_t atts_added = 0;
282         int ret;
283
284         while (p < (char *)query->data + query->size) {
285                 struct osl_object objs[NUM_ATT_COLUMNS];
286                 struct osl_row *row;
287                 unsigned char bitnum;
288
289                 objs[ATTCOL_BITNUM].size = 1;
290                 objs[ATTCOL_NAME].data = p;
291                 objs[ATTCOL_NAME].size = strlen(p) + 1;
292                 ret = osl_get_row(attribute_table, ATTCOL_NAME,
293                         &objs[ATTCOL_NAME], &row); /* expected to fail */
294                 if (ret >= 0)
295                         return -E_ATTR_EXISTS;
296                 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
297                         return ret;
298                 /* find smallest non-used attribute */
299                 for (bitnum = 0; bitnum < 64; bitnum++) {
300                         objs[ATTCOL_BITNUM].data = &bitnum;
301                         ret = osl_get_row(attribute_table, ATTCOL_BITNUM,
302                                 &objs[ATTCOL_BITNUM], &row);
303                         if (ret == -E_RB_KEY_NOT_FOUND)
304                                 break; /* this bitnum is unused, use it */
305                         if (ret < 0) /* error */
306                                 return ret;
307                         /* this bit is already in use, try next bit */
308                 }
309                 if (bitnum == 64)
310                         return -E_ATTR_TABLE_FULL;
311                 ret = osl_add_row(attribute_table, objs);
312                 if (ret < 0)
313                         return ret;
314                 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
315                 atts_added |= 1 << bitnum;
316                 p += strlen(p) + 1;
317         }
318         if (!atts_added)
319                 return 1;
320         atts_added = ~atts_added;
321         ret = audio_file_loop(&atts_added, logical_and_attribute);
322         if (ret < 0)
323                 return ret;
324         find_greatest_att_bitnum();
325         return reload_current_mood(); /* FIXME: mood_reload() returns an error */
326 }
327
328 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
329 {
330         if (argc < 2)
331                 return -E_ATTR_SYNTAX;
332         return send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
333                 NULL);
334 }
335 struct remove_attribute_action_data {
336         struct para_buffer pb;
337         int num_removed;
338 };
339
340 static int remove_attribute(struct osl_table *table, struct osl_row *row,
341                 const char *name, void *data)
342 {
343         struct remove_attribute_action_data *raad = data;
344         int ret = osl_del_row(table, row);
345         if (ret < 0)
346                 para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
347         else {
348                 para_printf(&raad->pb, "removed %s\n", name);
349                 raad->num_removed++;
350         }
351         return 1;
352 }
353
354 static int com_rmatt_callback(const struct osl_object *query,
355                 struct osl_object *result)
356 {
357         struct remove_attribute_action_data raad = {.num_removed = 0};
358         int ret;
359         struct pattern_match_data pmd = {
360                 .table = attribute_table,
361                 .patterns = *query,
362                 .loop_col_num = ATTCOL_BITNUM,
363                 .match_col_num = ATTCOL_NAME,
364                 .data = &raad,
365                 .action = remove_attribute
366         };
367         ret = for_each_matching_row(&pmd);
368         if (ret < 0)
369                 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
370         if (raad.num_removed) {
371                 find_greatest_att_bitnum();
372                 ret = reload_current_mood();
373                 if (ret < 0)
374                         para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
375         }
376         if (!raad.pb.buf)
377                 para_printf(&raad.pb, "no match -- nothing removed\n");
378         result->data = raad.pb.buf;
379         result->size = raad.pb.size;
380         return 1;
381 }
382
383 int com_rmatt(int fd, int argc, char * const * const argv)
384 {
385         int ret;
386         struct osl_object result;
387
388         if (argc < 2)
389                 return -E_ATTR_SYNTAX;
390         ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
391                 &result);
392         if (ret > 0) {
393                 send_buffer(fd, (char *)result.data);
394                 free(result.data);
395         } else
396                 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
397         return ret;
398 }
399
400 /**
401  * Return a binary representation of the geiven attribute value.
402  *
403  * \param atts Pointer to the attribute value.
404  * \param buf Result.
405  *
406  * This function prints a string of at most 64 characters plus the terminating
407  * \p NULL character into \a buf which must be provided by the caller and at
408  * least 65 bytes long. The "x" character is used for set attributes and "-" is
409  * used for unset attributes.
410  *
411  * In practice, not all 64 attributes are defined. In this case, the function
412  * only prints \a N + 1 charaters where \a N is the greatest id of a defined
413  * attribute.
414  */
415 void get_attribute_bitmap(const uint64_t *atts, char *buf)
416 {
417         int i;
418         const uint64_t one = 1;
419
420         for (i = 0; i <= greatest_att_bitnum; i++)
421                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
422         buf[i] = '\0';
423 }
424
425 /**
426  * Get a string containing the set attributes in text form.
427  *
428  * \param atts The attribute bitmap.
429  * \param delim The delimiter to separate matching attribute names.
430  * \param text Result pointer.
431  *
432  * \return Positive on success, negative on errors. If no attributes have
433  * been defined, \a *text is NULL.
434  */
435 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
436 {
437         int i, ret;
438         const uint64_t one = 1;
439
440         *text = NULL;
441         if (greatest_att_bitnum < 0) /* no attributes available */
442                 return 1;
443         for (i = 0; i <= greatest_att_bitnum; i++) {
444                 unsigned char bn = i;
445                 struct osl_object obj = {.data = &bn, .size = 1};
446                 struct osl_row *row;
447
448                 if (!(*atts & (one << i)))
449                         continue;
450                 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
451                 if (ret < 0)
452                         goto err;
453                 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
454                 if (ret < 0)
455                         goto err;
456                 if (*text) {
457                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
458                         free(*text);
459                         *text = tmp;
460                 } else
461                         *text = para_strdup(obj.data);
462         }
463         if (!*text) /* no attributes set */
464                 *text = para_strdup("");
465         return 1;
466 err:
467         free(*text);
468         return ret;
469 }
470
471 /**
472  * Close the attribute table.
473  *
474  * \param flags Ususal flags that are passed to osl_close_table().
475  *
476  * \sa osl_close_table().
477  */
478 void attribute_shutdown(enum osl_close_flags flags)
479 {
480         osl_close_table(attribute_table, flags);
481         attribute_table = NULL;
482 }
483
484 /**
485  * Open the attribute table.
486  *
487  * \param ti Gets initialized by this function.
488  * \param db The database directory.
489  *
490  * \return Positive on success, negative on errors.
491  *
492  * \sa osl_open_table().
493  */
494 int attribute_init(struct table_info *ti, const char *db)
495 {
496         int ret;
497
498         attribute_table_desc.dir = db;
499         ti->desc = &attribute_table_desc;
500         ret = osl_open_table(ti->desc, &attribute_table);
501         greatest_att_bitnum = -1; /* no atts available */
502         if (ret >= 0) {
503                 find_greatest_att_bitnum();
504                 return ret;
505         }
506         attribute_table = NULL;
507         if (ret == -E_NOENT)
508                 return 1;
509         return ret;
510 }