#include "install.hpp" #include "cli.hpp" InstallManager::InstallManager(InstallManager&& other) noexcept { m_valid = other.m_valid; other.m_valid = false; m_home_directory = std::move(other.m_home_directory); m_install_directory = std::move(other.m_install_directory); } InstallManager& InstallManager::operator=(InstallManager&& other) noexcept { if (this == &other) return *this; m_valid = other.m_valid; other.m_valid = false; m_home_directory = std::move(other.m_home_directory); m_install_directory = std::move(other.m_install_directory); return *this; } InstallManager::InstallManager() : m_valid{ false } { std::string hdstr; if (!home_directory(hdstr)) return; m_home_directory = std::filesystem::path{ hdstr }; m_install_directory = m_home_directory / std::filesystem::path{ INSTALL_PATH }; if (!std::filesystem::exists(m_install_directory) || !std::filesystem::is_directory(m_install_directory)) { if (!std::filesystem::create_directories(m_install_directory)) return; } m_valid = true; } InstallManager::~InstallManager() {} InstallManager::operator bool() const { return m_valid; } bool InstallManager::install(const char* from, const char* to, std::function check) { if (!m_valid || !from) return false; std::filesystem::path src { from }; std::filesystem::path dst; if (to) { dst = std::filesystem::path{ to }; if (dst != dst.filename()) return false; // Wirekit name may not include a path } else { std::string src_name { src.filename() }; if (src_name.ends_with(".so")) dst = std::filesystem::path{ src_name.substr(0, src_name.length() - 3) }; else dst = std::filesystem::path{ src_name }; } std::filesystem::path install_dst = m_install_directory / dst; if (!std::filesystem::exists(src) || !std::filesystem::is_regular_file(src)) return false; std::filesystem::path src_absolute = std::filesystem::absolute(src); if (!check(src_absolute)) return false; return std::filesystem::copy_file(src, install_dst, std::filesystem::copy_options::overwrite_existing); } bool InstallManager::uninstall(const char* name) { if (!m_valid || !name) return false; std::filesystem::path target { name }; if (target != target.filename()) return false; // Wirekit names never include paths (all are within/relative to installation directory) std::filesystem::path install_target = m_install_directory / target; if (!std::filesystem::exists(install_target) || !std::filesystem::is_regular_file(install_target)) return false; return std::filesystem::remove(install_target); } bool InstallManager::get(const char* name, std::filesystem::path& path) const { if (!m_valid || !name) return false; std::filesystem::path target { name }; if (target != target.filename()) return false; path = m_install_directory / target; return std::filesystem::exists(path) && std::filesystem::is_regular_file(path); } std::list InstallManager::installs() const { if (!m_valid) return {}; std::list res; for (const auto& entry : std::filesystem::directory_iterator{ m_install_directory }) res.emplace_back(entry.path().filename()); return res; }