diff mbox series

[ovs-dev,v3] ovsdb-client: Set binary mode when doing backup/restore

Message ID 20180312131742.13652-1-aserdean@ovn.org
State Accepted
Headers show
Series [ovs-dev,v3] ovsdb-client: Set binary mode when doing backup/restore | expand

Commit Message

Alin-Gabriel Serdean March 12, 2018, 1:17 p.m. UTC
Add some needed consistency on Windows for STD_IN/OUT file descriptors
when doing backup and restore.

Reported-at:https://mail.openvswitch.org/pipermail/ovs-dev/2018-January/343518.html
Suggested-by: Ben Pfaff <blp@ovn.org>
Co-authored-by: Ben Pfaff <blp@ovn.org>
Signed-off-by: Ben Pfaff <blp@ovn.org>
Signed-off-by: Alin Gabriel Serdean <aserdean@ovn.org>
---
 v3: Incorporate comments suggested by Ben.
 v2: Fix copy paste error.
---
 ovsdb/ovsdb-client.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

Comments

Ben Pfaff March 14, 2018, 6:11 p.m. UTC | #1
On Mon, Mar 12, 2018 at 03:17:42PM +0200, Alin Gabriel Serdean wrote:
> Add some needed consistency on Windows for STD_IN/OUT file descriptors
> when doing backup and restore.
> 
> Reported-at:https://mail.openvswitch.org/pipermail/ovs-dev/2018-January/343518.html
> Suggested-by: Ben Pfaff <blp@ovn.org>
> Co-authored-by: Ben Pfaff <blp@ovn.org>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> Signed-off-by: Alin Gabriel Serdean <aserdean@ovn.org>
> ---
>  v3: Incorporate comments suggested by Ben.
>  v2: Fix copy paste error.

I'd move the { to a line of its own here:

> +static void
> +set_binary_mode(FILE *stream OVS_UNUSED) {

Acked-by: Ben Pfaff <blp@ovn.org>
Alin-Gabriel Serdean March 15, 2018, 12:14 a.m. UTC | #2
> -----Mesaj original-----
> De la: ovs-dev-bounces@openvswitch.org <ovs-dev-
> bounces@openvswitch.org> În numele Ben Pfaff
> Trimis: Wednesday, March 14, 2018 8:11 PM
> Către: Alin Gabriel Serdean <aserdean@ovn.org>
> Cc: dev@openvswitch.org
> Subiect: Re: [ovs-dev] [PATCH v3] ovsdb-client: Set binary mode when doing
> backup/restore
> 
> On Mon, Mar 12, 2018 at 03:17:42PM +0200, Alin Gabriel Serdean wrote:
> > Add some needed consistency on Windows for STD_IN/OUT file
> descriptors
> > when doing backup and restore.
> >
> > Reported-at:https://mail.openvswitch.org/pipermail/ovs-dev/2018-Januar
> > y/343518.html
> > Suggested-by: Ben Pfaff <blp@ovn.org>
> > Co-authored-by: Ben Pfaff <blp@ovn.org>
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > Signed-off-by: Alin Gabriel Serdean <aserdean@ovn.org>
> > ---
> >  v3: Incorporate comments suggested by Ben.
> >  v2: Fix copy paste error.
> 
> I'd move the { to a line of its own here:
> 
> > +static void
> > +set_binary_mode(FILE *stream OVS_UNUSED) {
> 
> Acked-by: Ben Pfaff <blp@ovn.org>
Ooops. I folded in the change and applied on master. Ty!
diff mbox series

Patch

diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index 222bd6ca8..2aaec0648 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -18,6 +18,7 @@ 
 
 #include <ctype.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
 #include <signal.h>
@@ -1475,6 +1476,19 @@  print_and_free_log_record(struct json *record)
     json_destroy(record);
 }
 
+static void
+set_binary_mode(FILE *stream OVS_UNUSED) {
+#ifdef _WIN32
+    fflush(stream);
+    /* On Windows set binary mode on the file descriptor to avoid
+     * translation (i.e. CRLF line endings). */
+    if (_setmode(_fileno(stream), O_BINARY) == -1) {
+        ovs_fatal(errno, "could not set binary mode on fd %d",
+                  _fileno(stream));
+    }
+#endif
+}
+
 static void
 do_backup(struct jsonrpc *rpc, const char *database,
           int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
@@ -1483,6 +1497,7 @@  do_backup(struct jsonrpc *rpc, const char *database,
         ovs_fatal(0, "not writing backup to a terminal; "
                   "please redirect stdout to a file");
     }
+    set_binary_mode(stdout);
 
     /* Get schema. */
     struct ovsdb_schema *schema = fetch_schema(rpc, database);
@@ -1599,6 +1614,7 @@  do_restore(struct jsonrpc *rpc, const char *database,
         ovs_fatal(0, "not reading backup from a terminal; "
                   "please redirect stdin from a file");
     }
+    set_binary_mode(stdin);
 
     struct ovsdb *backup;
     check_ovsdb_error(ovsdb_file_open("/dev/stdin", true, &backup, NULL));