1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include <stdint.h>
#include "bundle.hh"
#include "check.hh"
#include "dir.hh"
#include "encryption.hh"
#include "utils.hh"
#include "message.hh"
#include "adler32.hh"
#include "compression.hh"
namespace Bundle {
enum
{
FileFormatVersion = 1,
// This means, we don't use LZMA in this file.
FileFormatVersionNotLZMA,
// <- add more versions here
// This is the first version, we do not support.
FileFormatVersionFirstUnsupported
};
void Creator::addChunk( string const & id, void const * data, size_t size )
{
BundleInfo_ChunkRecord * record = info.add_chunk_record();
record->set_id( id );
record->set_size( size );
payload.append( ( char const * ) data, size );
}
void Creator::write( std::string const & fileName, EncryptionKey const & key,
Reader & reader )
{
EncryptedFile::OutputStream os( fileName.c_str(), key, Encryption::ZeroIv );
os.writeRandomIv();
Message::serialize( reader.getBundleHeader(), os );
Message::serialize( reader.getBundleInfo(), os );
os.writeAdler32();
void * bufPrev = NULL;
const void * bufCurr = NULL;
int sizePrev = 0, sizeCurr = 0;
bool readPrev = false;
for ( ; ; )
{
bool readCurr = reader.is->Next( &bufCurr, &sizeCurr );
if ( readCurr )
{
if ( readPrev )
{
os.write( bufPrev, sizePrev );
readPrev = readCurr;
free( bufPrev );
bufPrev = malloc( sizeCurr );
memcpy( bufPrev, bufCurr, sizeCurr );
sizePrev = sizeCurr;
}
else
{
readPrev = readCurr;
bufPrev = malloc( sizeCurr );
memcpy( bufPrev, bufCurr, sizeCurr );
sizePrev = sizeCurr;
}
}
else
{
if ( readPrev )
{
sizePrev -= sizeof( Adler32::Value );
os.write( bufPrev, sizePrev );
os.writeAdler32();
free ( bufPrev );
break;
}
}
}
if ( reader.is.get() )
reader.is.reset();
}
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;
header.set_compression_method( compression->getName() );
// The old code only support lzma, so we will bump up the version, if we're
// using lzma. This will make it fail cleanly.
if ( compression->getName() == "lzma" )
header.set_version( FileFormatVersion );
else
header.set_version( FileFormatVersionNotLZMA );
Message::serialize( header, os );
Message::serialize( info, os );
os.writeAdler32();
// Compress
sptr<Compression::EnDecoder> encoder = compression->createEncoder(
config );
encoder->setInput( payload.data(), payload.size() );
for ( ; ; )
{
{
void * data;
int size;
if ( !os.Next( &data, &size ) )
{
encoder.reset();
throw exBundleWriteFailed();
}
if ( !size )
continue;
encoder->setOutput( data, size );
}
// Perform the compression
if ( encoder->process( true ) )
{
if ( encoder->getAvailableOutput() )
os.BackUp( encoder->getAvailableOutput() );
break;
}
}
encoder.reset();
os.writeAdler32();
}
Reader::Reader( string const & fileName, EncryptionKey const & key, bool keepStream )
{
is = new EncryptedFile::InputStream( fileName.c_str(), key, Encryption::ZeroIv );
is->consumeRandomIv();
Message::parse( header, *is );
if ( header.version() >= FileFormatVersionFirstUnsupported )
throw exUnsupportedVersion();
Message::parse( info, *is );
is->checkAdler32();
size_t payloadSize = 0;
for ( int x = info.chunk_record_size(); x--; )
payloadSize += info.chunk_record( x ).size();
payload.resize( payloadSize );
if ( keepStream )
return;
sptr<Compression::EnDecoder> decoder = Compression::CompressionMethod::findCompression(
header.compression_method() )->createDecoder();
decoder->setOutput( &payload[ 0 ], payload.size() );
for ( ; ; )
{
{
void const * data;
int size;
if ( !is->Next( &data, &size ) )
{
decoder.reset();
throw exBundleReadFailed();
}
if ( !size )
continue;
decoder->setInput( data, size );
}
if ( decoder->process( false ) )
{
if ( decoder->getAvailableInput() )
is->BackUp( decoder->getAvailableInput() );
break;
}
if ( !decoder->getAvailableOutput() && decoder->getAvailableInput() )
{
// Apparently we have more data than we were expecting
decoder.reset();
throw exTooMuchData();
}
}
decoder.reset();
is->checkAdler32();
if ( is.get() )
is.reset();
// Populate the map
char const * next = payload.data();
for ( int x = 0, count = info.chunk_record_size(); x < count; ++x )
{
BundleInfo_ChunkRecord const & record = info.chunk_record( x );
pair< Chunks::iterator, bool > res =
chunks.insert( Chunks::value_type( record.id(),
Chunks::mapped_type( next,
record.size() ) ) );
if ( !res.second )
throw exDuplicateChunks(); // Duplicate key encountered
next += record.size();
}
}
bool Reader::get( string const & chunkId, string & chunkData,
size_t & chunkDataSize )
{
Chunks::iterator i = chunks.find( chunkId );
if ( i != chunks.end() )
{
size_t sz = i->second.second;
if ( chunkData.size() < sz )
chunkData.resize( sz );
memcpy( &chunkData[ 0 ], i->second.first, sz );
chunkDataSize = sz;
return true;
}
else
return false;
}
string generateFileName( Id const & id, string const & bundlesDir,
bool createDirs )
{
string hex( Utils::toHex( ( unsigned char * ) &id, sizeof( id ) ) );
// TODO: make this scheme more flexible and allow it to scale, or at least
// be configurable
string level1( Dir::addPath( bundlesDir, hex.substr( 0, 2 ) ) );
if ( createDirs && !Dir::exists( level1 ) )
Dir::create( level1 );
return string( Dir::addPath( level1, hex ) );
}
}
|