attribute.c: Add doxygen file comment.
[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
336 static int com_rmatt_callback(const struct osl_object *query,
337 __a_unused struct osl_object *result)
338 {
339 char *p = query->data;
340 int ret, atts_removed = 0;
341 while (p < (char *)query->data + query->size) {
342 struct osl_object obj = {
343 .data = p,
344 .size = strlen(p) + 1
345 };
346 struct osl_row *row;
347 ret = osl_get_row(attribute_table, ATTCOL_NAME,
348 &obj, &row);
349 if (ret < 0)
350 return ret;
351 ret = osl_del_row(attribute_table, row);
352 if (ret < 0)
353 return ret;
354 atts_removed++;
355 p += strlen(p) + 1;
356 }
357 find_greatest_att_bitnum();
358 if (!atts_removed)
359 return 1;
360 return reload_current_mood();
361 }
362
363 int com_rmatt(__a_unused int fd, int argc, char * const * const argv)
364 {
365 if (argc < 2)
366 return -E_ATTR_SYNTAX;
367 return send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
368 NULL);
369 }
370
371 /**
372 * Return a binary representation of the geiven attribute value.
373 *
374 * \param atts Pointer to the attribute value.
375 * \param buf Result.
376 *
377 * This function prints a string of at most 64 characters plus the terminating
378 * \p NULL character into \a buf which must be provided by the caller and at
379 * least 65 bytes long. The "x" character is used for set attributes and "-" is
380 * used for unset attributes.
381 *
382 * In practice, not all 64 attributes are defined. In this case, the function
383 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
384 * attribute.
385 */
386 void get_attribute_bitmap(const uint64_t *atts, char *buf)
387 {
388 int i;
389 const uint64_t one = 1;
390
391 for (i = 0; i <= greatest_att_bitnum; i++)
392 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
393 buf[i] = '\0';
394 }
395
396 /**
397 * Get a string containing the set attributes in text form.
398 *
399 * \param atts The attribute bitmap.
400 * \param delim The delimiter to separate matching attribute names.
401 * \param text Result pointer.
402 *
403 * \return Positive on success, negative on errors. If no attributes have
404 * been defined, \a *text is NULL.
405 */
406 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
407 {
408 int i, ret;
409 const uint64_t one = 1;
410
411 *text = NULL;
412 if (greatest_att_bitnum < 0) /* no attributes available */
413 return 1;
414 for (i = 0; i <= greatest_att_bitnum; i++) {
415 unsigned char bn = i;
416 struct osl_object obj = {.data = &bn, .size = 1};
417 struct osl_row *row;
418
419 if (!(*atts & (one << i)))
420 continue;
421 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
422 if (ret < 0)
423 goto err;
424 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
425 if (ret < 0)
426 goto err;
427 if (*text) {
428 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
429 free(*text);
430 *text = tmp;
431 } else
432 *text = para_strdup(obj.data);
433 }
434 if (!*text) /* no attributes set */
435 *text = para_strdup("");
436 return 1;
437 err:
438 free(*text);
439 return ret;
440 }
441
442 /**
443 * Close the attribute table.
444 *
445 * \param flags Ususal flags that are passed to osl_close_table().
446 *
447 * \sa osl_close_table().
448 */
449 void attribute_shutdown(enum osl_close_flags flags)
450 {
451 osl_close_table(attribute_table, flags);
452 attribute_table = NULL;
453 }
454
455 /**
456 * Open the attribute table.
457 *
458 * \param ti Gets initialized by this function.
459 * \param db The database directory.
460 *
461 * \return Positive on success, negative on errors.
462 *
463 * \sa osl_open_table().
464 */
465 int attribute_init(struct table_info *ti, const char *db)
466 {
467 int ret;
468
469 attribute_table_desc.dir = db;
470 ti->desc = &attribute_table_desc;
471 ret = osl_open_table(ti->desc, &attribute_table);
472 greatest_att_bitnum = -1; /* no atts available */
473 if (ret >= 0) {
474 find_greatest_att_bitnum();
475 return ret;
476 }
477 attribute_table = NULL;
478 if (ret == -E_NOENT)
479 return 1;
480 return ret;
481 }