[Quickfix-developers] FileStore issues
Brought to you by:
orenmnero
From: Kenny S. <the...@ya...> - 2004-01-13 04:17:39
|
Hi, I've noticed a few potential issues with the FileStore::set method. Namely, that in the event of a write error, the files may be left in a bad state leading to corruption. The the method looks like: if ( fseek( m_msgFile, 0, SEEK_END ) ) throw IOException(); if ( fseek( m_headerFile, 0, SEEK_END ) ) throw IOException(); int offset = ftell( m_msgFile ); if ( offset < 0 ) throw IOException(); int size = msg.size(); if ( fprintf( m_headerFile, "%d,%d,%d ", msgSeqNum, offset, size ) < 0 ) throw IOException(); m_offsets[ msgSeqNum ] = std::make_pair( offset, size ); fwrite( msg.c_str(), sizeof( char ), msg.size(), m_msgFile ); if ( ferror( m_msgFile ) ) throw IOException(); if ( fflush( m_msgFile ) == EOF ) throw IOException(); if ( fflush( m_headerFile ) == EOF ) throw IOException(); return true; So, first off, a file may not be more then 2GB, otherwise the ftell may return a negative number. No sweat, I'm not dealing with such large things yet. The real problems come in the last part where the actual writing is done. If the fprintf fails by running out of space and doing a partial write, nothing is cleaned up. Since a seek to end of file is always done, any future writes will also be affected by the superfluous characters. Same for the fwrite - no cleanup is done on error, thereby leaving the file in a un-usable state. Not only this, but also note that the header info is not rolled-back in the event of failure to persist the message file - leading to corrupt state on recovery. Having said this, I'm not sure I know the correct fix within the restrictions of ANSI C. If this were a POSIX world, I'd use little guard objects to roll back (ftruncate) the header and message files in the event of an error. Is there any interest in a FileStore-compatible POSIX-only MessageStore? thanks, -Kenny __________________________________ Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes http://hotjobs.sweepstakes.yahoo.com/signingbonus |