detect offline drives

This commit is contained in:
Hendrik Schutter 2020-08-04 19:51:34 +02:00
parent 27b48de32a
commit 89f900c970
3 changed files with 32 additions and 29 deletions

View File

@ -1,3 +1,3 @@
/dev/sdc:4673974d-1af2-44fd-996b-a2d8e4c43d9a /dev/sdc:4673974d-1af2-44fd-996b-a2d8e4c43d9a
/dev/sda:508ef27d-5039-4e8b-9e2c-22d7528b7149 /dev/sda:508ef27d-5039-4e8b-9e2c-22d7528b7149
/dev/sdb:07f4ad14-c4b6-46e7-9cdf-3cfa9841d53d /dev/sdb:32b66944-ffa0-40e9-817c-3f0c52eefaf4

View File

@ -8,6 +8,8 @@
#ifndef REHDD_H_ #ifndef REHDD_H_
#define REHDD_H_ #define REHDD_H_
#define DRYRUN
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fstream> #include <fstream>

View File

@ -46,6 +46,7 @@ void reHDD::app_logic(void)
thread thDevices(ThreadScannDevices); //start thread that scanns for drives thread thDevices(ThreadScannDevices); //start thread that scanns for drives
while(1) { while(1) {
select(FD_SETSIZE, &selectSet, NULL, NULL, NULL); select(FD_SETSIZE, &selectSet, NULL, NULL, NULL);
@ -54,16 +55,16 @@ void reHDD::app_logic(void)
char dummy; char dummy;
read (fdSearchDrives[0],&dummy,1); read (fdSearchDrives[0],&dummy,1);
mxScannDrives.lock(); mxScannDrives.lock();
printDrives(&vecNewDrives);
//replace with old list //replace with old list
// action if needed // action if needed
filterNewDrives(&vecDrives, &vecDrives); filterNewDrives(&vecDrives, &vecNewDrives);
printDrives(&vecDrives);
mxScannDrives.unlock(); mxScannDrives.unlock();
} }
@ -72,7 +73,7 @@ void reHDD::app_logic(void)
thDevices.join(); // thDevices.join();
/* /*
@ -105,30 +106,33 @@ void reHDD::ThreadScannDevices() {
} }
} }
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives){ 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
vector <Drive> vecOfflineDrives; //TODO
for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld) for (itOld = pvecOldDrives->begin(); itOld != pvecOldDrives->end(); ++itOld)
{ {
for (itNew = pvecOldDrives->begin(); itNew != pvecOldDrives->end(); ++itNew) bool bOldDriveIsOffline = true;
for (itNew = pvecNewDrives->begin(); itNew != pvecNewDrives->end(); ++itNew)
{ {
if(itOld->getSerial() == itNew->getSerial()){ if(itOld->getSerial() == itNew->getSerial()) {
//drive exists already --> remove it from old list bOldDriveIsOffline = false;
pvecOldDrives->erase(itOld); // cout << "already online drive found: " << itOld->getPath() << endl;
} }
} }
}
copy(pvecOldDrives->begin(), pvecOldDrives->end(), back_inserter(vecOfflineDrives)); if(bOldDriveIsOffline == true) {
cout << "offline drive found: " << itOld->getPath() << endl;
//TODO kill wipe thread
}
}
cout << "start offline" << endl; pvecOldDrives->clear();
printDrives(&vecOfflineDrives);
cout << "end offline" << endl; for (long unsigned int i=0; i<pvecNewDrives->size(); i++) {
pvecOldDrives->push_back((*pvecNewDrives)[i]);
}
} }
@ -174,6 +178,8 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
vector<tuple<string, string>> vtlIgnoredDevices; //store drives from ingnore file vector<tuple<string, string>> vtlIgnoredDevices; //store drives from ingnore file
//vector <Drive> vecTmpDrives
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 );)
@ -197,21 +203,17 @@ 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)
{ {
cout << "file drive path found: " << get<0>(row) << endl; vector <Drive>::iterator it;
for (it = pvecDrives->begin(); it != pvecDrives->end(); ++it)
auto it = pvecDrives->begin();
while (it != pvecDrives->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
{ {
cout << "Same drive path found: " << it->getPath() << endl;
char * cLine = NULL; char * cLine = NULL;
size_t len = 0; size_t len = 0;
string sCMD = "blkid "; string sCMD = "blkid ";
sCMD.append(it->getPath()); sCMD.append(it->getPath());
cout << "cmd: " << sCMD << endl; //cout << "cmd: " << sCMD << endl;
FILE* outputfileBlkid = popen(sCMD.c_str(), "r"); //get UUID from drive FILE* outputfileBlkid = popen(sCMD.c_str(), "r"); //get UUID from drive
if (outputfileBlkid == NULL) if (outputfileBlkid == NULL)
{ {
@ -230,7 +232,7 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
} }
} }
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
{ {
@ -241,10 +243,9 @@ void reHDD::filterIgnoredDrives(vector <Drive>* pvecDrives)
{ {
// same uuid found than in ignore file --> ignore this drive // same uuid found than in ignore file --> ignore this drive
it = pvecDrives->erase(it); it = pvecDrives->erase(it);
cout << "same uuid found than in ignore file --> ignore this drive:" << it->getPath() << endl; it--;
//cout << "same uuid found than in ignore file --> ignore this drive:" << it->getPath() << endl;
} }
} }
} }
} }