refactoring and minor changes

This commit is contained in:
Guillermo Roche 2020-05-15 21:39:23 +02:00
parent 726d990d20
commit 9f7dbd6cf7
5 changed files with 65 additions and 4 deletions

View File

@ -10,6 +10,8 @@ public:
data_acces(); data_acces();
virtual std::string get_passwd(std::string username) = 0; virtual std::string get_passwd(std::string username) = 0;
virtual std::list<std::string> get_pinfo()=0; virtual std::list<std::string> 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 #endif // DATA_ACCES_H

View File

@ -37,3 +37,18 @@ std::list<std::string> msql_acces::get_pinfo(){
delete res; delete res;
return ret; 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();
}

View File

@ -14,6 +14,8 @@ public:
msql_acces(); msql_acces();
std::string get_passwd(std::string username); std::string get_passwd(std::string username);
std::list<std::string> get_pinfo(); std::list<std::string> get_pinfo();
void write_install(std::string package, std::string user);
void write_remove(std::string);
private: private:
sql::Connection *con; sql::Connection *con;
sql::Driver *driver; sql::Driver *driver;

View File

@ -20,6 +20,7 @@ bool session_manager::validate_pass(){
std::string pass=buffer; std::string pass=buffer;
if(this->data->get_passwd(user)==pass){ if(this->data->get_passwd(user)==pass){
this->write_data("pass"); this->write_data("pass");
this->user=user;
return true; return true;
}else{ }else{
this->write_data("fail"); this->write_data("fail");
@ -35,6 +36,8 @@ void session_manager::start_dialog(){
this->execute(); this->execute();
}else if(strcmp(buffer, "info")==0){ }else if(strcmp(buffer, "info")==0){
this->send_information(); this->send_information();
}else if(strcmp(buffer, "remv")==0){
this->remove();
}else if(strcmp(buffer,"exit")){ }else if(strcmp(buffer,"exit")){
break; break;
} }
@ -44,14 +47,47 @@ void session_manager::start_dialog(){
int session_manager::execute(){ int session_manager::execute(){
char* n_package = new char[256]; char* n_package = new char[256];
this->read_data(n_package, 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=new char*[4];
this->args[0]="emerge"; this->args[0]="emerge";
this->args[1]="--ask"; this->args[1]=comand;
this->args[2]=n_package; this->args[2]=n_package;
this->args[3]=nullptr; this->args[3]=nullptr;
int pid = fork(); int pid = fork();
int status=-2; int status=-2;
std::string ret;
if(pid==0){ if(pid==0){
if(execvp(this->args[0],this->args)==-1){ if(execvp(this->args[0],this->args)==-1){
std::cout << "error inesperado" << std::endl; std::cout << "error inesperado" << std::endl;
@ -61,12 +97,15 @@ int session_manager::execute(){
waitpid(pid, &status, WCONTINUED); waitpid(pid, &status, WCONTINUED);
if(status>0){ if(status>0){
this->write_data("ok"); this->write_data("ok");
ret = n_package;
delete[] (n_package);
}else{ }else{
this->write_data("bad"); this->write_data("bad");
delete[] (n_package);
ret = "err";
} }
} }
delete[] (n_package); return ret;
return status;
} }
void session_manager::send_information(){ void session_manager::send_information(){

View File

@ -8,14 +8,17 @@ public:
session_manager(int fd); session_manager(int fd);
void start_dialog(); void start_dialog();
int execute(); int execute();
int remove();
void send_information(); void send_information();
bool validate_pass(); bool validate_pass();
private: private:
std::string appli_command(char comand[], char* n_package);
virtual int read_data(char* input, int size); virtual int read_data(char* input, int size);
virtual int write_data(std::string output); virtual int write_data(std::string output);
int fd; int fd;
data_acces* data; data_acces* data;
char** args; char** args;
std::string user;
}; };
#endif // LAUNCHER_H #endif // LAUNCHER_H