Make com_lsblob() and com_lsatt() more similar to each other.
[paraslash.git] / attribute.c
index f33987271d7b5352c03a9eddbf8438da8cbe505f..b8d4a5938beb792e5e5c7ebca62548407a91d12a 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file attribute.c Attribute handling functions. */
 #include "para.h"
 #include "error.h"
 #include "afh.h"
 #include "para.h"
 #include "error.h"
 #include "afh.h"
@@ -5,9 +12,19 @@
 #include "string.h"
 #include "net.h"
 
 #include "string.h"
 #include "net.h"
 
-static void *attribute_table;
+static struct osl_table *attribute_table;
 static int greatest_att_bitnum;
 
 static int greatest_att_bitnum;
 
+/** The columns of the attribute table. */
+enum attribute_table_columns {
+       /** The bit number (0-63). */
+       ATTCOL_BITNUM,
+       /** The name of the attribute. */
+       ATTCOL_NAME,
+       /** Number of columns in this table. */
+       NUM_ATT_COLUMNS
+};
+
 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
 {
        const unsigned char *c1 = (const unsigned char*)obj1->data;
 static int char_compare(const struct osl_object *obj1, const struct osl_object *obj2)
 {
        const unsigned char *c1 = (const unsigned char*)obj1->data;
@@ -19,8 +36,6 @@ static int char_compare(const struct osl_object *obj1, const struct osl_object *
        return 0;
 }
 
        return 0;
 }
 
-enum attribute_table_columns {ATTCOL_BITNUM, ATTCOL_NAME, NUM_ATT_COLUMNS};
-
 static struct osl_column_description att_cols[] = {
        [ATTCOL_BITNUM] = {
                .storage_type = OSL_MAPPED_STORAGE,
 static struct osl_column_description att_cols[] = {
        [ATTCOL_BITNUM] = {
                .storage_type = OSL_MAPPED_STORAGE,
@@ -60,6 +75,14 @@ static void find_greatest_att_bitnum(void)
        greatest_att_bitnum = -E_NO_ATTRIBUTES;
 }
 
        greatest_att_bitnum = -E_NO_ATTRIBUTES;
 }
 
+/**
+ * Retrieve the identifier (number) of an attribute.
+ *
+ * \param att_name The name of the attribute.
+ * \param bitnum Result pointer.
+ *
+ * \return Positive on success, negative on errors.
+ */
 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
 {
        struct osl_object obj = {.data = (char *)att_name,
 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
 {
        struct osl_object obj = {.data = (char *)att_name,
@@ -76,40 +99,79 @@ int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
        return 1;
 }
 
        return 1;
 }
 
-#define LAA_FLAG_ALPHA 1
-#define LAA_FLAG_LONG 2
+enum lsatt_flags {
+       /** Whether "-a" was given for the lsatt command. */
+       LSATT_FLAG_SORT_BY_ID = 1,
+       /** Whether "-l" was given for the lsatt command. */
+       LSATT_FLAG_LONG = 2,
+       LSATT_FLAG_REVERSE = 4
+};
 
 
-struct private_laa_data {
-       int fd;
+/** Data passed to the action function of lsatt */
+struct lsatt_action_data {
+       /** The result buffer. */
+       struct para_buffer pb;
+       /** The given flags for the lsatt command. */
        unsigned flags;
 };
 
        unsigned flags;
 };
 
-static int log_attribute(struct osl_row *row, void *private_data)
+static int print_attribute(struct osl_table *table, struct osl_row *row,
+               const char *name, void *data)
 {
 {
-       struct private_laa_data *pld = private_data;
+       struct lsatt_action_data *laad = data;
+       struct osl_object bitnum_obj;
        int ret;
        int ret;
-       struct osl_object name_obj, bitnum_obj;
 
 
-       ret = osl_get_object(attribute_table, row, ATTCOL_NAME, &name_obj);
-       if (ret < 0)
-               return ret;
-       if (!(pld->flags & LAA_FLAG_LONG)) {
-               send_buffer(pld->fd, (char *)name_obj.data);
+       if (!(laad->flags & LSATT_FLAG_LONG)) {
+               para_printf(&laad->pb, "%s\n", name);
                return 1;
        }
                return 1;
        }
-       ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
-       if (ret < 0)
+       ret = osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj);
+       if (ret < 0) {
+               para_printf(&laad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
                return ret;
                return ret;
-       send_va_buffer(pld->fd, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
-               (char *)name_obj.data);
+       }
+       para_printf(&laad->pb, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
+               name);
+       return 1;
+}
+
+static int com_lsatt_callback(const struct osl_object *query,
+               struct osl_object *result)
+{
+       struct lsatt_action_data laad = {.flags = *(unsigned *) query->data};
+       struct pattern_match_data pmd = {
+               .table = attribute_table,
+               .loop_col_num = ATTCOL_BITNUM,
+               .match_col_num = ATTCOL_NAME,
+               .patterns = {.data = (char *)query->data + sizeof(laad.flags),
+                       .size = query->size - sizeof(laad.flags)},
+               .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
+               .data = &laad,
+               .action = print_attribute
+       };
+       int ret;
+
+       if (laad.flags & LSATT_FLAG_SORT_BY_ID)
+               pmd.loop_col_num = ATTCOL_NAME;
+       if (laad.flags & LSATT_FLAG_REVERSE)
+               pmd.pm_flags |= PM_REVERSE_LOOP;
+       ret = for_each_matching_row(&pmd);
+       if (ret < 0)
+               para_printf(&laad.pb, "%s\n", PARA_STRERROR(-ret));
+       if (!laad.pb.buf)
+               return 0;
+       result->data = laad.pb.buf;
+       result->size = laad.pb.size;
        return 1;
 }
 
        return 1;
 }
 
-/* FIXME: Need callback */
 int com_lsatt(int fd, int argc, char * const * const argv)
 {
 int com_lsatt(int fd, int argc, char * const * const argv)
 {
-       struct private_laa_data pld = {.fd = fd, .flags = 0};
-       int i;
+       unsigned flags = 0;
+       struct osl_object options = {.data = &flags, .size = sizeof(flags)},
+               result;
+       int ret, i;
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
@@ -119,22 +181,27 @@ int com_lsatt(int fd, int argc, char * const * const argv)
                        i++;
                        break;
                }
                        i++;
                        break;
                }
-               if (!strcmp(arg, "-a")) {
-                       pld.flags |= LAA_FLAG_ALPHA;
+               if (!strcmp(arg, "-i")) {
+                       flags |= LSATT_FLAG_SORT_BY_ID;
                        continue;
                }
                if (!strcmp(arg, "-l")) {
                        continue;
                }
                if (!strcmp(arg, "-l")) {
-                       pld.flags |= LAA_FLAG_LONG;
+                       flags |= LSATT_FLAG_LONG;
+                       continue;
+               }
+               if (!strcmp(arg, "-r")) {
+                       flags |= LSATT_FLAG_REVERSE;
                        continue;
                }
        }
                        continue;
                }
        }
-       if (argc > i)
-               return -E_ATTR_SYNTAX;
-       if (pld.flags & LAA_FLAG_ALPHA)
-               return osl_rbtree_loop(attribute_table, ATTCOL_NAME,
-                       &pld, log_attribute);
-       return osl_rbtree_loop(attribute_table, ATTCOL_BITNUM,
-               &pld, log_attribute);
+       ret = send_option_arg_callback_request(&options, argc -i, argv + i,
+               com_lsatt_callback, &result);
+       if (ret > 0) {
+               ret = send_buffer(fd, (char *)result.data);
+               free(result.data);
+       } else
+               send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
+       return ret;
 }
 
 static int com_setatt_callback(const struct osl_object *query,
 }
 
 static int com_setatt_callback(const struct osl_object *query,
@@ -183,7 +250,7 @@ static int com_setatt_callback(const struct osl_object *query,
                ret = aft_get_row_of_path(p, &aft_row);
                if (ret < 0)
                        return ret;
                ret = aft_get_row_of_path(p, &aft_row);
                if (ret < 0)
                        return ret;
-               ret = get_afsi_object_of_row(p, &obj);
+               ret = get_afsi_object_of_row(aft_row, &obj);
                if (ret < 0)
                        return ret;
                ret = load_afsi(&old_afsi, &obj);
                if (ret < 0)
                        return ret;
                ret = load_afsi(&old_afsi, &obj);
@@ -193,9 +260,9 @@ static int com_setatt_callback(const struct osl_object *query,
                new_afsi.attributes |= add_mask;
                new_afsi.attributes &= ~del_mask;
                save_afsi(&new_afsi, &obj); /* in-place update */
                new_afsi.attributes |= add_mask;
                new_afsi.attributes &= ~del_mask;
                save_afsi(&new_afsi, &obj); /* in-place update */
-               ret = mood_update_audio_file(aft_row, &old_afsi);
-               if (ret < 0)
-                       return ret;
+//             ret = mood_update_audio_file(aft_row, &old_afsi);
+//             if (ret < 0)
+//                     return ret;
        }
        return 1;
 }
        }
        return 1;
 }
@@ -204,7 +271,7 @@ int com_setatt(__a_unused int fd, int argc, char * const * const argv)
 {
        if (argc < 2)
                return -E_ATTR_SYNTAX;
 {
        if (argc < 2)
                return -E_ATTR_SYNTAX;
-       return send_standard_callback_request(argc, argv, com_setatt_callback,
+       return send_standard_callback_request(argc - 1, argv + 1, com_setatt_callback,
                NULL);
 }
 
                NULL);
 }
 
@@ -273,53 +340,97 @@ static int com_addatt_callback(const struct osl_object *query,
        if (ret < 0)
                return ret;
        find_greatest_att_bitnum();
        if (ret < 0)
                return ret;
        find_greatest_att_bitnum();
-       return mood_reload();
+       return reload_current_mood(); /* FIXME: mood_reload() returns an error */
 }
 
 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
 {
        if (argc < 2)
                return -E_ATTR_SYNTAX;
 }
 
 int com_addatt(__a_unused int fd, int argc, char * const * const argv)
 {
        if (argc < 2)
                return -E_ATTR_SYNTAX;
-       return send_standard_callback_request(argc, argv, com_addatt_callback,
+       return send_standard_callback_request(argc - 1, argv + 1, com_addatt_callback,
                NULL);
 }
                NULL);
 }
