Slight refactoring

master
Am1GO 2014-10-02 22:13:15 +04:00
parent ea893dd868
commit 1d3a568a1b
4 changed files with 51 additions and 28 deletions

View File

@ -15,6 +15,7 @@ The program has the following features:
* Repository consists of immutable files. No existing files are ever modified
* Written in C++ only with only modest library dependencies
* Safe to use in production (see [below](#safety))
* Possibility to exchange data between repos without recompression
# Build dependencies

View File

@ -6,40 +6,40 @@
namespace BackupExchanger {
vector< string > recreateDirectories( string const & src, string const & dst, string const & relaPath )
vector< string > recreateDirectories( string const & src, string const & dst, string const & relativePath )
{
vector< string > files;
Dir::Listing lst ( Dir::addPath( src, relaPath ) );
Dir::Listing lst ( Dir::addPath( src, relativePath ) );
Dir::Entry entry;
while ( lst.getNext( entry ) )
{
string curRelaPath ( relaPath );
if ( curRelaPath.empty() )
curRelaPath.assign( entry.getFileName() );
string currentRelativePath ( relativePath );
if ( currentRelativePath.empty() )
currentRelativePath.assign( entry.getFileName() );
else
curRelaPath.assign( Dir::addPath( relaPath, entry.getFileName() ) );
currentRelativePath.assign( Dir::addPath( relativePath, entry.getFileName() ) );
if ( entry.isDir() )
{
verbosePrintf( "Found directory %s...\n", curRelaPath.c_str() );
string srcFullPath ( Dir::addPath( src, curRelaPath ) );
string dstFullPath ( Dir::addPath( dst, curRelaPath ) );
verbosePrintf( "Found directory %s...\n", currentRelativePath.c_str() );
string srcFullPath ( Dir::addPath( src, currentRelativePath ) );
string dstFullPath ( Dir::addPath( dst, currentRelativePath ) );
if ( !Dir::exists( dstFullPath.c_str() ) )
{
verbosePrintf( "Directory %s not found in destination, creating...\n",
curRelaPath.c_str() );
currentRelativePath.c_str() );
Dir::create( dstFullPath.c_str() );
}
vector< string > subFiles ( recreateDirectories( src, dst, curRelaPath ) );
vector< string > subFiles ( recreateDirectories( src, dst, currentRelativePath ) );
files.insert( files.end(), subFiles.begin(), subFiles.end() );
}
else
{
verbosePrintf( "Found file %s...\n", curRelaPath.c_str() );
files.push_back( curRelaPath );
verbosePrintf( "Found file %s...\n", currentRelativePath.c_str() );
files.push_back( currentRelativePath );
}
}

View File

@ -3,15 +3,16 @@
#ifndef BACKUP_EXCHANGER_HH_INCLUDED__
#define BACKUP_EXCHANGER_HH_INCLUDED__
#include <bitset>
#include <exception>
#include <string>
#include <vector>
#include "sptr.hh"
#include "tmp_mgr.hh"
namespace BackupExchanger {
using std::string;
using std::vector;
namespace BackupExchanger {
using std::pair;
enum {
backups,
@ -21,7 +22,8 @@ enum {
};
/// Recreate source directory structure in destination
vector< string > recreateDirectories( string const & src, string const & dst, string const & relaPath = std::string() );
vector< string > recreateDirectories( string const & src, string const & dst, string const & relativePath = std::string() );
typedef pair< sptr< TemporaryFile >, string > PendingExchangeRename;
}
#endif

View File

@ -323,6 +323,8 @@ ZExchange::ZExchange( string const & srcStorageDir, string const & srcPassword,
void ZExchange::exchange( string const & srcPath, string const & dstPath,
bitset< BackupExchanger::Flags > const & exchange )
{
vector< BackupExchanger::PendingExchangeRename > pendingExchangeRenames;
if ( exchange.test( BackupExchanger::bundles ) )
{
verbosePrintf( "Searching for bundles...\n" );
@ -342,11 +344,12 @@ void ZExchange::exchange( string const & srcPath, string const & dstPath,
sptr< TemporaryFile > bundleTempFile = dstZBackupBase.tmpMgr.makeTemporaryFile();
creator->write( bundleTempFile->getFileName(), dstZBackupBase.encryptionkey, *reader );
if ( creator.get() )
if ( creator.get() && reader.get() )
{
creator.reset();
bundleTempFile->moveOverTo( outputFileName );
bundleTempFile.reset();
reader.reset();
pendingExchangeRenames.push_back( BackupExchanger::PendingExchangeRename(
bundleTempFile, outputFileName ) );
verbosePrintf( "done.\n" );
}
}
@ -371,25 +374,25 @@ void ZExchange::exchange( string const & srcPath, string const & dstPath,
string outputFileName ( Dir::addPath( dstZBackupBase.getIndexPath(), *it ) );
if ( !File::exists( outputFileName ) )
{
IndexFile::Reader reader( srcZBackupBase.encryptionkey,
sptr< IndexFile::Reader > reader = new IndexFile::Reader( srcZBackupBase.encryptionkey,
Dir::addPath( srcZBackupBase.getIndexPath(), *it ) );
sptr< TemporaryFile > indexTempFile = dstZBackupBase.tmpMgr.makeTemporaryFile();
sptr< IndexFile::Writer > writer = new IndexFile::Writer( dstZBackupBase.encryptionkey,
indexTempFile->getFileName() );
BundleInfo bundleInfo;
Bundle::Id bundleId;
while( reader.readNextRecord( bundleInfo, bundleId ) )
while( reader->readNextRecord( bundleInfo, bundleId ) )
{
writer->add( bundleInfo, bundleId );
}
if ( writer.get() )
if ( writer.get() && reader.get() )
{
writer.reset();
indexTempFile->moveOverTo( outputFileName );
indexTempFile.reset();
reader.reset();
pendingExchangeRenames.push_back( BackupExchanger::PendingExchangeRename(
indexTempFile, outputFileName ) );
verbosePrintf( "done.\n" );
}
}
@ -421,7 +424,8 @@ void ZExchange::exchange( string const & srcPath, string const & dstPath,
sptr< TemporaryFile > tmpFile = dstZBackupBase.tmpMgr.makeTemporaryFile();
BackupFile::save( tmpFile->getFileName(), dstZBackupBase.encryptionkey,
backupInfo );
tmpFile->moveOverTo( outputFileName );
pendingExchangeRenames.push_back( BackupExchanger::PendingExchangeRename(
tmpFile, outputFileName ) );
verbosePrintf( "done.\n" );
}
else
@ -432,6 +436,22 @@ void ZExchange::exchange( string const & srcPath, string const & dstPath,
verbosePrintf( "Backup exchange completed.\n" );
}
if ( pendingExchangeRenames.size() > 0 )
{
verbosePrintf( "Moving files from temp directory to appropriate places... " );
for ( size_t x = pendingExchangeRenames.size(); x--; )
{
BackupExchanger::PendingExchangeRename & r = pendingExchangeRenames[ x ];
r.first->moveOverTo( r.second );
if ( r.first.get() )
{
r.first.reset();
}
}
pendingExchangeRenames.clear();
verbosePrintf( "done.\n" );
}
}
DEF_EX( exExchangeWithLessThanTwoKeys, "Specify password flag (--non-encrypted or --password-file)"