Add some needed consitentcy 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]> Signed-off-by: Alin Gabriel Serdean <[email protected]> --- ovsdb/ovsdb-client.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c index 222bd6ca8..3a0cb3d63 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,11 +1476,32 @@ print_and_free_log_record(struct json *record) json_destroy(record); } +/* Wrapper to verify if the file descriptor `fd` refers to a terminal. + * On Windows also set binary mode on the file descriptor to avoid + * translation (i.e. CRLF line endings). */ +static bool +check_and_set_stdout(int fd) +{ + if (isatty(fd)) { + return false; + } +#ifdef _WIN32 + if (fd == STDOUT_FILENO) { + fflush(stdout); + } + int result = _setmode(fd, O_BINARY); + if (result == -1) { + ovs_fatal(0, "could not _setmode O_BINARY on fd"); + } +#endif + return true; +} + static void do_backup(struct jsonrpc *rpc, const char *database, int argc OVS_UNUSED, char *argv[] OVS_UNUSED) { - if (isatty(STDOUT_FILENO)) { + if (!check_and_set_stdout(STDOUT_FILENO)) { ovs_fatal(0, "not writing backup to a terminal; " "please redirect stdout to a file"); } @@ -1595,7 +1617,7 @@ static void do_restore(struct jsonrpc *rpc, const char *database, int argc OVS_UNUSED, char *argv[] OVS_UNUSED) { - if (isatty(STDIN_FILENO)) { + if (!check_and_set_stdout(STDIN_FILENO)) { ovs_fatal(0, "not reading backup from a terminal; " "please redirect stdin from a file"); } -- 2.16.1.windows.1 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
