Merge the new afs code.
[paraslash.git] / attribute.c
1 #include "para.h"
2 #include "error.h"
3 #include "afs.h"
4 #include "string.h"
5
6 static void *attribute_table;
7 static int greatest_att_bitnum;
8
9 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
10 {
11 const unsigned char *c1 = (const unsigned char*)obj1->data;
12 const unsigned char *c2 = (const unsigned char*)obj2->data;
13 if (*c1 > *c2)
14 return 1;
15 if (*c1 < *c2)
16 return -1;
17 return 0;
18 }
19
20 enum attribute_table_columns {ATTCOL_BITNUM, ATTCOL_NAME, NUM_ATT_COLUMNS};
21
22 static struct osl_column_description att_cols[] = {
23 [ATTCOL_BITNUM] = {
24 .storage_type = OSL_MAPPED_STORAGE,
25 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
26 .name = "bitnum",
27 .compare_function = char_compare,
28 .data_size = 1
29 },
30 [ATTCOL_NAME] = {
31 .storage_type = OSL_MAPPED_STORAGE,
32 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
33 .name = "name",
34 .compare_function = string_compare,
35 }
36 };
37
38 static const struct osl_table_description attribute_table_desc = {
39 .dir = DATABASE_DIR,
40 .name = "attributes",
41 .num_columns = NUM_ATT_COLUMNS,
42 .flags = 0,
43 .column_descriptions = att_cols
44 };
45
46 static void find_greatest_att_bitnum(void)
47 {
48 unsigned char c = 63;
49 do {
50 struct osl_row *row;
51 struct osl_object obj = {.data = &c, .size = 1};
52 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
53 &row) >= 0) {
54 greatest_att_bitnum = c;
55 return;
56 }
57 } while (c--);
58 PARA_INFO_LOG("%s\n", "no attributes");
59 greatest_att_bitnum = -E_NO_ATTRIBUTES;
60 }
61
62 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
63 {
64 struct osl_object obj = {.data = (char *)att_name,
65 .size = strlen(att_name) + 1};
66 struct osl_row *row;
67 int ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
68
69 if (ret < 0)
70 return ret;
71 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj);
72 if (ret < 0)
73 return ret;
74 *bitnum = *(unsigned char *)obj.data;
75 return 1;
76 }
77
78 #define LAA_FLAG_ALPHA 1
79 #define LAA_FLAG_LONG 2
80
81 struct private_laa_data {
82 int fd;
83 unsigned flags;
84 };
85
86 static int log_attribute(struct osl_row *row, void *private_data)
87 {
88 struct private_laa_data *pld = private_data;
89 int ret;
90 struct osl_object name_obj, bitnum_obj;
91
92 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &name_obj);
93 if (ret < 0)
94 return ret;
95 if (!(pld->flags & LAA_FLAG_LONG)) {
96 printf("%s\n", (char *)name_obj.data);
97 return 1;
98 }
99 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
100 if (ret < 0)
101 return ret;
102 printf("%u\t%s\n", *(unsigned char*)bitnum_obj.data,
103 (char *)name_obj.data);
104 return 1;
105 }
106
107 int com_lsatt(int fd, int argc, const char **argv)
108 {
109 struct private_laa_data pld = {.fd = fd, .flags = 0};
110 int i;
111
112 for (i = 1; i < argc; i++) {
113 const char *arg = argv[i];
114 if (arg[0] != '-')
115 break;
116 if (!strcmp(arg, "--")) {
117 i++;
118 break;
119 }
120 if (!strcmp(arg, "-a")) {
121 pld.flags |= LAA_FLAG_ALPHA;
122 continue;
123 }
124 if (!strcmp(arg, "-l")) {
125 pld.flags |= LAA_FLAG_LONG;
126 continue;
127 }
128 }
129 if (argc > i)
130 return -E_ATTR_SYNTAX;
131 if (pld.flags & LAA_FLAG_ALPHA)
132 return osl_rbtree_loop(attribute_table, ATTCOL_NAME,
133 &pld, log_attribute);
134 return osl_rbtree_loop(attribute_table, ATTCOL_BITNUM,
135 &pld, log_attribute);
136 }
137
138 static int com_setatt_callback(const struct osl_object *query,
139 __a_unused struct osl_object *result)
140 {
141 char *p;
142 uint64_t add_mask = 0, del_mask = 0;
143 int ret;
144 size_t len;
145 struct osl_object obj;
146 struct osl_row *row;
147
148 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
149 char c;
150
151 len = strlen(p);
152 if (!*p)
153 return -E_ATTR_SYNTAX;
154 c = p[len - 1];
155 if (c != '+' && c != '-')
156 break;
157 p[len - 1] = '\0';
158 obj.data = p;
159 obj.size = len + 1;
160 ret = osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row);
161 if (ret < 0)
162 return ret;
163 ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM,
164 &obj);
165 if (ret < 0)
166 return ret;
167 if (c == '+')
168 add_mask |= (1UL << *(unsigned char *)obj.data);
169 else
170 del_mask |= (1UL << *(unsigned char *)obj.data);
171 }
172 if (!add_mask && !del_mask)
173 return -E_ATTR_SYNTAX;
174 PARA_DEBUG_LOG("masks: %llx:%llx\n", add_mask, del_mask);
175 for (; p < (char *)query->data + query->size; p += len + 1) { /* TODO: fnmatch */
176 struct afs_info old_afsi, new_afsi;
177 struct osl_row *aft_row;
178
179 len = strlen(p);
180 ret = aft_get_row_of_path(p, &aft_row);
181 if (ret < 0)
182 return ret;
183 ret = get_afsi_object_of_row(p, &obj);
184 if (ret < 0)
185 return ret;
186 ret = load_afsi(&old_afsi, &obj);
187 if (ret < 0)
188 return ret;
189 new_afsi = old_afsi;
190 new_afsi.attributes |= add_mask;
191 new_afsi.attributes &= ~del_mask;
192 save_afsi(&new_afsi, &obj); /* in-place update */
193 ret = mood_update_audio_file(aft_row, &old_afsi);
194 if (ret < 0)
195 return ret;
196 }
197 return 1;
198 }
199
200 int com_setatt(__a_unused int fd, int argc, const char **argv)
201 {
202 if (argc < 2)
203 return -E_ATTR_SYNTAX;
204 return send_standard_callback_request(argc, argv, com_setatt_callback,
205 NULL);
206 }
207
208 /* TODO: make it faster by only extracting the attribute member from afsi */
209 static int logical_and_attribute(struct osl_row *aft_row, void *attribute_ptr)
210 {
211 struct afs_info afsi;
212 uint64_t *att = attribute_ptr;
213 struct osl_object obj;
214 int ret = get_afsi_object_of_row(aft_row, &obj);
215 if (ret < 0)
216 return ret;
217 ret = load_afsi(&afsi, &obj);
218 if (ret < 0)
219 return ret;
220 afsi.attributes &= *att;
221 save_afsi(&afsi, &obj);
222 return 1;
223 }
224
225 static int com_addatt_callback(const struct osl_object *query,
226 __a_unused struct osl_object *result)
227 {
228 char *p = query->data;
229 uint64_t atts_added = 0;
230 int ret;
231
232 while (p < (char *)query->data + query->size) {
233 struct osl_object objs[NUM_ATT_COLUMNS];
234 struct osl_row *row;
235 unsigned char bitnum;
236
237 objs[ATTCOL_BITNUM].size = 1;
238 objs[ATTCOL_NAME].data = p;
239 objs[ATTCOL_NAME].size = strlen(p) + 1;
240 ret = osl_get_row(attribute_table, ATTCOL_NAME,
241 &objs[ATTCOL_NAME], &row); /* expected to fail */
242 if (ret >= 0)
243 return -E_ATTR_EXISTS;
244 if (ret != -E_RB_KEY_NOT_FOUND) /* error */
245 return ret;
246 /* find smallest non-used attribute */
247 for (bitnum = 0; bitnum < 64; bitnum++) {
248 objs[ATTCOL_BITNUM].data = &bitnum;
249 ret = osl_get_row(attribute_table, ATTCOL_BITNUM,
250 &objs[ATTCOL_BITNUM], &row);
251 if (ret == -E_RB_KEY_NOT_FOUND)
252 break; /* this bitnum is unused, use it */
253 if (ret < 0) /* error */
254 return ret;
255 /* this bit is already in use, try next bit */
256 }
257 if (bitnum == 64)
258 return -E_ATTR_TABLE_FULL;
259 ret = osl_add_row(attribute_table, objs);
260 if (ret < 0)
261 return ret;
262 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, bitnum);
263 atts_added |= 1 << bitnum;
264 p += strlen(p) + 1;
265 }
266 if (!atts_added)
267 return 1;
268 atts_added = ~atts_added;
269 ret = audio_file_loop(&atts_added, logical_and_attribute);
270 if (ret < 0)
271 return ret;
272 find_greatest_att_bitnum();
273 return mood_reload();
274 }
275
276 int com_addatt(__a_unused int fd, int argc, const char **argv)
277 {
278 if (argc < 2)
279 return -E_ATTR_SYNTAX;
280 return send_standard_callback_request(argc, argv, com_addatt_callback,
281 NULL);
282 }
283
284 static int com_rmatt_callback(const struct osl_object *query,
285 __a_unused struct osl_object *result)
286 {
287 char *p = query->data;
288 int ret, atts_removed = 0;
289 while (p < (char *)query->data + query->size) {
290 struct osl_object obj = {
291 .data = p,
292 .size = strlen(p) + 1
293 };
294 struct osl_row *row;
295 ret = osl_get_row(attribute_table, ATTCOL_NAME,
296 &obj, &row);
297 if (ret < 0)
298 return ret;
299 ret = osl_del_row(attribute_table, row);
300 if (ret < 0)
301 return ret;
302 atts_removed++;
303 p += strlen(p) + 1;
304 }
305 find_greatest_att_bitnum();
306 if (!atts_removed)
307 return 1;
308 return mood_reload();
309 }
310
311 int com_rmatt(__a_unused int fd, int argc, const char **argv)
312 {
313 if (argc < 2)
314 return -E_ATTR_SYNTAX;
315 return send_standard_callback_request(argc, argv, com_rmatt_callback,
316 NULL);
317 }
318
319 void get_attribute_bitmap(uint64_t *atts, char *buf)
320 {
321 int i;
322 const uint64_t one = 1;
323
324 for (i = 0; i <= greatest_att_bitnum; i++)
325 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
326 buf[i] = '\0';
327 }
328 /**
329 * Get a string containing the set attributes in text form.
330 *
331 * \param atts The attribute bitmap.
332 * \param delim The delimiter to separate matching attribute names.
333 * \param text Result pointer.
334 *
335 * \return Positive on success, negative on errors. If no attributes have
336 * been defined, \a *text is NULL.
337 */
338 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
339 {
340 int i, ret;
341 const uint64_t one = 1;
342
343 *text = NULL;
344 if (greatest_att_bitnum < 0) /* no attributes available */
345 return 1;
346 for (i = 0; i <= greatest_att_bitnum; i++) {
347 unsigned char bn = i;
348 struct osl_object obj = {.data = &bn, .size = 1};
349 struct osl_row *row;
350
351 if (!(*atts & (one << i)))
352 continue;
353 ret = osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row);
354 if (ret < 0)
355 goto err;
356 ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &obj);
357 if (ret < 0)
358 goto err;
359 if (*text) {
360 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
361 free(*text);
362 *text = tmp;
363 } else
364 *text = para_strdup(obj.data);
365 }
366 if (!*text) /* no attributes set */
367 *text = para_strdup("");
368 return 1;
369 err:
370 free(*text);
371 return ret;
372 }
373
374 void attribute_shutdown(enum osl_close_flags flags)
375 {
376 osl_close_table(attribute_table, flags);
377 }
378
379 int attribute_init(struct table_info *ti)
380 {
381 int ret;
382
383 ti->desc = &attribute_table_desc;
384 ret = osl_open_table(ti->desc, &ti->table);
385 greatest_att_bitnum = -1; /* no atts available */
386 if (ret >= 0) {
387 attribute_table = ti->table;
388 find_greatest_att_bitnum();
389 return ret;
390 }
391 attribute_table = NULL;
392 if (ret == -E_NOENT)
393 return 1;
394 return ret;
395 }