Skip to content

Commit

Permalink
ovsdb-client: Set binary mode when doing backup/restore
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Ben Pfaff <[email protected]>
Signed-off-by: Ben Pfaff <[email protected]>
Signed-off-by: Alin Gabriel Serdean <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
aserdean and blp committed Mar 15, 2018
1 parent efde188 commit cb8cbbb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ovsdb/ovsdb-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <signal.h>
Expand Down Expand Up @@ -1475,6 +1476,20 @@ 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)
Expand All @@ -1483,6 +1498,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);
Expand Down Expand Up @@ -1599,6 +1615,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));
Expand Down

0 comments on commit cb8cbbb

Please sign in to comment.