Changeset 13594

Show
Ignore:
Timestamp:
01/25/08 08:53:00 (5 months ago)
Author:
dron
Message:

Ensure that errno variable is being properly saved and restored
in VSI wrappers.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/port/cpl_vsil_unix_stdio_64.cpp

    r13549 r13594  
    2727 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  
    2828 * DEALINGS IN THE SOFTWARE. 
     29 **************************************************************************** 
     30 * 
     31 * NB: Note that in wrappers we are always saving the error state (errno 
     32 * variable) to avoid side effects during debug prints or other possible 
     33 * standard function calls (error states will be overwritten after such 
     34 * a call). 
     35 * 
    2936 ****************************************************************************/ 
    3037 
     
    140147 
    141148{ 
     149    int     nResult = VSI_FSEEK64( fp, nOffset, nWhence ); 
     150    int     nError = errno; 
     151 
    142152#ifdef VSI_DEBUG 
    143  
    144     int nResult = VSI_FSEEK64( fp, nOffset, nWhence ); 
    145153 
    146154    if( nWhence == SEEK_SET ) 
     
    165173    } 
    166174 
     175#endif  
     176 
     177    errno = nError; 
    167178    return nResult; 
    168  
    169 #else 
    170  
    171     return( VSI_FSEEK64( fp, nOffset, nWhence ) ); 
    172  
    173 #endif  
    174179} 
    175180 
     
    181186 
    182187{ 
    183     vsi_l_offset nOffset = VSI_FTELL64( fp ); 
     188    vsi_l_offset    nOffset = VSI_FTELL64( fp ); 
     189    int             nError = errno; 
    184190 
    185191    VSIDebug2( "VSIUnixStdioHandle::Tell(%p) = %ld", fp, (long)nOffset ); 
    186192 
     193    errno = nError; 
    187194    return nOffset; 
    188195} 
     
    208215{ 
    209216    size_t  nResult = fread( pBuffer, nSize, nCount, fp ); 
    210  
    211     // Here we saving the error state to avoid side effects during debug 
    212     // prints (file stream and error states will be cleared after print call). 
    213     // We want debug line to come before the possible error lines. 
    214217    int     nError = errno; 
    215     int     nFpError = ferror(fp); 
    216218 
    217219    VSIDebug4( "VSIUnixStdioHandle::Read(%p,%ld,%ld) = %ld",  
    218220               fp, (long)nSize, (long)nCount, (long)nResult ); 
    219221 
    220     if ( !nResult && nFpError ) 
    221     { 
    222 #ifdef notdef 
    223         CPLError( CE_Failure, CPLE_FileIO, 
    224                   "Failed to read %ld blocks of %ld byte(s).\n%s", 
    225                   (long)nCount, (long)nSize, VSIStrerror(nError) ); 
    226 #endif 
    227     } 
    228  
     222    errno = nError; 
    229223    return nResult; 
    230224} 
     
    238232 
    239233{ 
    240     size_t nResult = fwrite( pBuffer, nSize, nCount, fp ); 
     234    size_t  nResult = fwrite( pBuffer, nSize, nCount, fp ); 
     235    int     nError = errno; 
    241236 
    242237    VSIDebug4( "VSIUnixStdioHandle::Write(%p,%ld,%ld) = %ld",  
    243238               fp, (long)nSize, (long)nCount, (long)nResult ); 
    244239 
     240    errno = nError; 
    245241    return nResult; 
    246242} 
     
    276272    VSIDebug3( "VSIUnixStdioFilesystemHandler::Open(\"%s\",\"%s\") = %p", 
    277273               pszFilename, pszAccess, fp ); 
    278      
     274 
    279275    if( fp == NULL ) 
    280276    { 
    281 #ifdef notdef 
    282         CPLError( CE_Failure, CPLE_FileIO, 
    283                   "Failed to open \"%s\" file.\n%s", 
    284                   pszFilename, VSIStrerror(nError) ); 
    285 #endif 
     277        errno = nError; 
    286278        return NULL; 
    287279    } 
     
    291283    poHandle->fp = fp; 
    292284 
     285    errno = nError; 
    293286    return poHandle; 
    294287} 
  • trunk/gdal/port/cpl_vsisimple.cpp

    r13549 r13594  
    2626 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
    2727 * DEALINGS IN THE SOFTWARE. 
     28 **************************************************************************** 
     29 * 
     30 * NB: Note that in wrappers we are always saving the error state (errno 
     31 * variable) to avoid side effects during debug prints or other possible 
     32 * standard function calls (error states will be overwritten after such 
     33 * a call). 
     34 * 
    2835 ****************************************************************************/ 
    2936 
     
    7178    VSIDebug3( "VSIFOpen(%s,%s) = %p", pszFilename, pszAccess, fp ); 
    7279 
    73     if( fp == NULL ) 
    74     { 
    75 #ifdef notdef 
    76         CPLError( CE_Failure, CPLE_FileIO, 
    77                   "Failed to open \"%s\" file.\n%s", 
    78                   pszFilename, VSIStrerror(nError) ); 
    79 #endif 
    80         return NULL; 
    81     } 
    82  
     80    errno = nError; 
    8381    return( fp ); 
    8482} 
     
    103101 
    104102{ 
     103    int     nResult = fseek( fp, nOffset, nWhence ); 
     104    int     nError = errno; 
     105 
    105106#ifdef VSI_DEBUG 
    106107    if( nWhence == SEEK_SET ) 
    107108    { 
    108         VSIDebug2( "VSIFSeek(%p,%d,SEEK_SET)", fp, nOffset ); 
     109        VSIDebug3( "VSIFSeek(%p,%d,SEEK_SET) = %d", fp, nOffset, nResult ); 
    109110    } 
    110111    else if( nWhence == SEEK_END ) 
    111112    { 
    112         VSIDebug2( "VSIFSeek(%p,%d,SEEK_END)", fp, nOffset ); 
     113        VSIDebug3( "VSIFSeek(%p,%d,SEEK_END) = %d", fp, nOffset, nResult ); 
    113114    } 
    114115    else if( nWhence == SEEK_CUR ) 
    115116    { 
    116         VSIDebug2( "VSIFSeek(%p,%d,SEEK_CUR)", fp, nOffset ); 
     117        VSIDebug3( "VSIFSeek(%p,%d,SEEK_CUR) = %d", fp, nOffset, nResult ); 
    117118    } 
    118119    else 
    119120    { 
    120         VSIDebug3( "VSIFSeek(%p,%d,%d-Unknown)", fp, nOffset, nWhence ); 
     121        VSIDebug4( "VSIFSeek(%p,%d,%d-Unknown) = %d", 
     122                   fp, nOffset, nWhence, nResult ); 
    121123    } 
    122124#endif  
    123125 
    124     return( fseek( fp, nOffset, nWhence ) ); 
     126    errno = nError; 
     127    return nResult; 
    125128} 
    126129 
     
    132135 
    133136{ 
    134     VSIDebug2( "VSIFTell(%p) = %ld", fp, ftell(fp) ); 
    135  
    136     return( ftell( fp ) ); 
     137    long    nOffset = ftell(fp); 
     138    int     nError = errno; 
     139 
     140    VSIDebug2( "VSIFTell(%p) = %ld", fp, nOffset ); 
     141 
     142    errno = nError; 
     143    return nOffset; 
    137144} 
    138145 
     
    157164    size_t  nResult = fread( pBuffer, nSize, nCount, fp ); 
    158165    int     nError = errno; 
    159     int     nFpError = ferror(fp); 
    160166 
    161167    VSIDebug4( "VSIFRead(%p,%ld,%ld) = %ld",  
    162168               fp, (long)nSize, (long)nCount, (long)nResult ); 
    163169 
    164     if ( !nResult && nFpError ) 
    165     { 
    166 #ifdef notdef 
    167         CPLError( CE_Failure, CPLE_FileIO, 
    168                   "Failed to read %ld blocks of %ld byte(s).\n%s", 
    169                   (long)nCount, (long)nSize, VSIStrerror(nError) ); 
    170 #endif 
    171     } 
    172  
     170    errno = nError; 
    173171    return nResult; 
    174172} 
     
    181179 
    182180{ 
    183     size_t nResult = fwrite( pBuffer, nSize, nCount, fp ); 
     181    size_t  nResult = fwrite( pBuffer, nSize, nCount, fp ); 
     182    int     nError = errno; 
    184183 
    185184    VSIDebug4( "VSIFWrite(%p,%ld,%ld) = %ld",  
    186185               fp, (long)nSize, (long)nCount, (long)nResult ); 
    187186 
     187    errno = nError; 
    188188    return nResult; 
    189189} 
     
    242242{ 
    243243    va_list     args; 
    244     int         nReturn, nError
     244    int         nReturn
    245245 
    246246    va_start( args, pszFormat ); 
    247247    nReturn = vfprintf( fp, pszFormat, args ); 
    248     nError = errno; 
    249248    va_end( args ); 
    250  
    251     if ( nReturn < 0 ) 
    252     { 
    253         CPLError( CE_Failure, CPLE_FileIO, 
    254                   "VSIFPrintf() failed.\n%s", VSIStrerror(nError) ); 
    255     } 
    256249 
    257250    return( nReturn );