change "ctdb pfetch" to take an optional third argument
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 24 Aug 2010 22:07:03 +0000 (08:07 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 24 Aug 2010 22:07:47 +0000 (08:07 +1000)
as a file to store the record in.

tools/ctdb.c

index b986f4e7ccb8111515f9c1ed72311074bc33f743..0d8ca35dbdc1a4bac98e1b519e5ded34f21b3cec 100644 (file)
@@ -2866,7 +2866,7 @@ static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv
        TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
        struct ctdb_transaction_handle *h;
        TDB_DATA key, data;
-       int ret;
+       int fd, ret;
 
        if (argc < 2) {
                talloc_free(tmp_ctx);
@@ -2912,7 +2912,18 @@ static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv
                return -1;
        }
 
-       fwrite(data.dptr, data.dsize, 1, stdout);
+       if (argc == 3) {
+               fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC);
+               if (fd == -1) {
+                       DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
+                       talloc_free(tmp_ctx);
+                       return -1;
+               }
+               write(fd, data.dptr, data.dsize);
+               close(fd);
+       } else {
+               write(1, data.dptr, data.dsize);
+       }
 
        /* abort the transaction */
        talloc_free(h);
@@ -4697,7 +4708,7 @@ static const struct {
        { "msglisten",        control_msglisten,        false,  false, "Listen on a srvid port for messages", "<msg srvid>"},
        { "msgsend",          control_msgsend,  false,  false, "Send a message to srvid", "<srvid> <message>"},
        { "sync",            control_ipreallocate,      true,   false,  "wait until ctdbd has synced all state changes" },
-       { "pfetch",          control_pfetch,            true,   false,  "fetch a record from a persistent database", "<db> <key>" },
+       { "pfetch",          control_pfetch,            true,   false,  "fetch a record from a persistent database", "<db> <key> [<file>]" },
        { "pstore",          control_pstore,            true,   false,  "write a record to a persistent database", "<db> <key> <file containing record>" },
 };