prepare lzo support for pull request - 2nd half

- move compression_method field from BundleInfo to file header
- add a test case to make sure this doesn't break compatibility
master
benny 2013-10-08 01:43:33 +02:00
parent bb623bbb19
commit f2a7446716
4 changed files with 78 additions and 16 deletions

View File

@ -33,12 +33,11 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key )
os.writeRandomIv();
FileHeader header;
BundleFileHeader header;
header.set_version( FileFormatVersion );
Message::serialize( header, os );
const_sptr<Compression> compression = Compression::default_compression;
info.set_compression_method( compression->getName() );
header.set_compression_method( compression->getName() );
Message::serialize( header, os );
Message::serialize( info, os );
os.writeAdler32();
@ -49,8 +48,6 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key )
encoder->setInput( payload.data(), payload.size() );
// deliberately break the test: ((uint8_t*)strm.next_in)[10] = 7;
for ( ; ; )
{
{
@ -83,7 +80,7 @@ Reader::Reader( string const & fileName, EncryptionKey const & key )
is.consumeRandomIv();
FileHeader header;
BundleFileHeader header;
Message::parse( header, is );
if ( header.version() != FileFormatVersion )
@ -99,7 +96,7 @@ Reader::Reader( string const & fileName, EncryptionKey const & key )
payload.resize( payloadSize );
sptr<EnDecoder> decoder = Compression::findCompression( info.compression_method() )->createDecoder();
sptr<EnDecoder> decoder = Compression::findCompression( header.compression_method() )->createDecoder();
decoder->setOutput( &payload[ 0 ], payload.size() );

View File

@ -46,4 +46,5 @@ HEADERS += \
../../message.hh \
../../hex.hh \
../../compression.hh \
../../message.hh \
../../zbackup.pb.h

View File

@ -12,9 +12,68 @@
#include "../../adler32.hh"
#include "../../bundle.hh"
#include "../../compression.hh"
#include "../../message.hh"
char tmpbuf[100];
void testCompatibility()
{
// The LZO code uses a different file header than the previous code
// because it adds the compression_method field. Nevertheless, it
// must be compatible with previous code.
TmpMgr tmpMgr( "/dev/shm" );
sptr< TemporaryFile > tempFile = tmpMgr.makeTemporaryFile();
std::string fileName = tempFile->getFileName();
EncryptionKey noKey( std::string(), NULL );
// Write old header, read as new header
{
{
EncryptedFile::OutputStream os( fileName.c_str(), noKey, Encryption::ZeroIv );
FileHeader header;
header.set_version( 42 );
Message::serialize( header, os );
}
{
EncryptedFile::InputStream is( fileName.c_str(), noKey, Encryption::ZeroIv );
BundleFileHeader header;
Message::parse( header, is );
CHECK( header.version() == 42, "version is wrong when reading old header with new program" );
CHECK( header.compression_method() == "lzma", "compression_method is wrong when reading old header with new program" );
}
}
// Write new header, read as old header
{
{
EncryptedFile::OutputStream os( fileName.c_str(), noKey, Encryption::ZeroIv );
BundleFileHeader header;
header.set_version( 42 );
Message::serialize( header, os );
}
{
EncryptedFile::InputStream is( fileName.c_str(), noKey, Encryption::ZeroIv );
FileHeader header;
Message::parse( header, is );
CHECK( header.version() == 42, "version is wrong when reading new header with old program" );
// cannot check compression_method because the field doesn't exist
}
}
printf("compatibility test successful.\n");
}
void readAndWrite( EncryptionKey const & key,
const_sptr<Compression> compression1, const_sptr<Compression> compression2 )
{
@ -83,6 +142,8 @@ int main()
EncryptionKey key( "blah", &keyInfo );
EncryptionKey noKey( std::string(), NULL );
testCompatibility();
std::vector< const_sptr<Compression> > compressions;
for ( Compression::iterator it = Compression::begin(); it!=Compression::end(); ++it ) {
printf( "supported compression: %s\n", (*it)->getName().c_str() );

View File

@ -53,14 +53,6 @@ message BundleInfo
// A sequence of chunk records
repeated ChunkRecord chunk_record = 1;
// Compression method that is used for this file
// If the program doesn't support that field, it will try LZMA. If it is
// LZMA, that will work. If it isn't, liblzma will recognize this and abort.
//NOTE If I was mainting this code (instead of adding this without asking for
// permission or support), I would probably use a string and I would
// definetly use tag 2 instead of 101.
optional string compression_method = 101 [default = "lzma"];
}
message FileHeader
@ -69,6 +61,17 @@ message FileHeader
required uint32 version = 1;
}
message BundleFileHeader
{
// File format version
required uint32 version = 1;
// Compression method that is used for this file
// If the program doesn't support that field, it will try LZMA. If it is
// LZMA, that will work. If it isn't, liblzma will recognize this and abort.
optional string compression_method = 2 [default = "lzma"];
}
message IndexBundleHeader
{
// Id of the bundle following in the stream. If not present, indicates the