Skip to content

Commit

Permalink
Added support for -o lzma.compression_level:
Browse files Browse the repository at this point in the history
* Closes #10 completely
* editConfigInteractively moved to ZBackupBase again (to make possible
  to use Config almost anywhere)
  • Loading branch information
Vlad1mir-D committed Jan 23, 2015
1 parent c5b821d commit 30fe01a
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 70 deletions.
14 changes: 9 additions & 5 deletions bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key,
}
}

void Creator::write( std::string const & fileName, EncryptionKey const & key )
void Creator::write( Config const & config, std::string const & fileName,
EncryptionKey const & key )
{
EncryptedFile::OutputStream os( fileName.c_str(), key, Encryption::ZeroIv );

os.writeRandomIv();

BundleFileHeader header;

const_sptr<Compression::CompressionMethod> compression = Compression::CompressionMethod::selectedCompression;
const_sptr<Compression::CompressionMethod> compression =
Compression::CompressionMethod::selectedCompression;
header.set_compression_method( compression->getName() );

// The old code only support lzma, so we will bump up the version, if we're
Expand All @@ -115,7 +117,8 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key )

// Compress

sptr<Compression::EnDecoder> encoder = compression->createEncoder();
sptr<Compression::EnDecoder> encoder = compression->createEncoder(
config );

encoder->setInput( payload.data(), payload.size() );

Expand All @@ -135,7 +138,7 @@ void Creator::write( std::string const & fileName, EncryptionKey const & key )
}

