diff --git a/include/drive.h b/include/drive.h index 071b08a..b766f5c 100644 --- a/include/drive.h +++ b/include/drive.h @@ -14,7 +14,7 @@ class Drive { public: - enum TaskState + enum class TaskState { NONE, SHRED_SELECTED, @@ -23,15 +23,15 @@ public: DELETE_SELECTED, DELETE_ACTIVE, FROZEN - } state; + }; - enum ConnectionType + enum class ConnectionType { UNKNOWN, USB, SATA, NVME - } connectionType; + }; struct { @@ -41,6 +41,9 @@ public: unsigned long ulSpeedMetricBytesWritten; } sShredSpeed; + std::atomic state; + std::atomic connectionType; + bool bWasShredded = false; // all shred iterations done bool bWasShredStarted = false; // shred was atleast once started bool bWasChecked = false; // all shred iterations and optional checking done diff --git a/include/reHDD.h b/include/reHDD.h index ee71711..01da1fd 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -56,6 +56,7 @@ #include #include #include +#include #include "drive.h" #include "smart.h" #include "shred.h" diff --git a/include/tui.h b/include/tui.h index d5585d5..7c170ac 100644 --- a/include/tui.h +++ b/include/tui.h @@ -67,7 +67,7 @@ private: static void centerTitle(WINDOW *pwin, const char *title); static WINDOW *createOverViewWindow(int iXSize, int iYSize); - static WINDOW *createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive drive); + 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, int iListIndex, std::string sModelFamily, std::string sSerial, std::string sCapacity, std::string sState, std::string sTime, std::string sSpeed, std::string sTemp, std::string sConnection, bool bSelected); static WINDOW *createSystemStats(int iXSize, int iYSize, int iXStart, int iYStart); @@ -77,7 +77,7 @@ private: static WINDOW *createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature); static WINDOW *createZeroChecksumWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, uint32_t u32Checksum); - void displaySelectedDrive(Drive drive, int stdscrX, int stdscrY); + void displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY); std::string formatTimeDuration(time_t u32Duration); std::string formatSpeed(time_t u32ShredTimeDelta, unsigned long ulWrittenBytes); static void vTruncateText(std::string *psText, uint16_t u16MaxLenght); diff --git a/src/drive.cpp b/src/drive.cpp index 8a05b0e..71af4da 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -205,6 +205,6 @@ void Drive::checkFrozenDrive(void) Logger::logThis()->warning("Drive Frozen: " + this->getModelName() + " " + this->getSerial()); this->bWasDeleted = false; this->bWasShredded = false; - this->state = Drive::FROZEN; + this->state = Drive::TaskState::FROZEN; } } \ No newline at end of file diff --git a/src/printer.cpp b/src/printer.cpp index 6bd043f..c69433c 100644 --- a/src/printer.cpp +++ b/src/printer.cpp @@ -7,7 +7,6 @@ #include "../include/reHDD.h" - bool Printer::instanceFlag = false; Printer *Printer::single = NULL; @@ -57,16 +56,16 @@ void Printer::print(Drive *drive) switch (drive->connectionType) { - case Drive::USB: + case Drive::ConnectionType::USB: strncpy(msgQueueData.driveData.caDriveConnectionType, "usb", STR_BUFFER_SIZE); break; - case Drive::SATA: + case Drive::ConnectionType::SATA: strncpy(msgQueueData.driveData.caDriveConnectionType, "sata", STR_BUFFER_SIZE); break; - case Drive::NVME: + case Drive::ConnectionType::NVME: strncpy(msgQueueData.driveData.caDriveConnectionType, "nvme", STR_BUFFER_SIZE); break; - case Drive::UNKNOWN: + case Drive::ConnectionType::UNKNOWN: default: strncpy(msgQueueData.driveData.caDriveConnectionType, "na", STR_BUFFER_SIZE); } diff --git a/src/reHDD.cpp b/src/reHDD.cpp index 2156da9..abd5ab1 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -133,7 +133,7 @@ void reHDD::ThreadCheckFrozenDrives() mxDrives.lock(); for (auto it = begin(listDrives); it != end(listDrives); ++it) { - if (it->state == Drive::SHRED_ACTIVE) + if (it->state == Drive::TaskState::SHRED_ACTIVE) { it->checkFrozenDrive(); } @@ -175,9 +175,9 @@ void reHDD::ThreadUserInput() if (tmpSelectedDrive != nullptr) { - if (tmpSelectedDrive->state == Drive::NONE) + if (tmpSelectedDrive->state == Drive::TaskState::NONE) { - tmpSelectedDrive->state = Drive::DELETE_SELECTED; + tmpSelectedDrive->state = Drive::TaskState::DELETE_SELECTED; } } @@ -187,9 +187,9 @@ void reHDD::ThreadUserInput() // cout << "Shred" << endl; if (tmpSelectedDrive != nullptr) { - if (tmpSelectedDrive->state == Drive::NONE) + if (tmpSelectedDrive->state == Drive::TaskState::NONE) { - tmpSelectedDrive->state = Drive::SHRED_SELECTED; + tmpSelectedDrive->state = Drive::TaskState::SHRED_SELECTED; } } ui->updateTUI(&listDrives, u16SelectedEntry); @@ -302,7 +302,7 @@ void reHDD::filterNewDrives(list *plistOldDrives, list *plistNewDr { // cout << "offline drive found: " << itOld->getPath() << endl; Logger::logThis()->warning("Mark offline drive found: " + itOld->getPath()); - itOld->state = Drive::NONE; // clear state --> shred task will terminate + itOld->state = Drive::TaskState::NONE; // clear state --> shred task will terminate } } @@ -340,26 +340,26 @@ void reHDD::searchDrives(std::list *plistDrives) continue; Drive tmpDrive("/dev/" + devName); - tmpDrive.state = Drive::NONE; + tmpDrive.state = Drive::TaskState::NONE; tmpDrive.bIsOffline = false; // Set connection type if (transport == "sata") - tmpDrive.connectionType = Drive::SATA; + tmpDrive.connectionType = Drive::ConnectionType::SATA; else if (transport == "usb") - tmpDrive.connectionType = Drive::USB; + tmpDrive.connectionType = Drive::ConnectionType::USB; else if (transport == "nvme") - tmpDrive.connectionType = Drive::NVME; + tmpDrive.connectionType = Drive::ConnectionType::NVME; else - tmpDrive.connectionType = Drive::UNKNOWN; + tmpDrive.connectionType = Drive::ConnectionType::UNKNOWN; plistDrives->push_back(tmpDrive); Logger::logThis()->info( "Drive found: " + tmpDrive.getPath() + " (type: " + - (tmpDrive.connectionType == Drive::USB ? "USB" : tmpDrive.connectionType == Drive::SATA ? "SATA" - : tmpDrive.connectionType == Drive::NVME ? "NVME" + (tmpDrive.connectionType == Drive::ConnectionType::USB ? "USB" : tmpDrive.connectionType == Drive::ConnectionType::SATA ? "SATA" + : tmpDrive.connectionType == Drive::ConnectionType::NVME ? "NVME" : "UNKNOWN") + ")"); } @@ -478,7 +478,7 @@ void reHDD::startShredAllDrives(list *plistDrives) mxDrives.lock(); for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { - if (it->state == Drive::NONE) + if (it->state == Drive::TaskState::NONE) { Drive *pTmpDrive = iterator_to_pointer::iterator>(it); #ifdef LOG_LEVEL_HIGH @@ -505,9 +505,9 @@ void reHDD::stopShredAllDrives(list *plistDrives) for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { - if (it->state == Drive::SHRED_ACTIVE || it->state == Drive::DELETE_ACTIVE) + if (it->state == Drive::TaskState::SHRED_ACTIVE || it->state == Drive::TaskState::DELETE_ACTIVE) { - it->state = Drive::NONE; + it->state = Drive::TaskState::NONE; Logger::logThis()->info("Abort-Shred-Signal for: " + it->getModelName() + "-" + it->getSerial()); // task for drive is running --> remove selection } @@ -566,7 +566,7 @@ void reHDD::updateShredMetrics(list *plistDrives) list::iterator it; for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { - if (it->state == Drive::SHRED_ACTIVE) + if (it->state == Drive::TaskState::SHRED_ACTIVE) { Drive *pTmpDrive = iterator_to_pointer::iterator>(it); // set metrics for calculating shred speed @@ -675,9 +675,9 @@ void reHDD::handleAbort() Drive *tmpSelectedDrive = getSelectedDrive(); if (tmpSelectedDrive != nullptr) { - if (tmpSelectedDrive->state == Drive::SHRED_ACTIVE || tmpSelectedDrive->state == Drive::DELETE_ACTIVE) + if (tmpSelectedDrive->state == Drive::TaskState::SHRED_ACTIVE || tmpSelectedDrive->state == Drive::TaskState::DELETE_ACTIVE) { - tmpSelectedDrive->state = Drive::NONE; + tmpSelectedDrive->state = Drive::TaskState::NONE; Logger::logThis()->info("Abort-Shred-Signal for: " + tmpSelectedDrive->getModelName() + "-" + tmpSelectedDrive->getSerial()); // task for drive is running --> remove selection } diff --git a/src/shred.cpp b/src/shred.cpp index 5be6ce4..85d4184 100644 --- a/src/shred.cpp +++ b/src/shred.cpp @@ -42,7 +42,7 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd) #ifdef DRYRUN for (int i = 0; i <= 500; i++) { - if (drive->state != Drive::SHRED_ACTIVE) + if (drive->state.load() != Drive::TaskState::SHRED_ACTIVE) { return 0; } @@ -202,9 +202,9 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd) cleanup(); - if ((drive->state == Drive::SHRED_ACTIVE) || (drive->state == Drive::CHECK_ACTIVE)) + if ((drive->state.load() == Drive::TaskState::SHRED_ACTIVE) || (drive->state.load() == Drive::TaskState::CHECK_ACTIVE)) { - drive->state = Drive::NONE; + drive->state = Drive::TaskState::NONE; drive->setTaskPercentage(0.0); Printer::getPrinter()->print(drive); Logger::logThis()->info("Finished shred/check for: " + drive->getModelName() + "-" + drive->getSerial()); diff --git a/src/tui.cpp b/src/tui.cpp index 4ec6efa..cebc317 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -122,7 +122,7 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) switch (it->state) { - case Drive::SHRED_ACTIVE: + case Drive::TaskState::SHRED_ACTIVE: stream << fixed << setprecision(3) << (it->getTaskPercentage()); sState = "Shredding: " + stream.str() + "%"; @@ -131,21 +131,21 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) sTime = this->formatTimeDuration(it->getTaskDuration()); sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes); break; - case Drive::CHECK_ACTIVE: + case Drive::TaskState::CHECK_ACTIVE: stream << fixed << setprecision(3) << (it->getTaskPercentage()); sState = "Checking: " + stream.str() + "%"; it->calculateTaskDuration(); sTime = this->formatTimeDuration(it->getTaskDuration()); sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes); break; - case Drive::DELETE_ACTIVE: + case Drive::TaskState::DELETE_ACTIVE: sState = "Deleting ..."; it->calculateTaskDuration(); sTime = this->formatTimeDuration(it->getTaskDuration()); break; - case Drive::NONE: - case Drive::SHRED_SELECTED: - case Drive::DELETE_SELECTED: + case Drive::TaskState::NONE: + case Drive::TaskState::SHRED_SELECTED: + case Drive::TaskState::DELETE_SELECTED: if (it->bWasDeleted) { sState = "DELETED"; // mark drive as deleted previously @@ -174,7 +174,7 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) #endif break; - case Drive::FROZEN: + case Drive::TaskState::FROZEN: stream << fixed << setprecision(3) << (it->getTaskPercentage()); #ifdef FROZEN_ALERT if (bSelectedEntry) @@ -290,7 +290,7 @@ WINDOW *TUI::createOverViewWindow(int iXSize, int iYSize) return newWindow; } -WINDOW *TUI::createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive drive) +WINDOW *TUI::createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive &drive) { WINDOW *newWindow; newWindow = newwin(iYSize, iXSize, 1, iXStart); @@ -633,7 +633,7 @@ void TUI::vTruncateText(string *psText, uint16_t u16MaxLenght) } } -void TUI::displaySelectedDrive(Drive drive, int stdscrX, int stdscrY) +void TUI::displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY) { struct MenuState menustate; static bool dialogIsActive; @@ -646,27 +646,27 @@ void TUI::displaySelectedDrive(Drive drive, int stdscrX, int stdscrY) // set menustate based on drive state switch (drive.state) { - case Drive::NONE: // no task running or selected for this drive + case Drive::TaskState::NONE: // no task running or selected for this drive menustate.bShred = true; menustate.bDelete = true; break; - case Drive::DELETE_ACTIVE: // delete task running for this drive + case Drive::TaskState::DELETE_ACTIVE: // delete task running for this drive menustate.bAbort = true; break; - case Drive::SHRED_ACTIVE: // shred task running for this drive + case Drive::TaskState::SHRED_ACTIVE: // shred task running for this drive menustate.bAbort = true; break; - case Drive::CHECK_ACTIVE: // check task running for this drive + case Drive::TaskState::CHECK_ACTIVE: // check task running for this drive menustate.bAbort = true; break; - case Drive::DELETE_SELECTED: // delete task selected for this drive + case Drive::TaskState::DELETE_SELECTED: // delete task selected for this drive menustate.bConfirmDelete = true; break; - case Drive::SHRED_SELECTED: // shred task selected for this drive + case Drive::TaskState::SHRED_SELECTED: // shred task selected for this drive menustate.bConfirmShred = true; break; default: