From: Amitay Isaacs Date: Tue, 8 Mar 2016 06:20:30 +0000 (+1100) Subject: ctdb-protocol: Add file IO functions for ctdb_rec_buffer X-Git-Tag: tdb-1.3.9~109 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=76f653f0bc016dfa98a4d8688469be4ac450a17a;p=thirdparty%2Fsamba.git ctdb-protocol: Add file IO functions for ctdb_rec_buffer Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- diff --git a/ctdb/protocol/protocol_api.h b/ctdb/protocol/protocol_api.h index d07b4198d0a..40753a9de97 100644 --- a/ctdb/protocol/protocol_api.h +++ b/ctdb/protocol/protocol_api.h @@ -52,6 +52,10 @@ int ctdb_rec_buffer_traverse(struct ctdb_rec_buffer *recbuf, ctdb_rec_parser_func_t func, void *private_data); +int ctdb_rec_buffer_write(struct ctdb_rec_buffer *recbuf, int fd); +int ctdb_rec_buffer_read(int fd, TALLOC_CTX *mem_ctx, + struct ctdb_rec_buffer **out); + size_t ctdb_server_id_len(struct ctdb_server_id *sid); void ctdb_server_id_push(struct ctdb_server_id *sid, uint8_t *buf); int ctdb_server_id_pull(uint8_t *buf, size_t buflen, diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index a0263336319..cd04aca0e64 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -800,6 +800,68 @@ int ctdb_rec_buffer_traverse(struct ctdb_rec_buffer *recbuf, return ret; } +int ctdb_rec_buffer_write(struct ctdb_rec_buffer *recbuf, int fd) +{ + ssize_t n; + + n = write(fd, &recbuf->db_id, sizeof(uint32_t)); + if (n == -1 || n != sizeof(uint32_t)) { + return (errno != 0 ? errno : EIO); + } + n = write(fd, &recbuf->count, sizeof(uint32_t)); + if (n == -1 || n != sizeof(uint32_t)) { + return (errno != 0 ? errno : EIO); + } + n = write(fd, &recbuf->buflen, sizeof(size_t)); + if (n == -1 || n != sizeof(size_t)) { + return (errno != 0 ? errno : EIO); + } + n = write(fd, recbuf->buf, recbuf->buflen); + if (n == -1 || n != recbuf->buflen) { + return (errno != 0 ? errno : EIO); + } + + return 0; +} + +int ctdb_rec_buffer_read(int fd, TALLOC_CTX *mem_ctx, + struct ctdb_rec_buffer **out) +{ + struct ctdb_rec_buffer *recbuf; + ssize_t n; + + recbuf = talloc(mem_ctx, struct ctdb_rec_buffer); + if (recbuf == NULL) { + return ENOMEM; + } + + n = read(fd, &recbuf->db_id, sizeof(uint32_t)); + if (n == -1 || n != sizeof(uint32_t)) { + return (errno != 0 ? errno : EIO); + } + n = read(fd, &recbuf->count, sizeof(uint32_t)); + if (n == -1 || n != sizeof(uint32_t)) { + return (errno != 0 ? errno : EIO); + } + n = read(fd, &recbuf->buflen, sizeof(size_t)); + if (n == -1 || n != sizeof(size_t)) { + return (errno != 0 ? errno : EIO); + } + + recbuf->buf = talloc_size(recbuf, recbuf->buflen); + if (recbuf->buf == NULL) { + return ENOMEM; + } + + n = read(fd, recbuf->buf, recbuf->buflen); + if (n == -1 || n != recbuf->buflen) { + return (errno != 0 ? errno : EIO); + } + + *out = recbuf; + return 0; +} + size_t ctdb_traverse_start_len(struct ctdb_traverse_start *traverse) { return sizeof(struct ctdb_traverse_start); diff --git a/ctdb/tests/src/protocol_types_test.c b/ctdb/tests/src/protocol_types_test.c index 55ee743def5..e39a2c9a4cf 100644 --- a/ctdb/tests/src/protocol_types_test.c +++ b/ctdb/tests/src/protocol_types_test.c @@ -19,6 +19,7 @@ #include "replace.h" #include "system/network.h" +#include "system/filesys.h" #include @@ -1235,6 +1236,52 @@ DEFINE_TEST(struct ctdb_srvid_message, ctdb_srvid_message); DEFINE_TEST(struct ctdb_disable_message, ctdb_disable_message); DEFINE_TEST(struct ctdb_g_lock_list, ctdb_g_lock_list); +static void test_ctdb_rec_buffer_read_write(void) +{ + TALLOC_CTX *mem_ctx = talloc_new(NULL); + struct ctdb_rec_buffer *p1, **p2; + const char *filename = "ctdb_rec_buffer_test.dat"; + int count = 100; + int fd, i, ret; + off_t offset; + + p1 = talloc_array(mem_ctx, struct ctdb_rec_buffer, count); + assert(p1 != NULL); + for (i=0; i