select drive based on arrow keys

This commit is contained in:
Hendrik Schutter 2020-08-07 11:38:00 +02:00
parent a92a9c2a2a
commit 1b0ea97ed1
7 changed files with 355 additions and 267 deletions

View File

@ -50,18 +50,19 @@ public:
private: private:
vector <Drive> vecDrives; //stores all drive data
static void searchDrives(vector <Drive>* pvecDrives); static void searchDrives(vector <Drive>* pvecDrives);
static void printDrives(vector <Drive>* pvecDrives); static void printDrives(vector <Drive>* pvecDrives);
static void filterIgnoredDrives(vector <Drive>* pvecDrives); static void filterIgnoredDrives(vector <Drive>* pvecDrives);
static void addSMARTData(vector <Drive>* pvecDrives); static void addSMARTData(vector <Drive>* pvecDrives);
static void ThreadScannDevices(); static void ThreadScannDevices();
static void ThreadUserInput(); static void ThreadUserInput();
void filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives); static void handleArrowKey(TUI::UserInput userInput);
static void filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives);
TUI *ui;

View File

@ -13,6 +13,7 @@
#define COLOR_AREA_STDSCR 1 #define COLOR_AREA_STDSCR 1
#define COLOR_AREA_OVERVIEW 2 #define COLOR_AREA_OVERVIEW 2
#define COLOR_AREA_ENTRY 3 #define COLOR_AREA_ENTRY 3
#define COLOR_AREA_ENTRY_SELECTED 4
#define COLOR_GRAY 8 #define COLOR_GRAY 8
@ -28,7 +29,7 @@ public:
static void initTUI(); static void initTUI();
void updateTUI(vector <Drive>* pvecDrives); void updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry);
static enum UserInput readUserInput(); static enum UserInput readUserInput();
@ -43,7 +44,7 @@ private:
static void centerTitle(WINDOW *pwin, const char * title); static void centerTitle(WINDOW *pwin, const char * title);
static WINDOW *createOverViewWindow( int iXSize, int iYSize); static WINDOW *createOverViewWindow( int iXSize, int iYSize);
static WINDOW *createDetailViewWindow( int iXSize, int iYSize, int iXStart); static WINDOW *createDetailViewWindow( int iXSize, int iYSize, int iXStart);
static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart,string sModelFamily, string sModelName, string sCapacity); static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart,string sModelFamily, string sModelName, string sCapacity, bool bSelected);
}; };

View File

