ctdb:tools: Use correct C99 initializer for ltdb_header
[samba.git] / ctdb / tools / ltdbtool.c
index b7ad5a56a6eb6b7d8b425555ad3d0212c89df2ca..edb17ce55573ba1efae2f25f321e8a4f20c2fc9e 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdio.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <ctype.h> /* isprint */
-#include <string.h> /* strstr */
-#include <fcntl.h> /* mode_t */
-#include <sys/stat.h> /* S_IRUSR */
-#include <stdint.h> /* uint32_t */
-#include <netinet/in.h> /* struct sockaddr_in */
-#include <sys/socket.h> /* struct sockaddr */
-#include <sys/param.h>  /* MIN */
+#include "replace.h"
+#include "system/filesys.h"
+#include "system/network.h"
+#include "system/locale.h"
+
 #include <tdb.h>
-#include <unistd.h> /* getopt */
-#include <errno.h>
 
-#include "ctdb_protocol.h"
+#include "protocol/protocol.h"
 
 enum {
        MAX_HEADER_SIZE=24,
@@ -48,7 +39,9 @@ union  ltdb_header {
 };
 
 static const union ltdb_header DEFAULT_HDR = {
-       .hdr.dmaster = -1,
+       .hdr = {
+               .dmaster = -1,
+       }
 };
 
 static int help(const char* cmd)
@@ -77,7 +70,9 @@ static int help(const char* cmd)
 "   -O <num>        the number of bytes to interpret as ctdb record header\n"
 "                   for the output database (beware!)\n"
 "\n"
-"   -p              print header (for the dump command), defaults ot off\n"
+"   -e              Include empty records, defaults to off\n"
+"\n"
+"   -p              print header (for the dump command), defaults to off\n"
 "\n"
 "   -h              print this help\n"
 "\n"
@@ -91,8 +86,8 @@ static int help(const char* cmd)
 static int usage(const char* cmd)
 {
        fprintf(stderr,
-               "Usage: %s dump [-p] [-s{0|32|64}] <idb>\n"
-               "       %s convert [-s{0|32|64}] [-o{0|32|64}] <idb> <odb>\n"
+               "Usage: %s dump [-e] [-p] [-s{0|32|64}] <idb>\n"
+               "       %s convert [-e] [-s{0|32|64}] [-o{0|32|64}] <idb> <odb>\n"
                "       %s {help|-h}\n"
                , cmd, cmd, cmd);
        return -1;
@@ -210,7 +205,7 @@ int main(int argc, char* argv[])
        int opt;
        const char *cmd, *idb, *odb;
 
-       while ((opt = getopt(argc, argv, "s:o:S:O:ph:e")) != -1) {
+       while ((opt = getopt(argc, argv, "s:o:S:O:phe")) != -1) {
                switch (opt) {
                case 's':
                case 'S':
@@ -229,6 +224,7 @@ int main(int argc, char* argv[])
                        break;
                case 'e':
                        keep_empty = true;
+                       break;
                case 'h':
                        return help(argv[0]);
                default:
@@ -308,9 +304,10 @@ ltdb_traverse_fn(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
        return ctx->fn(tdb, key, val, &hdr.hdr, ctx->state);
 }
 
-int ltdb_traverse(TDB_CONTEXT *tdb,
-                 int (*fn)(TDB_CONTEXT *,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *),
-                 void *state, int hsize, bool skip_empty)
+static int ltdb_traverse(TDB_CONTEXT *tdb,
+                        int (*fn)(TDB_CONTEXT*, TDB_DATA, TDB_DATA,
+                                  struct ctdb_ltdb_header*, void *),
+                        void *state, int hsize, bool skip_empty)
 {
        struct ltdb_traverse_ctx ctx = {
                .fn = fn,
@@ -324,38 +321,39 @@ int ltdb_traverse(TDB_CONTEXT *tdb,
        return (ret < 0) ? ret : (ret - ctx.nempty);
 }
 
-int write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
-                struct ctdb_ltdb_header* hdr,
-                void* write_record_ctx)
+static int write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
+                       struct ctdb_ltdb_header* hdr,
+                       void* write_record_ctx)
 {
        struct write_record_ctx* ctx
                = (struct write_record_ctx*)write_record_ctx;
+       int ret;
 
        if (ctx->hsize == 0) {
-               if (tdb_store(ctx->tdb, key, val, ctx->tdb_store_flags) == -1) {
-                       fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
-                       return -1;
-               }
+               ret = tdb_store(ctx->tdb, key, val, ctx->tdb_store_flags);
        } else {
-               TDB_DATA h = {
-                       .dptr = (void*)hdr,
-                       .dsize = ctx->hsize,
-               };
-               if(tdb_store(ctx->tdb, key, h, ctx->tdb_store_flags) == -1) {
-                       fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
-                       return -1;
-               }
-               if(tdb_append(ctx->tdb, key, val) == -1) {
-                       fprintf(stderr, "tdb_append: %s\n", tdb_errorstr(ctx->tdb));
-                       return -1;
-               }
+               TDB_DATA rec[2];
+
+               rec[0].dsize = ctx->hsize;
+               rec[0].dptr = (uint8_t *)hdr;
+
+               rec[1].dsize = val.dsize;
+               rec[1].dptr = val.dptr;
+
+               ret = tdb_storev(ctx->tdb, key, rec, 2, ctx->tdb_store_flags);
+       }
+
+       if (ret == -1) {
+               fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
+               return -1;
        }
+
        return 0;
 }
 
-int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
-               struct ctdb_ltdb_header* hdr,
-               void* dump_record_ctx)
+static int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
+                      struct ctdb_ltdb_header* hdr,
+                      void* dump_record_ctx)
 {
        struct dump_record_ctx* ctx = (struct dump_record_ctx*)dump_record_ctx;
 
@@ -369,14 +367,16 @@ int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
        return 0;
 }
 
-void dump_header_full(struct dump_record_ctx* c, struct ctdb_ltdb_header* h)
+static void dump_header_full(struct dump_record_ctx* c,
+                            struct ctdb_ltdb_header* h)
 {
        fprintf(c->file, "dmaster: %d\nrsn: %llu\nflags: 0x%X\n",
                (int)h->dmaster,
                (unsigned long long)h->rsn, h->flags);
 }
 
-void print_data_tdbdump(FILE* file, TDB_DATA data) {
+static void print_data_tdbdump(FILE* file, TDB_DATA data)
+{
        unsigned char *ptr = data.dptr;
        fputc('"', file);
        while (data.dsize--) {