reHDD/src/shred.cpp

58 lines
1.5 KiB
C++

/**
* @file shred.cpp
* @brief shred drive
* @author hendrik schutter
* @date 03.05.2020
*/
#include "../include/reHDD.h"
/**
* \brief shred drive with shred
* \param pointer of Drive instance
* \return void
*/
void Shred::shredDrive(Drive* drive, int* fdInformPipeWrite)
{
size_t len = 0; //lenght of found line
char* cLine = NULL; //found line
#ifndef DRYRUN
string sCMD = ("shred -v ");
sCMD.append(drive->getPath());
#endif
#ifdef DRYRUN
//cout << "dryrun for " << drive->getPath() << endl;
string sCMD = ("bash shred_dummy.sh");
#endif
const char* cpComand = sCMD.c_str();
//cout << "shred: " << cpComand << endl;
FILE* shredCmdOutput = popen(cpComand, "r");
while ((getline(&cLine, &len, shredCmdOutput)) != -1)
{
if(drive->state == Drive::SHRED_ACTIVE)
{
string sLine = string(cLine);
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);
}
}
else
{
return; //end shredding task
}
}
fclose(shredCmdOutput);
}