@ -8,7 +8,8 @@
#include "../../include/reHDD.h" #include "../../include/reHDD.h"
TUI::TUI(void) { TUI::TUI(void)
{
@ -26,12 +27,15 @@ void TUI::initTUI()
initscr(); initscr();
raw(); raw();
keypad(stdscr,TRUE); keypad(stdscr,TRUE);
if(has_colors() == TRUE) { if(has_colors() == TRUE)
start_color(); {
} else { start_color();
printf("Your terminal does not support color\n"); }
exit(1); else
} {
printf("Your terminal does not support color\n");
exit(1);
}
clear(); clear();
curs_set(0); curs_set(0);
noecho(); noecho();
@ -41,11 +45,18 @@ void TUI::initTUI()
init_pair(COLOR_AREA_STDSCR,COLOR_WHITE, COLOR_BLUE); init_pair(COLOR_AREA_STDSCR,COLOR_WHITE, COLOR_BLUE);
wbkgd(stdscr, COLOR_PAIR(COLOR_AREA_STDSCR)); wbkgd(stdscr, COLOR_PAIR(COLOR_AREA_STDSCR));
init_pair(COLOR_AREA_ENTRY, COLOR_BLACK, COLOR_GRAY);
init_pair(COLOR_AREA_ENTRY_SELECTED, COLOR_BLACK, COLOR_WHITE);
init_pair(COLOR_AREA_OVERVIEW, COLOR_BLACK, COLOR_GRAY);
init_pair(COLOR_AREA_OVERVIEW, COLOR_BLACK, COLOR_GRAY);
mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 "); mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 ");
} }
void TUI::updateTUI(vector <Drive>* pvecDrives) { void TUI::updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry)
{
int stdscrX, stdscrY; int stdscrX, stdscrY;
getmaxyx(stdscr, stdscrY, stdscrX); getmaxyx(stdscr, stdscrY, stdscrX);
@ -63,19 +74,27 @@ void TUI::updateTUI(vector <Drive>* pvecDrives) {
vector <Drive>::iterator it; vector <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
{ {
string sModelFamily = it->getModelFamily(); string sModelFamily = it->getModelFamily();
string sModelName = it->getModelName(); string sModelName = it->getModelName();
string sCapacity = it->sCapacityToText(); string sCapacity = it->sCapacityToText();
WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity); bool bSelectedEntry = false;
wrefresh(tmp);
} if(i32SelectedEntry == (it - pvecDrives->begin()))
{
bSelectedEntry = true;
}
WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity, bSelectedEntry);
wrefresh(tmp);
}
} }
enum TUI::UserInput TUI::readUserInput() { enum TUI::UserInput TUI::readUserInput()
int ch = wgetch(stdscr); {
switch(ch) int ch = wgetch(stdscr);
switch(ch)
{ {
case KEY_UP: case KEY_UP:
return TUI::UserInput::UpKey; return TUI::UserInput::UpKey;
@ -106,7 +125,8 @@ enum TUI::UserInput TUI::readUserInput() {
} }
void TUI::centerTitle(WINDOW *pwin, const char * title) { void TUI::centerTitle(WINDOW *pwin, const char * title)
{
int x, maxX, stringSize; int x, maxX, stringSize;
getmaxyx(pwin, maxX, maxX); getmaxyx(pwin, maxX, maxX);
stringSize = 4 + strlen(title); stringSize = 4 + strlen(title);
@ -118,10 +138,11 @@ void TUI::centerTitle(WINDOW *pwin, const char * title) {
waddch(pwin, ACS_LTEE); waddch(pwin, ACS_LTEE);
} }
WINDOW* TUI::createOverViewWindow( int iXSize, int iYSize) { WINDOW* TUI::createOverViewWindow( int iXSize, int iYSize)
{
WINDOW *newWindow; WINDOW *newWindow;
newWindow = newwin(iYSize, iXSize, 2, 2); newWindow = newwin(iYSize, iXSize, 2, 2);
init_pair(COLOR_AREA_OVERVIEW, COLOR_BLACK, COLOR_GRAY);
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW)); wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW));
box(newWindow, ACS_VLINE, ACS_HLINE); box(newWindow, ACS_VLINE, ACS_HLINE);
@ -142,10 +163,11 @@ WINDOW* TUI::createOverViewWindow( int iXSize, int iYSize) {
return newWindow; return newWindow;
} }
WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart) { WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart)
{
WINDOW *newWindow; WINDOW *newWindow;
newWindow = newwin(iYSize, iXSize, 2, iXStart); newWindow = newwin(iYSize, iXSize, 2, iXStart);
init_pair(COLOR_AREA_OVERVIEW, COLOR_BLACK, COLOR_GRAY);
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW)); wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_OVERVIEW));
box(newWindow, ACS_VLINE, ACS_HLINE); box(newWindow, ACS_VLINE, ACS_HLINE);
centerTitle(newWindow, "Selected Drive: xyz"); centerTitle(newWindow, "Selected Drive: xyz");
@ -154,14 +176,27 @@ WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart) {
return newWindow; return newWindow;
} }
WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity) { WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, bool bSelected)
{
WINDOW *newWindow; WINDOW *newWindow;
newWindow = newwin(iYSize, iXSize, iYStart, iXStart); newWindow = newwin(iYSize, iXSize, iYStart, iXStart);
init_pair(COLOR_AREA_ENTRY, COLOR_BLACK, COLOR_GRAY);
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_ENTRY));
box(newWindow, ACS_VLINE, ACS_HLINE);
attron(COLOR_PAIR(COLOR_AREA_ENTRY));
if(!bSelected)
{
// entry is NOT selected
attron(COLOR_PAIR(COLOR_AREA_ENTRY));
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_ENTRY));
}
else
{
// entry IS selected
attron(COLOR_PAIR(COLOR_AREA_ENTRY));
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_ENTRY_SELECTED));
}
box(newWindow, ACS_VLINE, ACS_HLINE);
mvwaddstr(newWindow,1, 1, sModelFamily.c_str()); mvwaddstr(newWindow,1, 1, sModelFamily.c_str());
mvwaddstr(newWindow,2, 1, sModelName.c_str()); mvwaddstr(newWindow,2, 1, sModelName.c_str());

