]> git.tuebingen.mpg.de Git - paraslash.git/blob - attribute.c
2beabafa7a3d4b65f7ea87207b0c1e020f15a1d3
[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         int ret;
322         struct pattern_match_data pmd = {
323                 .table = attribute_table,
324                 .patterns = aca->query,
325                 .loop_col_num = ATTCOL_BITNUM,
326                 .match_col_num = ATTCOL_NAME,
327                 .data = aca,
328                 .action = remove_attribute
329         };
330         ret = for_each_matching_row(&pmd);
331         if (ret < 0)
332                 goto out;
333         if (pmd.num_matches == 0)
334                 ret = -E_NO_MATCH;
335 out:
336         return ret;
337 }
338
339 int com_rmatt(struct command_context *cc)
340 {
341         if (cc->argc < 2)
342                 return -E_ATTR_SYNTAX;
343         return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
344                 com_rmatt_callback, afs_cb_result_handler, cc);
345 }
346
347 /**
348  * Return a binary representation of the given attribute value.
349  *
350  * \param atts Pointer to the attribute value.
351  * \param buf Result.
352  *
353  * This function prints a string of at most 64 characters plus the terminating
354  * \p NULL character into \a buf which must be provided by the caller and at
355  * least 65 bytes long. The "x" character is used for set attributes and "-" is
356  * used for unset attributes.
357  *
358  * In practice, not all 64 attributes are defined. In this case, the function
359  * only prints \a N + 1 characters where \a N is the greatest id of a defined
360  * attribute.
361  */
362 void get_attribute_bitmap(const uint64_t *atts, char *buf)
363 {
364         int i;
365         const uint64_t one = 1;
366
367         for (i = 0; i <= greatest_att_bitnum; i++)
368                 buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
369         buf[i] = '\0';
370 }
371
372 /**
373  * Get a string containing the set attributes in text form.
374  *
375  * \param atts The attribute bitmap.
376  * \param delim The delimiter to separate matching attribute names.
377  * \param text Result pointer.
378  *
379  * \return Positive on success, negative on errors. If no attributes have
380  * been defined, \a *text is NULL.
381  */
382 int get_attribute_text(uint64_t *atts, const char *delim, char **text)
383 {
384         int i, ret;
385         const uint64_t one = 1;
386
387         *text = NULL;
388         if (greatest_att_bitnum < 0) { /* no attributes available */
389                 *text = para_strdup("(no attributes available)");
390                 return 1;
391         }
392         for (i = 0; i <= greatest_att_bitnum; i++) {
393                 unsigned char bn = i;
394                 struct osl_object obj = {.data = &bn, .size = 1};
395                 struct osl_row *row;
396
397                 if (!(*atts & (one << i)))
398                         continue;
399                 ret = osl(osl_get_row(attribute_table, ATTCOL_BITNUM, &obj, &row));
400                 if (ret < 0)
401                         goto err;
402                 ret = osl(osl_get_object(attribute_table, row, ATTCOL_NAME, &obj));
403                 if (ret < 0)
404                         goto err;
405                 if (*text) {
406                         char *tmp = make_message("%s%s%s", *text, delim, (char *)obj.data);
407                         free(*text);
408                         *text = tmp;
409                 } else
410                         *text = para_strdup(obj.data);
411         }
412         if (!*text) /* no attributes set */
413                 *text = para_strdup("");
414         return 1;
415 err:
416         free(*text);
417         return ret;
418 }
419
420 static int att_logical_or(struct osl_row *row, void *data)
421 {
422         uint64_t *att_mask = data, one = 1;
423         struct osl_object bitnum_obj;
424         int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
425
426         if (ret < 0)
427                 return ret;
428         *att_mask |= one << *(unsigned char *)bitnum_obj.data;
429         return 0;
430 }
431
432 /**
433  * Compute the attribute bit mask and check each afs info bitmap.
434  *
435  * \param aca The query field of \a aca is ignored.
436  *
437  * This iterates over all attributes in the attribute table and computes the
438  * logical or of 1 << b where b is the bit number of the attribute. The
439  * resulting bit mask is passed to aft_check_attributes() which performs the
440  * actual check.
441  *
442  * \return Standard.
443  *
444  * \sa \ref aft_check_attributes().
445  */
446 int attribute_check_callback(struct afs_callback_arg *aca)
447 {
448         int ret;
449         uint64_t att_mask = 0; /* bits corresponding to a attributes */
450
451         ret = osl_rbtree_loop(attribute_table, ATTCOL_BITNUM, &att_mask,
452                 att_logical_or);
453         if (ret < 0) {
454                 PARA_ERROR_LOG("attribute table loop failed: %s\n",
455                         para_strerror(-ret));
456                 return ret;
457         }
458         return aft_check_attributes(att_mask, &aca->pbout);
459 }
460
461 /**
462  * Close the attribute table.
463  *
464  * \sa osl_close_table().
465  */
466 static void attribute_close(void)
467 {
468         osl_close_table(attribute_table, OSL_MARK_CLEAN);
469         attribute_table = NULL;
470 }
471
472 /**
473  * Open the attribute table.
474  *
475  * \param dir The database directory.
476  *
477  * \return Positive on success, negative on errors.
478  *
479  * \sa osl_open_table().
480  */
481 static int attribute_open(const char *dir)
482 {
483         int ret;
484
485         attribute_table_desc.dir = dir;
486         ret = osl(osl_open_table(&attribute_table_desc, &attribute_table));
487         greatest_att_bitnum = -1; /* no atts available */
488         if (ret >= 0) {
489                 find_greatest_att_bitnum();
490                 return ret;
491         }
492         attribute_table = NULL;
493         if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
494                 return 1;
495         return ret;
496 }
497
498 static int attribute_create(const char *dir)
499 {
500         attribute_table_desc.dir = dir;
501         return osl(osl_create_table(&attribute_table_desc));
502 }
503
504 /**
505  * Initialize the attribute table structure.
506  *
507  * \param t The table structure to initialize.
508  */
509 void attribute_init(struct afs_table *t)
510 {
511         t->open = attribute_open;
512         t->close = attribute_close;
513         t->create = attribute_create;
514 }