183 lines
5.7 KiB
C++
183 lines
5.7 KiB
C++
/**
|
|
* @file app.cpp
|
|
* @brief app logic
|
|
* @author hendrik schutter
|
|
* @date 01.05.2020
|
|
*/
|
|
|
|
#include "app.h"
|
|
|
|
/**
|
|
* \brief app constructor
|
|
* \param void
|
|
* \return instance of App
|
|
*/
|
|
App::App(void) {
|
|
cout << "created app" << endl;
|
|
}
|
|
|
|
/**
|
|
* \brief app logic
|
|
* \param void
|
|
* \return void
|
|
*/
|
|
void App::app_logic(void)
|
|
{
|
|
cout << "app logic" << endl;
|
|
|
|
//Drive* tmp = new Drive("test");
|
|
|
|
searchDrives(&listDrives); //search for new drives and store them in list
|
|
filterIgnoredDrives(&listDrives); //filter out ignored drives
|
|
|
|
addSMARTData(&listDrives);
|
|
|
|
printDrives(&listDrives); //print currently attached drives
|
|
}
|
|
|
|
/**
|
|
* \brief search attached drives on /dev/sd*
|
|
* \param pointer of list <struct drive> *listDrives
|
|
* \return void
|
|
*/
|
|
void App::searchDrives(list <Drive> *listDrives)
|
|
{
|
|
cout << "search drives ..." << endl;
|
|
char * cLine = NULL;
|
|
size_t len = 0;
|
|
|
|
FILE* outputfileHwinfo = popen("./hwinfo --short --disk", "r");
|
|
|
|
if (outputfileHwinfo == NULL)
|
|
{
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
while ((getline(&cLine, &len, outputfileHwinfo)) != -1)
|
|
{
|
|
if (string(cLine).find("/dev/sd") != string::npos)
|
|
{
|
|
Drive* tmpDrive = new Drive(string(cLine).substr (2,8));
|
|
listDrives->push_back(*tmpDrive);
|
|
}
|
|
}
|
|
fclose(outputfileHwinfo);
|
|
}
|
|
|
|
/**
|
|
* \brief filter out drives that are listed in "ignoreDrives.conf"
|
|
* \param pointer of list <struct drive> *listDrives
|
|
* \return void
|
|
*/
|
|
void App::filterIgnoredDrives(list <Drive> *listDrives)
|
|
{
|
|
string sDelimiter = ":";
|
|
string sIgnoredDrivePath;
|
|
string sIgnoredDriveUUID;
|
|
|
|
vector<tuple<string, string>> vtlIgnoredDevices; //store drives from ingnore file
|
|
|
|
ifstream input( "ignoreDrives.conf" ); //read ingnore file
|
|
|
|
for(string sLine; getline( input, sLine );)
|
|
{
|
|
if (string(sLine).find("/dev/sd") != string::npos)
|
|
{
|
|
size_t pos = 0;
|
|
string token;
|
|
while ((pos = sLine.find(sDelimiter)) != string::npos)
|
|
{
|
|
token = sLine.substr(0, pos);
|
|
sIgnoredDrivePath = token;
|
|
sLine.erase(0, pos + sDelimiter.length());
|
|
sIgnoredDriveUUID = sLine;
|
|
} //end while
|
|
//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
|
|
for(auto row : vtlIgnoredDevices)
|
|
{
|
|
//cout << get<0>(row) << " is " << get<1>(row) << endl;
|
|
list <Drive>::iterator it;
|
|
// loop through found drives
|
|
for (it = listDrives->begin(); it != listDrives->end(); ++it)
|
|
{
|
|
string sUUID;
|
|
if (!get<0>(row).compare(it->getPath())) //find same drive based on path
|
|
{
|
|
// cout << "Same drive path found" << endl;
|
|
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
|
|
{
|
|
if (string(cLine).find("PTUUID") != string::npos)
|
|
{
|
|
string sBlkidOut = string(cLine);
|
|
sBlkidOut.erase(0, 18);
|
|
sBlkidOut.erase(36, sBlkidOut.length() - 36);
|
|
sUUID = sBlkidOut;
|
|
//cout << "blkid uuid:" << sUUID << endl;
|
|
}
|
|
}
|
|
fclose(outputfileBlkid);
|
|
//cout << "blkid uuid:" << sUUID << endl;
|
|
|
|
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;
|
|
exit(EXIT_FAILURE); // exit to prevent accidentally shred a system drive
|
|
}
|
|
else
|
|
{
|
|
// same uuid found than in ignore file --> ignore this drive
|
|
it = listDrives->erase(it);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* \brief print drives with all information
|
|
* \param pointer of list <struct drive> *listDrives
|
|
* \return void
|
|
*/
|
|
void App::printDrives(list <Drive> *listDrives)
|
|
{
|
|
cout << "------------DRIVES---------------" << endl;
|
|
list <Drive>::iterator it;
|
|
for (it = listDrives->begin(); it != listDrives->end(); ++it)
|
|
{
|
|
cout << "Path: " << it->getPath() << endl;
|
|
cout << "ModelFamily: " << it->getModelFamily() << endl;
|
|
cout << "ModelName: " << it->getModelName() << endl;
|
|
cout << "Capacity: " << it->getCapacity() << endl;
|
|
cout << "Serial: " << it->getSerial() << endl;
|
|
cout << "PowerOnHours: " << it->getPowerOnHours() << endl;
|
|
cout << "SpinUpCount: " << it->getSpinUpCount() << endl;
|
|
cout << "ErrorCount: " << it->getErrorCount() << endl;
|
|
cout << endl;
|
|
}
|
|
cout << "---------------------------------" << endl;
|
|
}
|
|
|
|
void App::addSMARTData(list <Drive> *listDrives) {
|
|
list <Drive>::iterator it;
|
|
for (it = listDrives->begin(); it != listDrives->end(); ++it)
|
|
{
|
|
SMART::readSMARTData(*it);
|
|
}
|
|
} |