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