Move com_setatt() from attribute.c to aft.c.
[paraslash.git] / attribute.c
1 /*
2 * Copyright (C) 1997-2013 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
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 struct lsatt_action_data {
125 /** The result buffer. */
126 struct para_buffer pb;
127 /** The given flags for the lsatt command. */
128 unsigned flags;
129 };
130
131 static int print_attribute(struct osl_table *table, struct osl_row *row,
132 const char *name, void *data)
133 {
134 struct lsatt_action_data *laad = data;
135 struct osl_object bitnum_obj;
136 int ret;
137
138 if (!(laad->flags & LSATT_FLAG_LONG))
139 return para_printf(&laad->pb, "%s\n", name);
140 ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
141 if (ret < 0) {
142 para_printf(&laad->pb, "%s: %s\n", name, para_strerror(-ret));
143 return ret;
144 }
145 return para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
146 name);
147 }
148
149 static void com_lsatt_callback(int fd, const struct osl_object *query)
150 {
151 struct lsatt_action_data laad = {
152 .flags = *(unsigned *) query->data,
153 .pb = {
154 .max_size = shm_get_shmmax(),
155 .private_data = &(struct afs_max_size_handler_data) {
156 .fd = fd,
157 .band = SBD_OUTPUT
158 },
159 .max_size_handler = afs_max_size_handler
160 }
161
162 };
163 struct pattern_match_data pmd = {
164 .table = attribute_table,
165 .loop_col_num = ATTCOL_BITNUM,
166 .match_col_num = ATTCOL_NAME,
167 .patterns = {.data = (char *)query->data + sizeof(laad.flags),
168 .size = query->size - sizeof(laad.flags)},
169 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
170 .data = &laad,
171 .action = print_attribute
172 };
173 if (laad.flags & LSATT_FLAG_SORT_BY_ID)
174 pmd.loop_col_num = ATTCOL_NAME;
175 if (laad.flags & LSATT_FLAG_REVERSE)
176 pmd.pm_flags |= PM_REVERSE_LOOP;
177 for_each_matching_row(&pmd);
178 if (laad.pb.offset)
179 pass_buffer_as_shm(fd, SBD_OUTPUT, laad.pb.buf, laad.pb.offset);
180 free(laad.pb.buf);
181 }
182
183 int com_lsatt(struct command_context *cc)
184 {
185 unsigned flags = 0;
186 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
187 int ret, i;
188
189 for (i = 1; i < cc->argc; i++) {
190 const char *arg = cc->argv[i];
191 if (arg[0] != '-')
192 break;
193 if (!strcmp(arg, "--")) {
194 i++;
195 break;
196 }
197 if (!strcmp(arg, "-i")) {
198 flags |= LSATT_FLAG_SORT_BY_ID;
199 continue;
200 }
201 if (!strcmp(arg, "-l")) {
202 flags |= LSATT_FLAG_LONG;
203 continue;
204 }
205 if (!strcmp(arg, "-r")) {
206 flags |= LSATT_FLAG_REVERSE;
207 continue;
208 }
209 }
210 ret = send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i,
211 com_lsatt_callback, afs_cb_result_handler, cc);
212 if (ret < 0)
213 send_strerror(cc, -ret);
214 else if (ret == 0 && cc->argc > 1)
215 ret = cc->use_sideband?
216 send_sb_va(&cc->scc, SBD_ERROR_LOG, "no matches\n")
217 :
218 sc_send_va_buffer(&cc->scc, "no matches\n");
219 return ret;
220 }
221
222 struct addatt_event_data {
223 const char *name;
224 unsigned char bitnum;
225 };
226
227
228 static void com_addatt_callback(int fd, const struct osl_object *query)
229 {
230 char *p;
231 int ret = 1, ret2 = 0;
232 struct para_buffer pb = {
233 .max_size = shm_get_shmmax(),
234 .private_data = &(struct afs_max_size_handler_data) {
235 .fd = fd,
236 .band = SBD_OUTPUT
237 },
238 .max_size_handler = afs_max_size_handler
239 };
240 size_t len;
241
242 for (p = query->data; p < (char *)query->data + query->size; p += len + 1) {
243 struct osl_object objs[NUM_ATT_COLUMNS];
244 struct osl_row *row;
245 unsigned char bitnum;
246 struct addatt_event_data aed;
247
248 len = strlen(p);
249 if (!len || p[len - 1] == '-' || p[len - 1] == '+') {
250 ret2 = para_printf(&pb, "invalid attribute name: %s\n", p);
251 if (ret2 < 0)
252 goto out;
253 continue;
254 }
255 ret = get_attribute_bitnum_by_name(p, &bitnum);
256 if (ret >= 0) {
257 ret2 = para_printf(&pb, "attribute \"%s\" already exists\n", p);
258 if (ret2 < 0)
259 goto out;
260 continue;
261 }
262 if (ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* error */
263 goto out;
264 objs[ATTCOL_BITNUM].size = 1;
265 /* find smallest unused attribute */
266 for (bitnum = 0; bitnum < 64; bitnum++) {
267 objs[ATTCOL_BITNUM].data = &bitnum;
268 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM,
269 &objs[ATTCOL_BITNUM], &row));
270 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
271 break; /* this bitnum is unused, use it */
272 if (ret < 0) /* error */
273 goto out;
274 /* this bit is already in use, try next bit */
275 }
276 if (bitnum == 64) {
277 ret = -E_ATT_TABLE_FULL;
278 goto out;
279 }
280 objs[ATTCOL_NAME].data = p;
281 objs[ATTCOL_NAME].size = len + 1;
282 ret = osl(osl_add_row(attribute_table, objs));
283 if (ret < 0)
284 goto out;
285 aed.name = p;
286 aed.bitnum = bitnum;
287 afs_event(ATTRIBUTE_ADD, &pb, &aed);
288 greatest_att_bitnum = PARA_MAX(greatest_att_bitnum, (int)bitnum);
289 }
290 out:
291 if (ret < 0 && ret2 >= 0)
292 para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
293 if (pb.offset)
294 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
295 free(pb.buf);
296 }
297
298 int com_addatt(struct command_context *cc)
299 {
300 int ret;
301
302 if (cc->argc < 2)
303 return -E_ATTR_SYNTAX;
304 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
305 com_addatt_callback, afs_cb_result_handler, cc);
306 if (ret < 0)
307 send_strerror(cc, -ret);
308 return ret;
309 }
310
311 static void com_mvatt_callback(int fd, const struct osl_object *query)
312 {
313 char *old = query->data;
314 size_t size = strlen(old) + 1;
315 char *new = old + size;
316 struct osl_object obj = {.data = old, .size = size};
317 struct osl_row *row;
318 struct para_buffer pb = {
319 .max_size = shm_get_shmmax(),
320 .private_data = &(struct afs_max_size_handler_data) {
321 .fd = fd,
322 .band = SBD_OUTPUT
323 },
324 .max_size_handler = afs_max_size_handler,
325 };
326 int ret;
327
328 ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
329 if (ret < 0)
330 goto out;
331 obj.data = new;
332 obj.size = strlen(new) + 1;
333 ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
334 out:
335 if (ret < 0)
336 para_printf(&pb, "%s\n", para_strerror(-ret));
337 else
338 afs_event(ATTRIBUTE_RENAME, &pb, NULL);
339 if (pb.offset)
340 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
341 free(pb.buf);
342 }
343
344 int com_mvatt(struct command_context *cc)
345 {
346 int ret;
347
348 if (cc->argc != 3)
349 return -E_ATTR_SYNTAX;
350 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
351 com_mvatt_callback, afs_cb_result_handler, cc);
352 if (ret < 0)
353 send_strerror(cc, -ret);
354 return ret;
355 }
356
357 /** Data passed to the action handler of com_rmatt(). */
358 struct remove_attribute_action_data {
359 /** Message buffer. */
360 struct para_buffer pb;
361 /** Numver of attributes removed. */
362 int num_removed;
363 /** Bitwise "or" of the removed attributes. */
364 uint64_t mask_of_removed_atts;
365 };
366
367 static int remove_attribute(struct osl_table *table, struct osl_row *row,
368 const char *name, void *data)
369 {
370 struct remove_attribute_action_data *raad = data;
371 int ret;
372 struct rmatt_event_data red = {.name = name};
373
374 ret = get_attribute_bitnum_by_name(name, &red.bitnum);
375 if (ret < 0)
376 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
377 ret = osl(osl_del_row(table, row));
378 if (ret < 0)
379 return para_printf(&raad->pb, "%s: %s\n", name, para_strerror(-ret));
380 ret = para_printf(&raad->pb, "removed attribute %s\n", name);
381 raad->num_removed++;
382 raad->mask_of_removed_atts |= (1 << red.bitnum);
383 afs_event(ATTRIBUTE_REMOVE, &raad->pb, &red);
384 return ret;
385 }
386
387 static void com_rmatt_callback(int fd, const struct osl_object *query)
388 {
389 struct remove_attribute_action_data raad = {
390 .num_removed = 0,
391 .pb = {
392 .max_size = shm_get_shmmax(),
393 .private_data = &(struct afs_max_size_handler_data) {
394 .fd = fd,
395 .band = SBD_OUTPUT
396 },
397 .max_size_handler = afs_max_size_handler,
398 }
399 };
400 int ret, ret2 = 0;
401 struct pattern_match_data pmd = {
402 .table = attribute_table,
403 .patterns = *query,
404 .loop_col_num = ATTCOL_BITNUM,
405 .match_col_num = ATTCOL_NAME,
406 .data = &raad,
407 .action = remove_attribute
408 };
409 ret = for_each_matching_row(&pmd);
410 if (ret < 0)
411 ret2 = para_printf(&raad.pb, "%s\n", para_strerror(-ret));
412 else if (!raad.num_removed)
413 ret2 = para_printf(&raad.pb, "no match -- nothing removed\n");
414 if (ret2 >= 0 && raad.pb.offset)
415 pass_buffer_as_shm(fd, SBD_OUTPUT, raad.pb.buf, raad.pb.offset);
416 free(raad.pb.buf);
417 }
418
419 int com_rmatt(struct command_context *cc)
420 {
421 int ret;
422
423 if (cc->argc < 2)
424 return -E_ATTR_SYNTAX;
425 ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1,
426 com_rmatt_callback, afs_cb_result_handler, cc);
427 if (ret < 0)
428 send_strerror(cc, -ret);
429 return ret;
430 }
431
432 /**
433 * Return a binary representation of the given attribute value.
434 *
435 * \param atts Pointer to the attribute value.
436 * \param buf Result.
437 *
438 * This function prints a string of at most 64 characters plus the terminating
439 * \p NULL character into \a buf which must be provided by the caller and at
440 * least 65 bytes long. The "x" character is used for set attributes and "-" is
441 * used for unset attributes.
442 *
443 * In practice, not all 64 attributes are defined. In this case, the function
444 * only prints \a N + 1 charaters where \a N is the greatest id of a defined
445 * attribute.
446 */
447 void get_attribute_bitmap(const uint64_t *atts, char *buf)
448 {
449 int i;
450 const uint64_t one = 1;
451
452 for (i = 0; i <= greatest_att_bitnum; i++)
453 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
454 buf[i] = '\0';
455 }
456
457 /**
458 * Get a string containing the set attributes in text form.
459 *
460 * \param atts The attribute bitmap.
461 * \param delim The delimiter to separate matching attribute names.
462 * \param text Result pointer.
463 *
464 * \return Positive on success, negative on errors. If no attributes have
465 * been defined, \a *text is NULL.
466 */
467 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
468 {
469 int i, ret;
470 const uint64_t one = 1;
471
472 *text = NULL;
473 if (greatest_att_bitnum < 0) { /* no attributes available */
474 *text = para_strdup("(no attributes available)");
475 return 1;
476 }
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(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
485 if (ret < 0)
486 goto err;
487 ret = osl(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 * \sa osl_close_table().
509 */
510 static void attribute_close(void)
511 {
512 osl_close_table(attribute_table, OSL_MARK_CLEAN);
513 attribute_table = NULL;
514 }
515
516 /**
517 * Open the attribute table.
518 *
519 * \param dir The database directory.
520 *
521 * \return Positive on success, negative on errors.
522 *
523 * \sa osl_open_table().
524 */
525 static int attribute_open(const char *dir)
526 {
527 int ret;
528
529 attribute_table_desc.dir = dir;
530 ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
531 greatest_att_bitnum = -1; /* no atts available */
532 if (ret >= 0) {
533 find_greatest_att_bitnum();
534 return ret;
535 }
536 attribute_table = NULL;
537 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
538 return 1;
539 return ret;
540 }
541
542 static int attribute_create(const char *dir)
543 {
544 attribute_table_desc.dir = dir;
545 return osl(osl_create_table(&attribute_table_desc));
546 }
547
548 /**
549 * Initialize the attribute table structure.
550 *
551 * \param t The table structure to initialize.
552 */
553 void attribute_init(struct afs_table *t)
554 {
555 t->open = attribute_open;
556 t->close = attribute_close;
557 t->create = attribute_create;
558 }