changed vector to list

This commit is contained in:
Hendrik Schutter 2020-09-16 15:13:32 +02:00
parent 6c06943f3e
commit 0fc3bc222e
4 changed files with 73 additions and 68 deletions

View File

@ -10,7 +10,7 @@
#define REHDD_VERSION "bV0.1.0" #define REHDD_VERSION "bV0.1.0"
//#define DRYRUN #define DRYRUN
// Drive handling Settings // Drive handling Settings
#define WORSE_HOURS 19200 //mark drive if at this limit or beyond #define WORSE_HOURS 19200 //mark drive if at this limit or beyond
@ -38,7 +38,7 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <tuple> #include <tuple>
#include <vector> #include <list>
#include <time.h> #include <time.h>
#include <chrono> #include <chrono>
#include <curses.h> #include <curses.h>
@ -78,11 +78,11 @@ public:
private: private:
static void searchDrives(vector <Drive>* pvecDrives); static void searchDrives(list <Drive>* plistDrives);
static void printDrives(vector <Drive>* pvecDrives); static void printDrives(list <Drive>* plistDrives);
static void filterIgnoredDrives(vector <Drive>* pvecDrives); static void filterIgnoredDrives(list <Drive>* plistDrives);
static void filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives); static void filterNewDrives(list <Drive>* plistOldDrives, list <Drive>* plistNewDrives);
static void addSMARTData(vector <Drive>* pvecDrives); static void addSMARTData(list <Drive>* plistDrives);
static void ThreadScannDevices(); static void ThreadScannDevices();
static void ThreadUserInput(); static void ThreadUserInput();
static void ThreadShred(); static void ThreadShred();

View File

@ -36,7 +36,7 @@ public:
static void initTUI(); static void initTUI();
void updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry); void updateTUI(list <Drive>* plistDrives, uint8_t u8SelectedEntry);
static enum UserInput readUserInput(); static enum UserInput readUserInput();

View File

