diff options
Diffstat (limited to 'toolsrc/src/vcpkg.cpp')
| -rw-r--r-- | toolsrc/src/vcpkg.cpp | 368 |
1 files changed, 185 insertions, 183 deletions
diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp index 4748aeb54..e94d2538b 100644 --- a/toolsrc/src/vcpkg.cpp +++ b/toolsrc/src/vcpkg.cpp @@ -1,248 +1,250 @@ -#include "vcpkg.h" +#define WIN32_LEAN_AND_MEAN +#include <Windows.h> + #include <iostream> -#include <iomanip> #include <fstream> -#include <functional> -#include <string> -#include <unordered_map> #include <memory> -#include <vector> +#include <cassert> +#include "vcpkg_Commands.h" +#include "metrics.h" +#include <Shlobj.h> #include "vcpkg_Files.h" +#include "vcpkg_System.h" +#include "vcpkg_Input.h" #include "Paragraphs.h" -#include <regex> -#include "metrics.h" +#include "vcpkg_Strings.h" +#include "vcpkg_Chrono.h" using namespace vcpkg; -static StatusParagraphs load_current_database(const fs::path& vcpkg_dir_status_file, const fs::path& vcpkg_dir_status_file_old) +bool g_debugging = false; + +void invalid_command(const std::string& cmd) { - if (!fs::exists(vcpkg_dir_status_file)) - { - if (!fs::exists(vcpkg_dir_status_file_old)) - { - // no status file, use empty db - return StatusParagraphs(); - } + System::println(System::color::error, "invalid command: %s", cmd); + Commands::Help::print_usage(); + exit(EXIT_FAILURE); +} - fs::rename(vcpkg_dir_status_file_old, vcpkg_dir_status_file); +static void inner(const vcpkg_cmd_arguments& args) +{ + TrackProperty("command", args.command); + if (args.command.empty()) + { + Commands::Help::print_usage(); + exit(EXIT_FAILURE); } - auto text = Files::read_contents(vcpkg_dir_status_file).get_or_throw(); - auto pghs = Paragraphs::parse_paragraphs(text); - - std::vector<std::unique_ptr<StatusParagraph>> status_pghs; - for (auto&& p : pghs) + if (auto command_function = Commands::find(args.command, Commands::get_available_commands_type_c())) { - status_pghs.push_back(std::make_unique<StatusParagraph>(p)); + return command_function(args); } - return StatusParagraphs(std::move(status_pghs)); -} - -StatusParagraphs vcpkg::database_load_check(const vcpkg_paths& paths) -{ - auto updates_dir = paths.vcpkg_dir_updates; - - std::error_code ec; - fs::create_directory(paths.installed, ec); - fs::create_directory(paths.vcpkg_dir, ec); - fs::create_directory(paths.vcpkg_dir_info, ec); - fs::create_directory(updates_dir, ec); + fs::path vcpkg_root_dir; + if (args.vcpkg_root_dir != nullptr) + { + vcpkg_root_dir = fs::absolute(Strings::utf8_to_utf16(*args.vcpkg_root_dir)); + } + else + { + const optional<std::wstring> vcpkg_root_dir_env = System::get_environmental_variable(L"VCPKG_ROOT"); + if (vcpkg_root_dir_env) + { + vcpkg_root_dir = fs::absolute(*vcpkg_root_dir_env); + } + else + { + vcpkg_root_dir = Files::find_file_recursively_up(fs::absolute(System::get_exe_path_of_current_process()), ".vcpkg-root"); + } + } - const fs::path& status_file = paths.vcpkg_dir_status_file; - const fs::path status_file_old = status_file.parent_path() / "status-old"; - const fs::path status_file_new = status_file.parent_path() / "status-new"; + Checks::check_exit(!vcpkg_root_dir.empty(), "Error: Could not detect vcpkg-root."); - StatusParagraphs current_status_db = load_current_database(status_file, status_file_old); + const expected<vcpkg_paths> expected_paths = vcpkg_paths::create(vcpkg_root_dir); + Checks::check_exit(!expected_paths.error_code(), "Error: Invalid vcpkg root directory %s: %s", vcpkg_root_dir.string(), expected_paths.error_code().message()); + const vcpkg_paths paths = expected_paths.get_or_throw(); + int exit_code = _wchdir(paths.root.c_str()); + Checks::check_exit(exit_code == 0, "Changing the working dir failed"); - auto b = fs::directory_iterator(updates_dir); - auto e = fs::directory_iterator(); - if (b == e) + if (auto command_function = Commands::find(args.command, Commands::get_available_commands_type_b())) { - // updates directory is empty, control file is up-to-date. - return current_status_db; + return command_function(args, paths); } - for (; b != e; ++b) + triplet default_target_triplet; + if (args.target_triplet != nullptr) { - if (!fs::is_regular_file(b->status())) - continue; - if (b->path().filename() == "incomplete") - continue; - - auto text = Files::read_contents(b->path()).get_or_throw(); - auto pghs = Paragraphs::parse_paragraphs(text); - for (auto&& p : pghs) + default_target_triplet = triplet::from_canonical_name(*args.target_triplet); + } + else + { + const optional<std::wstring> vcpkg_default_triplet_env = System::get_environmental_variable(L"VCPKG_DEFAULT_TRIPLET"); + if (vcpkg_default_triplet_env) { - current_status_db.insert(std::make_unique<StatusParagraph>(p)); + default_target_triplet = triplet::from_canonical_name(Strings::utf16_to_utf8(*vcpkg_default_triplet_env)); + } + else + { + default_target_triplet = triplet::X86_WINDOWS; } } - std::fstream(status_file_new, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc) << current_status_db; - - if (fs::exists(status_file_old)) - fs::remove(status_file_old); - if (fs::exists(status_file)) - fs::rename(status_file, status_file_old); - fs::rename(status_file_new, status_file); - fs::remove(status_file_old); + Input::check_triplet(default_target_triplet, paths); - b = fs::directory_iterator(updates_dir); - for (; b != e; ++b) + if (auto command_function = Commands::find(args.command, Commands::get_available_commands_type_a())) { - if (!fs::is_regular_file(b->status())) - continue; - fs::remove(b->path()); + return command_function(args, paths, default_target_triplet); } - return current_status_db; -} - -void vcpkg::write_update(const vcpkg_paths& paths, const StatusParagraph& p) -{ - static int update_id = 0; - auto my_update_id = update_id++; - auto tmp_update_filename = paths.vcpkg_dir_updates / "incomplete"; - auto update_filename = paths.vcpkg_dir_updates / std::to_string(my_update_id); - std::fstream fs(tmp_update_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); - fs << p; - fs.close(); - fs::rename(tmp_update_filename, update_filename); + return invalid_command(args.command); } -static void upgrade_to_slash_terminated_sorted_format(std::vector<std::string>* lines, const fs::path& listfile_path) +static void loadConfig() { - static bool was_tracked = false; + fs::path localappdata; + { + // Config path in AppDataLocal + wchar_t* localappdatapath = nullptr; + if (S_OK != SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &localappdatapath)) + __fastfail(1); + localappdata = localappdatapath; + CoTaskMemFree(localappdatapath); + } - if (lines->empty()) + try { + std::string config_contents = Files::read_contents(localappdata / "vcpkg" / "config").get_or_throw(); + + std::unordered_map<std::string, std::string> keys; + auto pghs = Paragraphs::parse_paragraphs(config_contents); + if (pghs.size() > 0) + keys = pghs[0]; + + for (size_t x = 1; x < pghs.size(); ++x) + { + for (auto&& p : pghs[x]) + keys.insert(p); + } + + auto user_id = keys["User-Id"]; + auto user_time = keys["User-Since"]; + Checks::check_throw(!user_id.empty() && !user_time.empty(), ""); // Use as goto to the catch statement + + SetUserInformation(user_id, user_time); return; } - - if (lines->at(0).back() == '/') + catch (...) { - return; // File already in the new format } - if (!was_tracked) + // config file not found, could not be read, or invalid + std::string user_id, user_time; + InitUserInformation(user_id, user_time); + SetUserInformation(user_id, user_time); + try { - was_tracked = true; - TrackProperty("listfile", "update to new format"); + std::error_code ec; + fs::create_directory(localappdata / "vcpkg", ec); + std::ofstream(localappdata / "vcpkg" / "config", std::ios_base::out | std::ios_base::trunc) + << "User-Id: " << user_id << "\n" + << "User-Since: " << user_time << "\n"; } - - // The files are sorted such that directories are placed just before the files they contain - // (They are not necessarily sorted alphabetically, e.g. libflac) - // Therefore 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 (size_t i = 0; i < lines->size() - 1; i++) + catch (...) { - std::string& current_string = lines->at(i); - const std::string& next_string = lines->at(i + 1); + } +} - 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 as a directory - } +static std::string trim_path_from_command_line(const std::string& full_command_line) +{ + Checks::check_exit(full_command_line.size() > 0, "Internal failure - cannot have empty command line"); + + if (full_command_line[0] == '"') + { + auto it = std::find(full_command_line.cbegin() + 1, full_command_line.cend(), '"'); + if (it != full_command_line.cend()) // Skip over the quote + ++it; + while (it != full_command_line.cend() && *it == ' ') // Skip over a space + ++it; + return std::string(it, full_command_line.cend()); } - // After suffixing the directories with a slash, we can now sort. - // We cannot sort before adding the suffixes because the following (actual example): - /* - x86-windows/include/FLAC <<<<<< This would be separated from its group due to sorting - x86-windows/include/FLAC/all.h - x86-windows/include/FLAC/assert.h - x86-windows/include/FLAC/callback.h - x86-windows/include/FLAC++ - x86-windows/include/FLAC++/all.h - x86-windows/include/FLAC++/decoder.h - x86-windows/include/FLAC++/encoder.h - * - x86-windows/include/FLAC/ <<<<<< This will now be kept with its group when sorting - x86-windows/include/FLAC/all.h - x86-windows/include/FLAC/assert.h - x86-windows/include/FLAC/callback.h - x86-windows/include/FLAC++/ - x86-windows/include/FLAC++/all.h - x86-windows/include/FLAC++/decoder.h - x86-windows/include/FLAC++/encoder.h - */ - // Note that after sorting, the FLAC++/ group will be placed before the FLAC/ group - // The new format is lexicographically sorted - std::sort(lines->begin(), lines->end()); - -#if 0 - // Replace the listfile on disk - const fs::path updated_listfile_path = listfile_path.generic_string() + "_updated"; - Files::write_all_lines(updated_listfile_path, *lines); - fs::rename(updated_listfile_path, listfile_path); -#endif + auto it = std::find(full_command_line.cbegin(), full_command_line.cend(), ' '); + while (it != full_command_line.cend() && *it == ' ') + ++it; + return std::string(it, full_command_line.cend()); } -std::vector<StatusParagraph_and_associated_files> vcpkg::get_installed_files(const vcpkg_paths& paths, const StatusParagraphs& status_db) +static ElapsedTime g_timer; + +int wmain(const int argc, const wchar_t* const* const argv) { - std::vector<StatusParagraph_and_associated_files> installed_files; + if (argc == 0) + std::abort(); - for (const std::unique_ptr<StatusParagraph>& pgh : status_db) - { - if (pgh->state != install_state_t::installed) + g_timer = ElapsedTime::createStarted(); + atexit([]() { - continue; - } + auto elapsed_us = g_timer.microseconds(); + TrackMetric("elapsed_us", elapsed_us); + Flush(); + }); + + TrackProperty("version", Commands::Version::version()); + + const std::string trimmed_command_line = trim_path_from_command_line(Strings::utf16_to_utf8(GetCommandLineW())); + TrackProperty("cmdline", trimmed_command_line); + loadConfig(); + TrackProperty("sqmuser", GetSQMUser()); - const fs::path listfile_path = paths.listfile_path(pgh->package); - std::vector<std::string> installed_files_of_current_pgh = Files::read_all_lines(listfile_path).get_or_throw(); - Strings::trim_all_and_remove_whitespace_strings(&installed_files_of_current_pgh); - upgrade_to_slash_terminated_sorted_format(&installed_files_of_current_pgh, listfile_path); + const vcpkg_cmd_arguments args = vcpkg_cmd_arguments::create_from_command_line(argc, argv); - // Remove the directories - 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) -> bool - { - return file.back() == '/'; - } - ), installed_files_of_current_pgh.end()); + if (args.printmetrics != opt_bool_t::UNSPECIFIED) + SetPrintMetrics(args.printmetrics == opt_bool_t::ENABLED); + if (args.sendmetrics != opt_bool_t::UNSPECIFIED) + SetSendMetrics(args.sendmetrics == opt_bool_t::ENABLED); - StatusParagraph_and_associated_files pgh_and_files = {*pgh, std::move(installed_files_of_current_pgh)}; - installed_files.push_back(std::move(pgh_and_files)); + if (args.debug != opt_bool_t::UNSPECIFIED) + { + g_debugging = (args.debug == opt_bool_t::ENABLED); } - return installed_files; -} + if (g_debugging) + { + inner(args); + exit(EXIT_FAILURE); + } -expected<SourceParagraph> vcpkg::try_load_port(const fs::path& path) -{ + std::string exc_msg; try { - auto pghs = Paragraphs::get_paragraphs(path / "CONTROL"); - Checks::check_exit(pghs.size() == 1, "Invalid control file at %s\\CONTROL", path.string()); - return SourceParagraph(pghs[0]); + inner(args); + exit(EXIT_FAILURE); } - catch (std::runtime_error const&) + catch (std::exception& e) { + exc_msg = e.what(); } - - return std::errc::no_such_file_or_directory; -} - -expected<BinaryParagraph> vcpkg::try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec) -{ - const fs::path path = paths.package_dir(spec) / "CONTROL"; - - auto control_contents_maybe = Files::read_contents(path); - if (auto control_contents = control_contents_maybe.get()) + catch (...) { - std::vector<std::unordered_map<std::string, std::string>> pghs; - try - { - pghs = Paragraphs::parse_paragraphs(*control_contents); - } - catch (std::runtime_error) - { - } - Checks::check_exit(pghs.size() == 1, "Invalid control file at %s", path.string()); - return BinaryParagraph(pghs[0]); + exc_msg = "unknown error(...)"; } - return control_contents_maybe.error_code(); + TrackProperty("error", exc_msg); + + fflush(stdout); + System::print( + "vcpkg.exe has crashed.\n" + "Please send an email to:\n" + " %s\n" + "containing a brief summary of what you were trying to do and the following data blob:\n" + "\n" + "Version=%s\n" + "EXCEPTION='%s'\n" + "CMD=\n", + Commands::Contact::email(), + Commands::Version::version(), + exc_msg); + fflush(stdout); + for (int x = 0; x < argc; ++x) + System::println("%s|", Strings::utf16_to_utf8(argv[x])); + fflush(stdout); } |
