mirror of https://github.com/vitalif/zbackup
commit
b4091bd8fc
104
config.cc
104
config.cc
|
@ -1,6 +1,7 @@
|
|||
// 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 <algorithm>
|
||||
#include "config.hh"
|
||||
#include "ex.hh"
|
||||
#include "debug.hh"
|
||||
|
@ -36,28 +37,20 @@
|
|||
|
||||
DEF_EX_STR( exInvalidThreadsValue, "Invalid threads value specified:", std::exception )
|
||||
|
||||
namespace ConfigHelper {
|
||||
Config defaultConfig;
|
||||
|
||||
void Config::prefillKeywords()
|
||||
{
|
||||
/* Textual representations of the tokens. */
|
||||
|
||||
static struct
|
||||
{
|
||||
const char * name;
|
||||
const Config::OpCodes opcode;
|
||||
const Config::OptionType type;
|
||||
const char * description;
|
||||
const string defaultValue;
|
||||
} keywords[] = {
|
||||
Keyword defaultKeywords[] = {
|
||||
// Storable options
|
||||
{
|
||||
"chunk.max_size",
|
||||
Config::oChunk_max_size,
|
||||
Config::Storable,
|
||||
"Maximum chunk size used when storing chunks\n"
|
||||
"Affects deduplication ratio directly"
|
||||
//"Default is %s",
|
||||
//Utils::numberToString( defaultConfig.GET_STORABLE( chunk, max_size ) )
|
||||
"Affects deduplication ratio directly\n"
|
||||
"Default is %s",
|
||||
Utils::numberToString( GET_STORABLE( chunk, max_size ) )
|
||||
},
|
||||
{
|
||||
"bundle.max_payload_size",
|
||||
|
@ -66,26 +59,26 @@ static struct
|
|||
"Maximum number of bytes a bundle can hold. Only real chunk bytes are\n"
|
||||
"counted, not metadata. Any bundle should be able to contain at least\n"
|
||||
"one arbitrary single chunk, so this should not be smaller than\n"
|
||||
"chunk.max_size"
|
||||
//"Default is %s",
|
||||
//Utils::numberToString( defaultConfig.GET_STORABLE( bundle, max_payload_size ) )
|
||||
"chunk.max_size\n"
|
||||
"Default is %s",
|
||||
Utils::numberToString( GET_STORABLE( bundle, max_payload_size ) )
|
||||
},
|
||||
{
|
||||
"bundle.compression_method",
|
||||
Config::oBundle_compression_method,
|
||||
Config::Storable,
|
||||
"Compression method for new bundles"
|
||||
//"Default is %s",
|
||||
//defaultConfig.GET_STORABLE( bundle, compression_method )
|
||||
"Compression method for new bundles\n"
|
||||
"Default is %s",
|
||||
GET_STORABLE( bundle, compression_method )
|
||||
},
|
||||
{
|
||||
"lzma.compression_level",
|
||||
Config::oLZMA_compression_level,
|
||||
Config::Storable,
|
||||
"Compression level for new LZMA-compressed files\n"
|
||||
"Valid values: 0-19 (values over 9 enables extreme mode)"
|
||||
//"Default is %s",
|
||||
//Utils::numberToString( defaultConfig.GET_STORABLE( lzma, compression_level ) )
|
||||
"Valid values: 0-19 (values over 9 enables extreme mode)\n"
|
||||
"Default is %s",
|
||||
Utils::numberToString( GET_STORABLE( lzma, compression_level ) )
|
||||
},
|
||||
|
||||
// Shortcuts for storable options
|
||||
|
@ -93,9 +86,9 @@ static struct
|
|||
"compression",
|
||||
Config::oBundle_compression_method,
|
||||
Config::Storable,
|
||||
"Shortcut for bundle.compression_method"
|
||||
//"Default is %s",
|
||||
//defaultConfig.GET_STORABLE( bundle, compression_method )
|
||||
"Shortcut for bundle.compression_method\n"
|
||||
"Default is %s",
|
||||
GET_STORABLE( bundle, compression_method )
|
||||
},
|
||||
|
||||
// Runtime options
|
||||
|
@ -105,7 +98,7 @@ static struct
|
|||
Config::Runtime,
|
||||
"Maximum number of compressor threads to use in backup process\n"
|
||||
"Default is %s on your system",
|
||||
Utils::numberToString( defaultConfig.runtime.threads )
|
||||
Utils::numberToString( runtime.threads )
|
||||
},
|
||||
{
|
||||
"cache-size",
|
||||
|
@ -115,7 +108,7 @@ static struct
|
|||
"Affects restore process speed directly\n"
|
||||
VALID_SUFFIXES
|
||||
"Default is %sMiB",
|
||||
Utils::numberToString( defaultConfig.runtime.cacheSize / 1024 / 1024 )
|
||||
Utils::numberToString( runtime.cacheSize / 1024 / 1024 )
|
||||
},
|
||||
{
|
||||
"exchange",
|
||||
|
@ -130,31 +123,39 @@ static struct
|
|||
"No default value, you should specify it explicitly"
|
||||
},
|
||||
|
||||
{ NULL, Config::oBadOption, Config::None }
|
||||
{ "", Config::oBadOption, Config::None }
|
||||
};
|
||||
|
||||
keywords = new Keyword[ sizeof( defaultKeywords) / sizeof( Keyword ) ];
|
||||
std::copy( defaultKeywords, defaultKeywords +
|
||||
sizeof( defaultKeywords) / sizeof( Keyword ), keywords );
|
||||
cleanup_keywords = true;
|
||||
}
|
||||
|
||||
Config::~Config()
|
||||
{
|
||||
// prevent memleak
|
||||
if ( want_cleanup )
|
||||
// prevent memleaks
|
||||
// TODO: use sptr
|
||||
if ( cleanup_storable )
|
||||
delete storable;
|
||||
if ( cleanup_keywords )
|
||||
delete[] keywords;
|
||||
}
|
||||
|
||||
Config::Config():
|
||||
want_cleanup( false )
|
||||
cleanup_storable( true )
|
||||
{
|
||||
ConfigInfo * configInfo = new ConfigInfo;
|
||||
storable = configInfo;
|
||||
storable = new ConfigInfo;
|
||||
prefillKeywords();
|
||||
dPrintf( "%s is instantiated and initialized with default values\n",
|
||||
__CLASS );
|
||||
}
|
||||
|
||||
Config::Config( ConfigInfo * configInfo ):
|
||||
want_cleanup( false )
|
||||
cleanup_storable( false )
|
||||
{
|
||||
storable = configInfo;
|
||||
prefillKeywords();
|
||||
dPrintf( "%s is instantiated and initialized with supplied ConfigInfo\n",
|
||||
__CLASS );
|
||||
}
|
||||
|
@ -164,24 +165,25 @@ Config::Config( const Config & configIn, ConfigInfo * configInfo )
|
|||
configInfo->MergeFrom( *configIn.storable );
|
||||
*this = configIn;
|
||||
storable = configInfo;
|
||||
want_cleanup = false;
|
||||
cleanup_storable = false;
|
||||
cleanup_keywords = false;
|
||||
dPrintf( "%s is instantiated and initialized with supplied values\n",
|
||||
__CLASS );
|
||||
}
|
||||
|
||||
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; !keywords[ i ].name.empty(); i++ )
|
||||
{
|
||||
if ( strcasecmp( option, ConfigHelper::keywords[ i ].name ) == 0 )
|
||||
if ( strcasecmp( option, keywords[ i ].name.c_str() ) == 0 )
|
||||
{
|
||||
if ( ConfigHelper::keywords[ i ].type != type )
|
||||
if ( keywords[ i ].type != type )
|
||||
{
|
||||
fprintf( stderr, "Invalid option type specified for %s\n", option );
|
||||
break;
|
||||
}
|
||||
|
||||
return ConfigHelper::keywords[ i ].opcode;
|
||||
return keywords[ i ].opcode;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -227,11 +229,11 @@ bool Config::parseOrValidate( const char * option, const OptionType type,
|
|||
SKIP_ON_VALIDATION;
|
||||
REQUIRE_VALUE;
|
||||
|
||||
if ( sscanf( optionValue, "%zu %n", &uint32Value, &n ) == 1
|
||||
if ( sscanf( optionValue, "%u %n", &uint32Value, &n ) == 1
|
||||
&& !optionValue[ n ] )
|
||||
{
|
||||
SET_STORABLE( chunk, max_size, uint32Value );
|
||||
dPrintf( "storable[chunk][max_size] = %zu\n",
|
||||
dPrintf( "storable[chunk][max_size] = %u\n",
|
||||
GET_STORABLE( chunk, max_size ) );
|
||||
|
||||
return true;
|
||||
|
@ -245,11 +247,11 @@ bool Config::parseOrValidate( const char * option, const OptionType type,
|
|||
SKIP_ON_VALIDATION;
|
||||
REQUIRE_VALUE;
|
||||
|
||||
if ( sscanf( optionValue, "%zu %n", &uint32Value, &n ) == 1
|
||||
if ( sscanf( optionValue, "%u %n", &uint32Value, &n ) == 1
|
||||
&& !optionValue[ n ] )
|
||||
{
|
||||
SET_STORABLE( bundle, max_payload_size, uint32Value );
|
||||
dPrintf( "storable[bundle][max_payload_size] = %zu\n",
|
||||
dPrintf( "storable[bundle][max_payload_size] = %u\n",
|
||||
GET_STORABLE( bundle, max_payload_size ) );
|
||||
|
||||
return true;
|
||||
|
@ -263,7 +265,7 @@ bool Config::parseOrValidate( const char * option, const OptionType type,
|
|||
REQUIRE_VALUE;
|
||||
|
||||
if ( PARSE_OR_VALIDATE(
|
||||
sscanf( optionValue, "%zu %n", &uint32Value, &n ) != 1 ||
|
||||
sscanf( optionValue, "%u %n", &uint32Value, &n ) != 1 ||
|
||||
optionValue[ n ] || uint32Value > 19,
|
||||
GET_STORABLE( lzma, compression_level ) > 19 )
|
||||
)
|
||||
|
@ -271,7 +273,7 @@ bool Config::parseOrValidate( const char * option, const OptionType type,
|
|||
|
||||
SKIP_ON_VALIDATION;
|
||||
SET_STORABLE( lzma, compression_level, uint32Value );
|
||||
dPrintf( "storable[lzma][compression_level] = %zu\n",
|
||||
dPrintf( "storable[lzma][compression_level] = %u\n",
|
||||
GET_STORABLE( lzma, compression_level ) );
|
||||
|
||||
return true;
|
||||
|
@ -465,14 +467,14 @@ void Config::showHelp( const OptionType type )
|
|||
"show this message\n"
|
||||
"", ( type == Runtime ? "runtime" : ( type == Storable ? "storable" : "" ) ) );
|
||||
|
||||
for ( u_int i = 0; ConfigHelper::keywords[ i ].name; i++ )
|
||||
for ( u_int i = 0; !keywords[ i ].name.empty(); i++ )
|
||||
{
|
||||
if ( ConfigHelper::keywords[ i ].type != type )
|
||||
if ( keywords[ i ].type != type )
|
||||
continue;
|
||||
|
||||
fprintf( stderr, "\n== %s ==\n", ConfigHelper::keywords[ i ].name );
|
||||
fprintf( stderr, ConfigHelper::keywords[ i ].description,
|
||||
ConfigHelper::keywords[ i ].defaultValue.c_str() );
|
||||
fprintf( stderr, "\n== %s ==\n", keywords[ i ].name.c_str() );
|
||||
fprintf( stderr, keywords[ i ].description.c_str(),
|
||||
keywords[ i ].defaultValue.c_str() );
|
||||
fprintf( stderr, "\n" );
|
||||
}
|
||||
}
|
||||
|
|
20
config.hh
20
config.hh
|
@ -67,14 +67,14 @@ public:
|
|||
|
||||
static bool parseProto( const string &, google::protobuf::Message * );
|
||||
|
||||
static void showHelp( const OptionType );
|
||||
|
||||
static string toString( google::protobuf::Message const & );
|
||||
|
||||
// Print configuration to screen
|
||||
static void show( const ConfigInfo & );
|
||||
void show();
|
||||
|
||||
void showHelp( const OptionType );
|
||||
|
||||
OpCodes parseToken( const char *, const OptionType );
|
||||
bool parseOrValidate( const char *, const OptionType, bool validate = false );
|
||||
|
||||
|
@ -88,7 +88,21 @@ public:
|
|||
RuntimeConfig runtime;
|
||||
ConfigInfo * storable;
|
||||
private:
|
||||
bool want_cleanup;
|
||||
struct Keyword
|
||||
{
|
||||
string name;
|
||||
Config::OpCodes opcode;
|
||||
Config::OptionType type;
|
||||
string description;
|
||||
string defaultValue;
|
||||
};
|
||||
|
||||
Keyword * keywords;
|
||||
|
||||
bool cleanup_storable;
|
||||
bool cleanup_keywords;
|
||||
|
||||
void prefillKeywords();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -418,7 +418,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if ( strcmp( option, "help" ) == 0 )
|
||||
{
|
||||
Config::showHelp( optionType );
|
||||
config.showHelp( optionType );
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue