summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKostyantyn Ovechko <fastinetserver@gmail.com>2010-06-27 17:09:32 +0300
committerKostyantyn Ovechko <fastinetserver@gmail.com>2010-06-27 17:09:32 +0300
commit3240fb9f31850bbea870bc5d5adfe3b8a9061fac (patch)
treea4ce427087a23c6705cce2370ad7cefb867ce8ea
parentAdd explanation for options [mirrors].use_benchmark_stats and [mirrors].bench... (diff)
downloadidfetch-3240fb9f31850bbea870bc5d5adfe3b8a9061fac.tar.gz
idfetch-3240fb9f31850bbea870bc5d5adfe3b8a9061fac.tar.bz2
idfetch-3240fb9f31850bbea870bc5d5adfe3b8a9061fac.zip
Failprove config.cpp.
1) Catch exception if segget.conf can't be opened. 2) Catch exceptions if while reading segget.conf. 3) Check if settings suplied by segget.conf can be converted to appropriate type. 4) Check if settings fit allowed range from min_limit to max_limit. 5) In case there was an error apply default settings. 6) Log all described errors.
-rw-r--r--segget/config.cpp125
-rw-r--r--segget/segget.cpp63
-rw-r--r--segget/settings.cpp28
-rw-r--r--segget/settings.h2
4 files changed, 156 insertions, 62 deletions
diff --git a/segget/config.cpp b/segget/config.cpp
index 8dac6a4..0e93f41 100644
--- a/segget/config.cpp
+++ b/segget/config.cpp
@@ -16,7 +16,7 @@ public:
ConfigFile(std::string const& configFile);
int set(string &dst,string const& section, string const& entry) const;
- int set(uint &dst,string const& section, string const& entry) const;
+ int set(uint &dst,string const& section, string const& entry, uint const& min_limit, uint const& max_limit) const;
int set(bool &dst,string const& section, string const& entry) const;
};
@@ -38,37 +38,52 @@ string trim(std::string const& source, char const* delims = " \t\r\n") {
}
ConfigFile::ConfigFile(string const& configFile) {
- ifstream file(configFile.c_str());
- string line;
- string name;
- string value;
- string inSection;
- int posEqual;
- while (std::getline(file,line)) {
- if (! line.length()) continue;
- if (line[0] == '#') continue;
- if (line[0] == ';') continue;
-
- line=noupper(line);
- if (line[0] == '[') {
- inSection=trim(line.substr(1,line.find(']')-1));
- continue;
+ ifstream file;
+ file.exceptions (ifstream::failbit | ifstream::badbit);
+ try{
+ file.open(configFile.c_str());
+ }
+ catch(...){
+ error_log("Error opening settings file: "+configFile+". Default settings will be used. Check if config file exists and segget has rights to access it.");
+ return;
+ }
+ try{
+ //processing file
+ string line;
+ string name;
+ string value;
+ string inSection;
+ int posEqual;
+ while (not(file.eof())) {
+ getline(file,line);
+ if (! line.length()) continue;
+ if (line[0] == '#') continue;
+ if (line[0] == ';') continue;
+
+ line=noupper(line);
+ if (line[0] == '[') {
+ inSection=trim(line.substr(1,line.find(']')-1));
+ continue;
+ }
+
+ posEqual=line.find('=');
+ name = trim(line.substr(0,posEqual));
+ value = trim(line.substr(posEqual+1));
+
+ content_[inSection+'/'+name]=value;
}
-
- posEqual=line.find('=');
- name = trim(line.substr(0,posEqual));
- value = trim(line.substr(posEqual+1));
-
- content_[inSection+'/'+name]=value;
+ }
+ catch(...){
+ error_log("Settings file: "+configFile+" was opened, but an error occured while reading settings from it.");
}
}
int ConfigFile::set(string &dst, string const& section, string const& entry) const {
-
map<string,string>::const_iterator ci = content_.find(section + '/' + entry);
if (ci == content_.end()){
- log("segget.conf has no settings for "+entry+" in ["+section+"] section. Will be set to default:"+dst);
+ log("! Settings: ["+section+"]."+entry+" has not been set in segget.conf.");
+ log("! Settings: ["+section+"]."+entry+"="+dst+". Default value forced.");
return 1;
}
else{
@@ -77,31 +92,71 @@ int ConfigFile::set(string &dst, string const& section, string const& entry) con
return 0;
}
}
-int ConfigFile::set(uint &dst, string const& section, string const& entry) const {
-
+int ConfigFile::set(uint &dst, string const& section, string const& entry, uint const& min_limit, uint const& max_limit) const {
+ uint return_value;
map<string,string>::const_iterator ci = content_.find(section + '/' + entry);
if (ci == content_.end()){
- log("segget.conf has no settings for "+entry+" in ["+section+"] section. Will be set to default:"+toString(dst));
+ log("! Settings: ["+section+"]."+entry+" has not been set in segget.conf.");
+ log("! Settings: ["+section+"]."+entry+"="+toString(dst)+". Default value forced.");
return 1;
}
else{
- dst=atoi(ci->second.c_str());
- log("Settings: ["+section+"]."+entry+"="+toString(dst));
- return 0;
+ return_value=atoi(ci->second.c_str());
+ if (return_value==0)
+ if (toString(return_value)!=ci->second){
+ log("! Settings: ["+section+"]."+entry
+ +" must have an integer value in range from "+toString(min_limit)
+ +" to "+toString(max_limit)
+ +". Can't convert "+ci->second
+ +" to integer. ");
+ log("! Settings: ["+section+"]."+entry+"="+toString(dst)+". Default value forced.");
+ return 1;
+ }
+ if ((return_value>=min_limit) and (return_value<=max_limit)){
+ //everything is ok
+ log("Settings: ["+section+"]."+entry+"="+toString(return_value));
+ dst=return_value;
+ return 0;
+ }else{
+ log("! Settings: ["+section+"]."+entry
+ +" must have an integer value in range from "+toString(min_limit)
+ +" to "+toString(max_limit)+".");
+ log("! Settings: ["+section+"]."+entry+"="+toString(dst)+". Default value forced.");
+ return 1;
+ }
}
}
int ConfigFile::set(bool &dst, string const& section, string const& entry) const {
-
+ uint return_value;
map<std::string,string>::const_iterator ci = content_.find(section + '/' + entry);
if (ci == content_.end()){
- log("segget.conf has no settings for "+entry+" in ["+section+"] section. Will be set to default:"+toString(dst));
+ log("! Settings: ["+section+"]."+entry+" has not been set in segget.conf.");
+ log("! Settings: ["+section+"]."+entry+"="+toString(dst)+". Default value forced.");
return 1;
}
else{
- dst=atoi(ci->second.c_str());
- log("Settings: ["+section+"]."+entry+"="+toString(dst));
- return 0;
+ return_value=atoi(ci->second.c_str());
+ if (return_value==0)
+ if (toString(return_value)!=ci->second){
+ log("! Settings: ["+section+"]."+entry
+ +" must have a boolean value: 0 or 1"
+ +". Can't convert "+ci->second
+ +" to 0 or 1. ");
+ log("! Settings: ["+section+"]."+entry+"="+toString(dst)+". Default value forced.");
+ return 1;
+ }
+ if ((return_value==0) or (return_value==1)){
+ //everything is ok
+ log("Settings: ["+section+"]."+entry+"="+toString(return_value));
+ dst=return_value;
+ return 0;
+ }else{
+ log("! Settings: ["+section+"]."+entry
+ +" must have a boolean value: 0 or 1");
+ log("! Settings: ["+section+"]."+entry+"="+toString(dst)+". Default value forced.");
+ return 1;
+ }
}
} \ No newline at end of file
diff --git a/segget/segget.cpp b/segget/segget.cpp
index 492f7c1..1cfa71d 100644
--- a/segget/segget.cpp
+++ b/segget/segget.cpp
@@ -191,17 +191,56 @@ int download_pkgs(){
int main()
{
- //set_settings();
- prev_time=time((time_t *)NULL);
- initscr();
- curs_set(0);
- refresh();
- settings.load_from_conf_file();
- load_pkgs();
- //show_pkgs();
- stats.show_totals();
- download_pkgs();
- getch();
- endwin();
+ try{
+ prev_time=time((time_t *)NULL);
+ try{
+ //init curses
+ initscr();
+ curs_set(0);
+ refresh();
+ }
+ catch(...)
+ {
+ //error while init curses
+ }
+ try{
+ //load settings
+ settings.load_from_conf_file();
+ }
+ catch(...)
+ {
+ //error while loading settings
+ }
+ try{
+ load_pkgs();
+ }
+ catch(...){
+ //error while loading pkgs
+ }
+ try{
+ //show_pkgs();
+ stats.show_totals();
+ }
+ catch(...){
+ //error while showing stats
+ }
+ try{
+ download_pkgs();
+ }
+ catch(...){
+ //error while downloading_pkgs
+ }
+ getch();
+ }
+ catch(...){
+ //error during init and downloading process
+ }
+ try{
+ endwin();
+ }
+ catch(...)
+ {
+ //error while ending curses
+ }
return 0;
}
diff --git a/segget/settings.cpp b/segget/settings.cpp
index 5b246ca..613bcdb 100644
--- a/segget/settings.cpp
+++ b/segget/settings.cpp
@@ -13,28 +13,28 @@ void Tsettings::load_from_conf_file(){
conf.set(segments_dir, "folders", "segments_dir");
// log("segments_dir set to:"+segments_dir);
- conf.set(max_connection_num_per_distfile, "distfiles", "max_connection_num_per_distfile");
+ conf.set(max_connection_num_per_distfile, "distfiles", "max_connection_num_per_distfile",1,20);
conf.set(resume_on, "segments", "resume_on");
- conf.set(max_segment_size, "segments", "max_segment_size");
- conf.set(max_tries, "segments", "max_tries");
-
- conf.set(max_connections, "connections", "max_connections");
- conf.set(connection_timeout, "connections", "connection_timeout");
- conf.set(ftp_response_timeout, "connections", "ftp_response_timeout");
- conf.set(time_out, "connections", "timeout");
- conf.set(low_connection_speed_limit, "connections", "low_connection_speed_limit");
- conf.set(low_connection_speed_time, "connections", "low_connection_speed_time");
- conf.set(max_connection_speed, "connections", "max_connection_speed");
+ conf.set(max_segment_size, "segments", "max_segment_size",10000,10000000);
+ conf.set(max_tries, "segments", "max_tries",1,-1);
+
+ conf.set(max_connections, "connections", "max_connections",1,20);
+ conf.set(connection_timeout, "connections", "connection_timeout",1,1000);
+ conf.set(ftp_response_timeout, "connections", "ftp_response_timeout",1,-1);
+ conf.set(time_out, "connections", "timeout",100,-1);
+ conf.set(low_connection_speed_limit, "connections", "low_connection_speed_limit",0,-1);
+ conf.set(low_connection_speed_time, "connections", "low_connection_speed_time",1,600);
+ conf.set(max_connection_speed, "connections", "max_connection_speed",0,-1);
conf.set(bind_interface, "connections", "bind_interface");
- conf.set(max_connections_num_per_mirror, "mirrors", "max_connections_num_per_mirror");
- conf.set(benchmark_oblivion, "mirrors", "benchmark_oblivion");
+ conf.set(max_connections_num_per_mirror, "mirrors", "max_connections_num_per_mirror",1,10);
+ conf.set(benchmark_oblivion, "mirrors", "benchmark_oblivion",1,1000);
conf.set(user_agent, "user-data", "user_agent");
conf.set(proxy_ip_or_name, "proxy", "proxy_ip_or_name");
- conf.set(proxy_port, "proxy", "proxy_port");
+ conf.set(proxy_port, "proxy", "proxy_port",1,65535);
conf.set(proxy_off, "proxy", "proxy_off");
conf.set(proxy_user, "proxy", "proxy_user");
conf.set(proxy_password, "proxy", "proxy_password");
diff --git a/segget/settings.h b/segget/settings.h
index aac711c..c39e71b 100644
--- a/segget/settings.h
+++ b/segget/settings.h
@@ -36,7 +36,7 @@ class Tsettings{
//proxy
string proxy_ip_or_name;
uint proxy_port;
- uint proxy_off;
+ bool proxy_off;
string proxy_user;
string proxy_password;
//logs