A little bit more engineering for Config

master
Vladimir Stackov 2015-01-19 15:19:45 +03:00
parent 2f736764ac
commit 4a7314cb7e
6 changed files with 58 additions and 18 deletions

View File

@ -105,10 +105,10 @@ ChunkIndex::ChunkIndex( EncryptionKey const & key, TmpMgr & tmpMgr,
key( key ), tmpMgr( tmpMgr ), indexPath( indexPath ), storage( 65536, 1 ), key( key ), tmpMgr( tmpMgr ), indexPath( indexPath ), storage( 65536, 1 ),
lastBundleId( NULL ) lastBundleId( NULL )
{ {
dPrintf( "Chunk index (%s) instantiated and initialized, hasKey: %s\n", indexPath.c_str(),
key.hasKey() ? "true" : "false" );
if ( !prohibitChunkIndexLoading ) if ( !prohibitChunkIndexLoading )
loadIndex( *this ); loadIndex( *this );
dPrintf( "Chunk index for %s is instantiated and initialized, hasKey: %s\n",
indexPath.c_str(), key.hasKey() ? "true" : "false" );
} }
Bundle::Id const * ChunkIndex::findChunk( ChunkId::RollingHashPart rollingHash, Bundle::Id const * ChunkIndex::findChunk( ChunkId::RollingHashPart rollingHash,

View File

@ -101,6 +101,24 @@ static struct
} }
Config::Config()
{
ConfigInfo * configInfo = new ConfigInfo;
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 )
{
*this = configIn;
storable = configInfo;
dPrintf( "Config is instantiated and initialized with supplied values\n" );
}
Config::OpCodes Config::parseToken( const char * option, const OptionType type ) Config::OpCodes Config::parseToken( const char * option, const OptionType type )
{ {
for ( u_int i = 0; ConfigHelper::keywords[ i ].name; i++ ) for ( u_int i = 0; ConfigHelper::keywords[ i ].name; i++ )
@ -190,6 +208,9 @@ bool Config::parseOption( const char * option, const OptionType type )
return false; return false;
} }
SET_STORABLE( bundle, compression_method, optionValue );
dPrintf( "storable[bundle][compression_method] = %s\n", GET_STORABLE( bundle, compression_method ).c_str() );
return true; return true;
/* NOTREACHED */ /* NOTREACHED */
break; break;

View File

@ -11,6 +11,14 @@
#include "mt.hh" #include "mt.hh"
#include "backup_exchanger.hh" #include "backup_exchanger.hh"
#define SET_STORABLE( storage, property, value ) (\
{\
dPrintf( "storable->mutable_"#storage"()->set_"#property"( "#value" )\n" ); \
storable->mutable_##storage()->set_##property( value ); \
})
#define GET_STORABLE( storage, property ) storable->storage().property()
using std::string; using std::string;
using std::bitset; using std::bitset;
@ -31,8 +39,6 @@ public:
} }
}; };
RuntimeConfig runtime;
enum OptionType enum OptionType
{ {
Runtime, Runtime,
@ -67,8 +73,14 @@ public:
bool parseOption( const char * option, const OptionType ); bool parseOption( const char * option, const OptionType );
string toString( google::protobuf::Message const & message ); string toString( google::protobuf::Message const & message );
Config( const Config &, ConfigInfo * );
Config( ConfigInfo * );
Config();
RuntimeConfig runtime;
ConfigInfo * storable;
private: private:
ConfigInfo storableConfig;
}; };
#endif #endif

View File

@ -394,7 +394,7 @@ int main( int argc, char *argv[] )
else else
if ( strcmp( argv[ x ], "--compression" ) == 0 && x + 1 < argc ) if ( strcmp( argv[ x ], "--compression" ) == 0 && x + 1 < argc )
{ {
fprintf( stderr, "%s is deprecated, use -O bundle.compression_method instead\n", argv[ x ] ); fprintf( stderr, "%s is deprecated, use -o bundle.compression_method instead\n", argv[ x ] );
deprecated.assign( argv[ x ] + 2 ); deprecated.assign( argv[ x ] + 2 );
deprecated.append( "=" ); deprecated.append( "=" );
deprecated.append( argv[ x + 1 ] ); deprecated.append( argv[ x + 1 ] );
@ -654,7 +654,7 @@ invalid_option:
if ( args.size() > 2 && strcmp( args[ fieldAction ], "edit" ) == 0 ) if ( args.size() > 2 && strcmp( args[ fieldAction ], "edit" ) == 0 )
{ {
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ), ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ] ); passwords[ 0 ], true );
if ( zbb.editConfigInteractively() ) if ( zbb.editConfigInteractively() )
zbb.saveExtendedStorageInfo(); zbb.saveExtendedStorageInfo();
} }
@ -662,13 +662,13 @@ invalid_option:
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 ] ); passwords[ 0 ], true );
// -o ... like sysctl -w // -o ... like sysctl -w
} }
else else
{ {
ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ), ZBackupBase zbb( ZBackupBase::deriveStorageDirFromBackupsFile( args[ fieldStorage ], true ),
passwords[ 0 ] ); passwords[ 0 ], true );
zbb.showConfig(); zbb.showConfig();
} }
} }

