tdb2: change API to return the error value.
[ccan] / ccan / tdb2 / test / external-agent.c
index bcf5d215a7a210baf3d8beafa23b0c173c22e556..b5dc56f477e169d19008912f61d242dacdc86bf0 100644 (file)
@@ -1,5 +1,6 @@
 #include "external-agent.h"
 #include "logging.h"
+#include "lock-tracking.h"
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
 static struct tdb_context *tdb;
 
-#if 1 /* FIXME */
-static unsigned int locking_would_block = 0;
-static bool nonblocking_locks = false;
-#endif
-
 static enum agent_return do_operation(enum operation op, const char *name)
 {
        TDB_DATA k;
        enum agent_return ret;
        TDB_DATA data;
+       enum TDB_ERROR ecode;
 
        if (op != OPEN && !tdb) {
                diag("external: No tdb open!");
                return OTHER_FAILURE;
        }
 
+       diag("external: %s", operation_name(op));
+
        k.dptr = (void *)name;
        k.dsize = strlen(name);
 
@@ -46,29 +45,29 @@ static enum agent_return do_operation(enum operation op, const char *name)
                if (!tdb) {
                        if (!locking_would_block)
                                diag("Opening tdb gave %s", strerror(errno));
+                       forget_locking();
                        ret = OTHER_FAILURE;
                } else
                        ret = SUCCESS;
                break;
        case FETCH:
-               data = tdb_fetch(tdb, k);
-               if (data.dptr == NULL) {
-                       if (tdb_error(tdb) == TDB_ERR_NOEXIST)
-                               ret = FAILED;
-                       else
-                               ret = OTHER_FAILURE;
+               ecode = tdb_fetch(tdb, k, &data);
+               if (ecode == TDB_ERR_NOEXIST) {
+                       ret = FAILED;
+               } else if (ecode < 0) {
+                       ret = OTHER_FAILURE;
                } else if (data.dsize != k.dsize
                           || memcmp(data.dptr, k.dptr, k.dsize) != 0) {
                        ret = OTHER_FAILURE;
+                       free(data.dptr);
                } else {
                        ret = SUCCESS;
+                       free(data.dptr);
                }
-               free(data.dptr);
                break;
        case STORE:
                ret = tdb_store(tdb, k, k, 0) == 0 ? SUCCESS : OTHER_FAILURE;
                break;
-#if 0 /* FIXME */
        case TRANSACTION_START:
                ret = tdb_transaction_start(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
                break;
@@ -78,7 +77,6 @@ static enum agent_return do_operation(enum operation op, const char *name)
        case NEEDS_RECOVERY:
                ret = tdb_needs_recovery(tdb) ? SUCCESS : FAILED;
                break;
-#endif
        case CHECK:
                ret = tdb_check(tdb, NULL, NULL) == 0 ? SUCCESS : OTHER_FAILURE;
                break;
@@ -183,12 +181,17 @@ const char *operation_name(enum operation op)
        case FETCH: return "FETCH";
        case STORE: return "STORE";
        case CHECK: return "CHECK";
-#if 0
        case TRANSACTION_START: return "TRANSACTION_START";
        case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
        case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
-#endif
        case CLOSE: return "CLOSE";
        }
        return "**INVALID**";
 }
+
+void free_external_agent(struct agent *agent)
+{
+       close(agent->cmdfd);
+       close(agent->responsefd);
+       free(agent);
+}