From e4b3923f6dcf9148e785e2ec1d6075987c9fa1ee Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 22 Aug 2020 21:48:34 +0200 Subject: [PATCH] parse percentage from shred cmd --- include/reHDD.h | 2 +- include/shred.h | 25 +++++++++++++++++ include/wipe.h | 25 ----------------- shred_dummy.sh | 3 ++- src/reHDD.cpp | 16 ++++++----- src/shred.cpp | 57 +++++++++++++++++++++++++++++++++++++++ src/wipe.cpp | 72 ------------------------------------------------- 7 files changed, 94 insertions(+), 106 deletions(-) create mode 100644 include/shred.h delete mode 100644 include/wipe.h create mode 100644 src/shred.cpp delete mode 100644 src/wipe.cpp diff --git a/include/reHDD.h b/include/reHDD.h index e527eee..67277fc 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -36,7 +36,7 @@ using namespace std; #include "drive.h" #include "smart.h" -#include "wipe.h" +#include "shred.h" #include "tui.h" diff --git a/include/shred.h b/include/shred.h new file mode 100644 index 0000000..6c25649 --- /dev/null +++ b/include/shred.h @@ -0,0 +1,25 @@ +/** + * @file shred.h + * @brief shred drive + * @author hendrik schutter + * @date 03.05.2020 + */ + +#ifndef SHRED_H_ +#define SHRED_H_ + +#include "reHDD.h" + +class Shred +{ +protected: + +public: + static void shredDrive(Drive* drive); + +private: + Shred(void); + +}; + +#endif // SHRED_H_ \ No newline at end of file diff --git a/include/wipe.h b/include/wipe.h deleted file mode 100644 index 21c8d5f..0000000 --- a/include/wipe.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @file wipe.h - * @brief wipe drive - * @author hendrik schutter - * @date 03.05.2020 - */ - -#ifndef WIPE_H_ -#define WIPE_H_ - -#include "reHDD.h" - -class Wipe -{ -protected: - -public: - static void wipeDrive(Drive* drive); - -private: - Wipe(void); - -}; - -#endif // WIPE_H_ \ No newline at end of file diff --git a/shred_dummy.sh b/shred_dummy.sh index 0a3eea2..bbdfed9 100644 --- a/shred_dummy.sh +++ b/shred_dummy.sh @@ -2,9 +2,10 @@ echo "starting SHRED DUMMY" -for i in {1..101..3} +for i in {0..100..10} do echo "DUMMY shred $i%" + sleep 1 done echo "finished SHRED DUMMY" diff --git a/src/reHDD.cpp b/src/reHDD.cpp index ee4454f..35783cf 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -9,7 +9,7 @@ static int fdSearchDrives[2];//File descriptor for pipe that informs if new drives are found -static int fdWhipe[2];//File descriptor for pipe that informs if a wipe thread signals +static int fdShred[2];//File descriptor for pipe that informs if a wipe thread signals static std::mutex mxScannDrives; @@ -48,11 +48,11 @@ void reHDD::app_logic(void) ui->initTUI(); pipe(fdSearchDrives); - pipe(fdWhipe); + pipe(fdShred); FD_ZERO(&selectSet); FD_SET(fdSearchDrives[0], &selectSet); - FD_SET(fdWhipe[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 @@ -73,11 +73,11 @@ void reHDD::app_logic(void) ui->updateTUI(&vecDrives, i32SelectedEntry); } - else if (FD_ISSET(fdWhipe[0], &selectSet)) + else if (FD_ISSET(fdShred[0], &selectSet)) { - cout << "Whipe signal" << endl; - //update percantage & state - //update ui + cout << "shred signal" << endl; + //TODO update percantage & state + //TODO update ui } } //endless loop thDevices.join(); @@ -377,7 +377,9 @@ 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)); } if(SELECTED_DRIVE.state == Drive::TaskState::DELETE_SELECTED) diff --git a/src/shred.cpp b/src/shred.cpp new file mode 100644 index 0000000..646199b --- /dev/null +++ b/src/shred.cpp @@ -0,0 +1,57 @@ +/** + * @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) + +{ + 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 = ("ping ::1 -c 5"); + 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) + { + 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; + } + + + } + fclose(shredCmdOutput); +} diff --git a/src/wipe.cpp b/src/wipe.cpp deleted file mode 100644 index d37e5b0..0000000 --- a/src/wipe.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @file wipe.cpp - * @brief wipe drive - * @author hendrik schutter - * @date 03.05.2020 - */ - -#include "../include/reHDD.h" - -//#define DRYRUN //if defined the drive will not be wiped, a ping cmd is attempted instead - -/** - * \brief wipe drive with shred - * \param pointer of Drive instance - * \return void - */ -void Wipe::wipeDrive(Drive* drive) -{ - 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 = ("ping ::1 -c 5"); -#endif - const char* cpComand = sCMD.c_str(); - - cout << "wipe: " << cpComand << endl; - - auto t_start = chrono::high_resolution_clock::now(); - - FILE* outputfileSmart = popen(cpComand, "r"); - - while ((getline(&cLine, &len, outputfileSmart)) != -1) - { - string sLine = string(cLine); - cout << sLine; - } - fclose(outputfileSmart); - - auto t_end = chrono::high_resolution_clock::now(); - - uint64_t u64TimeMS = chrono::duration(t_end-t_start).count(); - - uint64_t u64hours = 0U; - uint64_t u64minutes = 0U; - uint64_t u64seconds = 0U; - - while(u64TimeMS >= 1000) - { - u64seconds++; - u64TimeMS = u64TimeMS - 1000; - } - - while(u64seconds >= 60) - { - u64minutes++; - u64seconds = u64seconds - 60; - } - - while(u64minutes >= 60) - { - u64hours++; - u64minutes = u64minutes - 60; - } - - cout << "Elapsed time: " << u64hours << " h - " << u64minutes << " min - " << u64seconds << " sec" << endl; -}