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