mirror of https://github.com/vitalif/zbackup
Finally, configuration management works fine
parent
4a7314cb7e
commit
f9fc3c0b52
45
config.cc
45
config.cc
|
@ -1,7 +1,6 @@
|
||||||
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
|
// 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
|
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include "config.hh"
|
#include "config.hh"
|
||||||
#include "ex.hh"
|
#include "ex.hh"
|
||||||
#include "debug.hh"
|
#include "debug.hh"
|
||||||
|
@ -107,13 +106,16 @@ Config::Config()
|
||||||
storable = configInfo;
|
storable = configInfo;
|
||||||
dPrintf( "Config is instantiated and initialized with default values\n" );
|
dPrintf( "Config is instantiated and initialized with default values\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::Config( ConfigInfo * configInfo )
|
Config::Config( ConfigInfo * configInfo )
|
||||||
{
|
{
|
||||||
storable = configInfo;
|
storable = configInfo;
|
||||||
dPrintf( "Config is instantiated and initialized with supplied ConfigInfo\n" );
|
dPrintf( "Config is instantiated and initialized with supplied ConfigInfo\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::Config( const Config & configIn, ConfigInfo * configInfo )
|
Config::Config( const Config & configIn, ConfigInfo * configInfo )
|
||||||
{
|
{
|
||||||
|
configInfo->MergeFrom( *configIn.storable );
|
||||||
*this = configIn;
|
*this = configIn;
|
||||||
storable = configInfo;
|
storable = configInfo;
|
||||||
dPrintf( "Config is instantiated and initialized with supplied values\n" );
|
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;
|
Compression::CompressionMethod::selectedCompression = lzma;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if ( strcmp( optionValue, "lzo" ) == 0 )
|
if ( strcmp( optionValue, "lzo1x_1" ) == 0 || strcmp( optionValue, "lzo" ) == 0 )
|
||||||
{
|
{
|
||||||
const_sptr< Compression::CompressionMethod > lzo =
|
const_sptr< Compression::CompressionMethod > lzo =
|
||||||
Compression::CompressionMethod::findCompression( "lzo1x_1" );
|
Compression::CompressionMethod::findCompression( "lzo1x_1" );
|
||||||
|
@ -208,7 +210,8 @@ bool Config::parseOption( const char * option, const OptionType type )
|
||||||
return false;
|
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() );
|
dPrintf( "storable[bundle][compression_method] = %s\n", GET_STORABLE( bundle, compression_method ).c_str() );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -382,3 +385,39 @@ bool Config::validate( const string & configData, const string & newConfigData )
|
||||||
ConfigInfo newConfig;
|
ConfigInfo newConfig;
|
||||||
return parse( newConfigData, &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;
|
||||||
|
}
|
||||||
|
|
17
config.hh
17
config.hh
|
@ -22,6 +22,8 @@
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::bitset;
|
using std::bitset;
|
||||||
|
|
||||||
|
class ZBackupBase;
|
||||||
|
|
||||||
class Config
|
class Config
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -69,18 +71,27 @@ public:
|
||||||
|
|
||||||
static void showHelp( const OptionType );
|
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 );
|
OpCodes parseToken( const char * option, const OptionType );
|
||||||
bool parseOption( const char * option, const OptionType );
|
bool parseOption( const char * option, const OptionType );
|
||||||
|
|
||||||
string toString( google::protobuf::Message const & message );
|
|
||||||
|
|
||||||
Config( const Config &, ConfigInfo * );
|
Config( const Config &, ConfigInfo * );
|
||||||
Config( ConfigInfo * );
|
Config( ConfigInfo * );
|
||||||
Config();
|
Config();
|
||||||
|
|
||||||
RuntimeConfig runtime;
|
RuntimeConfig runtime;
|
||||||
ConfigInfo * storable;
|
ConfigInfo * storable;
|
||||||
private:
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include "zbackup_base.hh"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -655,21 +655,22 @@ invalid_option:
|
||||||
{
|
{
|
||||||
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
|
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
|
||||||
passwords[ 0 ], true );
|
passwords[ 0 ], true );
|
||||||
if ( zbb.editConfigInteractively() )
|
if ( Config::editInteractively( &zbb ) )
|
||||||
zbb.saveExtendedStorageInfo();
|
zbb.saveExtendedStorageInfo();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if ( args.size() > 2 && strcmp( args[ fieldAction ], "set" ) == 0 )
|
if ( args.size() > 2 && strcmp( args[ fieldAction ], "set" ) == 0 )
|
||||||
{
|
{
|
||||||
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
|
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
|
||||||
passwords[ 0 ], true );
|
passwords[ 0 ], config, true );
|
||||||
// -o ... like sysctl -w
|
zbb.config.show();
|
||||||
|
zbb.saveExtendedStorageInfo();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
|
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
|
||||||
passwords[ 0 ], true );
|
passwords[ 0 ], true );
|
||||||
zbb.showConfig();
|
zbb.config.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -116,11 +116,14 @@ ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
|
||||||
storageDir.c_str() );
|
storageDir.c_str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update all internal variables after configuration change
|
||||||
|
// Dunno why someone need to store duplicate information
|
||||||
|
// in deduplication utility
|
||||||
void ZBackupBase::propagateUpdate()
|
void ZBackupBase::propagateUpdate()
|
||||||
{
|
{
|
||||||
const_sptr<Compression::CompressionMethod> compression =
|
const_sptr< Compression::CompressionMethod > compression =
|
||||||
Compression::CompressionMethod::findCompression(
|
Compression::CompressionMethod::findCompression(
|
||||||
extendedStorageInfo.config().bundle().compression_method() );
|
config.GET_STORABLE( bundle, compression_method ) );
|
||||||
Compression::CompressionMethod::selectedCompression = compression;
|
Compression::CompressionMethod::selectedCompression = compression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -396,34 +399,3 @@ end:
|
||||||
|
|
||||||
return isChanged;
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -67,13 +67,6 @@ public:
|
||||||
bool spawnEditor( std::string & data, bool( * validator )
|
bool spawnEditor( std::string & data, bool( * validator )
|
||||||
( string const &, string const & ) );
|
( string const &, string const & ) );
|
||||||
|
|
||||||
// Print current configuration to screen
|
|
||||||
void showConfig();
|
|
||||||
|
|
||||||
// Edit current configuration
|
|
||||||
// returns true if configuration is changed
|
|
||||||
bool editConfigInteractively();
|
|
||||||
|
|
||||||
StorageInfo storageInfo;
|
StorageInfo storageInfo;
|
||||||
EncryptionKey encryptionkey;
|
EncryptionKey encryptionkey;
|
||||||
ExtendedStorageInfo extendedStorageInfo;
|
ExtendedStorageInfo extendedStorageInfo;
|
||||||
|
|
Loading…
Reference in New Issue