shred thread
This commit is contained in:
@ -7,9 +7,9 @@
|
||||
|
||||
#include "../include/reHDD.h"
|
||||
|
||||
static int fdSearchDrives[2];//File descriptor for pipe that informs if new drives are found
|
||||
static int fdNewDrivesInformPipe[2];//File descriptor for pipe that informs if new drives are found
|
||||
|
||||
static int fdShred[2];//File descriptor for pipe that informs if a wipe thread signals
|
||||
static int fdShredInformPipe[2];//File descriptor for pipe that informs if a wipe thread signals
|
||||
|
||||
static std::mutex mxScannDrives;
|
||||
|
||||
@ -47,38 +47,42 @@ void reHDD::app_logic(void)
|
||||
ui = new TUI();
|
||||
ui->initTUI();
|
||||
|
||||
pipe(fdSearchDrives);
|
||||
pipe(fdShred);
|
||||
pipe(fdNewDrivesInformPipe);
|
||||
pipe(fdShredInformPipe);
|
||||
|
||||
|
||||
FD_ZERO(&selectSet);
|
||||
FD_SET(fdSearchDrives[0], &selectSet);
|
||||
FD_SET(fdShred[0], &selectSet);
|
||||
|
||||
thread thDevices(ThreadScannDevices); //start thread that scanns for drives
|
||||
thread thUserInput(ThreadUserInput); //start thread that reads user input
|
||||
|
||||
while(1)
|
||||
{
|
||||
FD_ZERO(&selectSet);
|
||||
FD_SET(fdNewDrivesInformPipe[0], &selectSet);
|
||||
FD_SET(fdShredInformPipe[0], &selectSet);
|
||||
|
||||
select(FD_SETSIZE, &selectSet, NULL, NULL, NULL);
|
||||
|
||||
if( FD_ISSET(fdSearchDrives[0], &selectSet))
|
||||
if( FD_ISSET(fdNewDrivesInformPipe[0], &selectSet))
|
||||
{
|
||||
char dummy;
|
||||
read (fdSearchDrives[0],&dummy,1);
|
||||
read (fdNewDrivesInformPipe[0],&dummy,1);
|
||||
mxScannDrives.lock();
|
||||
|
||||
filterNewDrives(&vecDrives, &vecNewDrives); //filter and copy to app logic vector
|
||||
mxScannDrives.unlock();
|
||||
//TODO update UI
|
||||
}
|
||||
|
||||
ui->updateTUI(&vecDrives, i32SelectedEntry);
|
||||
}
|
||||
else if (FD_ISSET(fdShred[0], &selectSet))
|
||||
if (FD_ISSET(fdShredInformPipe[0], &selectSet))
|
||||
{
|
||||
cout << "shred signal" << endl;
|
||||
//TODO update percantage & state
|
||||
//TODO update ui
|
||||
char dummy;
|
||||
read (fdShredInformPipe[0],&dummy,1);
|
||||
|
||||
checkShredComplete(&vecDrives);
|
||||
}
|
||||
|
||||
ui->updateTUI(&vecDrives, i32SelectedEntry);
|
||||
|
||||
} //endless loop
|
||||
thDevices.join();
|
||||
thUserInput.join();
|
||||
@ -95,7 +99,7 @@ void reHDD::ThreadScannDevices()
|
||||
filterIgnoredDrives(&vecNewDrives); //filter out ignored drives
|
||||
addSMARTData(&vecNewDrives); //add S.M.A.R.T. Data to the drives
|
||||
mxScannDrives.unlock();
|
||||
write (fdSearchDrives[1], "A",1);
|
||||
write(fdNewDrivesInformPipe[1], "A",1);
|
||||
sleep(5); //sleep 5 sec
|
||||
}
|
||||
}
|
||||
@ -157,6 +161,11 @@ void reHDD::ThreadUserInput()
|
||||
}
|
||||
}
|
||||
|
||||
void reHDD::ThreadShred()
|
||||
{
|
||||
Shred::shredDrive(&vecDrives.at(i32SelectedEntry), &fdShredInformPipe[1]);
|
||||
}
|
||||
|
||||
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives)
|
||||
{
|
||||
|
||||
@ -171,9 +180,11 @@ void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecN
|
||||
if(itOld->getSerial() == itNew->getSerial())
|
||||
{
|
||||
bOldDriveIsOffline = false;
|
||||
// cout << "already online drive found: " << itOld->getPath() << endl;
|
||||
itNew->state = itOld->state;
|
||||
itNew->setTaskPercentage(itOld->getTaskPercentage());
|
||||
// copy old drive instance date in new instance
|
||||
itNew->state = itOld->state; //copy state
|
||||
itNew->setTaskPercentage(itOld->getTaskPercentage()); //copy percentage
|
||||
itNew->bWasDeleteted = itOld->bWasDeleteted; //copy finished task delete
|
||||
itNew->bWasShredded = itOld->bWasShredded; //copy finished task shred
|
||||
}
|
||||
}
|
||||
|
||||
@ -377,9 +388,7 @@ void reHDD::handleEnter()
|
||||
{
|
||||
SELECTED_DRIVE.state = Drive::TaskState::SHRED_ACTIVE;
|
||||
//task for drive is running --> don´t show more task options
|
||||
|
||||
//TODO start shredding
|
||||
Shred::shredDrive(&vecDrives.at(i32SelectedEntry));
|
||||
thread(ThreadShred).detach();
|
||||
}
|
||||
|
||||
if(SELECTED_DRIVE.state == Drive::TaskState::DELETE_SELECTED)
|
||||
@ -413,4 +422,19 @@ void reHDD::handleAbort()
|
||||
SELECTED_DRIVE.state = Drive::NONE;
|
||||
//task for drive is running --> remove selection
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reHDD::checkShredComplete(vector <Drive>* pvecDrives)
|
||||
{
|
||||
vector <Drive>::iterator it;
|
||||
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
|
||||
{
|
||||
if(it->getTaskPercentage() == 100 )
|
||||
{
|
||||
it->bWasShredded = true; //mark this drive as shredded
|
||||
it->setTaskPercentage(0); //reset for an other shredding
|
||||
it->state = Drive::NONE; //reset for an other task#
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
* \param pointer of Drive instance
|
||||
* \return void
|
||||
*/
|
||||
void Shred::shredDrive(Drive* drive)
|
||||
void Shred::shredDrive(Drive* drive, int* fdInformPipeWrite)
|
||||
|
||||
{
|
||||
size_t len = 0; //lenght of found line
|
||||
@ -24,34 +24,27 @@ void Shred::shredDrive(Drive* drive)
|
||||
#endif
|
||||
|
||||
#ifdef DRYRUN
|
||||
cout << "dryrun for " << drive->getPath() << endl;
|
||||
// string sCMD = ("ping ::1 -c 5");
|
||||
//cout << "dryrun for " << drive->getPath() << endl;
|
||||
string sCMD = ("bash shred_dummy.sh");
|
||||
#endif
|
||||
|
||||
const char* cpComand = sCMD.c_str();
|
||||
cout << "shred: " << cpComand << endl;
|
||||
//cout << "shred: " << cpComand << endl;
|
||||
|
||||
FILE* shredCmdOutput = popen(cpComand, "r");
|
||||
|
||||
while ((getline(&cLine, &len, shredCmdOutput)) != -1)
|
||||
{
|
||||
string sLine = string(cLine);
|
||||
|
||||
|
||||
// TODO parse percentage
|
||||
|
||||
string search("%");
|
||||
size_t found = sLine.find(search);
|
||||
if (found!=string::npos){
|
||||
uint8_t percent = 0U;
|
||||
sLine.erase(0, sLine.find("%")-2);
|
||||
sLine.erase(std::remove(sLine.begin(), sLine.end(), '\n'), sLine.end());
|
||||
int i = std::stoi(sLine);
|
||||
cout << i << " percent" << endl;
|
||||
}
|
||||
|
||||
|
||||
string search("%");
|
||||
size_t found = sLine.find(search);
|
||||
if (found!=string::npos)
|
||||
{
|
||||
sLine.erase(0, sLine.find("%")-3);
|
||||
sLine.erase(std::remove(sLine.begin(), sLine.end(), '\n'), sLine.end());
|
||||
drive->setTaskPercentage(stoi(sLine));
|
||||
write(*fdInformPipeWrite, "A",1);
|
||||
}
|
||||
}
|
||||
fclose(shredCmdOutput);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
* @date 03.08.2020
|
||||
*/
|
||||
|
||||
#include "../../include/reHDD.h"
|
||||
#include "../include/reHDD.h"
|
||||
|
||||
|
||||
TUI::TUI(void)
|
||||
@ -78,11 +78,39 @@ void TUI::updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry)
|
||||
|
||||
if(i32SelectedEntry == (it - pvecDrives->begin()))
|
||||
{
|
||||
bSelectedEntry = true; //mark this drive in entries list
|
||||
bSelectedEntry = true; //mark this drive in entries list
|
||||
displaySelectedDrive(pvecDrives->at(i32SelectedEntry), stdscrX, stdscrY);
|
||||
}
|
||||
|
||||
WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity, bSelectedEntry);
|
||||
string sState = " ";
|
||||
|
||||
switch (it->state)
|
||||
{
|
||||
case Drive::SHRED_ACTIVE:
|
||||
sState = "Shredding: " + to_string(it->getTaskPercentage()) + "%";
|
||||
break;
|
||||
|
||||
case Drive::DELETE_ACTIVE:
|
||||
sState = "Deleting ...";
|
||||
break;
|
||||
|
||||
case Drive::NONE:
|
||||
case Drive::SHRED_SELECTED:
|
||||
case Drive::DELETE_SELECTED:
|
||||
if (it->bWasDeleteted)
|
||||
{
|
||||
sState = "DELETED"; //mark drive as deleted previously
|
||||
}
|
||||
if (it->bWasShredded)
|
||||
{
|
||||
sState = "SHREDDED"; //mark drive as shreded previously, overwrite if deleted
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity, sState, bSelectedEntry);
|
||||
wrefresh(tmp);
|
||||
}
|
||||
}
|
||||
@ -212,7 +240,7 @@ WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive
|
||||
return newWindow;
|
||||
}
|
||||
|
||||
WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, bool bSelected)
|
||||
WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, string sState, bool bSelected)
|
||||
{
|
||||
WINDOW *newWindow;
|
||||
newWindow = newwin(iYSize, iXSize, iYStart, iXStart);
|
||||
@ -236,6 +264,8 @@ WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart,
|
||||
mvwaddstr(newWindow,2, 1, sModelName.c_str());
|
||||
mvwaddstr(newWindow,3, 1, sCapacity.c_str());
|
||||
|
||||
mvwaddstr(newWindow,2, iXSize-sState.length()-5, sState.c_str());
|
||||
|
||||
keypad(newWindow, TRUE);
|
||||
|
||||
return newWindow;
|
||||
@ -345,9 +375,6 @@ void TUI::displaySelectedDrive(Drive drive, int stdscrX, int stdscrY)
|
||||
case Drive::SHRED_SELECTED : //shred task selected for this drive
|
||||
menustate.bConfirmShred = true;
|
||||
break;
|
||||
case Drive::DELETE_FINISHED : //delete task finished for this drive
|
||||
case Drive::SHRED_FINISHED : //shred task finished for this drive
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
Reference in New Issue
Block a user