com_ls: Don't use FNM_PATHNAME.
[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         unsigned 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++;
334                 p += strlen(p) + 1;
335         }
336         if (!atts_added)
337                 return 1;
338         find_greatest_att_bitnum();
339         return reload_current_mood(); /* FIXME: returns an error */
340 }
341
342 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
343 {
344         if (argc < 2)
345                 return -E_ATTR_SYNTAX;
346         return send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
347                 NULL);
348 }
349 struct remove_attribute_action_data {
350         struct para_buffer pb;
351         int num_removed;
352         uint64_t mask_of_removed_atts;
353 };
354
355 static int remove_attribute(struct osl_table *table, struct osl_row *row,
356                 const char *name, void *data)
357 {
358         struct remove_attribute_action_data *raad = data;
359         int ret = osl_del_row(table, row);
360         unsigned char bitnum;
361
362         if (ret < 0) {
363                 para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
364                 return 1;
365         }
366         ret = get_attribute_bitnum_by_name(name, &bitnum);
367         if (ret < 0) {
368                 para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
369                 return 1;
370         }
371         para_printf(&raad->pb, "removed %s\n", name);
372         raad->num_removed++;
373         raad->mask_of_removed_atts |= (1 << bitnum);
374         return 1;
375 }
376
377 static int com_rmatt_callback(const struct osl_object *query,
378                 struct osl_object *result)
379 {
380         struct remove_attribute_action_data raad = {.num_removed = 0};
381         int ret;
382         struct pattern_match_data pmd = {
383                 .table = attribute_table,
384                 .patterns = *query,
385                 .loop_col_num = ATTCOL_BITNUM,
386                 .match_col_num = ATTCOL_NAME,
387                 .data = &raad,
388                 .action = remove_attribute
389         };
390         ret = for_each_matching_row(&pmd);
391         if (ret < 0)
392                 para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
393         if (raad.num_removed) {
394                 uint64_t and_mask = ~raad.mask_of_removed_atts;
395                 ret = audio_file_loop(&and_mask, logical_and_attribute);
396                 if (ret < 0)
397                         para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
398                 find_greatest_att_bitnum();
399                 ret = reload_current_mood();
400                 if (ret < 0)
401                         para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
402         }
403         if (!raad.pb.buf)
404                 para_printf(&raad.pb, "no match -- nothing removed\n");
405         result->data = raad.pb.buf;
406         result->size = raad.pb.size;
407         return 1;
408 }
409
410 int com_rmatt(int fd, int argc, char * const * const argv)
411 {
412         int ret;
413         struct osl_object result;
414
415         if (argc < 2)
416                 return -E_ATTR_SYNTAX;
417         ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
418                 &result);
419         if (ret > 0) {
420                 send_buffer(fd, (char *)result.data);
421                 free(result.data);
422         } else
423                 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
424         return ret;
425 }
426
427 /**
428  * Return a binary representation of the geiven attribute value.
429  *
430  * \param atts Pointer to the attribute value.
431  * \param buf Result.
432  *
433  * This function prints a string of at most 64 characters plus the terminating
434  * \p NULL character into \a buf which must be provided by the caller and at
435  * least 65 bytes long. The "x" character is used for set attributes and "-" is
436  * used for unset attributes.
437  *
438  * In practice, not all 64 attributes are defined. In this case, the function
439  * only prints \a N + 1 charaters where \a N is the greatest id of a defined
440  * attribute.
441  */
442 void get_attribute_bitmap(const uint64_t *atts, char *buf)
443 {
444         int i;
445         const uint64_t one = 1;
446
447         for (i = 0; i <= greatest_att_bitnum; i++)
448                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
449         buf[i] = '\0';
450 }
451
452 /**
453  * Get a string containing the set attributes in text form.
454  *
455  * \param atts The attribute bitmap.
456  * \param delim The delimiter to separate matching attribute names.
457  * \param text Result pointer.
458  *
459  * \return Positive on success, negative on errors. If no attributes have
460  * been defined, \a *text is NULL.
461  */
462 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
463 {
464         int i, ret;
465         const uint64_t one = 1;
466
467         *text = NULL;
468         if (greatest_att_bitnum < 0) /* no attributes available */
469                 return 1;
470         for (i = 0; i <= greatest_att_bitnum; i++) {
471                 unsigned char bn = i;
472                 struct osl_object obj = {.data = &bn, .size = 1};
473                 struct osl_row *row;
474
475                 if (!(*atts & (one << i)))
476                         continue;
477                 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
478                 if (ret < 0)
479                         goto err;
480                 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
481                 if (ret < 0)
482                         goto err;
483                 if (*text) {
484                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
485                         free(*text);
486                         *text = tmp;
487                 } else
488                         *text = para_strdup(obj.data);
489         }
490         if (!*text) /* no attributes set */
491                 *text = para_strdup("");
492         return 1;
493 err:
494         free(*text);
495         return ret;
496 }
497
498 /**
499  * Close the attribute table.
500  *
501  * \param flags Ususal flags that are passed to osl_close_table().
502  *
503  * \sa osl_close_table().
504  */
505 void attribute_shutdown(enum osl_close_flags flags)
506 {
507         osl_close_table(attribute_table, flags);
508         attribute_table = NULL;
509 }
510
511 /**
512  * Open the attribute table.
513  *
514  * \param ti Gets initialized by this function.
515  * \param db The database directory.
516  *
517  * \return Positive on success, negative on errors.
518  *
519  * \sa osl_open_table().
520  */
521 int attribute_init(struct table_info *ti, const char *db)
522 {
523         int ret;
524
525         attribute_table_desc.dir = db;
526         ti->desc = &attribute_table_desc;
527         ret = osl_open_table(ti->desc, &attribute_table);
528         greatest_att_bitnum = -1; /* no atts available */
529         if (ret >= 0) {
530                 find_greatest_att_bitnum();
531                 return ret;
532         }
533         attribute_table = NULL;
534         if (ret == -E_NOENT)
535                 return 1;
536         return ret;
537 }