notifyd: Add notifyd_parse_db()
[bbaumbach/samba-autobuild/.git] / source3 / smbd / notifyd / notifyd.c
index 0f31a61fd1f3b7b3ca5d4eabc96fb9d5789be132..d9fb11d8a3652955553d384b2a853af8b99d0c88 100644 (file)
@@ -1417,3 +1417,74 @@ static void notifyd_snoop_broadcast(uint32_t src_vnn, uint32_t dst_vnn,
                return;
        }
 }
+
+struct notifyd_parse_db_state {
+       bool (*fn)(const char *path,
+                  struct server_id server,
+                  const struct notify_instance *instance,
+                  void *private_data);
+       void *private_data;
+};
+
+static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
+                                   void *private_data)
+{
+       struct notifyd_parse_db_state *state = private_data;
+       char path[key.dsize+1];
+       struct notifyd_instance *instances = NULL;
+       size_t num_instances = 0;
+       size_t i;
+       bool ok;
+
+       memcpy(path, key.dptr, key.dsize);
+       path[key.dsize] = 0;
+
+       ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
+                                &num_instances);
+       if (!ok) {
+               DEBUG(10, ("%s: Could not parse entry for path %s\n",
+                          __func__, path));
+               return true;
+       }
+
+       for (i=0; i<num_instances; i++) {
+               ok = state->fn(path, instances[i].client,
+                              &instances[i].instance,
+                              state->private_data);
+               if (!ok) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+int notifyd_parse_db(const uint8_t *buf, size_t buflen,
+                    uint64_t *log_index,
+                    bool (*fn)(const char *path,
+                               struct server_id server,
+                               const struct notify_instance *instance,
+                               void *private_data),
+                    void *private_data)
+{
+       struct notifyd_parse_db_state state = {
+               .fn = fn, .private_data = private_data
+       };
+       NTSTATUS status;
+
+       if (buflen < 8) {
+               return EINVAL;
+       }
+       *log_index = BVAL(buf, 0);
+
+       buf += 8;
+       buflen -= 8;
+
+       status = dbwrap_parse_marshall_buf(
+               buf, buflen, notifyd_parse_db_parser, &state);
+       if (!NT_STATUS_IS_OK(status)) {
+               return map_errno_from_nt_status(status);
+       }
+
+       return 0;
+}