From 0fc3bc222e2fb7ea68557e127e8f33088e7ec627 Mon Sep 17 00:00:00 2001 From: localhorst Date: Wed, 16 Sep 2020 15:13:32 +0200 Subject: [PATCH] changed vector to list --- include/reHDD.h | 14 +++---- include/tui.h | 2 +- src/reHDD.cpp | 109 +++++++++++++++++++++++++----------------------- src/tui.cpp | 16 +++---- 4 files changed, 73 insertions(+), 68 deletions(-) diff --git a/include/reHDD.h b/include/reHDD.h index f10f157..721c6bc 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -10,7 +10,7 @@ #define REHDD_VERSION "bV0.1.0" -//#define DRYRUN +#define DRYRUN // Drive handling Settings #define WORSE_HOURS 19200 //mark drive if at this limit or beyond @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include #include @@ -78,11 +78,11 @@ public: private: - static void searchDrives(vector * pvecDrives); - static void printDrives(vector * pvecDrives); - static void filterIgnoredDrives(vector * pvecDrives); - static void filterNewDrives(vector * pvecOldDrives, vector * pvecNewDrives); - static void addSMARTData(vector * pvecDrives); + static void searchDrives(list * plistDrives); + static void printDrives(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(); diff --git a/include/tui.h b/include/tui.h index 135ff0d..eed3d75 100644 --- a/include/tui.h +++ b/include/tui.h @@ -36,7 +36,7 @@ public: static void initTUI(); - void updateTUI(vector * pvecDrives, uint8_t u8SelectedEntry); + void updateTUI(list * plistDrives, uint8_t u8SelectedEntry); static enum UserInput readUserInput(); diff --git a/src/reHDD.cpp b/src/reHDD.cpp index e1f91bc..42b5390 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -13,9 +13,9 @@ static int fdShredInformPipe[2];//File descriptor for pipe that informs if a wip static std::mutex mxScannDrives; -vector vecNewDrives; //store found drives that are updated every 5sec +list listNewDrives; //store found drives that are updated every 5sec -static vector vecDrives; //stores all drive data from scann thread +static list listDrives; //stores all drive data from scann thread TUI *ui; @@ -33,8 +33,6 @@ static fd_set selectSet; reHDD::reHDD(void) { u8SelectedEntry = 0U; - - vecDrives.reserve(128); } /** @@ -66,8 +64,8 @@ void reHDD::app_logic(void) { char dummy; read (fdNewDrivesInformPipe[0],&dummy,1); - filterNewDrives(&vecDrives, &vecNewDrives); //filter and copy to app logic vector - printDrives(&vecDrives); + filterNewDrives(&listDrives, &listNewDrives); //filter and copy to app logic vector + printDrives(&listDrives); } @@ -76,7 +74,7 @@ void reHDD::app_logic(void) char dummy; read (fdShredInformPipe[0],&dummy,1); } - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); mxScannDrives.unlock(); } //endless loop thDevices.join(); @@ -86,9 +84,11 @@ void reHDD::app_logic(void) Drive* reHDD::getSelectedDrive() { - if(u8SelectedEntry < vecDrives.size() ) + if(u8SelectedEntry < listDrives.size() ) { - return &(vecDrives.at(u8SelectedEntry)); + list::iterator it = listDrives.begin(); + advance(it, u8SelectedEntry); + return &(*it); } else { @@ -102,10 +102,10 @@ void reHDD::ThreadScannDevices() while(true) { mxScannDrives.lock(); - vecNewDrives.clear(); - searchDrives(&vecNewDrives); //search for new drives and store them in list - filterIgnoredDrives(&vecNewDrives); //filter out ignored drives - addSMARTData(&vecNewDrives); //add S.M.A.R.T. Data to the drives + listNewDrives.clear(); + searchDrives(&listNewDrives); //search for new drives and store them in list + filterIgnoredDrives(&listNewDrives); //filter out ignored drives + addSMARTData(&listNewDrives); //add S.M.A.R.T. Data to the drives mxScannDrives.unlock(); write(fdNewDrivesInformPipe[1], "A",1); sleep(5); //sleep 5 sec @@ -119,7 +119,7 @@ void reHDD::ThreadCheckFrozenDrives() while(true) { mxScannDrives.lock(); - for(auto it = begin(vecDrives); it != end(vecDrives); ++it) + for(auto it = begin(listDrives); it != end(listDrives); ++it) { if(it->state == Drive::SHRED_ACTIVE) { @@ -141,12 +141,12 @@ void reHDD::ThreadUserInput() case TUI::UserInput::DownKey: //cout << "Down" << endl; handleArrowKey(TUI::UserInput::DownKey); - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; case TUI::UserInput::UpKey: //cout << "Up" << endl; handleArrowKey(TUI::UserInput::UpKey); - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; case TUI::UserInput::Undefined: //cout << "Undefined" << endl; @@ -154,7 +154,7 @@ void reHDD::ThreadUserInput() case TUI::UserInput::Abort: //cout << "Abort" << endl; handleAbort(); - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; case TUI::UserInput::Delete: //cout << "Delete" << endl; @@ -166,7 +166,7 @@ void reHDD::ThreadUserInput() getSelectedDrive()->state = Drive::DELETE_SELECTED; } } - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; case TUI::UserInput::Shred: //cout << "Shred" << endl; @@ -178,17 +178,17 @@ void reHDD::ThreadUserInput() getSelectedDrive()->state = Drive::SHRED_SELECTED; } } - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; case TUI::UserInput::Enter: //cout << "Enter" << endl; handleEnter(); - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; case TUI::UserInput::ESC: //cout << "ESC" << endl; handleESC(); - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); break; default: break; @@ -203,24 +203,24 @@ void reHDD::ThreadShred() Shred* pShredTask = new Shred(); //create new shred task pShredTask->shredDrive(getSelectedDrive(), &fdShredInformPipe[1]); //start new shred task delete pShredTask; //delete shred task - ui->updateTUI(&vecDrives, u8SelectedEntry); + ui->updateTUI(&listDrives, u8SelectedEntry); } } -void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecNewDrives) +void reHDD::filterNewDrives(list * plistOldDrives, list * plistNewDrives) { - vector ::iterator itOld; //Iterator for current (old) drive list - vector ::iterator itNew; //Iterator for new drive list that was created from to scann thread + list ::iterator itOld; //Iterator for current (old) drive list + list ::iterator itNew; //Iterator for new drive list that was created from to scann thread //remove offline old drives from previously run - for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end();) + for (itOld = plistOldDrives->begin(); itOld != plistOldDrives->end();) { if(itOld->bIsOffline == true) { Logger::logThis()->warning("Offline drive found: " + itOld->getPath()); - itOld = pvecOldDrives->erase(itOld); + itOld = plistOldDrives->erase(itOld); /* - if(pvecOldDrives->size() > 0){ //This can be a risk if the user starts a task for the selected drive and the selected drive changes + if(plistOldDrives->size() > 0){ //This can be a risk if the user starts a task for the selected drive and the selected drive changes u8SelectedEntry = 0U; } */ @@ -232,10 +232,10 @@ void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecN } //search offline drives and mark them - for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld) + for (itOld = plistOldDrives->begin(); itOld != plistOldDrives->end(); ++itOld) { itOld->bIsOffline = true; //set offline befor seachring in the new list - for (itNew = pvecNewDrives->begin(); itNew != pvecNewDrives->end();) + for (itNew = plistNewDrives->begin(); itNew != plistNewDrives->end();) { if((itOld->getSerial() == itNew->getSerial()) || (itOld->getPath() == itNew->getPath())) { @@ -243,7 +243,7 @@ void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecN #ifdef LOG_LEVEL_HIGH Logger::logThis()->info("Delete new drive, because allready attached: " + itNew->getModelName()); #endif - itNew = pvecNewDrives->erase(itNew); //This drive is allready attached, remove from new list + itNew = plistNewDrives->erase(itNew); //This drive is allready attached, remove from new list } else { @@ -253,7 +253,7 @@ void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecN } //mark offline old drives - for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld) + for (itOld = plistOldDrives->begin(); itOld != plistOldDrives->end(); ++itOld) { if(itOld->bIsOffline == true) { @@ -264,12 +264,15 @@ void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecN } //add new drives to drive list - for (itNew = pvecNewDrives->begin(); itNew != pvecNewDrives->end(); ++itNew) + for (itNew = plistNewDrives->begin(); itNew != plistNewDrives->end(); ++itNew) { - pvecOldDrives->push_back(pvecNewDrives->at(itNew - pvecNewDrives->begin())); + //plistOldDrives->push_back(plistNewDrives->at(itNew - plistNewDrives->begin())); + + plistOldDrives->push_back(*itNew); + Logger::logThis()->info("Add new drive: " + itNew->getModelName()); } - pvecNewDrives->clear(); + plistNewDrives->clear(); } /** @@ -277,7 +280,7 @@ void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecN * \param pointer of vector * pvecDrives * \return void */ -void reHDD::searchDrives(vector * pvecDrives) +void reHDD::searchDrives(list * plistDrives) { // cout << "search drives ..." << endl; Logger::logThis()->info("--> search drives <--"); @@ -298,7 +301,7 @@ void reHDD::searchDrives(vector * pvecDrives) Drive* tmpDrive = new Drive(string(cLine).substr (2,8)); tmpDrive->state = Drive::NONE; tmpDrive->bIsOffline = false; - pvecDrives->push_back(*tmpDrive); + plistDrives->push_back(*tmpDrive); } } fclose(outputfileHwinfo); @@ -309,13 +312,13 @@ void reHDD::searchDrives(vector * pvecDrives) * \param pointer of vector * pvecDrives * \return void */ -void reHDD::filterIgnoredDrives(vector * pvecDrives) +void reHDD::filterIgnoredDrives(list * plistDrives) { string sDelimiter = ":"; string sIgnoredDrivePath; string sIgnoredDriveUUID; - vector> vtlIgnoredDevices; //store drives from ingnore file + list> vtlIgnoredDevices; //store drives from ingnore file ifstream input( "ignoreDrives.conf" ); //read ingnore file @@ -338,8 +341,8 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) //loop through found entries in ingnore file for(auto row : vtlIgnoredDevices) { - vector ::iterator it; - for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) + list ::iterator it; + for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { string sUUID; if (!get<0>(row).compare(it->getPath())) //find same drive based on path @@ -381,7 +384,7 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) #ifdef LOG_LEVEL_HIGH Logger::logThis()->info("same uuid found than in ignore file --> ignore this drive: " + it->getPath()); #endif - it = pvecDrives->erase(it); + it = plistDrives->erase(it); it--; } } @@ -394,13 +397,14 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) * \param pointer of vector * pvecDrives * \return void */ -void reHDD::printDrives(vector * pvecDrives) +void reHDD::printDrives(list * plistDrives) { #ifdef LOG_LEVEL_HIGH Logger::logThis()->info("------------DRIVES---------------"); //cout << "------------DRIVES---------------" << endl; - vector ::iterator it; - for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) + list ::iterator it; + uint8_t u8Index = 0; + for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { /* cout << " Drive: " << distance(pvecDrives->begin(), it) << endl; @@ -415,10 +419,9 @@ void reHDD::printDrives(vector * pvecDrives) cout << endl;*/ ostringstream address; - address << (void const *)&pvecDrives->at(it - pvecDrives->begin()); + address << (void const *)&(*it); - - Logger::logThis()->info(to_string(it - pvecDrives->begin()) + ": " + it->getPath() + " - " + it->getModelFamily() + " - " + it->getSerial() + " @" + address.str()); + Logger::logThis()->info(to_string(u8Index++) + ": " + it->getPath() + " - " + it->getModelFamily() + " - " + it->getSerial() + " @" + address.str()); } Logger::logThis()->info("---------------------------------"); //cout << "---------------------------------" << endl; @@ -430,19 +433,19 @@ void reHDD::printDrives(vector * pvecDrives) * \param pointer of vector * pvecDrives * \return void */ -void reHDD::addSMARTData(vector * pvecDrives) +void reHDD::addSMARTData(list * plistDrives) { - vector ::iterator it; - for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) + list ::iterator it; + for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { - Drive* pTmpDrive = iterator_to_pointer::iterator > (it); + Drive* pTmpDrive = iterator_to_pointer::iterator > (it); SMART::readSMARTData(pTmpDrive); } } void reHDD::handleArrowKey(TUI::UserInput userInput) { - int8_t u8EntrySize = (int8_t) vecDrives.size(); + int8_t u8EntrySize = (int8_t) listDrives.size(); switch (userInput) { case TUI::UserInput::DownKey: diff --git a/src/tui.cpp b/src/tui.cpp index 1f962cc..6bcf852 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -50,7 +50,7 @@ void TUI::initTUI() Logger::logThis()->info("UI successfully initialized"); } -void TUI::updateTUI(vector * pvecDrives, uint8_t u8SelectedEntry) +void TUI::updateTUI(list * plistDrives, uint8_t u8SelectedEntry) { mxUIrefresh.lock(); int stdscrX, stdscrY; @@ -69,8 +69,9 @@ void TUI::updateTUI(vector * pvecDrives, uint8_t u8SelectedEntry) delwin(detailview); - vector ::iterator it; - for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) + list ::iterator it; + uint8_t u8Index = 0U; + for (it = plistDrives->begin(); it != plistDrives->end(); ++it) { string sModelFamily = it->getModelFamily(); string sModelName = it->getModelName(); @@ -79,10 +80,10 @@ void TUI::updateTUI(vector * pvecDrives, uint8_t u8SelectedEntry) bool bSelectedEntry = false; - if(u8SelectedEntry == (it - pvecDrives->begin())) + if(u8SelectedEntry == u8Index) { bSelectedEntry = true; //mark this drive in entries list - displaySelectedDrive(pvecDrives->at(u8SelectedEntry), stdscrX, stdscrY); + displaySelectedDrive(*it, stdscrX, stdscrY); } stringstream stream; @@ -119,11 +120,12 @@ void TUI::updateTUI(vector * pvecDrives, uint8_t u8SelectedEntry) break; } - WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity, sState, bSelectedEntry); + WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (u8Index) )+3, sModelFamily, sModelName, sCapacity, sState, bSelectedEntry); wrefresh(tmp); + u8Index++; }//end loop though drives - if(pvecDrives->size() == 0) + if(plistDrives->size() == 0) { //no selected drive present Logger::logThis()->warning("no selected drive present");