From 9f7dbd6cf734d4f43b81580153e1c416c6345e26 Mon Sep 17 00:00:00 2001 From: groche97 Date: Fri, 15 May 2020 21:39:23 +0200 Subject: [PATCH] refactoring and minor changes --- data_acces.h | 2 ++ msql_acces.cpp | 15 +++++++++++++++ msql_acces.h | 2 ++ session_manager.cpp | 47 +++++++++++++++++++++++++++++++++++++++++---- session_manager.h | 3 +++ 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/data_acces.h b/data_acces.h index 9229605..4410a5f 100644 --- a/data_acces.h +++ b/data_acces.h @@ -10,6 +10,8 @@ public: data_acces(); virtual std::string get_passwd(std::string username) = 0; virtual std::list get_pinfo()=0; + virtual void write_install(std::string package, std::string user)=0; + virtual void write_remove(std::string)=0; }; #endif // DATA_ACCES_H diff --git a/msql_acces.cpp b/msql_acces.cpp index 1bb2ea9..61a537d 100644 --- a/msql_acces.cpp +++ b/msql_acces.cpp @@ -37,3 +37,18 @@ std::list msql_acces::get_pinfo(){ delete res; return ret; } + +void msql_acces::write_install(std::string package, std::string user){ + sql::PreparedStatement *pstmt = + con->prepareStatement("insert into packages(name,user) values(?,(select id from users where username=?))"); + pstmt->setString(1,package); + pstmt->setString(2,user); + pstmt->executeUpdate(); +} + +void msql_acces::write_remove(std::string package){ + sql::PreparedStatement *pstmt = + con->prepareStatement("delete from packages where name=?"); + pstmt->setString(1,package); + pstmt->executeUpdate(); +} diff --git a/msql_acces.h b/msql_acces.h index ec8ab9f..e264482 100644 --- a/msql_acces.h +++ b/msql_acces.h @@ -14,6 +14,8 @@ public: msql_acces(); std::string get_passwd(std::string username); std::list get_pinfo(); + void write_install(std::string package, std::string user); + void write_remove(std::string); private: sql::Connection *con; sql::Driver *driver; diff --git a/session_manager.cpp b/session_manager.cpp index c1317ec..bffeb04 100644 --- a/session_manager.cpp +++ b/session_manager.cpp @@ -20,6 +20,7 @@ bool session_manager::validate_pass(){ std::string pass=buffer; if(this->data->get_passwd(user)==pass){ this->write_data("pass"); + this->user=user; return true; }else{ this->write_data("fail"); @@ -35,6 +36,8 @@ void session_manager::start_dialog(){ this->execute(); }else if(strcmp(buffer, "info")==0){ this->send_information(); + }else if(strcmp(buffer, "remv")==0){ + this->remove(); }else if(strcmp(buffer,"exit")){ break; } @@ -44,14 +47,47 @@ void session_manager::start_dialog(){ int session_manager::execute(){ char* n_package = new char[256]; this->read_data(n_package, 256); - config_package conf = config_package(n_package); + char* use_conf=new char[256]; + this->read_data(use_conf,2); + if(strcmp(use_conf,"y")==0){ + config_package conf = config_package(n_package); + this->read_data(use_conf,256); + conf.change_uses(use_conf); + }else if(strcmp(use_conf,"n")!=0){ + perror("fail in protocol comunication"); + return -1; + } + delete [] (use_conf); + std::string result = this->appli_command("--ask", n_package); + if(result=="err"){ + return -1; + }else{ + this->data->write_install(n_package, user); + return 1; + } +} + +int session_manager::remove(){ + char* n_package = new char[256]; + this->read_data(n_package, 256); + std::string result = this->appli_command("--unmerge",n_package); + if(result=="err"){ + return -1; + }else{ + this->data->write_remove(n_package); + return 1; + } +} + +std::string session_manager::appli_command(char comand[], char* n_package){ this->args=new char*[4]; this->args[0]="emerge"; - this->args[1]="--ask"; + this->args[1]=comand; this->args[2]=n_package; this->args[3]=nullptr; int pid = fork(); int status=-2; + std::string ret; if(pid==0){ if(execvp(this->args[0],this->args)==-1){ std::cout << "error inesperado" << std::endl; @@ -61,12 +97,15 @@ int session_manager::execute(){ waitpid(pid, &status, WCONTINUED); if(status>0){ this->write_data("ok"); + ret = n_package; + delete[] (n_package); }else{ this->write_data("bad"); + delete[] (n_package); + ret = "err"; } } - delete[] (n_package); - return status; + return ret; } void session_manager::send_information(){ diff --git a/session_manager.h b/session_manager.h index 9bb4b57..c8dcdd8 100644 --- a/session_manager.h +++ b/session_manager.h @@ -8,14 +8,17 @@ public: session_manager(int fd); void start_dialog(); int execute(); + int remove(); void send_information(); bool validate_pass(); private: + std::string appli_command(char comand[], char* n_package); virtual int read_data(char* input, int size); virtual int write_data(std::string output); int fd; data_acces* data; char** args; + std::string user; }; #endif // LAUNCHER_H