ctdb-protocol: Add marshalling for bool
authorAmitay Isaacs <amitay@gmail.com>
Thu, 29 Jun 2017 14:11:45 +0000 (00:11 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:22 +0000 (14:59 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_basic.c
ctdb/protocol/protocol_private.h
ctdb/tests/src/protocol_basic_test.c
ctdb/tests/src/protocol_common.c
ctdb/tests/src/protocol_common.h

index 619a38045321aa49b6142e63e24cded2c9aa47da..4f08f8d335ce5d3322b2a99a57f4d83a12ca3128 100644 (file)
@@ -162,6 +162,45 @@ int ctdb_double_pull(uint8_t *buf, size_t buflen, double *out, size_t *npull)
        return 0;
 }
 
+size_t ctdb_bool_len(bool *in)
+{
+       uint8_t u8 = *in;
+
+       return ctdb_uint8_len(&u8);
+}
+
+void ctdb_bool_push(bool *in, uint8_t *buf, size_t *npush)
+{
+       size_t np;
+       uint8_t u8 = *in;
+
+       ctdb_uint8_push(&u8, buf, &np);
+       *npush = np;
+}
+
+int ctdb_bool_pull(uint8_t *buf, size_t buflen, bool *out, size_t *npull)
+{
+       size_t np;
+       uint8_t u8;
+       int ret;
+
+       ret = ctdb_uint8_pull(buf, buflen, &u8, &np);
+       if (ret != 0) {
+               return ret;
+       }
+
+       if (u8 == 0) {
+               *out = false;
+       } else if (u8 == 1) {
+               *out = true;
+       } else {
+               return EINVAL;
+       }
+
+       *npull = np;
+       return 0;
+}
+
 size_t ctdb_string_len(const char *str)
 {
        if (str == NULL) {
index 68ed7a9fdbb203ad9caa755d40236548a46d4e8c..e7c578d5b14d7c7afccab7d2b0e2df26ec00f1e4 100644 (file)
@@ -53,6 +53,10 @@ size_t ctdb_double_len(double *in);
 void ctdb_double_push(double *in, uint8_t *buf, size_t *npush);
 int ctdb_double_pull(uint8_t *buf, size_t buflen, double *out, size_t *npull);
 
+size_t ctdb_bool_len(bool *in);
+void ctdb_bool_push(bool *in, uint8_t *buf, size_t *npush);
+int ctdb_bool_pull(uint8_t *buf, size_t buflen, bool *out, size_t *npull);
+
 size_t ctdb_string_len(const char *str);
 void ctdb_string_push(const char *str, uint8_t *buf);
 int ctdb_string_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
index a64d2f624816a1ea914b4f1d3e1bb41448cb90f9..01e5fd69dad3b22dbd934dad5aeb7a47d0dfb92e 100644 (file)
@@ -31,6 +31,7 @@ PROTOCOL_TYPE1_TEST(int32_t, ctdb_int32);
 PROTOCOL_TYPE1_TEST(uint32_t, ctdb_uint32);
 PROTOCOL_TYPE1_TEST(uint64_t, ctdb_uint64);
 PROTOCOL_TYPE1_TEST(double, ctdb_double);
+PROTOCOL_TYPE1_TEST(bool, ctdb_bool);
 
 static void test_ctdb_string(void)
 {
@@ -91,6 +92,7 @@ int main(int argc, char *argv[])
        TEST_FUNC(ctdb_uint32)();
        TEST_FUNC(ctdb_uint64)();
        TEST_FUNC(ctdb_double)();
+       TEST_FUNC(ctdb_bool)();
 
        test_ctdb_string();
        test_ctdb_stringn();
index 0e6fca187eb3eb6102c71d324d41e2c4227058aa..724148df2187788f5a0944bde31dc32ca115c9f6 100644 (file)
@@ -148,6 +148,20 @@ void verify_ctdb_double(double *p1, double *p2)
        assert(*p1 == *p2);
 }
 
+void fill_ctdb_bool(bool *p)
+{
+       if (rand_int(2) == 0) {
+               *p = true;
+       } else {
+               *p = false;
+       }
+}
+
+void verify_ctdb_bool(bool *p1, bool *p2)
+{
+       assert(*p1 == *p2);
+}
+
 void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **out)
 {
        char *p;
index ec6f48490e0e9a24f7c4a574da45c8a1b2c3f812..b0f3507fb5c147cd720addec696f990792b6079d 100644 (file)
@@ -170,6 +170,9 @@ void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2);
 void fill_ctdb_double(double *p);
 void verify_ctdb_double(double *p1, double *p2);
 
+void fill_ctdb_bool(bool *p);
+void verify_ctdb_bool(bool *p1, bool *p2);
+
 void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **out);
 void verify_ctdb_string(const char *p1, const char *p2);