diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 82875fec7..5b15b7c22 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -26,6 +26,10 @@ Replace THROW_ON_SYS_ERROR() with THROW_SYS_ERROR(). The former macro was hiding missing branch coverage for critical error handling. + + + Start work on C handle io object and use it to output help. + diff --git a/src/Makefile b/src/Makefile index 62319c9e6..6d15399da 100644 --- a/src/Makefile +++ b/src/Makefile @@ -58,6 +58,7 @@ SRCS = \ command/command.c \ common/error.c \ common/exit.c \ + common/io/handle.c \ common/ini.c \ common/log.c \ common/memContext.c \ diff --git a/src/command/help/help.c b/src/command/help/help.c index 265232174..18f647fc2 100644 --- a/src/command/help/help.c +++ b/src/command/help/help.c @@ -4,6 +4,7 @@ Help Command #include #include +#include "common/io/handle.h" #include "common/memContext.h" #include "common/type.h" #include "config/config.h" @@ -337,10 +338,7 @@ cmdHelp() { MEM_CONTEXT_TEMP_BEGIN() { - String *help = helpRender(); - - if (write(STDOUT_FILENO, strPtr(help), strSize(help)) != (int)strSize(help)) // {uncovered - write does not fail} - THROW_SYS_ERROR(FileWriteError, "unable to write help to stdout"); // {uncovered+} + ioHandleWriteOneStr(STDOUT_FILENO, helpRender()); } MEM_CONTEXT_TEMP_END(); } diff --git a/src/common/io/handle.c b/src/common/io/handle.c new file mode 100644 index 000000000..24feff3cd --- /dev/null +++ b/src/common/io/handle.c @@ -0,0 +1,17 @@ +/*********************************************************************************************************************************** +Handle IO +***********************************************************************************************************************************/ +#include + +#include "common/error.h" +#include "common/io/handle.h" + +/*********************************************************************************************************************************** +Write a string to the specified handle +***********************************************************************************************************************************/ +void +ioHandleWriteOneStr(int handle, const String *string) +{ + if (write(handle, strPtr(string), strSize(string)) != (int)strSize(string)) + THROW_SYS_ERROR(FileWriteError, "unable to write to %u byte(s) to handle", strSize(string)); +} diff --git a/src/common/io/handle.h b/src/common/io/handle.h new file mode 100644 index 000000000..9bd140a5b --- /dev/null +++ b/src/common/io/handle.h @@ -0,0 +1,14 @@ +/*********************************************************************************************************************************** +Handle IO +***********************************************************************************************************************************/ +#ifndef COMMON_IO_HANDLE_H +#define COMMON_IO_HANDLE_H + +#include "common/type.h" + +/*********************************************************************************************************************************** +Functions +***********************************************************************************************************************************/ +void ioHandleWriteOneStr(int handle, const String *string); + +#endif diff --git a/test/lib/pgBackRestTest/Common/DefineTest.pm b/test/lib/pgBackRestTest/Common/DefineTest.pm index bb14e6c8b..2d316bc01 100644 --- a/test/lib/pgBackRestTest/Common/DefineTest.pm +++ b/test/lib/pgBackRestTest/Common/DefineTest.pm @@ -176,6 +176,16 @@ my $oTestDef = 'common/debug' => TESTDEF_COVERAGE_NOCODE, }, }, + { + &TESTDEF_NAME => 'io-handle', + &TESTDEF_TOTAL => 1, + &TESTDEF_C => true, + + &TESTDEF_COVERAGE => + { + 'common/io/handle' => TESTDEF_COVERAGE_FULL, + }, + }, { &TESTDEF_NAME => 'exit', &TESTDEF_TOTAL => 1, @@ -320,7 +330,7 @@ my $oTestDef = }, }, { - &TESTDEF_NAME => 'io-handle', + &TESTDEF_NAME => 'io-handle-perl', &TESTDEF_TOTAL => 6, &TESTDEF_COVERAGE => diff --git a/test/lib/pgBackRestTest/Module/Common/CommonIoHandleTest.pm b/test/lib/pgBackRestTest/Module/Common/CommonIoHandlePerlTest.pm similarity index 98% rename from test/lib/pgBackRestTest/Module/Common/CommonIoHandleTest.pm rename to test/lib/pgBackRestTest/Module/Common/CommonIoHandlePerlTest.pm index e2f4fbf4c..a61d92c41 100644 --- a/test/lib/pgBackRestTest/Module/Common/CommonIoHandleTest.pm +++ b/test/lib/pgBackRestTest/Module/Common/CommonIoHandlePerlTest.pm @@ -1,7 +1,7 @@ #################################################################################################################################### -# CommonIoHandleTest.pm - tests for Common::Io::Handle module +# Tests for Common::Io::Handle module #################################################################################################################################### -package pgBackRestTest::Module::Common::CommonIoHandleTest; +package pgBackRestTest::Module::Common::CommonIoHandlePerlTest; use parent 'pgBackRestTest::Common::RunTest'; #################################################################################################################################### diff --git a/test/src/module/common/ioHandleTest.c b/test/src/module/common/ioHandleTest.c new file mode 100644 index 000000000..e716ee073 --- /dev/null +++ b/test/src/module/common/ioHandleTest.c @@ -0,0 +1,25 @@ +/*********************************************************************************************************************************** +Test Handle IO +***********************************************************************************************************************************/ +#include + +/*********************************************************************************************************************************** +Test Run +***********************************************************************************************************************************/ +void +testRun() +{ + // ***************************************************************************************************************************** + if (testBegin("ioHandleWriteOneStr()")) + { + TEST_ERROR( + ioHandleWriteOneStr(999999, strNew("test")), FileWriteError, + "unable to write to 4 byte(s) to handle: [9] Bad file descriptor"); + + // ------------------------------------------------------------------------------------------------------------------------- + String *fileName = strNewFmt("%s/test.txt", testPath()); + int fileHandle = open(strPtr(fileName), O_CREAT | O_TRUNC | O_WRONLY, 0700); + + TEST_RESULT_VOID(ioHandleWriteOneStr(fileHandle, strNew("test1\ntest2")), "write string to file"); + } +}
Replace THROW_ON_SYS_ERROR() with THROW_SYS_ERROR(). The former macro was hiding missing branch coverage for critical error handling.
THROW_ON_SYS_ERROR()
THROW_SYS_ERROR()
Start work on C handle io object and use it to output help.