refactoring and minor changes

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

View File

@@ -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(){