base64: Use para_isspace() everywhere.
[paraslash.git] / attribute.c
1 /*
2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file attribute.c Attribute handling functions. */
8
9 #include <regex.h>
10 #include <osl.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "crypt.h"
15 #include "string.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "ipc.h"
19 #include "sideband.h"
20 #include "command.h"
21
22 static struct osl_table *attribute_table;
23 static int greatest_att_bitnum;
24
25 /** The columns of the attribute table. */
26 enum attribute_table_columns {
27 /** The bit number (0-63). */
28 ATTCOL_BITNUM,
29 /** The name of the attribute. */
30 ATTCOL_NAME,
31 /** Number of columns in this table. */
32 NUM_ATT_COLUMNS
33 };
34
35 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
36 {
37 const unsigned char *c1 = (const unsigned char*)obj1->data;
38 const unsigned char *c2 = (const unsigned char*)obj2->data;
39 if (*c1 > *c2)
40 return 1;
41 if (*c1 < *c2)
42 return -1;
43 return 0;
44 }
45
46 static struct osl_column_description att_cols[] = {
47 [ATTCOL_BITNUM] = {
48 .storage_type = OSL_MAPPED_STORAGE,
49 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
50 .name = "bitnum",
51 .compare_function = char_compare,
52 .data_size = 1
53 },
54 [ATTCOL_NAME] = {
55 .storage_type = OSL_MAPPED_STORAGE,
56 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
57 .name = "name",
58 .compare_function = string_compare,
59 }
60 };
61
62 static struct osl_table_description attribute_table_desc = {
63 .name = "attributes",
64 .num_columns = NUM_ATT_COLUMNS,
65 .flags = 0,
66 .column_descriptions = att_cols
67 };
68
69 static void find_greatest_att_bitnum(void)
70 {
71 unsigned char c = 63;
72 do {
73 struct osl_row *row;
74 struct osl_object obj = {.data = &c, .size = 1};
75 if (osl_get_row(attribute_table, ATTCOL_BITNUM, &obj,
76 &row) >= 0) {
77 greatest_att_bitnum = c;
78 return;
79 }
80 } while (c--);
81 PARA_INFO_LOG("no attributes\n");
82 greatest_att_bitnum = -E_NO_ATTRIBUTES;
83 }
84
85 /**
86 * Retrieve the identifier (number) of an attribute.
87 *
88 * \param att_name The name of the attribute.
89 * \param bitnum Result pointer.
90 *
91 * \return Positive on success, negative on errors.
92 */
93 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
94 {
95 struct osl_object obj = {.data = (char *)att_name,
96 .size = strlen(att_name) + 1};
97 struct osl_row *row;
98 int ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
99
100 if (ret < 0)
101 return ret;
102 ret = osl(osl_get_object(attribute_table, row, ATTCOL_BITNUM, &obj));
103 if (ret < 0)
104 return ret;
105 *bitnum = *(unsigned char *)obj.data;
106 return 1;
107 }
108
109 /**
110 * Flags used by the lsatt command.
111 *
112 * \param \sa com_lsatt().
113 */
114 enum lsatt_flags {
115 /** Whether "-a" was given for the lsatt command. */
116 LSATT_FLAG_SORT_BY_ID = 1,
117 /** Whether "-l" was given for the lsatt command. */
118 LSATT_FLAG_LONG = 2,
119 /** Reverse sort order. */
120 LSATT_FLAG_REVERSE = 4
121 };
122
123 /** Data passed to the action function of lsatt */
124 static int print_attribute(struct osl_table *table, struct osl_row *row,
125 const char *name, void *data)
126 {
127 struct afs_callback_arg *aca = data;
128 unsigned flags = *(unsigned *)aca->query.data;
129 struct osl_object bitnum_obj;
130 int ret;
131
132 if (!(flags & LSATT_FLAG_LONG)) {
133 para_printf(&aca->pbout, "%s\n", name);
134 return 1;
135 }
136 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
137 if (ret < 0) {
138 para_printf(&aca->pbout, "%s: %s\n", name, para_strerror(-ret));
139 return ret;
140 }
141 para_printf(&aca->pbout, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
142 name);
143 return 1;
144 }
145
146 static int com_lsatt_callback(struct afs_callback_arg *aca)
147 {
148 unsigned flags = *(unsigned *)aca->query.data;
149 int ret;
150 struct pattern_match_data pmd = {
151 .table = attribute_table,
152 .loop_col_num = ATTCOL_NAME,
153 .match_col_num = ATTCOL_NAME,
154 .patterns = {.data = (char *)aca->query.data + sizeof(flags),
155 .size = aca->query.size - sizeof(flags)},
156 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
157 .data = aca,
158 .action = print_attribute
159 };
160 if (flags & LSATT_FLAG_SORT_BY_ID)
161 pmd.loop_col_num = ATTCOL_BITNUM;
162 if (flags & LSATT_FLAG_REVERSE)
163 pmd.pm_flags |= PM_REVERSE_LOOP;
164 ret = for_each_matching_row(&pmd);
165 if (ret < 0)
166 goto out;
167 if (pmd.num_matches == 0)
168 ret = -E_NO_MATCH;
169 out:
170 return ret;
171 }
172
173 int com_lsatt(struct command_context *cc)
174 {
175 unsigned flags = 0;
176 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
177 int i;
178
179 for (i = 1; i < cc->argc; i++) {
180 const char *arg = cc->argv[i];
181 if (arg[0] != '-')
182 break;
183 if (!strcmp(arg, "--")) {
184 i++;
185 break;
186 }
187 if (!strcmp(arg, "-i")) {
188 flags |= LSATT_FLAG_SORT_BY_ID;
189 continue;
190 }
191 if (!strcmp(arg, "-l")) {
192 flags |= LSATT_FLAG_LONG;
193 continue;
194 }
195 if (!strcmp(arg, "-r")) {
196 flags |= LSATT_FLAG_REVERSE;
197 continue;
198 }
199 }
200 return send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i,
201 com_lsatt_callback, afs_cb_result_handler, cc);
202 }
203
204 struct addatt_event_data {
205 const char *name;
206 unsigned char bitnum;
207 };
208
209
210 static int com_addatt_callback(struct afs_callback_arg *aca)
211 {
212 char *p;
213 int ret = 1;
214 size_t len;
215
216 for (
217 p = aca->query.data;
218 p < (char *)aca->query.data + aca->query.size;
219 p += len + 1
220 ) {
221 struct osl_object objs[NUM_ATT_COLUMNS];
222 struct osl_row *row;
223 unsigned char bitnum;
224 struct addatt_event_data aed;
225
226 len = strlen(p);
227 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
228 para_printf(&aca->pbout, "invalid attribute name: %s\n", p);
229 continue;
230 }
231 ret = get_attribute_bitnum_by_name(p, &bitnum);
232 if (ret >= 0) {
233 para_printf(&aca->pbout, "attribute \"%s\" already exists\n", p);
234 continue;
235 }
236 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
237 goto out;
238 objs[ATTCOL_BITNUM].size = 1;
239 /* find smallest unused attribute */
240 for (bitnum = 0; bitnum < 64; bitnum++) {
241 objs[ATTCOL_BITNUM].data = &bitnum;
242 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
243 &objs[ATTCOL_BITNUM], &row));
244 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
245 break; /* this bitnum is unused, use it */
246 if (ret < 0) /* error */
247 goto out;
248 /* this bit is already in use, try next bit */
249 }
250 if (bitnum == 64) {
251 ret = -E_ATT_TABLE_FULL;
252 goto out;
253 }
254 objs[ATTCOL_NAME].data = p;
255 objs[ATTCOL_NAME].size = len + 1;
256 ret = osl(osl_add_row(attribute_table, objs));
257 if (ret < 0)
258 goto out;
259 aed.name = p;
260 aed.bitnum = bitnum;
261 ret = afs_event(ATTRIBUTE_ADD, &aca->pbout, &aed);
262 if (ret < 0)
263 goto out;
264 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
265 }
266 out:
267 if (ret < 0)
268 para_printf(&aca->pbout, "%s: %s\n", p, para_strerror(-ret));
269 return ret;
270 }
271
272 int com_addatt(struct command_context *cc)
273 {
274 int ret;
275
276 if (cc->argc < 2)
277 return -E_ATTR_SYNTAX;
278 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
279 com_addatt_callback, afs_cb_result_handler, cc);
280 if (ret < 0)
281 send_strerror(cc, -ret);
282 return ret;
283 }
284
285 static int com_mvatt_callback(struct afs_callback_arg *aca)
286 {
287 char *old = aca->query.data;
288 size_t size = strlen(old) + 1;
289 char *new = old + size;
290 struct osl_object obj = {.data = old, .size = size};
291 struct osl_row *row;
292 int ret;
293
294 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
295 if (ret < 0)
296 goto out;
297 obj.data = new;
298 obj.size = strlen(new) + 1;
299 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
300 out:
301 if (ret < 0)
302 para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
303 else
304 ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
305 return ret;
306 }
307
308 int com_mvatt(struct command_context *cc)
309 {
310 if (cc->argc != 3)
311 return -E_ATTR_SYNTAX;
312 return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
313 com_mvatt_callback, afs_cb_result_handler, cc);
314 }
315
316 static int remove_attribute(struct osl_table *table, struct osl_row *row,
317 const char *name, void *data)
318 {
319 struct afs_callback_arg *aca = data;
320 int ret;
321 struct rmatt_event_data red = {.name = name};
322
323 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
324 if (ret < 0) {
325 para_printf(&aca->pbout, "cannot remove %s\n", name);
326 return ret;
327 }
328 para_printf(&aca->pbout, "removing attribute %s\n", name);
329 ret = osl(osl_del_row(table, row));
330 if (ret < 0) {
331 para_printf(&aca->pbout, "cannot remove %s\n", name);
332 return ret;
333 }
334 return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
335 }
336
337 static int com_rmatt_callback(struct afs_callback_arg *aca)
338 {
339 int ret;
340 struct pattern_match_data pmd = {
341 .table = attribute_table,
342 .patterns = aca->query,
343 .loop_col_num = ATTCOL_BITNUM,
344 .match_col_num = ATTCOL_NAME,
345 .data = aca,
346 .action = remove_attribute
347 };
348 ret = for_each_matching_row(&pmd);
349 if (ret < 0)
350 goto out;
351 if (pmd.num_matches == 0)
352 ret = -E_NO_MATCH;
353 out:
354 return ret;
355 }
356
357 int com_rmatt(struct command_context *cc)
358 {
359 if (cc->argc < 2)
360 return -E_ATTR_SYNTAX;
361 return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
362 com_rmatt_callback, afs_cb_result_handler, cc);
363 }
364
365 /**
366 * Return a binary representation of the given attribute value.
367 *
368 * \param atts Pointer to the attribute value.
369 * \param buf Result.
370 *
371 * This function prints a string of at most 64 characters plus the terminating
372 * \p NULL character into \a buf which must be provided by the caller and at
373 * least 65 bytes long. The "x" character is used for set attributes and "-" is
374 * used for unset attributes.
375 *
376 * In practice, not all 64 attributes are defined. In this case, the function
377 * only prints \a N + 1 characters where \a N is the greatest id of a defined
378 * attribute.
379 */
380 void get_attribute_bitmap(const uint64_t *atts, char *buf)
381 {
382 int i;
383 const uint64_t one = 1;
384
385 for (i = 0; i <= greatest_att_bitnum; i++)
386 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
387 buf[i] = '\0';
388 }
389
390 /**
391 * Get a string containing the set attributes in text form.
392 *
393 * \param atts The attribute bitmap.
394 * \param delim The delimiter to separate matching attribute names.
395 * \param text Result pointer.
396 *
397 * \return Positive on success, negative on errors. If no attributes have
398 * been defined, \a *text is NULL.
399 */
400 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
401 {
402 int i, ret;
403 const uint64_t one = 1;
404
405 *text = NULL;
406 if (greatest_att_bitnum < 0) { /* no attributes available */
407 *text = para_strdup("(no attributes available)");
408 return 1;
409 }
410 for (i = 0; i <= greatest_att_bitnum; i++) {
411 unsigned char bn = i;
412 struct osl_object obj = {.data = &bn, .size = 1};
413 struct osl_row *row;
414
415 if (!(*atts & (one << i)))
416 continue;
417 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
418 if (ret < 0)
419 goto err;
420 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
421 if (ret < 0)
422 goto err;
423 if (*text) {
424 char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
425 free(*text);
426 *text = tmp;
427 } else
428 *text = para_strdup(obj.data);
429 }
430 if (!*text) /* no attributes set */
431 *text = para_strdup("");
432 return 1;
433 err:
434 free(*text);
435 return ret;
436 }
437
438 static int att_logical_or(struct osl_row *row, void *data)
439 {
440 uint64_t *att_mask = data, one = 1;
441 struct osl_object bitnum_obj;
442 int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
443
444 if (ret < 0)
445 return ret;
446 *att_mask |= one << *(unsigned char *)bitnum_obj.data;
447 return 0;
448 }
449
450 /**
451 * Compute the attribute bit mask and check each afs info bitmap.
452 *
453 * \param aca The query field of \a aca is ignored.
454 *
455 * This iterates over all attributes in the attribute table and computes the
456 * logical or of 1 << b where b is the bit number of the attribute. The
457 * resulting bit mask is passed to aft_check_attributes() which performs the
458 * actual check.
459 *
460 * \return Standard.
461 *
462 * \sa \ref aft_check_attributes().
463 */
464 int attribute_check_callback(struct afs_callback_arg *aca)
465 {
466 int ret;
467 uint64_t att_mask = 0; /* bits corresponding to a attributes */
468
469 ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
470 att_logical_or);
471 if (ret < 0) {
472 PARA_ERROR_LOG("attribute table loop failed: %s\n",
473 para_strerror(-ret));
474 return ret;
475 }
476 return aft_check_attributes(att_mask, &aca->pbout);
477 }
478
479 /**
480 * Close the attribute table.
481 *
482 * \sa osl_close_table().
483 */
484 static void attribute_close(void)
485 {
486 osl_close_table(attribute_table, OSL_MARK_CLEAN);
487 attribute_table = NULL;
488 }
489
490 /**
491 * Open the attribute table.
492 *
493 * \param dir The database directory.
494 *
495 * \return Positive on success, negative on errors.
496 *
497 * \sa osl_open_table().
498 */
499 static int attribute_open(const char *dir)
500 {
501 int ret;
502
503 attribute_table_desc.dir = dir;
504 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
505 greatest_att_bitnum = -1; /* no atts available */
506 if (ret >= 0) {
507 find_greatest_att_bitnum();
508 return ret;
509 }
510 attribute_table = NULL;
511 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
512 return 1;
513 return ret;
514 }
515
516 static int attribute_create(const char *dir)
517 {
518 attribute_table_desc.dir = dir;
519 return osl(osl_create_table(&attribute_table_desc));
520 }
521
522 /**
523 * Initialize the attribute table structure.
524 *
525 * \param t The table structure to initialize.
526 */
527 void attribute_init(struct afs_table *t)
528 {
529 t->open = attribute_open;
530 t->close = attribute_close;
531 t->create = attribute_create;
532 }