diff --git a/update/agent/include/agent/linux/.datasid-agent.h.un~ b/update/agent/include/agent/linux/.datasid-agent.h.un~ deleted file mode 100644 index beef159e5d139bc9212337c2702eb57d9438d0c0..0000000000000000000000000000000000000000 Binary files a/update/agent/include/agent/linux/.datasid-agent.h.un~ and /dev/null differ diff --git a/update/agent/include/agent/linux/.datasid-common.h.un~ b/update/agent/include/agent/linux/.datasid-common.h.un~ deleted file mode 100644 index b1b377e888c141e0431a9148c0605a41aeda87c4..0000000000000000000000000000000000000000 Binary files a/update/agent/include/agent/linux/.datasid-common.h.un~ and /dev/null differ diff --git a/update/agent/include/agent/linux/datasid-agent.h b/update/agent/include/agent/linux/datasid-agent.h index 359b22382d9b2a9b4d5dc647f4b689a6964f1332..9c6674dbd80b21dbb4c29661e47aa7542e11f0db 100644 --- a/update/agent/include/agent/linux/datasid-agent.h +++ b/update/agent/include/agent/linux/datasid-agent.h @@ -1,5 +1,3 @@ -#ifndef __agent__ -#define __agent__ /* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR * @@ -20,29 +18,31 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ +#pragma once #include <iostream> -#include <cstring> class Agent{ private: std::string confDir; + std::string defaultConf; std::string logFile; std::string prefix; std::string version; public: void setConfDir(std::string); + void setDefaultConf(std::string); void setLogFile(std::string, std::string); void setPrefix(std::string, int); void setVersion(std::string); std::string getConfDir() const; + std::string getDefaultConf() const; std::string getLogFile() const; std::string getPrefix() const; std::string getVersion() const; -}; + }; -std::string cutString( std::string str, int tot ); +std::string getAbsolutePath( std::string str, int tot); int datasid_agent(); -bool prefixIsSet( std::string prefix ); -#endif +bool prefixIsSet(std::string prefix); diff --git a/update/agent/include/agent/linux/datasid-common.h b/update/agent/include/agent/linux/datasid-common.h index ef0848462e6ce2cda0eef5745b05d981eacba826..9093bf9508e63dc8f45c072050773a1639dd69a4 100644 --- a/update/agent/include/agent/linux/datasid-common.h +++ b/update/agent/include/agent/linux/datasid-common.h @@ -1,20 +1,29 @@ -#ifndef __common__ -#define __common__ -#include "datasid-agent.h" +/* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + * + * This file is part of datasid + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ +#pragma once #include <sys/stat.h> +#include "datasid-agent.h" #include "datasid-conf.h" #include "datasid-proxy.h" -class Common{ - private: - std::string defaultConf; - - public: - void setDefaultConf(std::string, std::string); - - std::string getDefaultConf() const; - }; - bool fileExist(const char * file); -bool datasid_common(Conf* conf, Proxy* proxy, Agent* agent, Common* Common); -#endif +bool datasid_common(Conf* conf, Proxy* proxy, Agent* agent); + diff --git a/update/agent/include/agent/linux/datasid-conf.h b/update/agent/include/agent/linux/datasid-conf.h index b163d4fd1cae493ed9198dc9d9e0767b5d27a541..7327e9c16188be9335ca2ba015f7a75e0432b0da 100644 --- a/update/agent/include/agent/linux/datasid-conf.h +++ b/update/agent/include/agent/linux/datasid-conf.h @@ -1,5 +1,3 @@ -#ifndef __conf__ -#define __conf__ /* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR * @@ -20,14 +18,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ -#include <iostream> -#include <cstring> -#include <cstdlib> -#include <locale> -#include <sstream> +#pragma once #include <map> #include "datasid-agent.h" -#include "datasid-conf.h" #include "datasid-parse.h" class Conf{ @@ -66,5 +59,4 @@ class Conf{ }; bool setConf(Conf* conf, Agent* agent, std::map<std::string, std::string>* content); -void datasid_conf(Agent* agent, Conf* conf); -#endif \ No newline at end of file +void datasid_conf(Agent* agent, Conf* conf); \ No newline at end of file diff --git a/update/agent/include/agent/linux/datasid-parse.h b/update/agent/include/agent/linux/datasid-parse.h index 6e695e4c60afd330493a460c9e39547b23a6ca59..b5888e5a2c5c096b301784ee4f3ef0d05d359a93 100644 --- a/update/agent/include/agent/linux/datasid-parse.h +++ b/update/agent/include/agent/linux/datasid-parse.h @@ -1,5 +1,3 @@ -#ifndef __parse__ -#define __parse__ /* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR * @@ -20,17 +18,17 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ -#include <iostream> -#include <fstream> +#pragma once #include <cstring> #include <cstdlib> +#include <fstream> +#include <iostream> #include <locale> #include <map> -bool isvalid(std::string * line); +bool isValid(std::string * line); void normalize(std::string * str); void parse(std::string * line, std::map<std::string, std::string> * content); -bool readfile(const std::string file, std::map<std::string, std::string> * content); -bool spaceonly(const std::string * line); -void trim(std::string& str); -#endif \ No newline at end of file +bool readFile(const std::string file, std::map<std::string, std::string> * content); +bool spaceOnly(const std::string * line); +void trim(std::string& str); \ No newline at end of file diff --git a/update/agent/include/agent/linux/datasid-proxy.h b/update/agent/include/agent/linux/datasid-proxy.h index 3bfe4cc8d1d7fb878010957c0769885392f33c30..32f00bc7b595f96086fd5a962fa606aecb4c03fe 100644 --- a/update/agent/include/agent/linux/datasid-proxy.h +++ b/update/agent/include/agent/linux/datasid-proxy.h @@ -1,5 +1,3 @@ -#ifndef __proxy__ -#define __proxy__ /* Copyright (C) 2004-2016 Centro de Computacao Cientifica e Software Livre * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR * @@ -20,13 +18,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ -#include <locale> -#include <sstream> +#pragma once #include <map> #include "datasid-agent.h" #include "datasid-conf.h" #include "datasid-parse.h" -#include "datasid-proxy.h" class Proxy{ private: @@ -45,8 +41,7 @@ class Proxy{ std::string getPort() const; std::string getUser() const; std::string getPassword() const; -}; + }; bool setProxy(Proxy* proxy, std::map<std::string, std::string>* content); void datasid_proxy(Conf* conf, Proxy* proxy); -#endif \ No newline at end of file diff --git a/update/agent/src/linux/.datasid-agent.cpp.un~ b/update/agent/src/linux/.datasid-agent.cpp.un~ deleted file mode 100644 index 49032e34321482cf2da2dc8b4df62a3bafe9481c..0000000000000000000000000000000000000000 Binary files a/update/agent/src/linux/.datasid-agent.cpp.un~ and /dev/null differ diff --git a/update/agent/src/linux/datasid-agent.cpp b/update/agent/src/linux/datasid-agent.cpp index 2fb5ae599491d2e48429d3bac1a206e6bb55eea1..3d71b94e46648b64ba3377598ed24b433b5ff951 100644 --- a/update/agent/src/linux/datasid-agent.cpp +++ b/update/agent/src/linux/datasid-agent.cpp @@ -29,12 +29,16 @@ void Agent::setConfDir(std::string dir) { this->confDir = this->prefix + dir; } +void Agent::setDefaultConf(std::string defaultConf) { + this->defaultConf = this->prefix + defaultConf; +} + void Agent::setLogFile(std::string logDir, std::string file) { this->logFile = logDir + file; } void Agent::setPrefix(std::string p, int t) { - this->prefix = cutString(p, t); + this->prefix = getAbsolutePath(p, t); } void Agent::setVersion(std::string v) { @@ -49,6 +53,10 @@ std::string Agent::getConfDir() const { return this->confDir; } +std::string Agent::getDefaultConf() const { + return this->defaultConf; +} + std::string Agent::getLogFile() const { return this->logFile; } @@ -62,11 +70,11 @@ std::string Agent::getVersion() const { } /* - * cutString Function: + * getAbsolutePath Function: * get the absolute path of the agent */ -std::string cutString(std::string str, int tot) { +std::string getAbsolutePath(std::string str, int tot) { int i = str.size(), count = 0; while (i-->-1) if (str[i] == '/') @@ -75,6 +83,11 @@ std::string cutString(std::string str, int tot) { return str.substr(0, i); } +/* + * prefixIsSet Function + * Check if the absolute path of agent was setted. + */ + bool prefixIsSet(std::string prefix) { if (prefix.size() == 0) throw " ERROR: Prefix not set. "; @@ -85,13 +98,15 @@ int datasid_agent() { Agent agent; Conf conf; Proxy proxy; - Common common; /* Set agent prefix */ agent.setPrefix(__FILE__, 3); + /* Set confif file */ + agent.setDefaultConf("/conf/datasid-conf.json"); + /* Run datasid-common.cpp */ - if (!datasid_common(&conf, &proxy, &agent, &common)) + if (!datasid_common(&conf, &proxy, &agent)) exit(1); return 1; diff --git a/update/agent/src/linux/datasid-common.cpp b/update/agent/src/linux/datasid-common.cpp index 52a1bb61c7d723a5fb1d3eeef6fd0d53d20df96c..bf89b0856e90b11f4f7f312057a3af3468ff7180 100644 --- a/update/agent/src/linux/datasid-common.cpp +++ b/update/agent/src/linux/datasid-common.cpp @@ -18,27 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ -#include "../../include/agent/linux/datasid-conf.h" -#include "../../include/agent/linux/datasid-proxy.h" -#include "../../include/agent/linux/datasid-agent.h" #include "../../include/agent/linux/datasid-common.h" -#include "../../include/agent/main.h" - -/* - * set Functions - */ - -void Common::setDefaultConf(std::string defaultConf, std::string prefix) { - this->defaultConf = prefix + defaultConf; -} - -/* - * get Functions - */ - -std::string Common::getDefaultConf() const { - return this->defaultConf; -} /* * fileExist Function @@ -56,7 +36,7 @@ bool fileExist(const std::string& file) { } -bool datasid_common(Conf* conf, Proxy* proxy, Agent* agent, Common* common) { +bool datasid_common(Conf* conf, Proxy* proxy, Agent* agent) { try { prefixIsSet(agent->getPrefix()); } catch (const char* msg) { @@ -66,13 +46,11 @@ bool datasid_common(Conf* conf, Proxy* proxy, Agent* agent, Common* common) { /* Read config file and declare only valid variables */ - common->setDefaultConf("/conf/datasid-conf.json", agent->getPrefix()); - if (!fileExist(common->getDefaultConf())) { + if (!fileExist(agent->getDefaultConf())) { std::cout << __DATE__ << "- ERROR: Config file not found. " << std::endl; return false; - } else { + } else datasid_conf(agent, conf); - } /* Read proxy file and declare only valid variables */ diff --git a/update/agent/src/linux/datasid-conf.cpp b/update/agent/src/linux/datasid-conf.cpp index 9a40ad0c3d8edf3e7c88828f3936964d4fe0a7d7..4dac53e9cdea0c773577681502aff4104e55fbbd 100644 --- a/update/agent/src/linux/datasid-conf.cpp +++ b/update/agent/src/linux/datasid-conf.cpp @@ -18,10 +18,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ +#include <iostream> +#include <cstring> +#include <cstdlib> +#include <locale> +#include <sstream> #include "../../include/agent/linux/datasid-conf.h" -#include "../../include/agent/linux/datasid-agent.h" -#include "../../include/agent/main.h" -#include "../../include/agent/linux/datasid-parse.h" /* * set Functions */ @@ -140,15 +142,14 @@ bool setConf(Conf* conf, Agent* agent, std::map<std::string, std::string>* conte void datasid_conf(Agent* agent, Conf* conf) { std::map<std::string, std::string> content; - std::string confFile = agent->getPrefix() + "/conf/datasid-conf.json"; - if (readfile(confFile, &content)) { + if (readFile(agent->getDefaultConf(), &content)) if (!setConf(conf, agent, &content)) { std::cout << __DATE__ << " ERROR: to parse variables " << "from config file." << std::endl; exit(1); } - } else { + else { std::cout << __DATE__ << " ERROR: config file not found." << std::endl; exit(1); } diff --git a/update/agent/src/linux/datasid-parse.cpp b/update/agent/src/linux/datasid-parse.cpp index f16c57f6f098b2f178a631512c3e5d8e61b15a51..f58bfc3d8784cbc4e7ee668b476cda52d7b0133e 100644 --- a/update/agent/src/linux/datasid-parse.cpp +++ b/update/agent/src/linux/datasid-parse.cpp @@ -18,8 +18,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ +#include <regex> #include "../../include/agent/linux/datasid-parse.h" + /* + * trim Function * Erases leading tabs, leading spaces; trailing tabs, trailing spaces. */ @@ -43,6 +46,7 @@ void trim(std::string* str) { } /* + * normalize Function * Erases tabs and spaces between the variable's name and its value. */ @@ -70,10 +74,11 @@ void normalize(std::string* str) { } /* + * spaceOnly Functio * Check if a line consists only of spaces and tabs */ -bool spaceonly(const std::string* line) { +bool spaceOnly(const std::string* line) { for (int i = 0, j = line->length(); i < j; ++i) if ((*line)[i] != ' ' && (*line)[i] != '\t') return false; @@ -81,42 +86,79 @@ bool spaceonly(const std::string* line) { } /* - * Check if a line is valid + * isValid Function + * Check if a line is valid + * The line is valide when has "key":"value"; */ -bool isvalid(std::string* line) { +bool isValid(std::string* line) { + std::string e = *line; normalize(line); int i = 0; - if ((*line)[i] == '[') { - int j = line->find_last_of(']'); + /* if the line is a comment */ + + if (std::regex_match((*line), std::regex("((\\/\\/)|(\\/\\*)|(\\#))(.*)"))) + return false; + + /* if the line start with a different character than quote */ + + if (std::regex_match((*line), std::regex("((\\=)|(\\])|(\\[)|(\\{)|(\\})|(\\:))(.*)"))) + return false; + + /* + * if the line start with quote, + * but is not a pattern : "key":"value" + */ - if (j == std::string::npos) + if ((*line)[i] = '"') { + + int n = std::count((*line).begin(), (*line).end(), '"'); + + /* if the line doesn't has four quotes */ + if (n < 4) return false; - if (j - i == 1) + /* if the second character is quote. */ + + if ((*line)[i+1] == '"') return false; - } else if ((*line)[i] == ';' || (*line)[i] == '#' || - ((*line)[i] == '/' && (*line)[i + 1] == '/')) { - return false; - } else if ((*line)[i] == '=' || (*line)[i] == ']' || - (*line)[i] == '{' || (*line)[i] == '}') { - return false; - } else if ((*line)[i] = '"') { - int j = line->find_last_of(':'); - if (j == i || j == -1) + + n = line->find_last_of('"'); + + /* if the second to last character is quote. */ + + if ((*line)[n-1] == '"') return false; - if (j == line->length()) + n = line->find_last_of(':'); + + /* if the line doesn't has the colon character. */ + + if (n == -1) + return false; + + /* if the position of the colon character is string ends. */ + + if (n == (line->length())-1) return false; + } return true; } +/* + * parse Function + * After check if the line is valid, + * than parse the "key":"value" to a map. + */ + void parse(std::string* line, std::map<std::string, std::string>* content) { std::locale loc; - // if the line is a variable + value + + /* if the line is a "key":"value" */ + if ((*line)[0] == '"') { std::string name = line->substr(1, line->find(':')-2); for (int i = 0; i < name.length(); i++) @@ -129,7 +171,16 @@ void parse(std::string* line, std::map<std::string, std::string>* content) { } } -bool readfile(const std::string file, std::map<std::string, std::string>* content) { +/* + * readFile Function + * read a file (config or proxy file) + * Than check if the line is valid. + * Case is valid, parse the "key":"value" to a map. + * After all, get de value by key and "initialize" + * a conf or proxy object. + */ + +bool readFile(const std::string file, std::map<std::string, std::string>* content) { content->clear(); std::ifstream config(file.c_str(), std::ifstream::in); @@ -140,8 +191,8 @@ bool readfile(const std::string file, std::map<std::string, std::string>* conten std::string buffer; while (getline(config, buffer, '\n')) { - if (!spaceonly(&buffer)) - if (isvalid(&buffer)) + if (!spaceOnly(&buffer)) + if (isValid(&buffer)) parse(&buffer, content); } diff --git a/update/agent/src/linux/datasid-proxy.cpp b/update/agent/src/linux/datasid-proxy.cpp index 963531f6aa41ea9341418a92193acbe875c75df4..2c4d6f06bf5a1c1959d53f95217ec111a108000b 100644 --- a/update/agent/src/linux/datasid-proxy.cpp +++ b/update/agent/src/linux/datasid-proxy.cpp @@ -18,10 +18,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. */ -#include "../../include/agent/linux/datasid-conf.h" -#include "../../include/agent/linux/datasid-parse.h" +#include <iostream> #include "../../include/agent/linux/datasid-proxy.h" -#include "../../include/agent/linux/datasid-agent.h" /* * set Functions @@ -63,6 +61,11 @@ std::string Proxy::getPassword() const { return this->password; } +/* + * setProxy Function + * If variables from proxy file setted, + * then set the proxy object + */ bool setProxy(Proxy* proxy, std::map<std::string, std::string>* content) { if (content->count("PROXYHOST") == 0 || @@ -79,22 +82,15 @@ bool setProxy(Proxy* proxy, std::map<std::string, std::string>* content) { return true; } -/* - * I suppose there are a file "proxy" that contain - * host, port, user and password variables. - */ - void datasid_proxy(Conf* conf, Proxy* proxy) { std::map<std::string, std::string> content; - std::string proxyFile = conf->getProxyConf(); - if (readfile(proxyFile, &content)) { + if (readFile(conf->getProxyConf(), &content)) if (!setProxy(proxy, &content)) { std::cout << __DATE__ << " ERROR: to parse variables" << " from proxy file." << std::endl; exit(1); } - } else { + else std::cout << __DATE__ << " WARNING: proxy file not found." << std::endl; - } }