reHDD/src/shred.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2020-08-22 21:48:34 +02:00
/**
* @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
*/
2020-08-23 09:26:32 +02:00
void Shred::shredDrive(Drive* drive, int* fdInformPipeWrite)
2020-08-22 21:48:34 +02:00
{
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
2020-08-23 09:26:32 +02:00
//cout << "dryrun for " << drive->getPath() << endl;
2020-08-22 21:48:34 +02:00
string sCMD = ("bash shred_dummy.sh");
#endif
const char* cpComand = sCMD.c_str();
2020-08-23 09:26:32 +02:00
//cout << "shred: " << cpComand << endl;
2020-08-22 21:48:34 +02:00
FILE* shredCmdOutput = popen(cpComand, "r");
while ((getline(&cLine, &len, shredCmdOutput)) != -1)
{
if(drive->state == Drive::SHRED_ACTIVE)
2020-08-23 09:26:32 +02:00
{
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
2020-08-23 09:26:32 +02:00
}
2020-08-22 21:48:34 +02:00
}
fclose(shredCmdOutput);
}