Skip to content
Snippets Groups Projects
Commit ae5773fe authored by Philip Müller's avatar Philip Müller
Browse files

Merge branch 'development'

parents 1ba19e37 6408a0a2
No related branches found
No related tags found
No related merge requests found
...@@ -22,28 +22,22 @@ ...@@ -22,28 +22,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Config.hpp"
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Config.hpp"
Config::Config(std::string configPath, std::string type) Config::Config(std::string configPath, std::string type)
: type_(type), basePath_(configPath.substr(0, configPath.find_last_of('/'))), : type_(type), basePath_(configPath.substr(0, configPath.find_last_of('/'))),
configPath_(configPath) configPath_(configPath), hwdIDs_(1)
{ {}
if (hwdIDs_.empty())
{
Config::HardwareID hwdID;
hwdIDs_.push_back(hwdID);
}
}
bool Config::readConfigFile(std::string configPath) bool Config::readConfigFile(std::string configPath)
{ {
std::ifstream file(configPath.c_str()); std::ifstream file(configPath);
if (!file.is_open()) if (!file)
{ {
return false; return false;
} }
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Data.hpp"
#include <dirent.h> #include <dirent.h>
#include <algorithm> #include <algorithm>
...@@ -31,20 +33,14 @@ ...@@ -31,20 +33,14 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Data.hpp"
Data::Data() Data::Data()
{ {
fillDevices("PCI"); fillDevices(hw_pci, PCIDevices);
fillDevices("USB"); fillDevices(hw_usb, USBDevices);
updateConfigData(); updateConfigData();
} }
Data::~Data()
{
}
void Data::updateInstalledConfigData() void Data::updateInstalledConfigData()
{ {
// Clear config vectors in each device element // Clear config vectors in each device element
...@@ -133,17 +129,10 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev ...@@ -133,17 +129,10 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
for (auto&& i_device = devices.begin(); i_device != devices.end(); for (auto&& i_device = devices.begin(); i_device != devices.end();
++i_device) ++i_device)
{ {
bool found = false;
// Check class ids // Check class ids
for (auto&& classID = hwdID->classIDs.begin(); bool found = std::find_if(hwdID->classIDs.begin(), hwdID->classIDs.end(), [i_device](const std::string& classID){
classID != hwdID->classIDs.end(); ++classID) return (("*" == classID) || (classID == (*i_device)->classID_));
{ }) != hwdID->classIDs.end();
if (*classID == "*" || *classID == (*i_device)->classID_)
{
found = true;
break;
}
}
if (!found) if (!found)
{ {
...@@ -152,19 +141,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev ...@@ -152,19 +141,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
else else
{ {
// Check blacklisted class ids // Check blacklisted class ids
found = false; found = std::find_if(hwdID->blacklistedClassIDs.begin(), hwdID->blacklistedClassIDs.end(), [i_device](const std::string& blacklistedClassID){
return (blacklistedClassID == (*i_device)->classID_);
for (auto&& blacklistedClassID = }) != hwdID->blacklistedClassIDs.end();
(*hwdID).blacklistedClassIDs.begin();
blacklistedClassID != (*hwdID).blacklistedClassIDs.end();
++blacklistedClassID)
{
if (*blacklistedClassID == (*i_device)->classID_)
{
found = true;
break;
}
}
if (found) if (found)
{ {
...@@ -173,17 +152,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev ...@@ -173,17 +152,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
else else
{ {
// Check vendor ids // Check vendor ids
found = false; found = std::find_if(hwdID->vendorIDs.begin(), hwdID->vendorIDs.end(), [i_device](const std::string& vendorID){
return (("*" == vendorID) || (vendorID == (*i_device)->vendorID_));
for (auto&& vendorID = hwdID->vendorIDs.begin(); }) != hwdID->vendorIDs.end();
vendorID != hwdID->vendorIDs.end(); ++vendorID)
{
if (("*" == *vendorID) || (*vendorID == (*i_device)->vendorID_))
{
found = true;
break;
}
}
if (!found) if (!found)
{ {
...@@ -192,19 +163,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev ...@@ -192,19 +163,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
else else
{ {
// Check blacklisted vendor ids // Check blacklisted vendor ids
found = false; found = std::find_if(hwdID->blacklistedVendorIDs.begin(), hwdID->blacklistedVendorIDs.end(), [i_device](const std::string& blacklistedVendorID){
return (blacklistedVendorID == (*i_device)->vendorID_);
for (auto&& blacklistedVendorID = }) != hwdID->blacklistedVendorIDs.end();
hwdID->blacklistedVendorIDs.begin();
blacklistedVendorID != hwdID->blacklistedVendorIDs.end();
++blacklistedVendorID)
{
if (*blacklistedVendorID == (*i_device)->vendorID_)
{
found = true;
break;
}
}
if (found) if (found)
{ {
...@@ -213,17 +174,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev ...@@ -213,17 +174,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
else else
{ {
// Check device ids // Check device ids
found = false; found = std::find_if(hwdID->deviceIDs.begin(), hwdID->deviceIDs.end(), [i_device](const std::string& deviceID){
return (("*" == deviceID) || (deviceID == (*i_device)->deviceID_));
for (auto&& deviceID = hwdID->deviceIDs.begin(); }) != hwdID->deviceIDs.end();
deviceID != hwdID->deviceIDs.end(); ++deviceID)
{
if (("*" == *deviceID) || (*deviceID == (*i_device)->deviceID_))
{
found = true;
break;
}
}
if (!found) if (!found)
{ {
...@@ -232,20 +185,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev ...@@ -232,20 +185,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
else else
{ {
// Check blacklisted device ids // Check blacklisted device ids
found = false; found = std::find_if(hwdID->blacklistedDeviceIDs.begin(), hwdID->blacklistedDeviceIDs.end(), [i_device](const std::string& blacklistedDeviceID){
return (blacklistedDeviceID == (*i_device)->deviceID_);
for (auto&& blacklistedDeviceID = }) != hwdID->blacklistedDeviceIDs.end();
hwdID->blacklistedDeviceIDs.begin();
blacklistedDeviceID != hwdID->blacklistedDeviceIDs.end();
++blacklistedDeviceID)
{
if (*blacklistedDeviceID == (*i_device)->deviceID_)
{
found = true;
break;
}
}
if (found) if (found)
{ {
continue; continue;
...@@ -300,8 +242,8 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config, ...@@ -300,8 +242,8 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config,
configDependency != config->dependencies_.end(); ++configDependency) configDependency != config->dependencies_.end(); ++configDependency)
{ {
auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(), auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(),
[configDependency](const std::shared_ptr<Config>& rhs) -> bool { [configDependency](const std::shared_ptr<Config>& config) -> bool {
return (rhs->name_ == *configDependency); return (config->name_ == *configDependency);
}); });
if (found != installedConfigs.end()) if (found != installedConfigs.end())
...@@ -311,8 +253,8 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config, ...@@ -311,8 +253,8 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config,
else else
{ {
found = std::find_if(dependencies->begin(), dependencies->end(), found = std::find_if(dependencies->begin(), dependencies->end(),
[configDependency](const std::shared_ptr<Config>& rhs) -> bool { [configDependency](const std::shared_ptr<Config>& config) -> bool {
return (rhs->name_ == *configDependency); return (config->name_ == *configDependency);
}); });
if (found != dependencies->end()) if (found != dependencies->end())
...@@ -479,22 +421,8 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_p ...@@ -479,22 +421,8 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_p
return requirements; return requirements;
} }
void Data::fillDevices(std::string type) void Data::fillDevices(hw_item hw, std::vector<std::shared_ptr<Device>>& devices)
{ {
hw_item hw;
std::vector<std::shared_ptr<Device>>* devices;
if ("USB" == type)
{
hw = hw_usb;
devices = &USBDevices;
}
else
{
hw = hw_pci;
devices = &PCIDevices;
}
// Get the hardware devices // Get the hardware devices
std::unique_ptr<hd_data_t> hd_data{new hd_data_t()}; std::unique_ptr<hd_data_t> hd_data{new hd_data_t()};
hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr); hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);
...@@ -503,7 +431,7 @@ void Data::fillDevices(std::string type) ...@@ -503,7 +431,7 @@ void Data::fillDevices(std::string type)
for (hd_t *hdIter = hd; hdIter; hdIter = hdIter->next) for (hd_t *hdIter = hd; hdIter; hdIter = hdIter->next)
{ {
device.reset(new Device()); device.reset(new Device());
device->type_ = type; device->type_ = (hw == hw_usb ? "USB" : "PCI");
device->classID_ = from_Hex(hdIter->base_class.id, 2) + from_Hex(hdIter->sub_class.id, 2).toLower(); device->classID_ = from_Hex(hdIter->base_class.id, 2) + from_Hex(hdIter->sub_class.id, 2).toLower();
device->vendorID_ = from_Hex(hdIter->vendor.id, 4).toLower(); device->vendorID_ = from_Hex(hdIter->vendor.id, 4).toLower();
device->deviceID_ = from_Hex(hdIter->device.id, 4).toLower(); device->deviceID_ = from_Hex(hdIter->device.id, 4).toLower();
...@@ -512,7 +440,7 @@ void Data::fillDevices(std::string type) ...@@ -512,7 +440,7 @@ void Data::fillDevices(std::string type)
device->deviceName_ = from_CharArray(hdIter->device.name); device->deviceName_ = from_CharArray(hdIter->device.name);
device->sysfsBusID_ = from_CharArray(hdIter->sysfs_bus_id); device->sysfsBusID_ = from_CharArray(hdIter->sysfs_bus_id);
device->sysfsID_ = from_CharArray(hdIter->sysfs_id); device->sysfsID_ = from_CharArray(hdIter->sysfs_id);
devices->emplace_back(device.release()); devices.emplace_back(device.release());
} }
hd_free_hd_list(hd); hd_free_hd_list(hd);
...@@ -551,23 +479,6 @@ void Data::fillAllConfigs(std::string type) ...@@ -551,23 +479,6 @@ void Data::fillAllConfigs(std::string type)
} }
} }
bool Data::fillConfig(std::shared_ptr<Config> config, std::string configPath, std::string type)
{
config->type_ = type;
config->priority_ = 0;
config->freedriver_ = true;
config->basePath_ = configPath.substr(0, configPath.find_last_of('/'));
config->configPath_ = configPath;
// Add new HardwareIDs group to vector if vector is empty
if (config->hwdIDs_.empty())
{
config->hwdIDs_.emplace_back();
}
return config->readConfigFile(config->configPath_);
}
std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& directoryPath, std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& directoryPath,
std::string onlyFilename) std::string onlyFilename)
{ {
...@@ -578,15 +489,14 @@ std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& ...@@ -578,15 +489,14 @@ std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string&
{ {
while (nullptr != (dir = readdir(d))) while (nullptr != (dir = readdir(d)))
{ {
std::string filename = dir->d_name; std::string filename {dir->d_name};
std::string filepath = directoryPath + "/" + filename;
if (("." == filename) || (".." == filename) || ("" == filename)) if (("." == filename) || (".." == filename) || ("" == filename))
{ {
continue; continue;
} }
else else
{ {
std::string filepath {directoryPath + "/" + filename};
struct stat filestatus; struct stat filestatus;
lstat(filepath.c_str(), &filestatus); lstat(filepath.c_str(), &filestatus);
...@@ -627,7 +537,7 @@ Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigP ...@@ -627,7 +537,7 @@ Vita::string Data::getRightConfigPath(Vita::string str, Vita::string baseConfigP
std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding) std::vector<std::string> Data::splitValue(Vita::string str, Vita::string onlyEnding)
{ {
std::vector<Vita::string> work = str.toLower().explode(" "); std::vector<Vita::string> work {str.toLower().explode(" ")};
std::vector<std::string> final; std::vector<std::string> final;
for (auto&& iterator = work.begin(); iterator != work.end(); for (auto&& iterator = work.begin(); iterator != work.end();
......
...@@ -34,16 +34,18 @@ ...@@ -34,16 +34,18 @@
#include <vector> #include <vector>
#include "Config.hpp" #include "Config.hpp"
#include "Device.hpp"
#include "const.h" #include "const.h"
#include "Device.hpp"
#include "vita/string.hpp" #include "vita/string.hpp"
class Data { class Data
{
public: public:
Data(); Data();
~Data(); ~Data() = default;
struct Environment { struct Environment
{
std::string PMCachePath {MHWD_PM_CACHE_DIR}; std::string PMCachePath {MHWD_PM_CACHE_DIR};
std::string PMConfigPath {MHWD_PM_CONFIG}; std::string PMConfigPath {MHWD_PM_CONFIG};
std::string PMRootPath {MHWD_PM_ROOT}; std::string PMRootPath {MHWD_PM_ROOT};
...@@ -61,7 +63,6 @@ public: ...@@ -61,7 +63,6 @@ public:
void updateInstalledConfigData(); void updateInstalledConfigData();
void getAllDevicesOfConfig(std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices); void getAllDevicesOfConfig(std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices);
bool fillConfig(std::shared_ptr<Config> config, std::string configPath, std::string type);
std::vector<std::shared_ptr<Config>> getAllDependenciesToInstall(std::shared_ptr<Config> config); std::vector<std::shared_ptr<Config>> getAllDependenciesToInstall(std::shared_ptr<Config> config);
void getAllDependenciesToInstall(std::shared_ptr<Config> config, void getAllDependenciesToInstall(std::shared_ptr<Config> config,
...@@ -76,7 +77,7 @@ private: ...@@ -76,7 +77,7 @@ private:
void getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& devices, void getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& devices,
std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices); std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices);
void fillInstalledConfigs(std::string type); void fillInstalledConfigs(std::string type);
void fillDevices(std::string type); void fillDevices(hw_item hw, std::vector<std::shared_ptr<Device>>& devices);
void fillAllConfigs(std::string type); void fillAllConfigs(std::string type);
void setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices, void setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices,
std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled); std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled);
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
*/ */
#include "Mhwd.hpp" #include "Mhwd.hpp"
#include "vita/string.hpp"
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -42,9 +41,7 @@ ...@@ -42,9 +41,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
Mhwd::Mhwd() : arguments_(), data_(), printer_() #include "vita/string.hpp"
{
}
bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE transactionType) bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE transactionType)
{ {
...@@ -167,53 +164,27 @@ bool Mhwd::isUserRoot() const ...@@ -167,53 +164,27 @@ bool Mhwd::isUserRoot() const
return true; return true;
} }
std::string Mhwd::checkEnvironment() std::vector<std::string> Mhwd::checkEnvironment() const
{ {
std::string missingDir; std::vector<std::string> missingDirs;
// Check if required directories exists. Otherwise return missing directory...
if (!dirExists(MHWD_USB_CONFIG_DIR)) if (!dirExists(MHWD_USB_CONFIG_DIR))
{ {
missingDir = MHWD_USB_CONFIG_DIR; missingDirs.emplace_back(MHWD_USB_CONFIG_DIR);
} }
if (!dirExists(MHWD_PCI_CONFIG_DIR)) if (!dirExists(MHWD_PCI_CONFIG_DIR))
{ {
missingDir = MHWD_PCI_CONFIG_DIR; missingDirs.emplace_back(MHWD_PCI_CONFIG_DIR);
} }
if (!dirExists(MHWD_USB_DATABASE_DIR)) if (!dirExists(MHWD_USB_DATABASE_DIR))
{ {
missingDir = MHWD_USB_DATABASE_DIR; missingDirs.emplace_back(MHWD_USB_DATABASE_DIR);
} }
if (!dirExists(MHWD_PCI_DATABASE_DIR)) if (!dirExists(MHWD_PCI_DATABASE_DIR))
{ {
missingDir = MHWD_PCI_DATABASE_DIR; missingDirs.emplace_back(MHWD_PCI_DATABASE_DIR);
}
return missingDir;
}
void Mhwd::printDeviceDetails(std::string type, FILE *f)
{
hw_item hw;
if ("USB" == type)
{
hw = hw_usb;
}
else
{
hw = hw_pci;
} }
std::unique_ptr<hd_data_t> hd_data{new hd_data_t()}; return missingDirs;
hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);
for (hd_t* hdIter = hd; hdIter; hdIter = hdIter->next)
{
hd_dump_entry(hd_data.get(), hdIter, f);
}
hd_free_hd_list(hd);
hd_free_hd_data(hd_data.get());
} }
std::shared_ptr<Config> Mhwd::getInstalledConfig(const std::string& configName, std::shared_ptr<Config> Mhwd::getInstalledConfig(const std::string& configName,
...@@ -231,15 +202,15 @@ std::shared_ptr<Config> Mhwd::getInstalledConfig(const std::string& configName, ...@@ -231,15 +202,15 @@ std::shared_ptr<Config> Mhwd::getInstalledConfig(const std::string& configName,
installedConfigs = &data_.installedPCIConfigs; installedConfigs = &data_.installedPCIConfigs;
} }
for (auto&& installedConfig = installedConfigs->begin(); auto installedConfig = std::find_if(installedConfigs->begin(), installedConfigs->end(),
installedConfig != installedConfigs->end(); installedConfig++) [configName](const std::shared_ptr<Config>& config) {
return configName == config->name_;
});
if (installedConfig != installedConfigs->end())
{ {
if (configName == (*installedConfig)->name_) return *installedConfig;
{
return (*installedConfig);
}
} }
return nullptr; return nullptr;
} }
...@@ -258,15 +229,14 @@ std::shared_ptr<Config> Mhwd::getDatabaseConfig(const std::string& configName, ...@@ -258,15 +229,14 @@ std::shared_ptr<Config> Mhwd::getDatabaseConfig(const std::string& configName,
allConfigs = &data_.allPCIConfigs; allConfigs = &data_.allPCIConfigs;
} }
for (auto&& iterator = allConfigs->begin(); auto config = std::find_if(allConfigs->begin(), allConfigs->end(),
iterator != allConfigs->end(); ++iterator) [configName](const std::shared_ptr<Config>& config) {
return config->name_ == configName;
});
if (config != allConfigs->end())
{ {
if (configName == (*iterator)->name_) return *config;
{
return (*iterator);
}
} }
return nullptr; return nullptr;
} }
...@@ -294,13 +264,14 @@ std::shared_ptr<Config> Mhwd::getAvailableConfig(const std::string& configName, ...@@ -294,13 +264,14 @@ std::shared_ptr<Config> Mhwd::getAvailableConfig(const std::string& configName,
} }
else else
{ {
for (auto&& availableConfig = (*device)->availableConfigs_.begin(); auto& availableConfigs = (*device)->availableConfigs_;
availableConfig != (*device)->availableConfigs_.end(); availableConfig++) auto availableConfig = std::find_if(availableConfigs.begin(), availableConfigs.end(),
[configName](const std::shared_ptr<Config>& config){
return config->name_ == configName;
});
if (availableConfig != availableConfigs.end())
{ {
if (configName == (*availableConfig)->name_) return *availableConfig;
{
return (*availableConfig);
}
} }
} }
} }
...@@ -430,8 +401,6 @@ bool Mhwd::copyDirectory(const std::string& source, const std::string& destinati ...@@ -430,8 +401,6 @@ bool Mhwd::copyDirectory(const std::string& source, const std::string& destinati
while ((dir = readdir(d)) != nullptr) while ((dir = readdir(d)) != nullptr)
{ {
std::string filename {dir->d_name}; std::string filename {dir->d_name};
std::string sourcePath {source + "/" + filename};
std::string destinationPath {destination + "/" + filename};
if (("." == filename) || (".." == filename) || ("" == filename)) if (("." == filename) || (".." == filename) || ("" == filename))
{ {
...@@ -439,6 +408,8 @@ bool Mhwd::copyDirectory(const std::string& source, const std::string& destinati ...@@ -439,6 +408,8 @@ bool Mhwd::copyDirectory(const std::string& source, const std::string& destinati
} }
else else
{ {
std::string sourcePath {source + "/" + filename};
std::string destinationPath {destination + "/" + filename};
lstat(sourcePath.c_str(), &filestatus); lstat(sourcePath.c_str(), &filestatus);
if (S_ISREG(filestatus.st_mode)) if (S_ISREG(filestatus.st_mode))
...@@ -492,8 +463,7 @@ bool Mhwd::removeDirectory(const std::string& directory) ...@@ -492,8 +463,7 @@ bool Mhwd::removeDirectory(const std::string& directory)
struct dirent *dir; struct dirent *dir;
while ((dir = readdir(d)) != nullptr) while ((dir = readdir(d)) != nullptr)
{ {
std::string filename = std::string(dir->d_name); std::string filename {dir->d_name};
std::string filepath = directory + "/" + filename;
if (("." == filename) || (".." == filename) || ("" == filename)) if (("." == filename) || (".." == filename) || ("" == filename))
{ {
...@@ -501,6 +471,7 @@ bool Mhwd::removeDirectory(const std::string& directory) ...@@ -501,6 +471,7 @@ bool Mhwd::removeDirectory(const std::string& directory)
} }
else else
{ {
std::string filepath {directory + "/" + filename};
struct stat filestatus; struct stat filestatus;
lstat(filepath.c_str(), &filestatus); lstat(filepath.c_str(), &filestatus);
...@@ -530,7 +501,7 @@ bool Mhwd::removeDirectory(const std::string& directory) ...@@ -530,7 +501,7 @@ bool Mhwd::removeDirectory(const std::string& directory)
} }
} }
bool Mhwd::dirExists(const std::string& path) bool Mhwd::dirExists(const std::string& path) const
{ {
struct stat filestatus; struct stat filestatus;
if (0 != stat(path.c_str(), &filestatus)) if (0 != stat(path.c_str(), &filestatus))
...@@ -956,6 +927,17 @@ bool Mhwd::optionsDontInterfereWithEachOther() const ...@@ -956,6 +927,17 @@ bool Mhwd::optionsDontInterfereWithEachOther() const
int Mhwd::launch(int argc, char *argv[]) int Mhwd::launch(int argc, char *argv[])
{ {
std::vector<std::string> missingDirs { checkEnvironment() };
if (!missingDirs.empty())
{
printer_.printError("Following directories do not exist:");
for (const auto& dir : missingDirs)
{
printer_.printStatus(dir);
}
return 1;
}
std::string operationType; std::string operationType;
bool autoConfigureNonFreeDriver = false; bool autoConfigureNonFreeDriver = false;
std::string autoConfigureClassID; std::string autoConfigureClassID;
...@@ -977,14 +959,6 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -977,14 +959,6 @@ int Mhwd::launch(int argc, char *argv[])
return 1; return 1;
} }
// Check environment
std::string missingDir { checkEnvironment() };
if (!missingDir.empty())
{
printer_.printError("directory '" + missingDir + "' does not exist!");
return 1;
}
// Check for invalid configs // Check for invalid configs
for (auto&& invalidConfig : data_.invalidConfigs) for (auto&& invalidConfig : data_.invalidConfigs)
{ {
...@@ -1104,7 +1078,7 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1104,7 +1078,7 @@ int Mhwd::launch(int argc, char *argv[])
{ {
if (arguments_.DETAIL) if (arguments_.DETAIL)
{ {
printDeviceDetails("PCI"); printer_.printDeviceDetails(hw_pci);
} }
else else
{ {
...@@ -1115,7 +1089,7 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1115,7 +1089,7 @@ int Mhwd::launch(int argc, char *argv[])
{ {
if (arguments_.DETAIL) if (arguments_.DETAIL)
{ {
printDeviceDetails("USB"); printer_.printDeviceDetails(hw_usb);
} }
else else
{ {
...@@ -1171,8 +1145,6 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1171,8 +1145,6 @@ int Mhwd::launch(int argc, char *argv[])
} }
else else
{ {
bool alreadyInList = std::find(configs_.begin(), configs_.end(), config->name_) != configs_.end();
// If force is not set then skip found config // If force is not set then skip found config
bool skip = false; bool skip = false;
if (!arguments_.FORCE) if (!arguments_.FORCE)
...@@ -1180,8 +1152,7 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1180,8 +1152,7 @@ int Mhwd::launch(int argc, char *argv[])
skip = std::find_if(installedConfigs->begin(), installedConfigs->end(), skip = std::find_if(installedConfigs->begin(), installedConfigs->end(),
[&config](const std::shared_ptr<Config>& conf) -> bool { [&config](const std::shared_ptr<Config>& conf) -> bool {
return conf->name_ == config->name_; return conf->name_ == config->name_;
}) }) != installedConfigs->end();
!= installedConfigs->end();
} }
// Print found config // Print found config
if (skip) if (skip)
...@@ -1203,6 +1174,7 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1203,6 +1174,7 @@ int Mhwd::launch(int argc, char *argv[])
device->deviceName_); device->deviceName_);
} }
bool alreadyInList = std::find(configs_.begin(), configs_.end(), config->name_) != configs_.end();
if (!alreadyInList && !skip) if (!alreadyInList && !skip)
{ {
configs_.push_back(config->name_); configs_.push_back(config->name_);
...@@ -1224,7 +1196,11 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1224,7 +1196,11 @@ int Mhwd::launch(int argc, char *argv[])
// Transaction // Transaction
if (arguments_.INSTALL || arguments_.REMOVE) if (arguments_.INSTALL || arguments_.REMOVE)
{ {
if (isUserRoot()) if (!isUserRoot())
{
printer_.printError("You cannot perform this operation unless you are root!");
}
else
{ {
for (auto&& configName = configs_.begin(); for (auto&& configName = configs_.begin();
configName != configs_.end(); configName++) configName != configs_.end(); configName++)
...@@ -1248,7 +1224,7 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1248,7 +1224,7 @@ int Mhwd::launch(int argc, char *argv[])
else else
{ {
config_.reset(new Config(filepath, operationType)); config_.reset(new Config(filepath, operationType));
if (!data_.fillConfig(config_, filepath, operationType)) if (!config_->readConfigFile(filepath))
{ {
printer_.printError("failed to read custom config '" + filepath + "'!"); printer_.printError("failed to read custom config '" + filepath + "'!");
return 1; return 1;
...@@ -1300,10 +1276,6 @@ int Mhwd::launch(int argc, char *argv[]) ...@@ -1300,10 +1276,6 @@ int Mhwd::launch(int argc, char *argv[])
} }
} }
} }
else
{
printer_.printError("You cannot perform this operation unless you are root!");
}
} }
return 0; return 0;
} }
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
class Mhwd class Mhwd
{ {
public: public:
Mhwd(); Mhwd() = default;
~Mhwd() = default; ~Mhwd() = default;
void setVersionMhwd(std::string versionOfSoftware, std::string yearCopyright); void setVersionMhwd(std::string versionOfSoftware, std::string yearCopyright);
int launch(int argc, char *argv[]); int launch(int argc, char *argv[]);
...@@ -77,9 +77,7 @@ private: ...@@ -77,9 +77,7 @@ private:
bool performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE type); bool performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE type);
bool isUserRoot() const; bool isUserRoot() const;
std::string checkEnvironment(); std::vector<std::string> checkEnvironment() const;
void printDeviceDetails(std::string type, FILE *f = stdout);
std::shared_ptr<Config> getInstalledConfig(const std::string& configName, const std::string& configType); std::shared_ptr<Config> getInstalledConfig(const std::string& configName, const std::string& configType);
std::shared_ptr<Config> getDatabaseConfig(const std::string& configName, const std::string& configType); std::shared_ptr<Config> getDatabaseConfig(const std::string& configName, const std::string& configType);
...@@ -94,7 +92,7 @@ private: ...@@ -94,7 +92,7 @@ private:
bool copyFile(const std::string& source, const std::string destination, const mode_t mode = bool copyFile(const std::string& source, const std::string destination, const mode_t mode =
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH); S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH);
bool removeDirectory(const std::string& directory); bool removeDirectory(const std::string& directory);
bool dirExists(const std::string& path); bool dirExists(const std::string& path) const;
bool createDir(const std::string& path, const mode_t mode = bool createDir(const std::string& path, const mode_t mode =
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH); S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
......
...@@ -22,28 +22,31 @@ ...@@ -22,28 +22,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Printer.hpp"
#include <hd.h>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Printer.hpp"
void Printer::printStatus(std::string statusMsg) const void Printer::printStatus(std::string statusMsg) const
{ {
std::cout << CONSOLE_MESSAGE_COLOR << "> " std::cout << CONSOLE_RED_MESSAGE_COLOR << "> "
<< CONSOLE_COLOR_RESET << statusMsg << std::endl; << CONSOLE_COLOR_RESET << statusMsg << std::endl;
} }
void Printer::printError(std::string errorMsg) const void Printer::printError(std::string errorMsg) const
{ {
std::cout << CONSOLE_MESSAGE_COLOR << "Error: " std::cout << CONSOLE_RED_MESSAGE_COLOR << "Error: "
<< CONSOLE_COLOR_RESET << errorMsg << std::endl; << CONSOLE_COLOR_RESET << errorMsg << std::endl;
} }
void Printer::printWarning(std::string warningMsg) const void Printer::printWarning(std::string warningMsg) const
{ {
std::cout << CONSOLE_MESSAGE_COLOR << "Warning: " std::cout << CONSOLE_RED_MESSAGE_COLOR << "Warning: "
<< CONSOLE_COLOR_RESET << warningMsg << std::endl; << CONSOLE_COLOR_RESET << warningMsg << std::endl;
} }
...@@ -102,9 +105,9 @@ void Printer::printHelp() const ...@@ -102,9 +105,9 @@ void Printer::printHelp() const
void Printer::printVersion(std::string versionMhwd, std::string yearCopy) const void Printer::printVersion(std::string versionMhwd, std::string yearCopy) const
{ {
std::cout << "Manjaro Hardware Detection version "<< versionMhwd <<"\n\n" std::cout << "Manjaro Hardware Detection v"<< versionMhwd <<"\n\n"
<< "Copyright (C) "<< yearCopy <<" Manjaro Linux Developers\n" << "Copyright (C) "<< yearCopy <<" Manjaro Linux Developers\n"
<< "This is free software licensed under GNU GPL v.3\n" << "This is free software licensed under GNU GPL v3.0\n"
<< "FITNESS FOR A PARTICULAR PURPOSE.\n" << "FITNESS FOR A PARTICULAR PURPOSE.\n"
<< std::endl; << std::endl;
} }
...@@ -268,3 +271,17 @@ void Printer::printLine() const ...@@ -268,3 +271,17 @@ void Printer::printLine() const
{ {
std::cout << std::setfill('-') << std::setw(80) << "-" << std::setfill(' ') << std::endl; std::cout << std::setfill('-') << std::setw(80) << "-" << std::setfill(' ') << std::endl;
} }
void Printer::printDeviceDetails(hw_item hw, FILE *f) const
{
std::unique_ptr<hd_data_t> hd_data{new hd_data_t()};
hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);
for (hd_t* hdIter = hd; hdIter; hdIter = hdIter->next)
{
hd_dump_entry(hd_data.get(), hdIter, f);
}
hd_free_hd_list(hd);
hd_free_hd_data(hd_data.get());
}
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#ifndef PRINTER_HPP_ #ifndef PRINTER_HPP_
#define PRINTER_HPP_ #define PRINTER_HPP_
#include <hd.h>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -50,11 +52,12 @@ public: ...@@ -50,11 +52,12 @@ public:
void printInstalledConfigs(const std::string& deviceType, void printInstalledConfigs(const std::string& deviceType,
const std::vector<std::shared_ptr<Config>>& installedConfigs) const; const std::vector<std::shared_ptr<Config>>& installedConfigs) const;
void printConfigDetails(const Config& config) const; void printConfigDetails(const Config& config) const;
void printDeviceDetails(hw_item hw, FILE *f = stdout) const;
private: private:
void printLine() const; void printLine() const;
const char* CONSOLE_COLOR_RESET {"\033[m"}; const char* CONSOLE_COLOR_RESET {"\033[m"};
const char* CONSOLE_MESSAGE_COLOR {"\033[1m\033[31m"}; const char* CONSOLE_RED_MESSAGE_COLOR {"\033[1m\033[31m"};
const char* CONSOLE_TEXT_OUTPUT_COLOR {"\033[0;32m"}; const char* CONSOLE_TEXT_OUTPUT_COLOR {"\033[0;32m"};
}; };
......
...@@ -22,16 +22,16 @@ ...@@ -22,16 +22,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Mhwd.hpp"
#include <iostream> #include <iostream>
#include "Mhwd.hpp"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
try try
{ {
Mhwd mhwd; Mhwd mhwd;
mhwd.setVersionMhwd("0.5.2","2012 2013 2014 2015"); mhwd.setVersionMhwd("0.5.3","2012-2015");
return mhwd.launch(argc, argv); return mhwd.launch(argc, argv);
} }
catch(...) catch(...)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment