aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/commands_remove.cpp
diff options
context:
space:
mode:
authorAlexander Karatarakis <alkarata@microsoft.com>2016-12-01 01:49:24 -0800
committerAlexander Karatarakis <alkarata@microsoft.com>2016-12-01 01:49:24 -0800
commit6eac44c9640a50fbde88535df6261a1a3f234350 (patch)
tree43e197b3836eb522cdbad0e8359e1a32a63f2b21 /toolsrc/src/commands_remove.cpp
parent89aaf195fbdfa63708fd6ac90103cac0cdedf3c6 (diff)
downloadvcpkg-6eac44c9640a50fbde88535df6261a1a3f234350.tar.gz
vcpkg-6eac44c9640a50fbde88535df6261a1a3f234350.zip
Move install_package() and deinstall_package() to the files of the
appropriate commands
Diffstat (limited to 'toolsrc/src/commands_remove.cpp')
-rw-r--r--toolsrc/src/commands_remove.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/toolsrc/src/commands_remove.cpp b/toolsrc/src/commands_remove.cpp
index 5bb9ecc96..31331fc2f 100644
--- a/toolsrc/src/commands_remove.cpp
+++ b/toolsrc/src/commands_remove.cpp
@@ -2,6 +2,7 @@
#include "vcpkg.h"
#include "vcpkg_System.h"
#include "vcpkg_Input.h"
+#include <fstream>
namespace vcpkg
{
@@ -21,6 +22,150 @@ namespace vcpkg
}
}
+ enum class deinstall_plan
+ {
+ not_installed,
+ dependencies_not_satisfied,
+ should_deinstall
+ };
+
+ static deinstall_plan deinstall_package_plan(
+ const StatusParagraphs::iterator package_it,
+ const StatusParagraphs& status_db,
+ std::vector<const StatusParagraph*>& dependencies_out)
+ {
+ dependencies_out.clear();
+
+ if (package_it == status_db.end() || (*package_it)->state == install_state_t::not_installed)
+ {
+ return deinstall_plan::not_installed;
+ }
+
+ auto& pkg = (*package_it)->package;
+
+ for (auto&& inst_pkg : status_db)
+ {
+ if (inst_pkg->want != want_t::install)
+ continue;
+ if (inst_pkg->package.spec.target_triplet() != pkg.spec.target_triplet())
+ continue;
+
+ const auto& deps = inst_pkg->package.depends;
+
+ if (std::find(deps.begin(), deps.end(), pkg.spec.name()) != deps.end())
+ {
+ dependencies_out.push_back(inst_pkg.get());
+ }
+ }
+
+ if (!dependencies_out.empty())
+ return deinstall_plan::dependencies_not_satisfied;
+
+ return deinstall_plan::should_deinstall;
+ }
+
+ static void deinstall_package(const vcpkg_paths& paths, const package_spec& spec, StatusParagraphs& status_db)
+ {
+ auto package_it = status_db.find(spec.name(), spec.target_triplet());
+ if (package_it == status_db.end())
+ {
+ System::println(System::color::success, "Package %s is not installed", spec);
+ return;
+ }
+
+ auto& pkg = **package_it;
+
+ std::vector<const StatusParagraph*> deps;
+ auto plan = deinstall_package_plan(package_it, status_db, deps);
+ switch (plan)
+ {
+ case deinstall_plan::not_installed:
+ System::println(System::color::success, "Package %s is not installed", spec);
+ return;
+ case deinstall_plan::dependencies_not_satisfied:
+ System::println(System::color::error, "Error: Cannot remove package %s:", spec);
+ for (auto&& dep : deps)
+ {
+ System::println(" %s depends on %s", dep->package.displayname(), pkg.package.displayname());
+ }
+ exit(EXIT_FAILURE);
+ case deinstall_plan::should_deinstall:
+ break;
+ default:
+ Checks::unreachable();
+ }
+
+ pkg.want = want_t::purge;
+ pkg.state = install_state_t::half_installed;
+ write_update(paths, pkg);
+
+ std::fstream listfile(paths.listfile_path(pkg.package), std::ios_base::in | std::ios_base::binary);
+ if (listfile)
+ {
+ std::vector<fs::path> dirs_touched;
+ std::string suffix;
+ while (std::getline(listfile, suffix))
+ {
+ if (!suffix.empty() && suffix.back() == '\r')
+ suffix.pop_back();
+
+ std::error_code ec;
+
+ auto target = paths.installed / suffix;
+
+ auto status = fs::status(target, ec);
+ if (ec)
+ {
+ System::println(System::color::error, "failed: %s", ec.message());
+ continue;
+ }
+
+ if (fs::is_directory(status))
+ {
+ dirs_touched.push_back(target);
+ }
+ else if (fs::is_regular_file(status))
+ {
+ fs::remove(target, ec);
+ if (ec)
+ {
+ System::println(System::color::error, "failed: %s: %s", target.u8string(), ec.message());
+ }
+ }
+ else if (!fs::status_known(status))
+ {
+ System::println(System::color::warning, "Warning: unknown status: %s", target.u8string());
+ }
+ else
+ {
+ System::println(System::color::warning, "Warning: %s: cannot handle file type", target.u8string());
+ }
+ }
+
+ auto b = dirs_touched.rbegin();
+ auto e = dirs_touched.rend();
+ for (; b != e; ++b)
+ {
+ if (fs::directory_iterator(*b) == fs::directory_iterator())
+ {
+ std::error_code ec;
+ fs::remove(*b, ec);
+ if (ec)
+ {
+ System::println(System::color::error, "failed: %s", ec.message());
+ }
+ }
+ }
+
+ listfile.close();
+ fs::remove(paths.listfile_path(pkg.package));
+ }
+
+ pkg.state = install_state_t::not_installed;
+ write_update(paths, pkg);
+ System::println(System::color::success, "Package %s was successfully removed", pkg.package.displayname());
+ }
+
void remove_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet)
{
static const std::string example = create_example_string("remove zlib zlib:x64-windows curl boost");