From 48bbad914f839144caa15c9de5feb3857375eb32 Mon Sep 17 00:00:00 2001 From: localhorst Date: Wed, 11 May 2022 23:45:05 +0200 Subject: [PATCH] display used time to shred drive --- include/drive.h | 4 ++++ include/reHDD.h | 2 +- include/tui.h | 4 +++- src/drive.cpp | 19 +++++++++++++++---- src/reHDD.cpp | 2 ++ src/shred/shred.cpp | 3 ++- src/tui.cpp | 29 ++++++++++++++++++++++++++--- 7 files changed, 53 insertions(+), 10 deletions(-) diff --git a/include/drive.h b/include/drive.h index e3862f7..88f527c 100644 --- a/include/drive.h +++ b/include/drive.h @@ -37,6 +37,7 @@ private: uint32_t u32PowerCycles = 0U; time_t u32Timestamp = 0U; //unix timestamp for detecting a frozen drive double d32TaskPercentage = 0U; //in percent for Shred (1 to 100) + time_t u32TimestampTaskStart = 0U; //unix timestamp for duration of an action private: void setTimestamp(); @@ -75,6 +76,9 @@ public: void setTaskPercentage(double d32TaskPercentage); double getTaskPercentage(void); + void setActionStartTimestamp(); + time_t getActionStartTimestamp(); + }; #endif // DRIVE_H_ \ No newline at end of file diff --git a/include/reHDD.h b/include/reHDD.h index 755ce7a..b213dc9 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -29,7 +29,7 @@ #endif // Logic -//#define DRYRUN //don´t touch the drives +#define DRYRUN //don´t touch the drives #define FROZEN_ALERT //show alert if drive is frozen //IPC pipes diff --git a/include/tui.h b/include/tui.h index 8eb9038..bb4573f 100644 --- a/include/tui.h +++ b/include/tui.h @@ -56,7 +56,7 @@ private: static WINDOW *createOverViewWindow( int iXSize, int iYSize); static WINDOW *createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive drive); static WINDOW *overwriteDetailViewWindow( int iXSize, int iYSize, int iXStart); - static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, string sState, bool bSelected); + static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, string sState, string sTime, bool bSelected); static WINDOW *createSystemStats(int iXSize, int iYSize, int iXStart, int iYStart); static WINDOW *createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate); static WINDOW *createDialog(int iXSize, int iYSize, int iXStart, int iYStart, string selectedTask, string optionA, string optionB); @@ -65,5 +65,7 @@ private: void displaySelectedDrive(Drive drive, int stdscrX, int stdscrY); + string calculateTimeDelta(time_t start, time_t end); + }; #endif // TUI_H_ \ No newline at end of file diff --git a/src/drive.cpp b/src/drive.cpp index 87dd50a..2579c4d 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -52,10 +52,11 @@ string Drive::sCapacityToText() double dSize = (double) getCapacity(); uint16_t u16UnitIndex = 0; const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; - while (dSize >= 1000) { //using the marketing capacity - dSize /= 1000; - u16UnitIndex++; - } + while (dSize >= 1000) //using the marketing capacity + { + dSize /= 1000; + u16UnitIndex++; + } sprintf(acBuffer, "%.*f %s", u16UnitIndex-3, dSize, units[u16UnitIndex]); return acBuffer; @@ -139,6 +140,16 @@ void Drive::setTimestamp() time(&this->u32Timestamp); } +void Drive::setActionStartTimestamp() +{ + time(&this->u32TimestampTaskStart); +} + +time_t Drive::getActionStartTimestamp() +{ + return this->u32TimestampTaskStart; +} + void Drive::checkFrozenDrive(void) { time_t u32localtime; diff --git a/src/reHDD.cpp b/src/reHDD.cpp index 380159c..3c05ddc 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -198,6 +198,7 @@ void reHDD::ThreadShred() { if (getSelectedDrive() != nullptr) { + getSelectedDrive()->setActionStartTimestamp(); //save timestamp at start of shredding Shred* pShredTask = new Shred(); //create new shred task pShredTask->shredDrive(getSelectedDrive(), &fdShredInformPipe[1]); //start new shred task delete pShredTask; //delete shred task @@ -209,6 +210,7 @@ void reHDD::ThreadDelete() { if (getSelectedDrive() != nullptr) { + getSelectedDrive()->setActionStartTimestamp(); //save timestamp at start of deleting Delete::deleteDrive(getSelectedDrive()); //blocking, no thread getSelectedDrive()->state = Drive::TaskState::NONE; //delete finished getSelectedDrive()->bWasDeleteted = true; diff --git a/src/shred/shred.cpp b/src/shred/shred.cpp index 7f9377b..1ace503 100644 --- a/src/shred/shred.cpp +++ b/src/shred/shred.cpp @@ -43,8 +43,9 @@ Shred::~Shred() */ void Shred::shredDrive(Drive* drive, int* ipSignalFd) { + #ifdef DRYRUN - for(int i = 0; i<=100; i++) + for(int i = 0; i<=500; i++) { if(drive->state != Drive::SHRED_ACTIVE) { diff --git a/src/tui.cpp b/src/tui.cpp index d171801..e989ceb 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -77,6 +77,8 @@ void TUI::updateTUI(list * plistDrives, uint8_t u8SelectedEntry) string sModelName = it->getModelName(); string sCapacity = it->sCapacityToText(); string sState = " "; + string sTime = " "; + bool bSelectedEntry = false; @@ -94,6 +96,7 @@ void TUI::updateTUI(list * plistDrives, uint8_t u8SelectedEntry) } stringstream stream; + time_t u32localtime; switch (it->state) { @@ -101,10 +104,14 @@ void TUI::updateTUI(list * plistDrives, uint8_t u8SelectedEntry) stream << fixed << setprecision(2) << (it->getTaskPercentage()); sState = "Shredding: " + stream.str() + "%"; - break; + time(&u32localtime); + sTime = this->calculateTimeDelta(it->getActionStartTimestamp(), u32localtime); + break; case Drive::DELETE_ACTIVE: sState = "Deleting ..."; + time(&u32localtime); + sTime = this->calculateTimeDelta(it->getActionStartTimestamp(), u32localtime); break; case Drive::NONE: @@ -134,7 +141,7 @@ void TUI::updateTUI(list * plistDrives, uint8_t u8SelectedEntry) break; } - WINDOW * tmp = createEntryWindow( ((int)(u16StdscrX/3) - 2), 5, 3, (5* (u8Index) )+3, sModelFamily, sModelName, sCapacity, sState, bSelectedEntry); + WINDOW * tmp = createEntryWindow( ((int)(u16StdscrX/3) - 2), 5, 3, (5* (u8Index) )+3, sModelFamily, sModelName, sCapacity, sState, sTime, bSelectedEntry); wrefresh(tmp); u8Index++; }//end loop though drives @@ -288,7 +295,7 @@ WINDOW* TUI::overwriteDetailViewWindow( int iXSize, int iYSize, int iXStart) return newWindow; } -WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, string sState, bool bSelected) +WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, string sState, string sTime, bool bSelected) { WINDOW *newWindow; newWindow = newwin(iYSize, iXSize, iYStart, iXStart); @@ -313,6 +320,7 @@ WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, mvwaddstr(newWindow,3, 1, sCapacity.c_str()); mvwaddstr(newWindow,2, iXSize-sState.length()-5, sState.c_str()); + mvwaddstr(newWindow,3, iXSize-sState.length()-5, sTime.c_str()); return newWindow; } @@ -439,6 +447,21 @@ WINDOW* TUI::createFrozenWarning(int iXSize, int iYSize, int iXStart, int iYStar return newWindow; } +string TUI::calculateTimeDelta(time_t start, time_t end) +{ + std::ostringstream out; + + int hr=(int)((end - start)/3600); + int min=((int)((end - start)/60))%60; + int sec=(int)((end - start)%60); + + char s[25]; + sprintf(s, "%02d:%02d:%02d", hr, min, sec); + out << s; + + return out.str(); +} + void TUI::displaySelectedDrive(Drive drive, int stdscrX, int stdscrY) { struct MenuState menustate;