From 27b48de32a748d338a815a4737b3934ae4131509 Mon Sep 17 00:00:00 2001 From: localhorst Date: Tue, 4 Aug 2020 17:18:32 +0200 Subject: [PATCH] added filter algo for existing drives --- ignoreDrives.conf | 2 +- include/drive.h | 4 +- include/reHDD.h | 9 +-- include/tui.h | 10 +-- include/tui_data.h | 18 +++--- src/TUI/tui_data.cpp | 2 +- src/main.cpp | 2 +- src/reHDD.cpp | 142 +++++++++++++++++++------------------------ src/wipe.cpp | 6 +- 9 files changed, 90 insertions(+), 105 deletions(-) diff --git a/ignoreDrives.conf b/ignoreDrives.conf index aef73e8..0605f89 100644 --- a/ignoreDrives.conf +++ b/ignoreDrives.conf @@ -1,3 +1,3 @@ +/dev/sdc:4673974d-1af2-44fd-996b-a2d8e4c43d9a /dev/sda:508ef27d-5039-4e8b-9e2c-22d7528b7149 /dev/sdb:07f4ad14-c4b6-46e7-9cdf-3cfa9841d53d -/dev/sdc:4673974d-1af2-44fd-996b-a2d8e4c43d9a diff --git a/include/drive.h b/include/drive.h index bfd33da..3f96677 100644 --- a/include/drive.h +++ b/include/drive.h @@ -48,8 +48,8 @@ private: uint32_t u32PowerCycles = 0U; uint32_t u32ShredPercentage = 0U; //in percent - - + + }; diff --git a/include/reHDD.h b/include/reHDD.h index 48fd308..94a5a06 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -16,10 +16,11 @@ #include #include #include -#include +#include #include #include #include +#include using namespace std; @@ -49,12 +50,12 @@ private: vector vecDrives; //stores all drive data - static void searchDrives(vector * pvecDrives); + static void searchDrives(vector * pvecDrives); static void printDrives(vector * pvecDrives); - static void filterIgnoredDrives(vector * pvecDrives); + static void filterIgnoredDrives(vector * pvecDrives); static void addSMARTData(vector * pvecDrives); @@ -62,11 +63,11 @@ private: static void ThreadScannDevices(); + void filterNewDrives(vector * pvecOldDrives, vector * pvecNewDrives); - }; diff --git a/include/tui.h b/include/tui.h index bcdb7e6..3266e70 100644 --- a/include/tui.h +++ b/include/tui.h @@ -22,17 +22,17 @@ protected: public: - TUI(void); + TUI(void); - void initTUI(); + void initTUI(); void updateTUI(TUI_DATA data); - + private: - - void centerTitle(WINDOW *pwin, const char * title); + + void centerTitle(WINDOW *pwin, const char * title); diff --git a/include/tui_data.h b/include/tui_data.h index 3d5a383..5dab10f 100644 --- a/include/tui_data.h +++ b/include/tui_data.h @@ -23,22 +23,22 @@ protected: public: - TUI_DATA(vector * pvecDrives); - - + TUI_DATA(vector * pvecDrives); + + + - private: - - - string sCpuUsage; - string sRamUsage; - string sLocalTime; + + + string sCpuUsage; + string sRamUsage; + string sLocalTime; }; diff --git a/src/TUI/tui_data.cpp b/src/TUI/tui_data.cpp index b0255fa..8fc2ad8 100644 --- a/src/TUI/tui_data.cpp +++ b/src/TUI/tui_data.cpp @@ -11,7 +11,7 @@ TUI_DATA::TUI_DATA(vector * pvecDrives) { - + diff --git a/src/main.cpp b/src/main.cpp index 6f287a6..96a915e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ */ int main(void) { - // cout << "refurbishingHddTool" << endl; + // cout << "refurbishingHddTool" << endl; reHDD* app = new reHDD(); app->app_logic(); diff --git a/src/reHDD.cpp b/src/reHDD.cpp index ddb783f..4185445 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -39,123 +39,106 @@ void reHDD::app_logic(void) cout << "app logic" << endl; - int result = pipe(fdSearchDrives); + pipe(fdSearchDrives); FD_ZERO(&selectSet); FD_SET(fdSearchDrives[0], &selectSet); - thread thDevices(ThreadScannDevices); + thread thDevices(ThreadScannDevices); //start thread that scanns for drives - + while(1) { -while(1){ + select(FD_SETSIZE, &selectSet, NULL, NULL, NULL); - int iSelectReturn = select(FD_SETSIZE, &selectSet, NULL, NULL, NULL); + if( FD_ISSET(fdSearchDrives[0], &selectSet)) { + char dummy; + read (fdSearchDrives[0],&dummy,1); + mxScannDrives.lock(); + printDrives(&vecNewDrives); + //replace with old list + // action if needed - if( FD_ISSET(fdSearchDrives[0], &selectSet)) { - - char ch; - - - result = read (fdSearchDrives[0],&ch,1); - - if (result != 1) { - perror("read"); - exit(3); - } - - printf ("From Main Thread: %c\n", ch); - - mxScannDrives.lock(); - printDrives(&vecNewDrives); - //replace with old list - // action if needed - mxScannDrives.unlock(); - - } + filterNewDrives(&vecDrives, &vecDrives); - + mxScannDrives.unlock(); } + } thDevices.join(); -/* + /* - size_t u64SelectedDriveIndex = 0U; - size_t u64DriveVecSize = (vecDrives.size()); + size_t u64SelectedDriveIndex = 0U; + size_t u64DriveVecSize = (vecDrives.size()); - cout << "Select drive to wipe:" << endl; - cin >> u64SelectedDriveIndex; - cout << "Selected drive index: " << u64SelectedDriveIndex << endl; + cout << "Select drive to wipe:" << endl; + cin >> u64SelectedDriveIndex; + cout << "Selected drive index: " << u64SelectedDriveIndex << endl; - if(u64SelectedDriveIndex < (u64DriveVecSize)) { - // Wipe::wipeDrive(&vecDrives[u64SelectedDriveIndex]); + if(u64SelectedDriveIndex < (u64DriveVecSize)) { + // Wipe::wipeDrive(&vecDrives[u64SelectedDriveIndex]); + } + */ +} + +void reHDD::ThreadScannDevices() { + while(true) { + cout << "Thread" << endl; + 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::ThreadScannDevices(){ +void reHDD::filterNewDrives(vector * pvecOldDrives, vector * pvecNewDrives){ + vector ::iterator itOld; //Iterator for current (old) drive list + vector ::iterator itNew; //Iterator for new drive list that was created from to scann thread -while(true){ + vector vecOfflineDrives; //TODO -cout << "Thread" << endl; - - - 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(); - - - - - - - int result = write (fdSearchDrives[1], "A",1); - //cout << result << endl; - if (result != 1){ - perror ("write error"); - // exit (2); - } - - sleep(5); //sleep 5 sec -} + for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld) + { + for (itNew = pvecOldDrives->begin(); itNew != pvecOldDrives->end(); ++itNew) + { + if(itOld->getSerial() == itNew->getSerial()){ + //drive exists already --> remove it from old list + pvecOldDrives->erase(itOld); + } + } + } + copy(pvecOldDrives->begin(), pvecOldDrives->end(), back_inserter(vecOfflineDrives)); + cout << "start offline" << endl; + printDrives(&vecOfflineDrives); + cout << "end offline" << endl; } - - /** * \brief search attached drives on /dev/sd* * \param pointer of vector * pvecDrives * \return void */ - void reHDD::searchDrives(vector * pvecDrives) +void reHDD::searchDrives(vector * pvecDrives) { - - - - - cout << "search drives ..." << endl; char * cLine = NULL; size_t len = 0; @@ -176,8 +159,6 @@ cout << "Thread" << endl; } } fclose(outputfileHwinfo); - - } /** @@ -213,10 +194,11 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) vtlIgnoredDevices.emplace_back(sIgnoredDrivePath, sIgnoredDriveUUID); //add found path and uuid from ingnore file to vector } } - //loop through found entries in ingnore file for(auto row : vtlIgnoredDevices) { + cout << "file drive path found: " << get<0>(row) << endl; + auto it = pvecDrives->begin(); while (it != pvecDrives->end()) { @@ -224,12 +206,12 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) string sUUID; if (!get<0>(row).compare(it->getPath())) //find same drive based on path { - // cout << "Same drive path found" << endl; + cout << "Same drive path found: " << it->getPath() << endl; char * cLine = NULL; size_t len = 0; string sCMD = "blkid "; sCMD.append(it->getPath()); - // cout << "cmd: " << sCMD << endl; + cout << "cmd: " << sCMD << endl; FILE* outputfileBlkid = popen(sCMD.c_str(), "r"); //get UUID from drive if (outputfileBlkid == NULL) { @@ -248,7 +230,7 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) } } 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 { @@ -259,6 +241,8 @@ void reHDD::filterIgnoredDrives(vector * pvecDrives) { // same uuid found than in ignore file --> ignore this drive it = pvecDrives->erase(it); + cout << "same uuid found than in ignore file --> ignore this drive:" << it->getPath() << endl; + } } diff --git a/src/wipe.cpp b/src/wipe.cpp index a5626f9..827328c 100644 --- a/src/wipe.cpp +++ b/src/wipe.cpp @@ -50,19 +50,19 @@ void Wipe::wipeDrive(Drive* drive) uint64_t u64minutes = 0U; uint64_t u64seconds = 0U; - while(u64TimeMS >= 1000) + while(u64TimeMS >= 1000) { u64seconds++; u64TimeMS = u64TimeMS - 1000; } - while(u64seconds >= 60) + while(u64seconds >= 60) { u64minutes++; u64seconds = u64seconds - 60; } - while(u64minutes >= 60) + while(u64minutes >= 60) { u64hours++; u64minutes = u64minutes - 60;