added filter algo for existing drives
This commit is contained in:
parent
a61d4321f6
commit
27b48de32a
|
@ -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
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
#include <sys/select.h>
|
||||
#include<algorithm>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
@ -62,7 +63,7 @@ private:
|
|||
static void ThreadScannDevices();
|
||||
|
||||
|
||||
|
||||
void filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives);
|
||||
|
||||
|
||||
|
||||
|
|
108
src/reHDD.cpp
108
src/reHDD.cpp
|
@ -39,47 +39,34 @@ 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) {
|
||||
|
||||
int iSelectReturn = select(FD_SETSIZE, &selectSet, NULL, NULL, NULL);
|
||||
|
||||
select(FD_SETSIZE, &selectSet, NULL, NULL, NULL);
|
||||
|
||||
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);
|
||||
|
||||
char dummy;
|
||||
read (fdSearchDrives[0],&dummy,1);
|
||||
mxScannDrives.lock();
|
||||
printDrives(&vecNewDrives);
|
||||
//replace with old list
|
||||
// action if needed
|
||||
|
||||
|
||||
|
||||
filterNewDrives(&vecDrives, &vecDrives);
|
||||
|
||||
|
||||
|
||||
|
||||
mxScannDrives.unlock();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -105,45 +92,46 @@ while(1){
|
|||
}
|
||||
|
||||
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();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int result = write (fdSearchDrives[1], "A",1);
|
||||
//cout << result << endl;
|
||||
if (result != 1){
|
||||
perror ("write error");
|
||||
// exit (2);
|
||||
}
|
||||
|
||||
write (fdSearchDrives[1], "A",1);
|
||||
sleep(5); //sleep 5 sec
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives){
|
||||
|
||||
|
||||
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> vecOfflineDrives; //TODO
|
||||
|
||||
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 <Drive>* pvecDrives
|
||||
|
@ -151,11 +139,6 @@ cout << "Thread" << endl;
|
|||
*/
|
||||
void reHDD::searchDrives(vector <Drive>* 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 <Drive>* 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 <Drive>* 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 <Drive>* 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 <Drive>* 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue