metadata: add kdbus_meta_offset_of()
authorDaniel Mack <zonque@gmail.com>
Fri, 15 Aug 2014 17:20:51 +0000 (19:20 +0200)
committerDaniel Mack <zonque@gmail.com>
Fri, 15 Aug 2014 20:09:02 +0000 (22:09 +0200)
kdbus_meta_offset_of() will iterate over the given metadata and return
the offset of the item of type @type, if it exists.

metadata.c
metadata.h

index ff1d203fcb9933da6975ab8923e00ca2283354ea..c8b86c3c00a2ca1d3f0ebcaea94c7bfcf93da6a5 100644 (file)
@@ -57,6 +57,30 @@ int kdbus_meta_new(struct kdbus_meta **meta)
        return 0;
 }
 
+/**
+ * kdbus_meta_offset_of() - return the offset of a meta data element
+ * @meta:              Metadata object
+ * @type:              The metadata type to look for (KDBUS_ITEM_*)
+ * @off:               Return pointer for the offset, if any
+ *
+ * This function will iterate over the given metadata and return the
+ * offset of the item of type @type, if it exists.
+ *
+ * Return: 0 on success, -ENOENT if the item was not found.
+ */
+int kdbus_meta_offset_of(struct kdbus_meta *meta, u64 type, off_t *off)
+{
+       const struct kdbus_item *item;
+
+       KDBUS_ITEMS_FOREACH(item, meta->data, meta->size)
+               if (item->type == type) {
+                       *off = (u8 *) item - (u8 *) meta->data;
+                       return 0;
+               }
+
+       return -ENOENT;
+}
+
 /**
  * kdbus_meta_free() - release metadata
  * @meta:              Metadata object
index 77b730318fce0bc558d04e6c624ba0d35bbf6569..82344b302ef03c58511b221b686a140602cba33b 100644 (file)
@@ -41,5 +41,6 @@ int kdbus_meta_append(struct kdbus_meta *meta,
                      struct kdbus_conn *conn,
                      u64 seq,
                      u64 which);
+int kdbus_meta_offset_of(struct kdbus_meta *meta, u64 type, off_t *off);
 void kdbus_meta_free(struct kdbus_meta *meta);
 #endif