// Perform the compression
if ( encoder->process(true) )
if ( encoder->process( true ) )
{
if ( encoder->getAvailableOutput() )
os.BackUp( encoder->getAvailableOutput() );
Expand Down Expand Up @@ -190,7 +193,8 @@ Reader::Reader( string const & fileName, EncryptionKey const & key, bool prohibi
decoder->setInput( data, size );
}

if ( decoder->process(false) ) {
if ( decoder->process( false ) )
{
if ( decoder->getAvailableInput() )
is.BackUp( decoder->getAvailableInput() );
break;
Expand Down
3 changes: 2 additions & 1 deletion bundle.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "static_assert.hh"
#include "zbackup.pb.h"
#include "encrypted_file.hh"
#include "config.hh"

namespace Bundle {

Expand Down Expand Up @@ -101,7 +102,7 @@ public:
/// Compresses and writes the bundle to the given file. The operation is
/// time-consuming - calling this function from a worker thread could be
/// warranted
void write( string const & fileName, EncryptionKey const & );
void write( Config const &, string const & fileName, EncryptionKey const & );
void write( string const & fileName, EncryptionKey const &,
Bundle::Reader & reader );

Expand Down
10 changes: 6 additions & 4 deletions chunk_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ void Writer::finishCurrentBundle()
while ( runningCompressors >= maxCompressorsToRun )
runningCompressorsCondition.wait( runningCompressorsMutex );

Compressor * compressor = new Compressor( *this, currentBundle,
Compressor * compressor = new Compressor( config,
*this, currentBundle,
file->getFileName() );

currentBundle.reset();
Expand Down Expand Up @@ -159,18 +160,19 @@ Bundle::Id const & Writer::getCurrentBundleId()
return currentBundleId;
}

Writer::Compressor::Compressor( Writer & writer,
Writer::Compressor::Compressor( Config const & configIn, Writer & writer,
sptr< Bundle::Creator > const & bundleCreator,
string const & fileName ):
writer( writer ), bundleCreator( bundleCreator ), fileName( fileName )
writer( writer ), bundleCreator( bundleCreator ), fileName( fileName ),
config( configIn )
{
}

void * Writer::Compressor::Compressor::threadFunction() throw()
{
try
{
bundleCreator->write( fileName, writer.encryptionKey );
bundleCreator->write( config, fileName, writer.encryptionKey );
}
catch( std::exception & e )
{
Expand Down
3 changes: 2 additions & 1 deletion chunk_storage.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ private:
Writer & writer;
sptr< Bundle::Creator > bundleCreator;
string fileName;
Config const & config;
public:
Compressor( Writer &, sptr< Bundle::Creator > const &,
Compressor( Config const &, Writer &, sptr< Bundle::Creator > const &,
string const & fileName );
protected:
virtual void * threadFunction() throw();
Expand Down
68 changes: 47 additions & 21 deletions compression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

namespace Compression {

EnDecoder::EnDecoder() { }
EnDecoder::~EnDecoder() { }
EnDecoder::EnDecoder()
{
}

CompressionMethod::~CompressionMethod() { }
EnDecoder::~EnDecoder()
{
}

CompressionMethod::~CompressionMethod()
{
}

// LZMA

Expand Down Expand Up @@ -72,8 +78,17 @@ class LZMAEncoder : public LZMAEnDecoder
public:
LZMAEncoder()
{
uint32_t preset = 6; // TODO: make this customizable, although 6 seems to be
// the best option
uint32_t preset = 6;
lzma_ret ret = lzma_easy_encoder( &strm, preset, LZMA_CHECK_CRC64 );
CHECK( ret == LZMA_OK, "lzma_easy_encoder error: %d", (int) ret );
}

LZMAEncoder( Config const & config )
{
uint32_t compressionLevel = config.GET_STORABLE( lzma, compression_level );
uint32_t preset = ( compressionLevel > 9 ) ?
( compressionLevel - 10 ) | LZMA_PRESET_EXTREME :
compressionLevel;
lzma_ret ret = lzma_easy_encoder( &strm, preset, LZMA_CHECK_CRC64 );
CHECK( ret == LZMA_OK, "lzma_easy_encoder error: %d", (int) ret );
}
Expand All @@ -92,6 +107,11 @@ class LZMADecoder : public LZMAEnDecoder
class LZMACompression : public CompressionMethod
{
public:
sptr<EnDecoder> createEncoder( Config const & config ) const
{
return new LZMAEncoder( config );
}

sptr<EnDecoder> createEncoder() const
{
return new LZMAEncoder();
Expand All @@ -105,7 +125,6 @@ class LZMACompression : public CompressionMethod
std::string getName() const { return "lzma"; }
};


// LZO

// liblzo implements a lot of algorithms "for unlimited backward compatibility"
Expand Down Expand Up @@ -232,7 +251,7 @@ class NoStreamEnDecoder : public EnDecoder
{
// data has been encoded or decoded, remaining output is in accDataOut
// -> copy to output
if (availOut > 0 && accDataOut.size() - posInAccDataOut > 0)
if ( availOut > 0 && accDataOut.size() - posInAccDataOut > 0 )
{
size_t sz = availOut;
if ( sz > accDataOut.size() - posInAccDataOut )
Expand Down Expand Up @@ -273,7 +292,7 @@ class NoStreamEnDecoder : public EnDecoder
// we use our own buffer
size_t bufferSize = suggestOutputSize( dataIn, availIn );
do {
accDataOut.resize(bufferSize);
accDataOut.resize( bufferSize );

size_t outputSize;
//TODO doc says we mustn't modify the pointer returned by data()...
Expand Down Expand Up @@ -312,7 +331,6 @@ class NoStreamAndUnknownSizeDecoder : public NoStreamEnDecoder
virtual bool doProcessNoSize( const char* dataIn, size_t availIn,
char* dataOut, size_t availOut, size_t& outputSize ) =0;


bool shouldTryWith( const char* dataIn, size_t availIn, size_t availOut )
{
return suggestOutputSize( dataIn, availIn ) <= availOut;
Expand Down Expand Up @@ -388,7 +406,6 @@ class NoStreamAndUnknownSizeEncoder : public NoStreamEnDecoder
virtual bool doProcessNoSize( const char* dataIn, size_t availIn,
char* dataOut, size_t availOut, size_t& outputSize ) =0;


bool shouldTryWith( const char*, size_t, size_t availOut )
{
// If the compression doesn't use any spaces...
Expand Down Expand Up @@ -443,7 +460,6 @@ class NoStreamAndUnknownSizeEncoder : public NoStreamEnDecoder
}
};


#ifdef HAVE_LIBLZO

#include <lzo/lzo1x.h>
Expand Down Expand Up @@ -472,9 +488,9 @@ class LZO1X_1_Compression;
class LZO1X_1_Encoder : public NoStreamAndUnknownSizeEncoder
{
const LZO1X_1_Compression* compression;
static size_t calcMaxCompressedSize(size_t availIn);
static size_t calcMaxCompressedSize( size_t availIn );
public:
LZO1X_1_Encoder(const LZO1X_1_Compression* compression)
LZO1X_1_Encoder( const LZO1X_1_Compression* compression )
{
this->compression = compression;
}
Expand All @@ -499,21 +515,26 @@ class LZO1X_1_Compression : public CompressionMethod
}
}
public:
sptr<EnDecoder> createEncoder() const
sptr< EnDecoder > createEncoder( Config const & config ) const
{
init();
return new LZO1X_1_Encoder(this);
}

sptr<EnDecoder> createDecoder() const
sptr< EnDecoder > createEncoder() const
{
init();
return new LZO1X_1_Encoder(this);
}

sptr< EnDecoder > createDecoder() const
{
init();
return new LZO1X_1_Decoder();
}

std::string getName() const { return "lzo1x_1"; }


lzo_voidp getWorkmem( size_t size ) const
{
return new char[size];
Expand All @@ -531,7 +552,6 @@ class LZO1X_1_Compression : public CompressionMethod

bool LZO1X_1_Compression::initialized = false;


size_t LZO1X_1_Encoder::calcMaxCompressedSize( size_t availIn )
{
// It seems that lzo1x_1_compress does NOT check whether the buffer is big enough.
Expand Down Expand Up @@ -596,12 +616,12 @@ const_sptr< CompressionMethod > const CompressionMethod::compressions[] = {
};

const_sptr< CompressionMethod > CompressionMethod::selectedCompression =
compressions[0];
compressions[ 0 ];

const_sptr< CompressionMethod > CompressionMethod::findCompression(
const std::string& name, bool optional )
{
for ( const const_sptr<CompressionMethod>* c = compressions+0; *c; ++c )
for ( const const_sptr<CompressionMethod>* c = compressions + 0; *c; ++c )
{
if ( (*c)->getName() == name )
{
Expand All @@ -616,9 +636,15 @@ const_sptr< CompressionMethod > CompressionMethod::findCompression(
}

// iterator over compressions
CompressionMethod::iterator::iterator( const const_sptr< CompressionMethod > * ptr ):
ptr( ptr )
{
}

CompressionMethod::iterator::iterator( const const_sptr<CompressionMethod>* ptr ) : ptr( ptr) { }
CompressionMethod::iterator::iterator( const iterator& it ) : ptr(it.ptr) { }
CompressionMethod::iterator::iterator( const iterator & it ):
ptr( it.ptr )
{
}

CompressionMethod::iterator& CompressionMethod::iterator::operator =( const iterator& it )
{
Expand Down
2 changes: 2 additions & 0 deletions compression.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "sptr.hh"
#include "ex.hh"
#include "nocopy.hh"
#include "config.hh"

namespace Compression {

Expand Down Expand Up @@ -48,6 +49,7 @@ public:
// This name is saved in the file header of the compressed file.
virtual std::string getName() const = 0;

virtual sptr< EnDecoder > createEncoder( Config const & ) const = 0;
virtual sptr< EnDecoder > createEncoder() const = 0;
virtual sptr< EnDecoder > createDecoder() const = 0;

Expand Down
Loading

0 comments on commit 30fe01a

Please sign in to comment.