+struct remove_attribute_action_data {
+       struct para_buffer pb;
+       int num_removed;
+};
+
+static int remove_attribute(struct osl_table *table, struct osl_row *row,
+               const char *name, void *data)
+{
+       struct remove_attribute_action_data *raad = data;
+       int ret = osl_del_row(table, row);
+       if (ret < 0)
+               para_printf(&raad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
+       else {
+               para_printf(&raad->pb, "removed %s\n", name);
+               raad->num_removed++;
+       }
+       return 1;
+}
 
 static int com_rmatt_callback(const struct osl_object *query,
 
 static int com_rmatt_callback(const struct osl_object *query,
-               __a_unused struct osl_object *result)
+               struct osl_object *result)
 {
 {
-       char *p = query->data;
-       int ret, atts_removed = 0;
-       while (p < (char *)query->data + query->size) {
-               struct osl_object obj = {
-                       .data = p,
-                       .size = strlen(p) + 1
-               };
-               struct osl_row *row;
-               ret = osl_get_row(attribute_table, ATTCOL_NAME,
-                       &obj, &row);
-               if (ret < 0)
-                       return ret;
-               ret = osl_del_row(attribute_table, row);
+       struct remove_attribute_action_data raad = {.num_removed = 0};
+       int ret;
+       struct pattern_match_data pmd = {
+               .table = attribute_table,
+               .patterns = *query,
+               .loop_col_num = ATTCOL_BITNUM,
+               .match_col_num = ATTCOL_NAME,
+               .data = &raad,
+               .action = remove_attribute
+       };
+       ret = for_each_matching_row(&pmd);
+       if (ret < 0)
+               para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
+       if (raad.num_removed) {
+               find_greatest_att_bitnum();
+               ret = reload_current_mood();
                if (ret < 0)
                if (ret < 0)
-                       return ret;
-               atts_removed++;
-               p += strlen(p) + 1;
+                       para_printf(&raad.pb, "%s\n", PARA_STRERROR(-ret));
        }
        }
-       find_greatest_att_bitnum();
-       if (!atts_removed)
-               return 1;
-       return mood_reload();
+       if (!raad.pb.buf)
+               para_printf(&raad.pb, "no match -- nothing removed\n");
+       result->data = raad.pb.buf;
+       result->size = raad.pb.size;
+       return 1;
 }
 
 }
 
-int com_rmatt(__a_unused int fd, int argc, char * const * const argv)
+int com_rmatt(int fd, int argc, char * const * const argv)
 {
 {
+       int ret;
+       struct osl_object result;
+
        if (argc < 2)
                return -E_ATTR_SYNTAX;
        if (argc < 2)
                return -E_ATTR_SYNTAX;
-       return send_standard_callback_request(argc, argv, com_rmatt_callback,
-               NULL);
+       ret = send_standard_callback_request(argc - 1, argv + 1, com_rmatt_callback,
+               &result);
+       if (ret > 0) {
+               send_buffer(fd, (char *)result.data);
+               free(result.data);
+       } else
+               send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
+       return ret;
 }
 
 }
 
-void get_attribute_bitmap(uint64_t *atts, char *buf)
+/**
+ * Return a binary representation of the geiven attribute value.
+ *
+ * \param atts Pointer to the attribute value.
+ * \param buf Result.
+ *
+ * This function prints a string of at most 64 characters plus the terminating
+ * \p NULL character into \a buf which must be provided by the caller and at
+ * least 65 bytes long. The "x" character is used for set attributes and "-" is
+ * used for unset attributes.
+ *
+ * In practice, not all 64 attributes are defined. In this case, the function
+ * only prints \a N + 1 charaters where \a N is the greatest id of a defined
+ * attribute.
+ */
+void get_attribute_bitmap(const uint64_t *atts, char *buf)
 {
        int i;
        const uint64_t one = 1;
 {
        int i;
        const uint64_t one = 1;
@@ -328,6 +439,7 @@ void get_attribute_bitmap(uint64_t *atts, char *buf)
                buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
        buf[i] = '\0';
 }
                buf[greatest_att_bitnum - i] = (*atts & (one << i))? 'x' : '-';
        buf[i] = '\0';
 }
+
 /**
  * Get a string containing the set attributes in text form.
  *
 /**
  * Get a string containing the set attributes in text form.
  *
@@ -374,22 +486,38 @@ err:
        return ret;
 }
 
        return ret;
 }
 
+/**
+ * Close the attribute table.
+ *
+ * \param flags Ususal flags that are passed to osl_close_table().
+ *
+ * \sa osl_close_table().
+ */
 void attribute_shutdown(enum osl_close_flags flags)
 {
        osl_close_table(attribute_table, flags);
        attribute_table = NULL;
 }
 
 void attribute_shutdown(enum osl_close_flags flags)
 {
        osl_close_table(attribute_table, flags);
        attribute_table = NULL;
 }
 
+/**
+ * Open the attribute table.
+ *
+ * \param ti Gets initialized by this function.
+ * \param db The database directory.
+ *
+ * \return Positive on success, negative on errors.
+ *
+ * \sa osl_open_table().
+ */
 int attribute_init(struct table_info *ti, const char *db)
 {
        int ret;
 
        attribute_table_desc.dir = db;
        ti->desc = &attribute_table_desc;
 int attribute_init(struct table_info *ti, const char *db)
 {
        int ret;
 
        attribute_table_desc.dir = db;
        ti->desc = &attribute_table_desc;
-       ret = osl_open_table(ti->desc, &ti->table);
+       ret = osl_open_table(ti->desc, &attribute_table);
        greatest_att_bitnum = -1; /* no atts available */
        if (ret >= 0) {
        greatest_att_bitnum = -1; /* no atts available */
        if (ret >= 0) {
-               attribute_table = ti->table;
                find_greatest_att_bitnum();
                return ret;
        }
                find_greatest_att_bitnum();
                return ret;
        }