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