View File

@ -59,6 +59,7 @@ message BundleConfigInfo
optional string compression_method = 3 [default = "lzma"]; optional string compression_method = 3 [default = "lzma"];
} }
// Storable config values should always have default values
message ConfigInfo message ConfigInfo
{ {
required ChunkConfigInfo chunk = 1; required ChunkConfigInfo chunk = 1;

View File

@ -63,10 +63,12 @@ ZBackupBase::ZBackupBase( string const & storageDir, string const & password ):
&storageInfo.encryption_key() : 0 ), &storageInfo.encryption_key() : 0 ),
extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ), extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ),
tmpMgr( getTmpPath() ), tmpMgr( getTmpPath() ),
chunkIndex( encryptionkey, tmpMgr, getIndexPath(), false ) chunkIndex( encryptionkey, tmpMgr, getIndexPath(), false ),
config( extendedStorageInfo.mutable_config() )
{ {
propagateUpdate(); propagateUpdate();
dPrintf("%s repo instantiated and initialized\n", storageDir.c_str() ); dPrintf("ZBackupBase for %s is instantiated and initialized\n",
storageDir.c_str() );
} }
ZBackupBase::ZBackupBase( string const & storageDir, string const & password, ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
@ -77,10 +79,11 @@ ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ), extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ),
tmpMgr( getTmpPath() ), tmpMgr( getTmpPath() ),
chunkIndex( encryptionkey, tmpMgr, getIndexPath(), false ), chunkIndex( encryptionkey, tmpMgr, getIndexPath(), false ),
config( configIn ) config( configIn, extendedStorageInfo.mutable_config() )
{ {
propagateUpdate(); propagateUpdate();
dPrintf("%s repo instantiated and initialized\n", storageDir.c_str() ); dPrintf("ZBackupBase for %s is instantiated and initialized\n",
storageDir.c_str() );
} }
ZBackupBase::ZBackupBase( string const & storageDir, string const & password, ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
@ -90,10 +93,12 @@ ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
&storageInfo.encryption_key() : 0 ), &storageInfo.encryption_key() : 0 ),
extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ), extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ),
tmpMgr( getTmpPath() ), tmpMgr( getTmpPath() ),
chunkIndex( encryptionkey, tmpMgr, getIndexPath(), prohibitChunkIndexLoading ) chunkIndex( encryptionkey, tmpMgr, getIndexPath(), prohibitChunkIndexLoading ),
config( extendedStorageInfo.mutable_config() )
{ {
propagateUpdate(); propagateUpdate();
dPrintf("%s repo instantiated and initialized\n", storageDir.c_str() ); dPrintf("ZBackupBase for %s is instantiated and initialized\n",
storageDir.c_str() );
} }
ZBackupBase::ZBackupBase( string const & storageDir, string const & password, ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
@ -104,10 +109,11 @@ ZBackupBase::ZBackupBase( string const & storageDir, string const & password,
extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ), extendedStorageInfo( loadExtendedStorageInfo( encryptionkey ) ),
tmpMgr( getTmpPath() ), tmpMgr( getTmpPath() ),
chunkIndex( encryptionkey, tmpMgr, getIndexPath(), prohibitChunkIndexLoading ), chunkIndex( encryptionkey, tmpMgr, getIndexPath(), prohibitChunkIndexLoading ),
config( configIn ) config( configIn, extendedStorageInfo.mutable_config() )
{ {
propagateUpdate(); propagateUpdate();
dPrintf("%s repo instantiated and initialized\n", storageDir.c_str() ); dPrintf("ZBackupBase for %s is instantiated and initialized\n",
storageDir.c_str() );
} }
void ZBackupBase::propagateUpdate() void ZBackupBase::propagateUpdate()
@ -393,7 +399,7 @@ end:
void ZBackupBase::showConfig() void ZBackupBase::showConfig()
{ {
printf( "%s", config.toString( extendedStorageInfo.config() ).c_str() ); printf( "%s", config.toString( *config.storable ).c_str() );
} }
bool ZBackupBase::editConfigInteractively() bool ZBackupBase::editConfigInteractively()