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