From e3aefb24eefbd5721cd6dc390ad5eb79bbbb3d91 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 20 Aug 2022 16:09:40 +0200 Subject: [PATCH] added feature to start shredding for all drives --- include/drive.h | 5 +++-- include/reHDD.h | 7 +++++-- include/shred.h | 2 +- include/tui.h | 2 +- src/reHDD.cpp | 44 +++++++++++++++++++++++++++++++++++--------- src/shred.cpp | 3 ++- src/tui.cpp | 13 ++++++++----- 7 files changed, 55 insertions(+), 21 deletions(-) diff --git a/include/drive.h b/include/drive.h index 6c76568..6d08433 100644 --- a/include/drive.h +++ b/include/drive.h @@ -22,12 +22,13 @@ public: FROZEN } state; - struct { + struct + { time_t u32ShredTimeDelta; std::chrono::time_point chronoShredTimestamp; unsigned long ulWrittenBytes; } sShredSpeed; - + bool bWasShredded = false; bool bWasDeleteted = false; bool bIsOffline = false; diff --git a/include/reHDD.h b/include/reHDD.h index 08d2ac4..d542b94 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -64,6 +64,8 @@ using namespace std; #include "tui.h" #include "logger/logger.h" +#include "chunk.h" + extern Logger* logging; template T* iterator_to_pointer(I i) @@ -83,12 +85,13 @@ private: static void searchDrives(list * plistDrives); static void printDrives(list * plistDrives); - static void filterIgnoredDrives(list * plistDrives); + static void startShredAllDrives(list * plistDrives); + static void filterIgnoredDrives(list * plistDrives); static void filterNewDrives(list * plistOldDrives, list * plistNewDrives); static void addSMARTData(list * plistDrives); static void ThreadScannDevices(); static void ThreadUserInput(); - static void ThreadShred(); + static void ThreadShred(Drive* const pDrive); static void ThreadDelete(); static void ThreadCheckFrozenDrives(); static void handleArrowKey(TUI::UserInput userInput); diff --git a/include/shred.h b/include/shred.h index a89457e..26f2154 100644 --- a/include/shred.h +++ b/include/shred.h @@ -22,7 +22,7 @@ //#define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB //#define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB -//#define DEMO_DRIVE_SIZE 1024*1024*1024*10L // 10GB +#define DEMO_DRIVE_SIZE 1024*1024*1024*10L // 10GB typedef int fileDescriptor; diff --git a/include/tui.h b/include/tui.h index 18ad654..d20e890 100644 --- a/include/tui.h +++ b/include/tui.h @@ -22,7 +22,7 @@ protected: public: - enum UserInput { UpKey, DownKey, Abort, Shred, Delete, Enter, ESC, Undefined}; + enum UserInput { UpKey, DownKey, Abort, Shred, ShredAll, Delete, Enter, ESC, Undefined}; struct MenuState { bool bAbort; diff --git a/src/reHDD.cpp b/src/reHDD.cpp index 3c05ddc..dc2dd19 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -178,6 +178,11 @@ void reHDD::ThreadUserInput() } ui->updateTUI(&listDrives, u8SelectedEntry); break; + case TUI::UserInput::ShredAll: + cout << "ShredAll" << endl; + startShredAllDrives(&listDrives); + ui->updateTUI(&listDrives, u8SelectedEntry); + break; case TUI::UserInput::Enter: //cout << "Enter" << endl; handleEnter(); @@ -194,13 +199,13 @@ void reHDD::ThreadUserInput() } } -void reHDD::ThreadShred() +void reHDD::ThreadShred(Drive* const pDrive) { - if (getSelectedDrive() != nullptr) + if (pDrive != nullptr) { - getSelectedDrive()->setActionStartTimestamp(); //save timestamp at start of shredding + pDrive->setActionStartTimestamp(); //save timestamp at start of shredding Shred* pShredTask = new Shred(); //create new shred task - pShredTask->shredDrive(getSelectedDrive(), &fdShredInformPipe[1]); //start new shred task + pShredTask->shredDrive(pDrive, &fdShredInformPipe[1]); //start new shred task delete pShredTask; //delete shred task ui->updateTUI(&listDrives, u8SelectedEntry); } @@ -279,7 +284,7 @@ void reHDD::filterNewDrives(list * plistOldDrives, list * plistNew for (itNew = plistNewDrives->begin(); itNew != plistNewDrives->end(); ++itNew) { plistOldDrives->push_back(*itNew); - Logger::logThis()->info("Add new drive: " + itNew->getModelName()); + //Logger::logThis()->info("Add new drive: " + itNew->getModelName()); } plistNewDrives->clear(); } @@ -291,7 +296,7 @@ void reHDD::filterNewDrives(list * plistOldDrives, list * plistNew */ void reHDD::searchDrives(list * plistDrives) { - Logger::logThis()->info("--> search drives <--"); + //Logger::logThis()->info("--> search drives <--"); char * cLine = NULL; size_t len = 0; @@ -329,7 +334,7 @@ void reHDD::filterIgnoredDrives(list * plistDrives) for(string sLine; getline( input, sLine );) { - Logger::logThis()->info("read uuid: " + sLine); + //Logger::logThis()->info("read uuid: " + sLine); vtlIgnoredDevices.emplace_back(sLine); //add found path and uuid from ignore file to vector } //loop through found entries in ingnore file @@ -377,6 +382,26 @@ void reHDD::filterIgnoredDrives(list * plistDrives) } } +/** + * \brief start shred for all drives + * \param pointer of list * plistDrives + * \return void + */ +void reHDD::startShredAllDrives(list * plistDrives) +{ + list ::iterator it; + for (it = plistDrives->begin(); it != plistDrives->end(); ++it) + { + if(it->state == Drive::NONE) + { + it->state = Drive::SHRED_ACTIVE; + Logger::logThis()->info("all start: " + it->getSerial()); + Drive* pTmpDrive = iterator_to_pointer::iterator > (it); + thread(ThreadShred, std::ref(pTmpDrive)).detach(); + } + } +} + /** * \brief print drives with all information * \param pointer of list * plistDrives @@ -454,7 +479,7 @@ void reHDD::handleArrowKey(TUI::UserInput userInput) break; } - Logger::logThis()->info("ArrowKey - selected drive: " + to_string(u8SelectedEntry)); + //Logger::logThis()->info("ArrowKey - selected drive: " + to_string(u8SelectedEntry)); } void reHDD::handleEnter() @@ -466,7 +491,8 @@ void reHDD::handleEnter() Logger::logThis()->info("Started shred for: " + getSelectedDrive()->getModelName() + "-" + getSelectedDrive()->getSerial()); getSelectedDrive()->state = Drive::TaskState::SHRED_ACTIVE; //task for drive is running --> donĀ“t show more task options - thread(ThreadShred).detach(); + Drive* pTmpDrive = getSelectedDrive(); + thread(ThreadShred, std::ref(pTmpDrive)).detach(); } if(getSelectedDrive()->state == Drive::TaskState::DELETE_SELECTED) diff --git a/src/shred.cpp b/src/shred.cpp index 8476e2d..258b526 100644 --- a/src/shred.cpp +++ b/src/shred.cpp @@ -26,6 +26,7 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd) { #ifdef DRYRUN + Logger::logThis()->info("Shred-Task started - Drive: " + drive->getSerial()); for(int i = 0; i<=500; i++) { if(drive->state != Drive::SHRED_ACTIVE) @@ -75,7 +76,7 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd) { unsigned long ulDriveByteCounter = 0U; //used for one shred-iteration to keep track of the current drive position uint32_t u32ChunkDimensionIndex = 0U; - + if(uiShredIterationCounter == (SHRED_ITERATIONS-1)) { diff --git a/src/tui.cpp b/src/tui.cpp index 2a02ee6..12a9432 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -97,7 +97,6 @@ void TUI::updateTUI(list * plistDrives, uint8_t u8SelectedEntry) stringstream stream; - switch (it->state) { case Drive::SHRED_ACTIVE: @@ -204,6 +203,9 @@ enum TUI::UserInput TUI::readUserInput() case 's': return TUI::UserInput::Shred; break; + case 'S': + return TUI::UserInput::ShredAll; + break; default: return TUI::UserInput::Undefined; break; @@ -386,19 +388,19 @@ WINDOW* TUI::createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, st if(menustate.bAbort) { - string sLineTmp = "Press A for Abort"; + string sLineTmp = "Press a for Abort"; mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLineTmp.size()/2), sLineTmp.c_str()); u16Line++; } if(menustate.bShred) { - string sLineTmp = "Press S for Shred "; + string sLineTmp = "Press s for Shred (S for all drives)"; mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLineTmp.size()/2), sLineTmp.c_str()); u16Line++; } if(menustate.bDelete) { - string sLineTmp = "Press D for Delete"; + string sLineTmp = "Press d for Delete"; mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLineTmp.size()/2), sLineTmp.c_str()); } @@ -508,7 +510,8 @@ string TUI::formatTimeDuration(time_t u32Duration) return out.str(); } -string TUI::formatSpeed(time_t u32ShredTimeDelta, unsigned long ulWrittenBytes){ +string TUI::formatSpeed(time_t u32ShredTimeDelta, unsigned long ulWrittenBytes) +{ std::ostringstream out; double dDeltaSec = ((double)((u32ShredTimeDelta)/1000000000.0)); //convert nano in sec double speed = ((ulWrittenBytes/1000000.0)/dDeltaSec);