@ -13,9 +13,9 @@ static int fdShredInformPipe[2];//File descriptor for pipe that informs if a wip
static std::mutex mxScannDrives; static std::mutex mxScannDrives;
vector <Drive> vecNewDrives; //store found drives that are updated every 5sec list <Drive> listNewDrives; //store found drives that are updated every 5sec
static vector <Drive> vecDrives; //stores all drive data from scann thread static list <Drive> listDrives; //stores all drive data from scann thread
TUI *ui; TUI *ui;
@ -33,8 +33,6 @@ static fd_set selectSet;
reHDD::reHDD(void) reHDD::reHDD(void)
{ {
u8SelectedEntry = 0U; u8SelectedEntry = 0U;
vecDrives.reserve(128);
} }
/** /**
@ -66,8 +64,8 @@ void reHDD::app_logic(void)
{ {
char dummy; char dummy;
read (fdNewDrivesInformPipe[0],&dummy,1); read (fdNewDrivesInformPipe[0],&dummy,1);
filterNewDrives(&vecDrives, &vecNewDrives); //filter and copy to app logic vector filterNewDrives(&listDrives, &listNewDrives); //filter and copy to app logic vector
printDrives(&vecDrives); printDrives(&listDrives);
} }
@ -76,7 +74,7 @@ void reHDD::app_logic(void)
char dummy; char dummy;
read (fdShredInformPipe[0],&dummy,1); read (fdShredInformPipe[0],&dummy,1);
} }
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
mxScannDrives.unlock(); mxScannDrives.unlock();
} //endless loop } //endless loop
thDevices.join(); thDevices.join();
@ -86,9 +84,11 @@ void reHDD::app_logic(void)
Drive* reHDD::getSelectedDrive() Drive* reHDD::getSelectedDrive()
{ {
if(u8SelectedEntry < vecDrives.size() ) if(u8SelectedEntry < listDrives.size() )
{ {
return &(vecDrives.at(u8SelectedEntry)); list<Drive>::iterator it = listDrives.begin();
advance(it, u8SelectedEntry);
return &(*it);
} }
else else
{ {
@ -102,10 +102,10 @@ void reHDD::ThreadScannDevices()
while(true) while(true)
{ {
mxScannDrives.lock(); mxScannDrives.lock();
vecNewDrives.clear(); listNewDrives.clear();
searchDrives(&vecNewDrives); //search for new drives and store them in list searchDrives(&listNewDrives); //search for new drives and store them in list
filterIgnoredDrives(&vecNewDrives); //filter out ignored drives filterIgnoredDrives(&listNewDrives); //filter out ignored drives
addSMARTData(&vecNewDrives); //add S.M.A.R.T. Data to the drives addSMARTData(&listNewDrives); //add S.M.A.R.T. Data to the drives
mxScannDrives.unlock(); mxScannDrives.unlock();
write(fdNewDrivesInformPipe[1], "A",1); write(fdNewDrivesInformPipe[1], "A",1);
sleep(5); //sleep 5 sec sleep(5); //sleep 5 sec
@ -119,7 +119,7 @@ void reHDD::ThreadCheckFrozenDrives()
while(true) while(true)
{ {
mxScannDrives.lock(); 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) if(it->state == Drive::SHRED_ACTIVE)
{ {
@ -141,12 +141,12 @@ void reHDD::ThreadUserInput()
case TUI::UserInput::DownKey: case TUI::UserInput::DownKey:
//cout << "Down" << endl; //cout << "Down" << endl;
handleArrowKey(TUI::UserInput::DownKey); handleArrowKey(TUI::UserInput::DownKey);
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
case TUI::UserInput::UpKey: case TUI::UserInput::UpKey:
//cout << "Up" << endl; //cout << "Up" << endl;
handleArrowKey(TUI::UserInput::UpKey); handleArrowKey(TUI::UserInput::UpKey);
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
case TUI::UserInput::Undefined: case TUI::UserInput::Undefined:
//cout << "Undefined" << endl; //cout << "Undefined" << endl;
@ -154,7 +154,7 @@ void reHDD::ThreadUserInput()
case TUI::UserInput::Abort: case TUI::UserInput::Abort:
//cout << "Abort" << endl; //cout << "Abort" << endl;
handleAbort(); handleAbort();
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
case TUI::UserInput::Delete: case TUI::UserInput::Delete:
//cout << "Delete" << endl; //cout << "Delete" << endl;
@ -166,7 +166,7 @@ void reHDD::ThreadUserInput()
getSelectedDrive()->state = Drive::DELETE_SELECTED; getSelectedDrive()->state = Drive::DELETE_SELECTED;
} }
} }
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
case TUI::UserInput::Shred: case TUI::UserInput::Shred:
//cout << "Shred" << endl; //cout << "Shred" << endl;
@ -178,17 +178,17 @@ void reHDD::ThreadUserInput()
getSelectedDrive()->state = Drive::SHRED_SELECTED; getSelectedDrive()->state = Drive::SHRED_SELECTED;
} }
} }
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
case TUI::UserInput::Enter: case TUI::UserInput::Enter:
//cout << "Enter" << endl; //cout << "Enter" << endl;
handleEnter(); handleEnter();
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
case TUI::UserInput::ESC: case TUI::UserInput::ESC:
//cout << "ESC" << endl; //cout << "ESC" << endl;
handleESC(); handleESC();
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
break; break;
default: default:
break; break;
@ -203,24 +203,24 @@ void reHDD::ThreadShred()
Shred* pShredTask = new Shred(); //create new shred task Shred* pShredTask = new Shred(); //create new shred task
pShredTask->shredDrive(getSelectedDrive(), &fdShredInformPipe[1]); //start new shred task pShredTask->shredDrive(getSelectedDrive(), &fdShredInformPipe[1]); //start new shred task
delete pShredTask; //delete shred task delete pShredTask; //delete shred task
ui->updateTUI(&vecDrives, u8SelectedEntry); ui->updateTUI(&listDrives, u8SelectedEntry);
} }
} }
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives) void reHDD::filterNewDrives(list <Drive>* plistOldDrives, list <Drive>* plistNewDrives)
{ {
vector <Drive>::iterator itOld; //Iterator for current (old) drive list list <Drive>::iterator itOld; //Iterator for current (old) drive list
vector <Drive>::iterator itNew; //Iterator for new drive list that was created from to scann thread list <Drive>::iterator itNew; //Iterator for new drive list that was created from to scann thread
//remove offline old drives from previously run //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) if(itOld->bIsOffline == true)
{ {
Logger::logThis()->warning("Offline drive found: " + itOld->getPath()); 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; u8SelectedEntry = 0U;
} }
*/ */
@ -232,10 +232,10 @@ void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecN
} }
//search offline drives and mark them //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 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())) if((itOld->getSerial() == itNew->getSerial()) || (itOld->getPath() == itNew->getPath()))
{ {
@ -243,7 +243,7 @@ void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecN
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Delete new drive, because allready attached: " + itNew->getModelName()); Logger::logThis()->info("Delete new drive, because allready attached: " + itNew->getModelName());
#endif #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 else
{ {
@ -253,7 +253,7 @@ void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecN
} }
//mark offline old drives //mark offline old drives
for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld) for (itOld = plistOldDrives->begin(); itOld != plistOldDrives->end(); ++itOld)
{ {
if(itOld->bIsOffline == true) if(itOld->bIsOffline == true)
{ {
@ -264,12 +264,15 @@ void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecN
} }
//add new drives to drive list //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()); Logger::logThis()->info("Add new drive: " + itNew->getModelName());
} }
pvecNewDrives->clear(); plistNewDrives->clear();
} }
/** /**
@ -277,7 +280,7 @@ void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecN
* \param pointer of vector <Drive>* pvecDrives * \param pointer of vector <Drive>* pvecDrives
* \return void * \return void
*/ */
void reHDD::searchDrives(vector <Drive>* pvecDrives) void reHDD::searchDrives(list <Drive>* plistDrives)
{ {
// cout << "search drives ..." << endl; // cout << "search drives ..." << endl;
Logger::logThis()->info("--> search drives <--"); Logger::logThis()->info("--> search drives <--");
@ -298,7 +301,7 @@ void reHDD::searchDrives(vector <Drive>* pvecDrives)
Drive* tmpDrive = new Drive(string(cLine).substr (2,8)); Drive* tmpDrive = new Drive(string(cLine).substr (2,8));
tmpDrive->state = Drive::NONE; tmpDrive->state = Drive::NONE;
tmpDrive->bIsOffline = false; tmpDrive->bIsOffline = false;
pvecDrives->push_back(*tmpDrive); plistDrives->push_back(*tmpDrive);
} }
} }
fclose(outputfileHwinfo); fclose(outputfileHwinfo);
@ -309,13 +312,13 @@ void reHDD::searchDrives(vector <Drive>* pvecDrives)
* \param pointer of vector <Drive>* pvecDrives * \param pointer of vector <Drive>* pvecDrives
* \return void * \return void
*/ */
void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives) void reHDD::filterIgnoredDrives(list <Drive>* plistDrives)
{ {
string sDelimiter = ":"; string sDelimiter = ":";
string sIgnoredDrivePath; string sIgnoredDrivePath;
string sIgnoredDriveUUID; string sIgnoredDriveUUID;
vector<tuple<string, string>> vtlIgnoredDevices; //store drives from ingnore file list<tuple<string, string>> vtlIgnoredDevices; //store drives from ingnore file
ifstream input( "ignoreDrives.conf" ); //read ingnore file ifstream input( "ignoreDrives.conf" ); //read ingnore file
@ -338,8 +341,8 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
//loop through found entries in ingnore file //loop through found entries in ingnore file
for(auto row : vtlIgnoredDevices) for(auto row : vtlIgnoredDevices)
{ {
vector <Drive>::iterator it; list <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{ {
string sUUID; string sUUID;
if (!get<0>(row).compare(it->getPath())) //find same drive based on path if (!get<0>(row).compare(it->getPath())) //find same drive based on path
@ -381,7 +384,7 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("same uuid found than in ignore file --> ignore this drive: " + it->getPath()); Logger::logThis()->info("same uuid found than in ignore file --> ignore this drive: " + it->getPath());
#endif #endif
it = pvecDrives->erase(it); it = plistDrives->erase(it);
it--; it--;
} }
} }
@ -394,13 +397,14 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
* \param pointer of vector <Drive>* pvecDrives * \param pointer of vector <Drive>* pvecDrives
* \return void * \return void
*/ */
void reHDD::printDrives(vector <Drive>* pvecDrives) void reHDD::printDrives(list <Drive>* plistDrives)
{ {
#ifdef LOG_LEVEL_HIGH #ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("------------DRIVES---------------"); Logger::logThis()->info("------------DRIVES---------------");
//cout << "------------DRIVES---------------" << endl; //cout << "------------DRIVES---------------" << endl;
vector <Drive>::iterator it; list <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) uint8_t u8Index = 0;
for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{ {
/* /*
cout << " Drive: " << distance(pvecDrives->begin(), it) << endl; cout << " Drive: " << distance(pvecDrives->begin(), it) << endl;
@ -415,10 +419,9 @@ void reHDD::printDrives(vector <Drive>* pvecDrives)
cout << endl;*/ cout << endl;*/
ostringstream address; ostringstream address;
address << (void const *)&pvecDrives->at(it - pvecDrives->begin()); address << (void const *)&(*it);
Logger::logThis()->info(to_string(u8Index++) + ": " + it->getPath() + " - " + it->getModelFamily() + " - " + it->getSerial() + " @" + address.str());
Logger::logThis()->info(to_string(it - pvecDrives->begin()) + ": " + it->getPath() + " - " + it->getModelFamily() + " - " + it->getSerial() + " @" + address.str());
} }
Logger::logThis()->info("---------------------------------"); Logger::logThis()->info("---------------------------------");
//cout << "---------------------------------" << endl; //cout << "---------------------------------" << endl;
@ -430,19 +433,19 @@ void reHDD::printDrives(vector <Drive>* pvecDrives)
* \param pointer of vector <Drive>* pvecDrives * \param pointer of vector <Drive>* pvecDrives
* \return void * \return void
*/ */
void reHDD::addSMARTData(vector <Drive>* pvecDrives) void reHDD::addSMARTData(list <Drive>* plistDrives)
{ {
vector <Drive>::iterator it; list <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{ {
Drive* pTmpDrive = iterator_to_pointer<Drive, std::vector<Drive>::iterator > (it); Drive* pTmpDrive = iterator_to_pointer<Drive, std::list<Drive>::iterator > (it);
SMART::readSMARTData(pTmpDrive); SMART::readSMARTData(pTmpDrive);
} }
} }
void reHDD::handleArrowKey(TUI::UserInput userInput) void reHDD::handleArrowKey(TUI::UserInput userInput)
{ {
int8_t u8EntrySize = (int8_t) vecDrives.size(); int8_t u8EntrySize = (int8_t) listDrives.size();
switch (userInput) switch (userInput)
{ {
case TUI::UserInput::DownKey: case TUI::UserInput::DownKey:

View File

@ -50,7 +50,7 @@ void TUI::initTUI()
Logger::logThis()->info("UI successfully initialized"); Logger::logThis()->info("UI successfully initialized");
} }
void TUI::updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry) void TUI::updateTUI(list <Drive>* plistDrives, uint8_t u8SelectedEntry)
{ {
mxUIrefresh.lock(); mxUIrefresh.lock();
int stdscrX, stdscrY; int stdscrX, stdscrY;
@ -69,8 +69,9 @@ void TUI::updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry)
delwin(detailview); delwin(detailview);
vector <Drive>::iterator it; list <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) uint8_t u8Index = 0U;
for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{ {
string sModelFamily = it->getModelFamily(); string sModelFamily = it->getModelFamily();
string sModelName = it->getModelName(); string sModelName = it->getModelName();
@ -79,10 +80,10 @@ void TUI::updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry)
bool bSelectedEntry = false; bool bSelectedEntry = false;
if(u8SelectedEntry == (it - pvecDrives->begin())) if(u8SelectedEntry == u8Index)
{ {
bSelectedEntry = true; //mark this drive in entries list bSelectedEntry = true; //mark this drive in entries list
displaySelectedDrive(pvecDrives->at(u8SelectedEntry), stdscrX, stdscrY); displaySelectedDrive(*it, stdscrX, stdscrY);
} }
stringstream stream; stringstream stream;
@ -119,11 +120,12 @@ void TUI::updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry)
break; 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); wrefresh(tmp);
u8Index++;
}//end loop though drives }//end loop though drives
if(pvecDrives->size() == 0) if(plistDrives->size() == 0)
{ {
//no selected drive present //no selected drive present
Logger::logThis()->warning("no selected drive present"); Logger::logThis()->warning("no selected drive present");