Refactored smack_rule_set_list away for full rule set iterator.
authorJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Sun, 30 Jan 2011 13:16:41 +0000 (05:16 -0800)
committerJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Mon, 31 Jan 2011 10:56:07 +0000 (02:56 -0800)
src/smack.c
src/smack.h

index a3d14ad..fecf2b9 100644 (file)
@@ -54,6 +54,11 @@ struct _SmackRuleSet {
        struct smack_subject *subjects;
 };
 
+struct _SmackRuleSetIter {
+       struct smack_subject *subject;
+       struct smack_object *object;
+};
+
 static int update_rule(struct smack_subject **subjects,
                       const char *subject_str, const char *object_str,
                       unsigned ac);
@@ -316,27 +321,56 @@ int smack_rule_set_have_access(SmackRuleSet handle, const char *subject,
        return ((o->ac & ac) == ac);
 }
 
-int smack_rule_set_list(SmackRuleSet handle, int index,
-                       const char **subject, const char **object,
-                       const char **access)
+SmackRuleSetIter smack_rule_set_iter_new(void)
 {
-       struct smack_subject *s, *stmp;
-       struct smack_object *o, *otmp;
-       int i = 0;
+       SmackRuleSetIter iter = calloc(1, sizeof(struct _SmackRuleSetIter));
+       return iter;
+}
 
-       HASH_ITER(hh, handle->subjects, s, stmp) {
-               HASH_ITER(hh, s->objects, o, otmp) {
-                       if (i == index) {
-                               *subject = s->subject;
-                               *object = o->object;
-                               *access = o->acstr;
-                               return 0;
-                       }
-                       i++;
-               }
+void smack_rule_set_iter_free(SmackRuleSetIter iter)
+{
+       if (iter != NULL)
+               free(iter);
+}
+
+void smack_rule_set_iter_get(SmackRuleSet handle,
+                            SmackRuleSetIter iter)
+{
+       iter->subject = handle->subjects;
+       iter->object = NULL;
+}
+
+int smack_rule_set_iter_next(SmackRuleSetIter iter,
+                            const char **subject,
+                            const char **object,
+                            const char **access)
+{
+       struct smack_subject *s;
+       struct smack_object *o;
+
+       if (iter->subject == NULL)
+               return -1;
+
+       if (iter->object == NULL)
+               iter->object = iter->subject->objects;
+       else
+               iter->object = iter->object->hh.next;
+
+       if (iter->object == NULL) {
+               iter->subject = iter->subject->hh.next;
+               if (iter->subject == NULL)
+                       return -1;
+               iter->object = iter->subject->objects;
        }
 
-       return -1;
+       if (iter->object == NULL)
+               return -1;
+
+       *subject = iter->subject->subject;
+       *object = iter->object->object;
+       *access = iter->object->acstr;
+
+       return 0;
 }
 
 static int update_rule(struct smack_subject **subjects,
index 22fc8ff..8603848 100644 (file)
@@ -37,6 +37,8 @@
  */
 typedef struct _SmackRuleSet *SmackRuleSet;
 
+typedef struct _SmackRuleSetIter *SmackRuleSetIter;
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -145,16 +147,40 @@ extern int smack_rule_set_have_access(SmackRuleSet handle, const char *subject,
                                      const char *object, const char *access);
 
 /*!
+ * Create new rule set iterator.
+ *
+ * @return new iterator instance
+ */
+extern SmackRuleSetIter smack_rule_set_iter_new(void);
+
+/*!
+ * Free rule set iterator.
+ *
+ * @param iter iterator
+ */
+extern void smack_rule_set_iter_free(SmackRuleSetIter iter);
+
+/*!
+ * Set iterator into beginning of the given rule set.
+ *
+ * @param handle handle to a rule set
+ * @param iter iterator
+ */
+extern void smack_rule_set_iter_get(SmackRuleSet handle,
+                                   SmackRuleSetIter iter);
+
+/*!
  * Iterate over rules.
  *
- * @param index Index of the rule.
+ * @param iter Iterator
  * @param subject Subject label of the rule.
  * @param object Object label of the rule.
  * @param access Access string for the rule.
  */
-extern int smack_rule_set_list(SmackRuleSet handle, int index,
-                              const char **subject, const char **object,
-                              const char **access);
+extern int smack_rule_set_iter_next(SmackRuleSetIter iter,
+                                   const char **subject,
+                                   const char **object,
+                                   const char **access);
 
 #ifdef __cplusplus
 }