ctdb-tools-ctdb: Generalise find_natgw() -> filter_nodemap_by_flags()
authorMartin Schwenke <martin@meltin.net>
Tue, 18 Feb 2014 06:11:53 +0000 (17:11 +1100)
committerAmitay Isaacs <amitay@samba.org>
Sun, 23 Mar 2014 03:20:14 +0000 (04:20 +0100)
Instead of just finding the first node that doesn't have any flags in
flag_mask set, change it into a function that filters a nodemap to
exclude nodes with the given flags.

This makes the NATGW code simpler but also provides a function that
can be used in other code.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tools/ctdb.c

index 4bb794e9f8eda3079adb9725fa87a48bcac5b9b8..3eef1e211797ae88cc47f856ce5de0a726a0e8de 100644 (file)
@@ -1166,22 +1166,29 @@ filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
        return ret;
 }
 
-
-static int find_natgw(struct ctdb_context *ctdb,
-                      struct ctdb_node_map *nodemap, uint32_t flags,
-                      uint32_t *pnn, const char **ip)
+static struct ctdb_node_map *
+filter_nodemap_by_flags(struct ctdb_context *ctdb,
+                       struct ctdb_node_map *nodemap,
+                       uint32_t flags_mask)
 {
        int i;
+       struct ctdb_node_map *ret;
 
-       for (i=0;i<nodemap->num;i++) {
-               if (!(nodemap->nodes[i].flags & flags)) {
-                       *pnn = nodemap->nodes[i].pnn;
-                       *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
-                       return 0;
+       ret = talloc_nodemap(nodemap);
+       CTDB_NO_MEMORY_NULL(ctdb, ret);
+
+       ret->num = 0;
+
+       for (i = 0; i < nodemap->num; i++) {
+               if (nodemap->nodes[i].flags & flags_mask) {
+                       continue;
                }
+
+               ret->nodes[ret->num] = nodemap->nodes[i];
+               ret->num++;
        }
 
-       return 2; /* matches ENOENT */
+       return ret;
 }
 
 /*
@@ -1300,15 +1307,21 @@ static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **a
        pnn = -1;
        ip = "0.0.0.0";
        for (i = 0; exclude_flags[i] != 0; i++) {
-               ret = find_natgw(ctdb, cnodemap,
-                                exclude_flags[i],
-                                &pnn, &ip);
-               if (ret == -1) {
+               struct ctdb_node_map *t =
+                       filter_nodemap_by_flags(ctdb, cnodemap,
+                                               exclude_flags[i]);
+               if (t == NULL) {
+                       /* No memory */
+                       ret = -1;
                        goto done;
                }
-               if (ret == 0) {
+               if (t->num > 0) {
+                       ret = 0;
+                       pnn = t->nodes[0].pnn;
+                       ip = ctdb_addr_to_str(&t->nodes[0].addr);
                        break;
                }
+               talloc_free(t);
        }
 
        if (options.machinereadable) {