paraslash 0.7.3
[paraslash.git] / blob.c
diff --git a/blob.c b/blob.c
index 2d7c6050cfb18c198675c1ce4a534dd824983310..1802de5d31f89bf6fe951c5977cbef4967f9c11e 100644 (file)
--- a/blob.c
+++ b/blob.c
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file blob.c Macros and functions for blob handling. */
 
@@ -33,8 +29,8 @@
  */
 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
 {
-       uint32_t d1 = read_u32((const char *)obj1->data);
-       uint32_t d2 = read_u32((const char *)obj2->data);
+       uint32_t d1 = read_u32(obj1->data);
+       uint32_t d2 = read_u32(obj2->data);
 
        if (d1 < d2)
                return 1;
@@ -76,7 +72,6 @@ static struct osl_column_description blob_cols[] = {
 /** Define a pointer to an osl blob table with a canonical name. */
 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
 
-
 /** Define a blob table. */
 #define INIT_BLOB_TABLE(table_name) \
        DEFINE_BLOB_TABLE_DESC(table_name); \
@@ -105,10 +100,10 @@ static int print_blob(struct osl_table *table, struct osl_row *row,
        }
        ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
        if (ret < 0) {
-               para_printf(&aca->pbout, "cannot list %s\n", name);
+               afs_error(aca, "cannot list %s\n", name);
                return ret;
        }
-       id = *(uint32_t *)obj.data;
+       id = read_u32(obj.data);
        para_printf(&aca->pbout, "%u\t%s\n", id, name);
        return 1;
 }
@@ -125,6 +120,7 @@ static int com_lsblob_callback(const struct lls_command * const cmd,
                .action = print_blob,
        };
        int ret;
+
        ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
        pmd.lpr = aca->lpr;
        assert(ret >= 0);
@@ -180,6 +176,7 @@ static int com_catblob_callback(const struct lls_command * const cmd,
                .data = &aca->fd,
                .action = cat_blob
        };
+
        ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
        assert(ret >= 0);
        pmd.lpr = aca->lpr;
@@ -211,8 +208,9 @@ static int remove_blob(struct osl_table *table, struct osl_row *row,
 {
        struct afs_callback_arg *aca = data;
        int ret = osl(osl_del_row(table, row));
+
        if (ret < 0) {
-               para_printf(&aca->pbout, "cannot remove %s\n", name);
+               afs_error(aca, "cannot remove %s\n", name);
                return ret;
        }
        return 1;
@@ -230,6 +228,7 @@ static int com_rmblob_callback(const struct lls_command * const cmd,
                .data = aca,
                .action = remove_blob
        };
+
        ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
        assert(ret >= 0);
        pmd.lpr = aca->lpr;
@@ -267,7 +266,8 @@ static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
        struct osl_object objs[NUM_BLOB_COLUMNS];
        char *name = aca->query.data;
        size_t name_len = strlen(name) + 1;
-       uint32_t id;
+       uint32_t id = (uint32_t)-1; /* STFU, gcc */
+       char id_buf[sizeof(id)];
        unsigned num_rows;
        int ret;
 
@@ -275,10 +275,15 @@ static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
        if (ret < 0)
                goto out;
        if (!num_rows) { /* this is the first entry ever added */
-               /* insert dummy row containing the id */
-               id = 2; /* this entry will be entry #1, so 2 is the next */
-               objs[BLOBCOL_ID].data = &id;
-               objs[BLOBCOL_ID].size = sizeof(id);
+               /*
+                * Insert dummy row containing the next free ID. Since we are
+                * about to insert the first blob with ID 1, the next free ID
+                * will be 2.
+                */
+               id = 2U;
+               write_u32(id_buf, id);
+               objs[BLOBCOL_ID].data = id_buf;
+               objs[BLOBCOL_ID].size = sizeof(id_buf);
                objs[BLOBCOL_NAME].data = "";
                objs[BLOBCOL_NAME].size = 1;
                objs[BLOBCOL_DEF].data = "";
@@ -297,7 +302,7 @@ static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
                        ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
                        if (ret < 0)
                                goto out;
-                       id = *(uint32_t *)obj.data;
+                       id = read_u32(obj.data);
                        obj.data = name + name_len;
                        obj.size = aca->query.size - name_len;
                        ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
@@ -312,15 +317,17 @@ static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
                ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
                if (ret < 0)
                        goto out;
-               id = *(uint32_t *)obj.data + 1;
-               obj.data = &id;
+               id = read_u32(obj.data) + 1;
+               write_u32(id_buf, id);
+               obj.data = &id_buf;
                ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
                if (ret < 0)
                        goto out;
        }
        id--;
-       objs[BLOBCOL_ID].data = &id;
-       objs[BLOBCOL_ID].size = sizeof(id);
+       write_u32(id_buf, id);
+       objs[BLOBCOL_ID].data = &id_buf;
+       objs[BLOBCOL_ID].size = sizeof(id_buf);
        objs[BLOBCOL_NAME].data = name;
        objs[BLOBCOL_NAME].size = name_len;
        objs[BLOBCOL_DEF].data = name + name_len;
@@ -331,7 +338,7 @@ static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
        ret = afs_event(BLOB_ADD, NULL, table);
 out:
        if (ret < 0)
-               para_printf(&aca->pbout, "cannot add %s\n", name);
+               afs_error(aca, "cannot add %s\n", name);
        else
                para_printf(&aca->pbout, "added %s as id %u\n", name, id);
        return ret;
@@ -378,7 +385,7 @@ again:
  *
  * This function is called from the addblob command handlers to instruct the
  * afs process to store the input in a blob table. Input is read and decrypted
- * from the file descriptor given by cc and appended to arg_obj, which contains
+ * from the file descriptor given by cc and appended to a buffer which also contains
  * the name of the blob to create. The combined buffer is made available to the
  * afs process via the callback method.
  */
@@ -396,7 +403,7 @@ static int stdin_command(struct command_context *cc,
        if (ret < 0)
                return ret;
        query.size = len + 1 + stdin_obj.size;
-       query.data = para_malloc(query.size);
+       query.data = alloc(query.size);
        memcpy(query.data, lls_input(0, lpr), len + 1);
        if (stdin_obj.size > 0)
                memcpy((char *)query.data + len + 1, stdin_obj.data,
@@ -439,15 +446,14 @@ static int com_mvblob_callback(const struct lls_command * const cmd,
        ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
 
        if (ret < 0) {
-               para_printf(&aca->pbout, "cannot find source blob %s\n", src);
+               afs_error(aca, "cannot find source blob %s\n", src);
                goto out;
        }
        obj.data = (char *)dest;
        obj.size = strlen(dest) + 1;
        ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
        if (ret < 0) {
-               para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
-                       src, dest);
+               afs_error(aca, "cannot rename blob %s to %s\n", src, dest);
                goto out;
        }
        ret = afs_event(BLOB_RENAME, NULL, table);
@@ -513,12 +519,11 @@ static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
                return blob_get_name_by_id(table_name ## _table, id, name); \
        }
 
-
-static int blob_get_def_by_name(struct osl_table *table, char *name,
+static int blob_get_def_by_name(struct osl_table *table, const char *name,
                struct osl_object *def)
 {
        struct osl_row *row;
-       struct osl_object obj = {.data = name, .size = strlen(name) + 1};
+       struct osl_object obj = {.data = (void *)name, .size = strlen(name) + 1};
        int ret;
 
        def->data = NULL;
@@ -532,7 +537,7 @@ static int blob_get_def_by_name(struct osl_table *table, char *name,
 
 /** Define the \p get_def_by_id function for this blob type. */
 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
-       int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
+       int cmd_prefix ## _get_def_by_name(const char *name, struct osl_object *def) \
        { \
                return blob_get_def_by_name(table_name ## _table, name, def); \
        }
@@ -565,6 +570,7 @@ static int blob_get_name_and_def_by_row(struct osl_table *table,
 {
        struct osl_object obj;
        int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
+
        if (ret < 0)
                return ret;
        *name = obj.data;
@@ -600,6 +606,7 @@ static int blob_open(struct osl_table **table,
                const char *dir)
 {
        int ret;
+
        desc->dir = dir;
        ret = osl(osl_open_table(desc, table));
        if (ret >= 0)
@@ -617,25 +624,21 @@ static int blob_open(struct osl_table **table,
                        &table_name ## _table_desc, dir); \
        }
 
-
-/** Define the \p init function for this blob type. */
-#define DEFINE_BLOB_INIT(table_name) \
-       void table_name ## _init(struct afs_table *t) \
-       { \
-               t->open = table_name ## _open; \
-               t->close = table_name ## _close; \
-               t->create = table_name ## _create;\
-               t->event_handler = table_name ##_event_handler; \
-               table_name ## _table = NULL; \
-       }
-
+/** Blob tables map integers to blobs. */
+#define DEFINE_BLOB_AFS_TABLE_OPS(table_name) \
+       const struct afs_table_operations table_name ## _ops = { \
+               .open = table_name ## _open, \
+               .close = table_name ## _close, \
+               .create = table_name ## _create, \
+               .event_handler = table_name ##_event_handler, \
+       };
 
 /** Define all functions for this blob type. */
 #define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name) \
        DEFINE_BLOB_OPEN(table_name) \
        DEFINE_BLOB_CLOSE(table_name) \
        DEFINE_BLOB_CREATE(table_name) \
-       DEFINE_BLOB_INIT(table_name) \
+       DEFINE_BLOB_AFS_TABLE_OPS(table_name) \
        DEFINE_BLOB_COMMAND(ls, LS, table_name, short_name, c_short_name) \
        DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \
        DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \