From 7c2abc755f58beaea36aa1cbc1c3b7f375e567a3 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 1 Dec 2016 15:36:39 -0800 Subject: Introduce function get_installed_files() --- toolsrc/src/vcpkg.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'toolsrc/src') diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp index 6c5224f56..57b2e7adb 100644 --- a/toolsrc/src/vcpkg.cpp +++ b/toolsrc/src/vcpkg.cpp @@ -109,6 +109,39 @@ void vcpkg::write_update(const vcpkg_paths& paths, const StatusParagraph& p) fs::rename(tmp_update_filename, update_filename); } +std::vector vcpkg::get_installed_files(const vcpkg_paths& paths, const StatusParagraphs& status_db) +{ + std::vector installed_files; + + std::string line; + + for (const std::unique_ptr& pgh : status_db) + { + if (pgh->state != install_state_t::installed) + { + continue; + } + + std::fstream listfile(paths.listfile_path(pgh->package)); + + std::vector installed_files_of_current_pgh; + while (std::getline(listfile, line)) + { + if (line.empty()) + { + continue; + } + + installed_files_of_current_pgh.push_back(line); + } + + const StatusParagraph_and_associated_files pgh_and_files = {*pgh, std::move(installed_files_of_current_pgh)}; + installed_files.push_back(pgh_and_files); + } + + return installed_files; +} + expected vcpkg::try_load_port(const fs::path& path) { try -- cgit v1.2.3 From 4d298be260ecafef2c7c17145aafbd4ea8a952cb Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 1 Dec 2016 15:37:08 -0800 Subject: [owns command] Use get_installed_files() --- toolsrc/src/commands_owns.cpp | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'toolsrc/src') diff --git a/toolsrc/src/commands_owns.cpp b/toolsrc/src/commands_owns.cpp index e5599ce01..45f073304 100644 --- a/toolsrc/src/commands_owns.cpp +++ b/toolsrc/src/commands_owns.cpp @@ -1,30 +1,21 @@ #include "vcpkg_Commands.h" #include "vcpkg_System.h" #include "vcpkg.h" -#include namespace vcpkg { static void search_file(const vcpkg_paths& paths, const std::string& file_substr, const StatusParagraphs& status_db) { - std::string line; - - for (auto&& pgh : status_db) + const std::vector installed_files = get_installed_files(paths, status_db); + for (const StatusParagraph_and_associated_files& pgh_and_file : installed_files) { - if (pgh->state != install_state_t::installed) - continue; + const StatusParagraph& pgh = pgh_and_file.pgh; - std::fstream listfile(paths.listfile_path(pgh->package)); - while (std::getline(listfile, line)) + for (const std::string& file : pgh_and_file.files) { - if (line.empty()) - { - continue; - } - - if (line.find(file_substr) != std::string::npos) + if (file.find(file_substr) != std::string::npos) { - System::println("%s: %s", pgh->package.displayname(), line); + System::println("%s: %s", pgh.package.displayname(), file); } } } -- cgit v1.2.3 From a8c189c3f29dcb081ffe7c1bda53bdc82ac6dc92 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 1 Dec 2016 17:22:20 -0800 Subject: [pre-install checks] Greatly improve the check for already isntalled files --- toolsrc/src/commands_installation.cpp | 74 +++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 30 deletions(-) (limited to 'toolsrc/src') diff --git a/toolsrc/src/commands_installation.cpp b/toolsrc/src/commands_installation.cpp index 23342070d..8bae0a182 100644 --- a/toolsrc/src/commands_installation.cpp +++ b/toolsrc/src/commands_installation.cpp @@ -128,53 +128,67 @@ namespace vcpkg listfile.close(); } - static std::map remove_first_n_chars_and_map(const std::vector absolute_paths, const size_t n) + static void remove_first_n_chars(std::vector* strings, const size_t n) { - std::map output; - - for (const fs::path& absolute_path : absolute_paths) + for (std::string& s : *strings) { - std::string suffix = absolute_path.generic_string(); - suffix.erase(0, n); - output.emplace(suffix, absolute_path); + s.erase(0, n); } + }; - return output; - } - - static void print_map_values(const std::vector keys, const std::map& map) + static std::vector extract_files_in_triplet(const std::vector& pgh_and_files, const triplet& triplet) { - System::println(""); - for (const std::string& key : keys) + std::vector output; + for (const StatusParagraph_and_associated_files& t : pgh_and_files) { - System::println(" %s", map.at(key).generic_string()); + if (t.pgh.package.spec.target_triplet() != triplet) + { + continue; + } + + output.insert(output.end(), t.files.cbegin(), t.files.cend()); } - System::println(""); + + std::sort(output.begin(), output.end()); + return output; } - static void install_package(const vcpkg_paths& paths, const BinaryParagraph& binary_paragraph, StatusParagraphs& status_db) + void install_package(const vcpkg_paths& paths, const BinaryParagraph& binary_paragraph, StatusParagraphs& status_db) { const fs::path package_dir = paths.package_dir(binary_paragraph.spec); - const std::vector package_files = Files::recursive_find_all_files_in_dir(package_dir); - - const fs::path installed_dir = paths.installed / binary_paragraph.spec.target_triplet().canonical_name(); - const std::vector installed_files = Files::recursive_find_all_files_in_dir(installed_dir); - - const std::map package_files_relative_paths_to_absolute_paths = remove_first_n_chars_and_map(package_files, package_dir.generic_string().size() + 1); - const std::map installed_files_relative_paths_to_absolute_paths = remove_first_n_chars_and_map(installed_files, installed_dir.generic_string().size() + 1); - - const std::vector package_files_set = Maps::extract_keys(package_files_relative_paths_to_absolute_paths); - const std::vector installed_files_set = Maps::extract_keys(installed_files_relative_paths_to_absolute_paths); + const std::vector package_file_paths = Files::recursive_find_all_files_in_dir(package_dir); + std::vector package_files; + const size_t package_remove_char_count = package_dir.generic_string().size() + 1; // +1 for the slash + std::transform(package_file_paths.cbegin(), package_file_paths.cend(), std::back_inserter(package_files), [package_remove_char_count](const fs::path& path) + { + return path.generic_string().erase(0, package_remove_char_count); + }); + std::sort(package_files.begin(), package_files.end()); + + const std::vector& pgh_and_files = get_installed_files(paths, status_db); + const triplet& triplet = binary_paragraph.spec.target_triplet(); + std::vector installed_files = extract_files_in_triplet(pgh_and_files, triplet); + const size_t installed_remove_char_count = triplet.canonical_name().size() + 1; // +1 for the slash + remove_first_n_chars(&installed_files, installed_remove_char_count); + std::sort(installed_files.begin(), installed_files.end()); std::vector intersection; - std::set_intersection(package_files_set.cbegin(), package_files_set.cend(), - installed_files_set.cbegin(), installed_files_set.cend(), + std::set_intersection(package_files.cbegin(), package_files.cend(), + installed_files.cbegin(), installed_files.cend(), std::back_inserter(intersection)); if (!intersection.empty()) { - System::println(System::color::error, "The following files are already installed and are in conflict with %s:", binary_paragraph.spec); - print_map_values(intersection, installed_files_relative_paths_to_absolute_paths); + const fs::path triplet_install_path = paths.installed / triplet.canonical_name(); + System::println(System::color::error, "The following files are already installed in %s and are in conflict with %s", + triplet_install_path.generic_string(), + binary_paragraph.spec); + System::println(""); + for (const std::string& s : intersection) + { + System::println(" %s", s); + } + System::println(""); exit(EXIT_FAILURE); } -- cgit v1.2.3 From a195dedf52e67b1aaac62f041cc767e0d12592b3 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 1 Dec 2016 20:39:28 -0800 Subject: get_installed_files() now filters out the directories --- toolsrc/src/commands_installation.cpp | 2 +- toolsrc/src/vcpkg.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'toolsrc/src') diff --git a/toolsrc/src/commands_installation.cpp b/toolsrc/src/commands_installation.cpp index 8bae0a182..1abd16796 100644 --- a/toolsrc/src/commands_installation.cpp +++ b/toolsrc/src/commands_installation.cpp @@ -170,7 +170,7 @@ namespace vcpkg std::vector installed_files = extract_files_in_triplet(pgh_and_files, triplet); const size_t installed_remove_char_count = triplet.canonical_name().size() + 1; // +1 for the slash remove_first_n_chars(&installed_files, installed_remove_char_count); - std::sort(installed_files.begin(), installed_files.end()); + std::sort(installed_files.begin(), installed_files.end()); // Should already be sorted std::vector intersection; std::set_intersection(package_files.cbegin(), package_files.cend(), diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp index 57b2e7adb..88b05b0a9 100644 --- a/toolsrc/src/vcpkg.cpp +++ b/toolsrc/src/vcpkg.cpp @@ -111,6 +111,8 @@ void vcpkg::write_update(const vcpkg_paths& paths, const StatusParagraph& p) std::vector vcpkg::get_installed_files(const vcpkg_paths& paths, const StatusParagraphs& status_db) { + static const std::string MARK_FOR_REMOVAL = ""; + std::vector installed_files; std::string line; @@ -135,6 +137,30 @@ std::vector vcpkg::get_installed_files(con installed_files_of_current_pgh.push_back(line); } + // Should already be sorted + std::sort(installed_files_of_current_pgh.begin(), installed_files_of_current_pgh.end()); + + // Since the files are sorted, we can detect the entries that represent directories + // by comparing every element with the next one and checking if the next has a slash immediately after the current one's length + for (int i = 1; i < installed_files_of_current_pgh.size(); i++) + { + std::string& current_string = installed_files_of_current_pgh.at(i - 1); + const std::string& next_string = installed_files_of_current_pgh.at(i); + + const size_t potential_slash_char_index = current_string.length(); + // Make sure the index exists first + if (next_string.size() > potential_slash_char_index && next_string.at(potential_slash_char_index) == '/') + { + current_string = MARK_FOR_REMOVAL; + } + } + + installed_files_of_current_pgh.erase(std::remove_if(installed_files_of_current_pgh.begin(), installed_files_of_current_pgh.end(), [](const std::string& file) + { + return file == MARK_FOR_REMOVAL; + }), + installed_files_of_current_pgh.end()); + const StatusParagraph_and_associated_files pgh_and_files = {*pgh, std::move(installed_files_of_current_pgh)}; installed_files.push_back(pgh_and_files); } -- cgit v1.2.3