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