From 2606996d15a33a06c16f441ef7eccf8fea040be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Czerpak?= Date: Mon, 31 Aug 2015 12:45:47 +0200 Subject: [PATCH] 2015-08-31 12:45 UTC+0200 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * contrib/hbtcpio/tcpio.c * contrib/hbpipeio/pipeio.c * modified hb_fileWrite() to return 0 in case of timeout or unblocking write and -1 on other errors. * contrib/hbcomio/comio.c * modified hb_fileRead() to return 0 in case of timeout or unblocking read and -1 on other errors. * src/rtl/filesys.c * return -1 instead of 0 from hb_fsPipeWrite() in MS-Windows and OS2 builds if PIPE state cannot be read * ChangeLog.txt ! c&p typo in previous ChangeLog entry --- ChangeLog.txt | 19 ++++++++++++++++++- contrib/hbcomio/comio.c | 16 ++++++++++++---- contrib/hbpipeio/pipeio.c | 2 +- contrib/hbtcpio/tcpio.c | 23 +++++++++++++++++------ src/rtl/filesys.c | 8 ++++++-- 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index e4a70471c3..6cd5b45518 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -10,6 +10,23 @@ * Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment */ +2015-08-31 12:45 UTC+0200 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) + * contrib/hbtcpio/tcpio.c + * contrib/hbpipeio/pipeio.c + * modified hb_fileWrite() to return 0 in case of timeout or unblocking + write and -1 on other errors. + + * contrib/hbcomio/comio.c + * modified hb_fileRead() to return 0 in case of timeout or unblocking + read and -1 on other errors. + + * src/rtl/filesys.c + * return -1 instead of 0 from hb_fsPipeWrite() in MS-Windows and OS2 + builds if PIPE state cannot be read + + * ChangeLog.txt + ! c&p typo in previous ChangeLog entry + 2015-08-27 17:49 UTC+0200 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) + contrib/hbpipeio/hbpipeio.hbc + contrib/hbpipeio/hbpipeio.hbp @@ -37,7 +54,7 @@ hProcess := hb_processOpen( cCommand, @hStdIn, @hStdOut ) pFile := hb_vfFromPipes( hStdOut, hStdIn, hProcess, 5000 ) The second one can be used directly: - pFile := hb_vfFromPipes( cCommand, FO_READWRITE, 5000 ) + pFile := hb_vfOpenProcess( cCommand, FO_READWRITE, 5000 ) Usually process which reads from its stdin works until its input stream is closed by other process. If user wants to close input stream for command redirected to Harbour PIPE FILE IO then he can diff --git a/contrib/hbcomio/comio.c b/contrib/hbcomio/comio.c index 0a521434a0..dd7ba2e8c0 100644 --- a/contrib/hbcomio/comio.c +++ b/contrib/hbcomio/comio.c @@ -315,7 +315,7 @@ static HB_SIZE s_fileRead( PHB_FILE pFile, void * data, HB_SIZE nSize, HB_MAXINT timeout ) { HB_ERRCODE errcode; - long lRead = 0; + long lRead = -1; if( pFile->fRead ) { @@ -324,20 +324,24 @@ static HB_SIZE s_fileRead( PHB_FILE pFile, void * data, timeout = pFile->timeout; lRead = hb_comRecv( pFile->port, data, lRead, timeout ); errcode = hb_comGetError( pFile->port ); + if( lRead <= 0 && errcode == HB_COM_ERR_TIMEOUT ) + lRead = 0; + else if( lRead == 0 ) + lRead = -1; } else errcode = HB_COM_ERR_ACCESS; hb_fsSetError( errcode ); - return HB_MAX( lRead, 0 ); + return lRead; } static HB_SIZE s_fileWrite( PHB_FILE pFile, const void * data, HB_SIZE nSize, HB_MAXINT timeout ) { HB_ERRCODE errcode; - long lSent = 0; + long lSent = -1; if( pFile->fWrite ) { @@ -346,13 +350,17 @@ static HB_SIZE s_fileWrite( PHB_FILE pFile, const void * data, timeout = pFile->timeout; lSent = hb_comSend( pFile->port, data, lSent, timeout ); errcode = hb_comGetError( pFile->port ); + if( lSent <= 0 && errcode == HB_COM_ERR_TIMEOUT ) + lSent = 0; + else if( lSent == 0 ) + lSent = -1; } else errcode = HB_COM_ERR_ACCESS; hb_fsSetError( errcode ); - return HB_MAX( 0, lSent ); + return lSent; } static HB_BOOL s_fileConfigure( PHB_FILE pFile, int iIndex, PHB_ITEM pValue ) diff --git a/contrib/hbpipeio/pipeio.c b/contrib/hbpipeio/pipeio.c index 697939f8e8..5eba5db867 100644 --- a/contrib/hbpipeio/pipeio.c +++ b/contrib/hbpipeio/pipeio.c @@ -163,7 +163,7 @@ static HB_SIZE s_fileRead( PHB_FILE pFile, void * data, static HB_SIZE s_fileWrite( PHB_FILE pFile, const void * data, HB_SIZE nSize, HB_MAXINT timeout ) { - HB_SIZE nWritten = 0; + HB_SIZE nWritten = ( HB_SIZE ) -1; if( pFile->hPipeWR == FS_ERROR ) hb_fsSetError( 6 ); diff --git a/contrib/hbtcpio/tcpio.c b/contrib/hbtcpio/tcpio.c index 87fc7c53a0..4e469c6b35 100644 --- a/contrib/hbtcpio/tcpio.c +++ b/contrib/hbtcpio/tcpio.c @@ -230,17 +230,28 @@ static HB_SIZE s_fileRead( PHB_FILE pFile, void * data, static HB_SIZE s_fileWrite( PHB_FILE pFile, const void * data, HB_SIZE nSize, HB_MAXINT timeout ) { - long lSend = nSize > LONG_MAX ? LONG_MAX : ( long ) nSize; + long lSent = nSize > LONG_MAX ? LONG_MAX : ( long ) nSize; + HB_ERRCODE errcode; if( timeout == -1 ) timeout = pFile->timeout; - lSend = hb_sockexWrite( pFile->sock, data, lSend, timeout ); - hb_fsSetError( hb_socketGetError() ); + lSent = hb_sockexWrite( pFile->sock, data, lSent, timeout ); + errcode = hb_socketGetError(); + hb_fsSetError( errcode ); - if( lSend < 0 ) - lSend = 0; + if( lSent < 0 ) + { + switch( errcode ) + { + case HB_SOCKET_ERR_TIMEOUT: + case HB_SOCKET_ERR_AGAIN: + case HB_SOCKET_ERR_TRYAGAIN: + lSent = 0; + break; + } + } - return lSend; + return lSent; } static HB_BOOL s_fileEof( PHB_FILE pFile ) diff --git a/src/rtl/filesys.c b/src/rtl/filesys.c index bad2787c70..2d14eb42c4 100644 --- a/src/rtl/filesys.c +++ b/src/rtl/filesys.c @@ -1079,7 +1079,6 @@ HB_SIZE hb_fsPipeWrite( HB_FHANDLE hPipeHandle, const void * buffer, HB_SIZE nSi HANDLE hPipe = ( HANDLE ) hb_fsGetOsHandle( hPipeHandle ); DWORD dwMode = 0; - nWritten = 0; if( GetNamedPipeHandleState( hPipe, &dwMode, NULL, NULL, NULL, NULL, 0 ) ) { HB_MAXUINT end_timer = nTimeOut > 0 ? hb_dateMilliSeconds() + nTimeOut : 0; @@ -1111,13 +1110,15 @@ HB_SIZE hb_fsPipeWrite( HB_FHANDLE hPipeHandle, const void * buffer, HB_SIZE nSi SetNamedPipeHandleState( hPipe, &dwMode, NULL, NULL ); } else + { hb_fsSetIOError( HB_FALSE, 0 ); + nWritten = ( HB_SIZE ) -1; + } } #elif defined( HB_OS_OS2 ) { ULONG state = 0; - nWritten = 0; if( DosQueryNPHState( ( HPIPE ) hPipeHandle, &state ) == NO_ERROR ) { HB_MAXUINT end_timer = nTimeOut > 0 ? hb_dateMilliSeconds() + nTimeOut : 0; @@ -1147,7 +1148,10 @@ HB_SIZE hb_fsPipeWrite( HB_FHANDLE hPipeHandle, const void * buffer, HB_SIZE nSi DosSetNPHState( ( HPIPE ) hPipeHandle, state ); } else + { hb_fsSetIOError( HB_FALSE, 0 ); + nWritten = ( HB_SIZE ) -1; + } } #elif defined( HB_OS_UNIX ) && ! defined( HB_OS_SYMBIAN ) {