From f9fc3c0b52082ac4b11bcd9680116010c3fbb10d Mon Sep 17 00:00:00 2001 From: Vladimir Stackov Date: Mon, 19 Jan 2015 17:29:01 +0300 Subject: [PATCH] Finally, configuration management works fine --- config.cc | 45 ++++++++++++++++++++++++++++++++++++++++++--- config.hh | 17 ++++++++++++++--- zbackup.cc | 9 +++++---- zbackup_base.cc | 38 +++++--------------------------------- zbackup_base.hh | 7 ------- 5 files changed, 66 insertions(+), 50 deletions(-) diff --git a/config.cc b/config.cc index 1e299c7..e722c4e 100644 --- a/config.cc +++ b/config.cc @@ -1,7 +1,6 @@ // Copyright (c) 2012-2014 Konstantin Isakov and ZBackup contributors, see CONTRIBUTORS // Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE -#include #include "config.hh" #include "ex.hh" #include "debug.hh" @@ -107,13 +106,16 @@ Config::Config() storable = configInfo; dPrintf( "Config is instantiated and initialized with default values\n" ); } + Config::Config( ConfigInfo * configInfo ) { storable = configInfo; dPrintf( "Config is instantiated and initialized with supplied ConfigInfo\n" ); } + Config::Config( const Config & configIn, ConfigInfo * configInfo ) { + configInfo->MergeFrom( *configIn.storable ); *this = configIn; storable = configInfo; dPrintf( "Config is instantiated and initialized with supplied values\n" ); @@ -188,7 +190,7 @@ bool Config::parseOption( const char * option, const OptionType type ) Compression::CompressionMethod::selectedCompression = lzma; } else - if ( strcmp( optionValue, "lzo" ) == 0 ) + if ( strcmp( optionValue, "lzo1x_1" ) == 0 || strcmp( optionValue, "lzo" ) == 0 ) { const_sptr< Compression::CompressionMethod > lzo = Compression::CompressionMethod::findCompression( "lzo1x_1" ); @@ -208,7 +210,8 @@ bool Config::parseOption( const char * option, const OptionType type ) return false; } - SET_STORABLE( bundle, compression_method, optionValue ); + SET_STORABLE( bundle, compression_method, + Compression::CompressionMethod::selectedCompression->getName() ); dPrintf( "storable[bundle][compression_method] = %s\n", GET_STORABLE( bundle, compression_method ).c_str() ); return true; @@ -382,3 +385,39 @@ bool Config::validate( const string & configData, const string & newConfigData ) ConfigInfo newConfig; return parse( newConfigData, &newConfig ); } + +void Config::show() +{ + printf( "%s", toString( *storable ).c_str() ); +} + +void Config::show( const ConfigInfo & config ) +{ + printf( "%s", toString( config ).c_str() ); +} + +bool Config::editInteractively( ZBackupBase * zbb ) +{ + string configData( toString( *zbb->config.storable ) ); + string newConfigData( configData ); + + if ( !zbb->spawnEditor( newConfigData, &validate ) ) + return false; + ConfigInfo newConfig; + if ( !parse( newConfigData, &newConfig ) ) + return false; + if ( toString( *zbb->config.storable ) == toString( newConfig ) ) + { + verbosePrintf( "No changes made to config\n" ); + return false; + } + + verbosePrintf( "Updating configuration...\n" ); + + zbb->config.storable->CopyFrom( newConfig ); + verbosePrintf( +"Configuration successfully updated!\n" +"Updated configuration:\n%s", toString( *zbb->config.storable ).c_str() ); + + return true; +} diff --git a/config.hh b/config.hh index f8dda6c..3dc33a0 100644 --- a/config.hh +++ b/config.hh @@ -22,6 +22,8 @@ using std::string; using std::bitset; +class ZBackupBase; + class Config { public: @@ -69,18 +71,27 @@ public: static void showHelp( const OptionType ); + static string toString( google::protobuf::Message const & message ); + + // Edit current configuration + // returns true if configuration is changed + static bool editInteractively( ZBackupBase * ); + + // Print configuration to screen + static void show( const ConfigInfo & ); + void show(); + OpCodes parseToken( const char * option, const OptionType ); bool parseOption( const char * option, const OptionType ); - string toString( google::protobuf::Message const & message ); - Config( const Config &, ConfigInfo * ); Config( ConfigInfo * ); Config(); RuntimeConfig runtime; ConfigInfo * storable; -private: }; +#include "zbackup_base.hh" + #endif diff --git a/zbackup.cc b/zbackup.cc index 9c7a37f..c6b86f9 100644 --- a/zbackup.cc +++ b/zbackup.cc @@ -655,21 +655,22 @@ invalid_option: { ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ), passwords[ 0 ], true ); - if ( zbb.editConfigInteractively() ) + if ( Config::editInteractively( &zbb ) ) zbb.saveExtendedStorageInfo(); } else if ( args.size() > 2 && strcmp( args[ fieldAction ], "set" ) == 0 ) { ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ), - passwords[ 0 ], true ); - // -o ... like sysctl -w + passwords[ 0 ], config, true ); + zbb.config.show(); + zbb.saveExtendedStorageInfo(); } else { ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ), passwords[ 0 ], true ); - zbb.showConfig(); + zbb.config.show(); } } else diff --git a/zbackup_base.cc b/zbackup_base.cc index b98d332..fe7c5a2 100644 --- a/zbackup_base.cc +++ b/zbackup_base.cc @@ -116,11 +116,14 @@ ZBackupBase::ZBackupBase( string const & storageDir, string const & password, storageDir.c_str() ); } +// Update all internal variables after configuration change +// Dunno why someone need to store duplicate information +// in deduplication utility void ZBackupBase::propagateUpdate() { - const_sptr compression = + const_sptr< Compression::CompressionMethod > compression = Compression::CompressionMethod::findCompression( - extendedStorageInfo.config().bundle().compression_method() ); + config.GET_STORABLE( bundle, compression_method ) ); Compression::CompressionMethod::selectedCompression = compression; } @@ -396,34 +399,3 @@ end: return isChanged; } - -void ZBackupBase::showConfig() -{ - printf( "%s", config.toString( *config.storable ).c_str() ); -} - -bool ZBackupBase::editConfigInteractively() -{ - string configData( config.toString( extendedStorageInfo.config() ) ); - string newConfigData( configData ); - - if ( !spawnEditor( newConfigData, &config.validate ) ) - return false; - ConfigInfo newConfig; - if ( !config.parse( newConfigData, &newConfig ) ) - return false; - if ( config.toString( extendedStorageInfo.config() ) == config.toString( newConfig ) ) - { - verbosePrintf( "No changes made to config\n" ); - return false; - } - - verbosePrintf( "Updating configuration...\n" ); - - extendedStorageInfo.mutable_config()->CopyFrom( newConfig ); - verbosePrintf( -"Configuration successfully updated!\n" -"Updated configuration:\n%s", config.toString( extendedStorageInfo.config() ).c_str() ); - - return true; -} diff --git a/zbackup_base.hh b/zbackup_base.hh index ba1c170..24dc047 100644 --- a/zbackup_base.hh +++ b/zbackup_base.hh @@ -67,13 +67,6 @@ public: bool spawnEditor( std::string & data, bool( * validator ) ( string const &, string const & ) ); - // Print current configuration to screen - void showConfig(); - - // Edit current configuration - // returns true if configuration is changed - bool editConfigInteractively(); - StorageInfo storageInfo; EncryptionKey encryptionkey; ExtendedStorageInfo extendedStorageInfo;