View File

@ -46,15 +46,18 @@ uint32_t Drive::getPowerCycles(void)
return u32PowerCycles; return u32PowerCycles;
} }
string Drive::sCapacityToText() { string Drive::sCapacityToText()
if(getCapacity() <= (999*1000000000UL)) { {
// Less or even 999 GB --> GB if(getCapacity() <= (999*1000000000UL))
return to_string(getCapacity() / 1000000000UL) + " GB"; {
} // Less or even 999 GB --> GB
else { return to_string(getCapacity() / 1000000000UL) + " GB";
// More 999 GB --> TB }
return to_string(getCapacity() / 1000000000000UL) + " TB"; else
} {
// More 999 GB --> TB
return to_string(getCapacity() / 1000000000000UL) + " TB";
}
return "ERROR"; return "ERROR";
} }

View File

@ -18,6 +18,11 @@ static std::mutex mxScannDrives;
static vector <Drive> vecNewDrives; //store found drives that are updated every 5sec static vector <Drive> vecNewDrives; //store found drives that are updated every 5sec
static vector <Drive> vecDrives; //stores all drive data from scann thread
TUI *ui;
static int32_t i32SelectedEntry;
static fd_set selectSet; static fd_set selectSet;
@ -29,8 +34,7 @@ static fd_set selectSet;
reHDD::reHDD(void) reHDD::reHDD(void)
{ {
cout << "created app" << endl; cout << "created app" << endl;
i32SelectedEntry = 0;
} }
/** /**
@ -41,9 +45,7 @@ reHDD::reHDD(void)
void reHDD::app_logic(void) void reHDD::app_logic(void)
{ {
cout << "app logic" << endl; cout << "app logic" << endl;
ui = new TUI(); ui = new TUI();
ui->initTUI(); ui->initTUI();
pipe(fdSearchDrives); pipe(fdSearchDrives);
@ -56,107 +58,124 @@ void reHDD::app_logic(void)
thread thDevices(ThreadScannDevices); //start thread that scanns for drives thread thDevices(ThreadScannDevices); //start thread that scanns for drives
thread thUserInput(ThreadUserInput); //start thread that reads user input thread thUserInput(ThreadUserInput); //start thread that reads user input
while(1) { while(1)
{
select(FD_SETSIZE, &selectSet, NULL, NULL, NULL); select(FD_SETSIZE, &selectSet, NULL, NULL, NULL);
if( FD_ISSET(fdSearchDrives[0], &selectSet)) { if( FD_ISSET(fdSearchDrives[0], &selectSet))
char dummy; {
read (fdSearchDrives[0],&dummy,1); char dummy;
mxScannDrives.lock(); read (fdSearchDrives[0],&dummy,1);
filterNewDrives(&vecDrives, &vecNewDrives); mxScannDrives.lock();
//printDrives(&vecDrives);
//TODO update UI
ui->updateTUI(&vecDrives); filterNewDrives(&vecDrives, &vecNewDrives); //filter and copy to app logic vector
mxScannDrives.unlock();
//printDrives(&vecDrives);
//TODO update UI
mxScannDrives.unlock(); ui->updateTUI(&vecDrives, i32SelectedEntry);
}
else if (FD_ISSET(fdWhipe[0], &selectSet)) {
cout << "Whipe signal" << endl; }
} else if (FD_ISSET(fdWhipe[0], &selectSet))
} //endless loop {
cout << "Whipe signal" << endl;
//update percantage & state
//update ui
}
} //endless loop
thDevices.join(); thDevices.join();
thUserInput.join(); thUserInput.join();
} }
void reHDD::ThreadScannDevices() { void reHDD::ThreadScannDevices()
while(true) { {
// cout << "Thread" << endl; 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
mxScannDrives.unlock();
write (fdSearchDrives[1], "A",1);
sleep(5); //sleep 5 sec
}
}
void reHDD::ThreadUserInput() {
while(true) {
// cout << TUI::readUserInput() << endl;
switch (TUI::readUserInput())
{ {
case TUI::UserInput::DownKey: // cout << "Thread" << endl;
/* code */ mxScannDrives.lock();
//cout << "Down" << endl; vecNewDrives.clear();
break; searchDrives(&vecNewDrives); //search for new drives and store them in list
case TUI::UserInput::UpKey: filterIgnoredDrives(&vecNewDrives); //filter out ignored drives
//cout << "Up" << endl; addSMARTData(&vecNewDrives); //add S.M.A.R.T. Data to the drives
break; mxScannDrives.unlock();
case TUI::UserInput::Undefined: write (fdSearchDrives[1], "A",1);
//cout << "Undefined" << endl; sleep(5); //sleep 5 sec
break;
case TUI::UserInput::Abort:
//cout << "Abort" << endl;
break;
case TUI::UserInput::Delete:
//cout << "Delete" << endl;
break;
case TUI::UserInput::Shred:
//cout << "Shred" << endl;
break;
case TUI::UserInput::Enter:
//cout << "Enter" << endl;
break;
case TUI::UserInput::ESC:
//cout << "ESC" << endl;
break;
default:
break;
} }
}
} }
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives) { void reHDD::ThreadUserInput()
{
while(true)
{
// cout << TUI::readUserInput() << endl;
switch (TUI::readUserInput())
{
case TUI::UserInput::DownKey:
//cout << "Down" << endl;
handleArrowKey(TUI::UserInput::DownKey);
ui->updateTUI(&vecDrives, i32SelectedEntry);
break;
case TUI::UserInput::UpKey:
//cout << "Up" << endl;
handleArrowKey(TUI::UserInput::UpKey);
ui->updateTUI(&vecDrives, i32SelectedEntry);
break;
case TUI::UserInput::Undefined:
//cout << "Undefined" << endl;
break;
case TUI::UserInput::Abort:
//cout << "Abort" << endl;
break;
case TUI::UserInput::Delete:
//cout << "Delete" << endl;
break;
case TUI::UserInput::Shred:
//cout << "Shred" << endl;
break;
case TUI::UserInput::Enter:
//cout << "Enter" << endl;
break;
case TUI::UserInput::ESC:
//cout << "ESC" << endl;
break;
default:
break;
}
}
}
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives)
{
vector <Drive>::iterator itOld; //Iterator for current (old) drive list vector <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 vector <Drive>::iterator itNew; //Iterator for new drive list that was created from to scann thread
for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld) for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld)
{
bool bOldDriveIsOffline = true;
for (itNew = pvecNewDrives->begin(); itNew != pvecNewDrives->end(); ++itNew)
{ {
if(itOld->getSerial() == itNew->getSerial()) { bool bOldDriveIsOffline = true;
bOldDriveIsOffline = false; for (itNew = pvecNewDrives->begin(); itNew != pvecNewDrives->end(); ++itNew)
// cout << "already online drive found: " << itOld->getPath() << endl; {
} if(itOld->getSerial() == itNew->getSerial())
} {
bOldDriveIsOffline = false;
// cout << "already online drive found: " << itOld->getPath() << endl;
}
}
if(bOldDriveIsOffline == true) { if(bOldDriveIsOffline == true)
//cout << "offline drive found: " << itOld->getPath() << endl; {
//TODO kill wipe thread //cout << "offline drive found: " << itOld->getPath() << endl;
//TODO kill wipe thread
}
} }
}
pvecOldDrives->clear(); pvecOldDrives->clear();
for (long unsigned int i=0; i<pvecNewDrives->size(); i++) { for (long unsigned int i=0; i<pvecNewDrives->size(); i++)
pvecOldDrives->push_back((*pvecNewDrives)[i]); {
} pvecOldDrives->push_back((*pvecNewDrives)[i]);
}
} }
/** /**
@ -173,18 +192,18 @@ void reHDD::searchDrives(vector <Drive>* pvecDrives)
FILE* outputfileHwinfo = popen("hwinfo --short --disk", "r"); FILE* outputfileHwinfo = popen("hwinfo --short --disk", "r");
if (outputfileHwinfo == NULL) if (outputfileHwinfo == NULL)
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while ((getline(&cLine, &len, outputfileHwinfo)) != -1) while ((getline(&cLine, &len, outputfileHwinfo)) != -1)
{
if (string(cLine).find("/dev/sd") != string::npos)
{ {
Drive* tmpDrive = new Drive(string(cLine).substr (2,8)); if (string(cLine).find("/dev/sd") != string::npos)
pvecDrives->push_back(*tmpDrive); {
Drive* tmpDrive = new Drive(string(cLine).substr (2,8));
pvecDrives->push_back(*tmpDrive);
}
} }
}
fclose(outputfileHwinfo); fclose(outputfileHwinfo);
} }
@ -206,72 +225,72 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
ifstream input( "ignoreDrives.conf" ); //read ingnore file ifstream input( "ignoreDrives.conf" ); //read ingnore file
for(string sLine; getline( input, sLine );) for(string sLine; getline( input, sLine );)
{
if (string(sLine).find("/dev/sd") != string::npos)
{ {
size_t pos = 0; if (string(sLine).find("/dev/sd") != string::npos)
string token; {
while ((pos = sLine.find(sDelimiter)) != string::npos) size_t pos = 0;
{ string token;
token = sLine.substr(0, pos); while ((pos = sLine.find(sDelimiter)) != string::npos)
sIgnoredDrivePath = token; {
sLine.erase(0, pos + sDelimiter.length()); token = sLine.substr(0, pos);
sIgnoredDriveUUID = sLine; sIgnoredDrivePath = token;
} //end while sLine.erase(0, pos + sDelimiter.length());
//cout << "Path: " << sIgnoredDrivePath << std::endl; sIgnoredDriveUUID = sLine;
//cout << "UUID: " << sIgnoredDriveUUID << std::endl; } //end while
vtlIgnoredDevices.emplace_back(sIgnoredDrivePath, sIgnoredDriveUUID); //add found path and uuid from ingnore file to vector //cout << "Path: " << sIgnoredDrivePath << std::endl;
//cout << "UUID: " << sIgnoredDriveUUID << std::endl;
vtlIgnoredDevices.emplace_back(sIgnoredDrivePath, sIgnoredDriveUUID); //add found path and uuid from ingnore file to vector
}
} }
}
//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;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
{ {
string sUUID; vector <Drive>::iterator it;
if (!get<0>(row).compare(it->getPath())) //find same drive based on path for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
{
char * cLine = NULL;
size_t len = 0;
string sCMD = "blkid ";
sCMD.append(it->getPath());
//cout << "cmd: " << sCMD << endl;
FILE* outputfileBlkid = popen(sCMD.c_str(), "r"); //get UUID from drive
if (outputfileBlkid == NULL)
{ {
exit(EXIT_FAILURE); string sUUID;
} if (!get<0>(row).compare(it->getPath())) //find same drive based on path
{
char * cLine = NULL;
size_t len = 0;
string sCMD = "blkid ";
sCMD.append(it->getPath());
//cout << "cmd: " << sCMD << endl;
FILE* outputfileBlkid = popen(sCMD.c_str(), "r"); //get UUID from drive
if (outputfileBlkid == NULL)
{
exit(EXIT_FAILURE);
}
while ((getline(&cLine, &len, outputfileBlkid)) != -1) //parse UUID from blkid while ((getline(&cLine, &len, outputfileBlkid)) != -1) //parse UUID from blkid
{ {
if (string(cLine).find("PTUUID") != string::npos) if (string(cLine).find("PTUUID") != string::npos)
{ {
string sBlkidOut = string(cLine); string sBlkidOut = string(cLine);
sBlkidOut.erase(0, 18); sBlkidOut.erase(0, 18);
sBlkidOut.erase(36, sBlkidOut.length() - 36); sBlkidOut.erase(36, sBlkidOut.length() - 36);
sUUID = sBlkidOut; sUUID = sBlkidOut;
//cout << "blkid uuid:" << sUUID << endl; //cout << "blkid uuid:" << sUUID << endl;
} }
} }
fclose(outputfileBlkid); fclose(outputfileBlkid);
//cout << "blkid uuid:" << sUUID << endl; //cout << "blkid uuid:" << sUUID << endl;
if (get<1>(row).compare(sUUID)) //compare uuid from ignore file and uuid from drive if (get<1>(row).compare(sUUID)) //compare uuid from ignore file and uuid from drive
{ {
cout << "[ERROR] different uuid found than in ignore file:" << it->getPath() << endl; cout << "[ERROR] different uuid found than in ignore file:" << it->getPath() << endl;
exit(EXIT_FAILURE); // exit to prevent accidentally shred a system drive exit(EXIT_FAILURE); // exit to prevent accidentally shred a system drive
}
else
{
// same uuid found than in ignore file --> ignore this drive
it = pvecDrives->erase(it);
it--;
//cout << "same uuid found than in ignore file --> ignore this drive:" << it->getPath() << endl;
}
}
} }
else
{
// same uuid found than in ignore file --> ignore this drive
it = pvecDrives->erase(it);
it--;
//cout << "same uuid found than in ignore file --> ignore this drive:" << it->getPath() << endl;
}
}
} }
}
} }
/** /**
@ -284,18 +303,18 @@ void reHDD::printDrives(vector <Drive>* pvecDrives)
cout << "------------DRIVES---------------" << endl; cout << "------------DRIVES---------------" << endl;
vector <Drive>::iterator it; vector <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
{ {
cout << " Drive: " << distance(pvecDrives->begin(), it) << endl; cout << " Drive: " << distance(pvecDrives->begin(), it) << endl;
cout << "Path: " << it->getPath() << endl; cout << "Path: " << it->getPath() << endl;
cout << "ModelFamily: " << it->getModelFamily() << endl; cout << "ModelFamily: " << it->getModelFamily() << endl;
cout << "ModelName: " << it->getModelName() << endl; cout << "ModelName: " << it->getModelName() << endl;
cout << "Capacity: " << it->getCapacity() << endl; cout << "Capacity: " << it->getCapacity() << endl;
cout << "Serial: " << it->getSerial() << endl; cout << "Serial: " << it->getSerial() << endl;
cout << "PowerOnHours: " << it->getPowerOnHours() << endl; cout << "PowerOnHours: " << it->getPowerOnHours() << endl;
cout << "PowerCycle: " << it->getPowerCycles() << endl; cout << "PowerCycle: " << it->getPowerCycles() << endl;
cout << "ErrorCount: " << it->getErrorCount() << endl; cout << "ErrorCount: " << it->getErrorCount() << endl;
cout << endl; cout << endl;
} }
cout << "---------------------------------" << endl; cout << "---------------------------------" << endl;
} }
@ -308,8 +327,33 @@ void reHDD::addSMARTData(vector <Drive>* pvecDrives)
{ {
vector <Drive>::iterator it; vector <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it) for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
{ {
Drive* pTmpDrive = iterator_to_pointer<Drive, std::vector<Drive>::iterator > (it); Drive* pTmpDrive = iterator_to_pointer<Drive, std::vector<Drive>::iterator > (it);
SMART::readSMARTData(pTmpDrive); SMART::readSMARTData(pTmpDrive);
} }
}
void reHDD::handleArrowKey(TUI::UserInput userInput)
{
int32_t i32EntrySize = (int32_t) vecDrives.size();
switch (userInput)
{
case TUI::UserInput::DownKey:
i32SelectedEntry++;
if(i32SelectedEntry >= i32EntrySize)
{
i32SelectedEntry = 0;
}
break;
case TUI::UserInput::UpKey:
i32SelectedEntry--;
if(i32SelectedEntry < 0)
{
i32SelectedEntry = (i32EntrySize-1);
}
break;
default:
i32SelectedEntry = 0;
break;
}
} }

View File

@ -40,17 +40,17 @@ void SMART::readSMARTData(Drive* drive)
FILE* outputfileSmart = popen(cpComand, "r"); FILE* outputfileSmart = popen(cpComand, "r");
while ((getline(&cLine, &len, outputfileSmart)) != -1) while ((getline(&cLine, &len, outputfileSmart)) != -1)
{ {
string sLine = string(cLine); string sLine = string(cLine);
SMART::parseModelFamily(sLine); SMART::parseModelFamily(sLine);
SMART::parseModelName(sLine); SMART::parseModelName(sLine);
SMART::parseSerial(sLine); SMART::parseSerial(sLine);
SMART::parseCapacity(sLine); SMART::parseCapacity(sLine);
SMART::parseErrorCount(sLine); SMART::parseErrorCount(sLine);
SMART::parsePowerOnHours(sLine); SMART::parsePowerOnHours(sLine);
SMART::parsePowerCycle(sLine); SMART::parsePowerCycle(sLine);
} }
fclose(outputfileSmart); fclose(outputfileSmart);
drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycle); //wirte data in drive drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycle); //wirte data in drive
} }
@ -64,11 +64,12 @@ void SMART::parseModelFamily(string sLine)
{ {
string search("\"model_family\": "); string search("\"model_family\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) { if (found!=string::npos)
sLine.erase(0, sLine.find(": ") + 3); {
sLine.erase(sLine.length()-3, 3); sLine.erase(0, sLine.find(": ") + 3);
modelFamily = sLine; sLine.erase(sLine.length()-3, 3);
} modelFamily = sLine;
}
} }
/** /**
@ -80,11 +81,12 @@ void SMART::parseModelName(string sLine)
{ {
string search("\"model_name\": "); string search("\"model_name\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) { if (found!=string::npos)
sLine.erase(0, sLine.find(": ") + 3); {
sLine.erase(sLine.length()-3, 3); sLine.erase(0, sLine.find(": ") + 3);
modelName = sLine; sLine.erase(sLine.length()-3, 3);
} modelName = sLine;
}
} }
/** /**
@ -96,11 +98,12 @@ void SMART::parseSerial(string sLine)
{ {
string search("\"serial_number\": "); string search("\"serial_number\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) { if (found!=string::npos)
sLine.erase(0, sLine.find(": ") + 3); {
sLine.erase(sLine.length()-3, 3); sLine.erase(0, sLine.find(": ") + 3);
serial = sLine; sLine.erase(sLine.length()-3, 3);
} serial = sLine;
}
} }
/** /**
@ -112,11 +115,12 @@ void SMART::parseCapacity(string sLine)
{ {
string search("\"bytes\": "); string search("\"bytes\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) { if (found!=string::npos)
sLine.erase(0, sLine.find(": ") + 2); {
sLine.erase(sLine.length()-1, 1); sLine.erase(0, sLine.find(": ") + 2);
capacity = stol(sLine); sLine.erase(sLine.length()-1, 1);
} capacity = stol(sLine);
}
} }
/** /**
@ -129,11 +133,11 @@ void SMART::parseErrorCount(string sLine)
string search("\"error_count_total\": "); string search("\"error_count_total\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) if (found!=string::npos)
{ {
sLine.erase(0, sLine.find(": ")+2); sLine.erase(0, sLine.find(": ")+2);
sLine.erase(sLine.length()-2, 2); sLine.erase(sLine.length()-2, 2);
errorCount = stol(sLine); errorCount = stol(sLine);
} }
} }
/** /**
@ -146,12 +150,12 @@ void SMART::parsePowerOnHours(string sLine)
string search("\"hours\": "); string search("\"hours\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) if (found!=string::npos)
{ {
sLine.erase(0, sLine.find(": ") + 2); sLine.erase(0, sLine.find(": ") + 2);
sLine.erase(sLine.length()-1, 1); sLine.erase(sLine.length()-1, 1);
powerOnHours = stol(sLine); powerOnHours = stol(sLine);
} }
} }
/** /**
@ -164,11 +168,11 @@ void SMART::parsePowerCycle(string sLine)
string search("\"power_cycle_count\": "); string search("\"power_cycle_count\": ");
size_t found = sLine.find(search); size_t found = sLine.find(search);
if (found!=string::npos) if (found!=string::npos)
{ {
sLine.erase(0, sLine.find(": ") + 2); sLine.erase(0, sLine.find(": ") + 2);
sLine.erase(sLine.length()-2, 2); sLine.erase(sLine.length()-2, 2);
powerCycle = stol(sLine); powerCycle = stol(sLine);
} }
} }

View File

@ -36,10 +36,10 @@ void Wipe::wipeDrive(Drive* drive)
FILE* outputfileSmart = popen(cpComand, "r"); FILE* outputfileSmart = popen(cpComand, "r");
while ((getline(&cLine, &len, outputfileSmart)) != -1) while ((getline(&cLine, &len, outputfileSmart)) != -1)
{ {
string sLine = string(cLine); string sLine = string(cLine);
cout << sLine; cout << sLine;
} }
fclose(outputfileSmart); fclose(outputfileSmart);
auto t_end = chrono::high_resolution_clock::now(); auto t_end = chrono::high_resolution_clock::now();
@ -51,22 +51,22 @@ void Wipe::wipeDrive(Drive* drive)
uint64_t u64seconds = 0U; uint64_t u64seconds = 0U;
while(u64TimeMS >= 1000) while(u64TimeMS >= 1000)
{ {
u64seconds++; u64seconds++;
u64TimeMS = u64TimeMS - 1000; u64TimeMS = u64TimeMS - 1000;
} }
while(u64seconds >= 60) while(u64seconds >= 60)
{ {
u64minutes++; u64minutes++;
u64seconds = u64seconds - 60; u64seconds = u64seconds - 60;
} }
while(u64minutes >= 60) while(u64minutes >= 60)
{ {
u64hours++; u64hours++;
u64minutes = u64minutes - 60; u64minutes = u64minutes - 60;
} }
cout << "Elapsed time: " << u64hours << " h - " << u64minutes << " min - " << u64seconds << " sec" << endl; cout << "Elapsed time: " << u64hours << " h - " << u64minutes << " min - " << u64seconds << " sec" << endl;
} }