Commit ae5773fe authored by Philip Müller's avatar Philip Müller

Merge branch 'development'

parents 1ba19e37 6408a0a2
......@@ -22,28 +22,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Config.hpp"
#include <fstream>
#include <string>
#include <vector>
#include "Config.hpp"
Config::Config(std::string configPath, std::string type)
: type_(type), basePath_(configPath.substr(0, configPath.find_last_of('/'))),
configPath_(configPath)
{
if (hwdIDs_.empty())
{
Config::HardwareID hwdID;
hwdIDs_.push_back(hwdID);
}
}
configPath_(configPath), hwdIDs_(1)
{}
bool Config::readConfigFile(std::string configPath)
{
std::ifstream file(configPath.c_str());
std::ifstream file(configPath);
if (!file.is_open())
if (!file)
{
return false;
}
......
This diff is collapsed.
......@@ -34,16 +34,18 @@
#include <vector>
#include "Config.hpp"
#include "Device.hpp"
#include "const.h"
#include "Device.hpp"
#include "vita/string.hpp"
class Data {
class Data
{
public:
Data();
~Data();
~Data() = default;
struct Environment {
struct Environment
{
std::string PMCachePath {MHWD_PM_CACHE_DIR};
std::string PMConfigPath {MHWD_PM_CONFIG};
std::string PMRootPath {MHWD_PM_ROOT};
......@@ -61,7 +63,6 @@ public:
void updateInstalledConfigData();
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);
void getAllDependenciesToInstall(std::shared_ptr<Config> config,
......@@ -76,7 +77,7 @@ private:
void getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& devices,
std::shared_ptr<Config> config, std::vector<std::shared_ptr<Device>>& foundDevices);
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 setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& devices,
std::vector<std::shared_ptr<Config>>& configs, bool setAsInstalled);
......
......@@ -23,7 +23,6 @@
*/
#include "Mhwd.hpp"
#include "vita/string.hpp"
#include <unistd.h>
#include <sys/stat.h>
......@@ -42,9 +41,7 @@
#include <string>
#include <vector>
Mhwd::Mhwd() : arguments_(), data_(), printer_()
{
}
#include "vita/string.hpp"
bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE transactionType)
{
......@@ -167,53 +164,27 @@ bool Mhwd::isUserRoot() const
return true;
}
std::string Mhwd::checkEnvironment()
std::vector<std::string> Mhwd::checkEnvironment() const
{
std::string missingDir;
// Check if required directories exists. Otherwise return missing directory...
std::vector<std::string> missingDirs;
if (!dirExists(MHWD_USB_CONFIG_DIR))
{
missingDir = MHWD_USB_CONFIG_DIR;
missingDirs.emplace_back(MHWD_USB_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))
{
missingDir = MHWD_USB_DATABASE_DIR;
missingDirs.emplace_back(MHWD_USB_DATABASE_DIR);
}
if (!dirExists(MHWD_PCI_DATABASE_DIR))
{
missingDir = 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;
missingDirs.emplace_back(MHWD_PCI_DATABASE_DIR);
}
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());
return missingDirs;
}
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;
}
for (auto&& installedConfig = installedConfigs->begin();
installedConfig != installedConfigs->end(); installedConfig++)
auto installedConfig = std::find_if(installedConfigs->begin(), installedConfigs->end(),
[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;
}
......@@ -258,15 +229,14 @@ std::shared_ptr<Config> Mhwd::getDatabaseConfig(const std::string& configName,
allConfigs = &data_.allPCIConfigs;
}
for (auto&& iterator = allConfigs->begin();
iterator != allConfigs->end(); ++iterator)
auto config = std::find_if(allConfigs->begin(), allConfigs->end(),
[configName](const std::shared_ptr<Config>& config) {
return config->name_ == configName;
});
if (config != allConfigs->end())
{
if (configName == (*iterator)->name_)
{
return (*iterator);
}
return *config;
}
return nullptr;
}
......@@ -294,13 +264,14 @@ std::shared_ptr<Config> Mhwd::getAvailableConfig(const std::string& configName,
}
else
{
for (auto&& availableConfig = (*device)->availableConfigs_.begin();
availableConfig != (*device)->availableConfigs_.end(); availableConfig++)
auto& availableConfigs = (*device)->availableConfigs_;
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
while ((dir = readdir(d)) != nullptr)
{
std::string filename {dir->d_name};
std::string sourcePath {source + "/" + filename};
std::string destinationPath {destination + "/" + filename};
if (("." == filename) || (".." == filename) || ("" == filename))
{
......@@ -439,6 +408,8 @@ bool Mhwd::copyDirectory(const std::string& source, const std::string& destinati
}
else
{
std::string sourcePath {source + "/" + filename};
std::string destinationPath {destination + "/" + filename};
lstat(sourcePath.c_str(), &filestatus);
if (S_ISREG(filestatus.st_mode))
......@@ -492,8 +463,7 @@ bool Mhwd::removeDirectory(const std::string& directory)
struct dirent *dir;
while ((dir = readdir(d)) != nullptr)
{
std::string filename = std::string(dir->d_name);
std::string filepath = directory + "/" + filename;
std::string filename {dir->d_name};
if (("." == filename) || (".." == filename) || ("" == filename))
{
......@@ -501,6 +471,7 @@ bool Mhwd::removeDirectory(const std::string& directory)
}
else
{
std::string filepath {directory + "/" + filename};
struct stat filestatus;
lstat(filepath.c_str(), &filestatus);
......@@ -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;
if (0 != stat(path.c_str(), &filestatus))
......@@ -956,6 +927,17 @@ bool Mhwd::optionsDontInterfereWithEachOther() const
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;
bool autoConfigureNonFreeDriver = false;
std::string autoConfigureClassID;
......@@ -977,14 +959,6 @@ int Mhwd::launch(int argc, char *argv[])
return 1;
}
// Check environment
std::string missingDir { checkEnvironment() };
if (!missingDir.empty())
{
printer_.printError("directory '" + missingDir + "' does not exist!");
return 1;
}
// Check for invalid configs
for (auto&& invalidConfig : data_.invalidConfigs)
{
......@@ -1104,7 +1078,7 @@ int Mhwd::launch(int argc, char *argv[])
{
if (arguments_.DETAIL)
{
printDeviceDetails("PCI");
printer_.printDeviceDetails(hw_pci);
}
else
{
......@@ -1115,7 +1089,7 @@ int Mhwd::launch(int argc, char *argv[])
{
if (arguments_.DETAIL)
{
printDeviceDetails("USB");
printer_.printDeviceDetails(hw_usb);
}
else
{
......@@ -1171,8 +1145,6 @@ int Mhwd::launch(int argc, char *argv[])
}
else
{
bool alreadyInList = std::find(configs_.begin(), configs_.end(), config->name_) != configs_.end();
// If force is not set then skip found config
bool skip = false;
if (!arguments_.FORCE)
......@@ -1180,8 +1152,7 @@ int Mhwd::launch(int argc, char *argv[])
skip = std::find_if(installedConfigs->begin(), installedConfigs->end(),
[&config](const std::shared_ptr<Config>& conf) -> bool {
return conf->name_ == config->name_;
})
!= installedConfigs->end();
}) != installedConfigs->end();
}
// Print found config
if (skip)
......@@ -1203,6 +1174,7 @@ int Mhwd::launch(int argc, char *argv[])
device->deviceName_);
}
bool alreadyInList = std::find(configs_.begin(), configs_.end(), config->name_) != configs_.end();
if (!alreadyInList && !skip)
{
configs_.push_back(config->name_);
......@@ -1224,7 +1196,11 @@ int Mhwd::launch(int argc, char *argv[])
// Transaction
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();
configName != configs_.end(); configName++)
......@@ -1248,7 +1224,7 @@ int Mhwd::launch(int argc, char *argv[])
else
{
config_.reset(new Config(filepath, operationType));
if (!data_.fillConfig(config_, filepath, operationType))
if (!config_->readConfigFile(filepath))
{
printer_.printError("failed to read custom config '" + filepath + "'!");
return 1;
......@@ -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;
}
......@@ -48,7 +48,7 @@
class Mhwd
{
public:
Mhwd();
Mhwd() = default;
~Mhwd() = default;
void setVersionMhwd(std::string versionOfSoftware, std::string yearCopyright);
int launch(int argc, char *argv[]);
......@@ -77,9 +77,7 @@ private:
bool performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE type);
bool isUserRoot() const;
std::string checkEnvironment();
void printDeviceDetails(std::string type, FILE *f = stdout);
std::vector<std::string> checkEnvironment() const;
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);
......@@ -94,7 +92,7 @@ private:
bool copyFile(const std::string& source, const std::string destination, const mode_t mode =
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH);
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 =
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
......
......@@ -22,28 +22,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Printer.hpp"
#include <hd.h>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "Printer.hpp"
void Printer::printStatus(std::string statusMsg) const
{
std::cout << CONSOLE_MESSAGE_COLOR << "> "
std::cout << CONSOLE_RED_MESSAGE_COLOR << "> "
<< CONSOLE_COLOR_RESET << statusMsg << std::endl;
}
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;
}
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;
}
......@@ -102,9 +105,9 @@ void Printer::printHelp() 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"
<< "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"
<< std::endl;
}
......@@ -268,3 +271,17 @@ void Printer::printLine() const
{
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 @@
#ifndef PRINTER_HPP_
#define PRINTER_HPP_
#include <hd.h>
#include <string>
#include <vector>
......@@ -50,11 +52,12 @@ public:
void printInstalledConfigs(const std::string& deviceType,
const std::vector<std::shared_ptr<Config>>& installedConfigs) const;
void printConfigDetails(const Config& config) const;
void printDeviceDetails(hw_item hw, FILE *f = stdout) const;
private:
void printLine() const;
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"};
};
......
......@@ -22,16 +22,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Mhwd.hpp"
#include <iostream>
#include "Mhwd.hpp"
int main(int argc, char *argv[])
{
try
{
Mhwd mhwd;
mhwd.setVersionMhwd("0.5.2","2012 2013 2014 2015");
mhwd.setVersionMhwd("0.5.3","2012-2015");
return mhwd.launch(argc, argv);
}
catch(...)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment