create an async version of the control to get the path top a ctdb database file
[sahlberg/ctdb.git] / client / ctdb_client.c
index a3349e0b4ad2b7ad33e9c066e687df2ed518acae..0840cfd295ffb51ac96f1668353c22e279f79f95 100644 (file)
@@ -791,25 +791,38 @@ int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint3
                   const char **path)
 {
        int ret;
-       int32_t res;
-       TDB_DATA data;
+       const char *tmppath = NULL;
+       ctdb_handle *handle;
 
-       data.dptr = (uint8_t *)&dbid;
-       data.dsize = sizeof(dbid);
+       handle = ctdb_getdbpath_send(ctdb, destnode, dbid, NULL, NULL);
+       if (handle == NULL) {
+               DEBUG(DEBUG_ERR, (__location__  " Failed to send getdbpath control\n"));
+               return -1;
+       }
 
-       ret = ctdb_control(ctdb, destnode, 0, 
-                          CTDB_CONTROL_GETDBPATH, 0, data, 
-                          mem_ctx, &data, &res, &timeout, NULL);
-       if (ret != 0 || res != 0) {
+       if (!timeval_is_zero(&timeout)) {
+               event_add_timed(ctdb->ev, handle, timeout, ctdb_control_timeout_func, handle);
+       }
+
+       ret = ctdb_getdbpath_recv(ctdb, handle, &tmppath);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb control for getdbpath failed\n"));
+               if (tmppath != NULL) {
+                       free(discard_const(tmppath));
+               }
                return -1;
        }
 
-       (*path) = talloc_strndup(mem_ctx, (const char *)data.dptr, data.dsize);
-       if ((*path) == NULL) {
+       if (tmppath == NULL) {
                return -1;
        }
 
-       talloc_free(data.dptr);
+       *path = talloc_strdup(mem_ctx, (const char *)tmppath);
+       free(discard_const(tmppath));
+
+       if (*path == NULL) {
+               return -1;
+       }
 
        return 0;
 }
@@ -885,22 +898,26 @@ int ctdb_ctrl_getdbhealth(struct ctdb_context *ctdb,
 /*
   create a database
  */
-int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, 
-                      TALLOC_CTX *mem_ctx, const char *name, bool persistent)
+int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout,
+                       uint32_t destnode, 
+                       const char *name, bool persistent)
 {
        int ret;
-       int32_t res;
-       TDB_DATA data;
+       ctdb_handle *handle;
 
-       data.dptr = discard_const(name);
-       data.dsize = strlen(name)+1;
+       handle = ctdb_createdb_send(ctdb, destnode, name, persistent, 0, NULL, NULL);
+       if (handle == NULL) {
+               DEBUG(DEBUG_ERR, (__location__  " Failed to send createdb control\n"));
+               return -1;
+       }
 
-       ret = ctdb_control(ctdb, destnode, 0, 
-                          persistent?CTDB_CONTROL_DB_ATTACH_PERSISTENT:CTDB_CONTROL_DB_ATTACH, 
-                          0, data, 
-                          mem_ctx, &data, &res, &timeout, NULL);
+       if (!timeval_is_zero(&timeout)) {
+               event_add_timed(ctdb->ev, handle, timeout, ctdb_control_timeout_func, handle);
+       }
 
-       if (ret != 0 || res != 0) {
+       ret = ctdb_createdb_recv(ctdb, handle, NULL);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb control for createdb failed\n"));
                return -1;
        }
 
@@ -1028,9 +1045,9 @@ static int ctdb_fetch_func(struct ctdb_call_info *call)
 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name, bool persistent, uint32_t tdb_flags)
 {
        struct ctdb_db_context *ctdb_db;
-       TDB_DATA data;
        int ret;
-       int32_t res;
+       uint32_t db_id;
+       ctdb_handle *handle;
 
        ctdb_db = ctdb_db_handle(ctdb, name);
        if (ctdb_db) {
@@ -1044,21 +1061,24 @@ struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name,
        ctdb_db->db_name = talloc_strdup(ctdb_db, name);
        CTDB_NO_MEMORY_NULL(ctdb, ctdb_db->db_name);
 
-       data.dptr = discard_const(name);
-       data.dsize = strlen(name)+1;
-
        /* tell ctdb daemon to attach */
-       ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, tdb_flags, 
-                          persistent?CTDB_CONTROL_DB_ATTACH_PERSISTENT:CTDB_CONTROL_DB_ATTACH,
-                          0, data, ctdb_db, &data, &res, NULL, NULL);
-       if (ret != 0 || res != 0 || data.dsize != sizeof(uint32_t)) {
+       handle = ctdb_createdb_send(ctdb, CTDB_CURRENT_NODE,
+                                   name, persistent, tdb_flags,
+                                   NULL, NULL);
+       if (handle == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " Failed to send CREATEDB control\n"));
+               talloc_free(ctdb_db);
+               return NULL;
+       }
+
+       ret = ctdb_createdb_recv(ctdb, handle, &db_id);
+       if (ret != 0) {
                DEBUG(DEBUG_ERR,("Failed to attach to database '%s'\n", name));
                talloc_free(ctdb_db);
                return NULL;
        }
        
-       ctdb_db->db_id = *(uint32_t *)data.dptr;
-       talloc_free(data.dptr);
+       ctdb_db->db_id = db_id;
 
        ret = ctdb_ctrl_getdbpath(ctdb, timeval_current_ofs(2, 0), CTDB_CURRENT_NODE, ctdb_db->db_id, ctdb_db, &ctdb_db->db_path);
        if (ret != 0) {
@@ -1213,7 +1233,7 @@ int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *
                           data, NULL, NULL, &status, NULL, NULL);
        if (ret != 0 || status != 0) {
                DEBUG(DEBUG_ERR,("ctdb_traverse_all failed\n"));
-               ctdb_remove_message_handler(ctdb_db->ctdb, srvid, &state);
+               ctdb_remove_message_handler(ctdb_db->ctdb, srvid);
                return -1;
        }
 
@@ -1221,7 +1241,7 @@ int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *
                event_loop_once(ctdb_db->ctdb->ev);
        }
 
-       ret = ctdb_remove_message_handler(ctdb_db->ctdb, srvid, &state);
+       ret = ctdb_remove_message_handler(ctdb_db->ctdb, srvid);
        if (ret != 0) {
                DEBUG(DEBUG_ERR,("Failed to remove ctdb_traverse handler\n"));
                return -1;
@@ -1408,7 +1428,7 @@ int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t
 
        ret = ctdb_getpnn_recv(ctdb, handle, &pnn);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getpnn failed\n"));
+               DEBUG(DEBUG_ERR,(__location__ " ctdb control for getpnn failed\n"));
                return -1;
        }