diff options
Diffstat (limited to 'toolsrc/src/vcpkglib.cpp')
| -rw-r--r-- | toolsrc/src/vcpkglib.cpp | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/toolsrc/src/vcpkglib.cpp b/toolsrc/src/vcpkglib.cpp index 253c0c7ea..90d3e99b9 100644 --- a/toolsrc/src/vcpkglib.cpp +++ b/toolsrc/src/vcpkglib.cpp @@ -3,6 +3,7 @@ #include "vcpkg_Files.h" #include "Paragraphs.h" #include "metrics.h" +#include "vcpkg_Util.h" #include "vcpkg_Strings.h" #include "vcpkg_Util.h" @@ -10,20 +11,20 @@ namespace vcpkg { bool g_debugging = false; - static StatusParagraphs load_current_database(const fs::path& vcpkg_dir_status_file, const fs::path& vcpkg_dir_status_file_old) + static StatusParagraphs load_current_database(Files::Filesystem& fs, const fs::path& vcpkg_dir_status_file, const fs::path& vcpkg_dir_status_file_old) { - if (!fs::exists(vcpkg_dir_status_file)) + if (!fs.exists(vcpkg_dir_status_file)) { - if (!fs::exists(vcpkg_dir_status_file_old)) + if (!fs.exists(vcpkg_dir_status_file_old)) { // no status file, use empty db return StatusParagraphs(); } - fs::rename(vcpkg_dir_status_file_old, vcpkg_dir_status_file); + fs.rename(vcpkg_dir_status_file_old, vcpkg_dir_status_file); } - auto pghs = Paragraphs::get_paragraphs(vcpkg_dir_status_file).value_or_exit(VCPKG_LINE_INFO); + auto pghs = Paragraphs::get_paragraphs(fs, vcpkg_dir_status_file).value_or_exit(VCPKG_LINE_INFO); std::vector<std::unique_ptr<StatusParagraph>> status_pghs; for (auto&& p : pghs) @@ -36,57 +37,52 @@ namespace vcpkg StatusParagraphs database_load_check(const VcpkgPaths& paths) { + auto& fs = paths.get_filesystem(); + 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.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); 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"; - StatusParagraphs current_status_db = load_current_database(status_file, status_file_old); + StatusParagraphs current_status_db = load_current_database(fs, status_file, status_file_old); - auto b = fs::directory_iterator(updates_dir); - auto e = fs::directory_iterator(); - if (b == e) + auto update_files = fs.get_files_non_recursive(updates_dir); + if (update_files.empty()) { // updates directory is empty, control file is up-to-date. return current_status_db; } - - for (; b != e; ++b) + for (auto&& file : update_files) { - if (!fs::is_regular_file(b->status())) + if (!fs.is_regular_file(file)) continue; - if (b->path().filename() == "incomplete") + if (file.filename() == "incomplete") continue; - auto pghs = Paragraphs::get_paragraphs(b->path()).value_or_exit(VCPKG_LINE_INFO); + auto pghs = Paragraphs::get_paragraphs(fs, file).value_or_exit(VCPKG_LINE_INFO); for (auto&& p : pghs) { current_status_db.insert(std::make_unique<StatusParagraph>(p)); } } - std::fstream(status_file_new, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc) << current_status_db; + fs.write_contents(status_file_new, Strings::serialize(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); + fs.rename(status_file_new, status_file); - b = fs::directory_iterator(updates_dir); - for (; b != e; ++b) + for (auto&& file : update_files) { - if (!fs::is_regular_file(b->status())) + if (!fs.is_regular_file(file)) continue; - fs::remove(b->path()); + + fs.remove(file); } return current_status_db; @@ -95,16 +91,17 @@ namespace vcpkg void write_update(const VcpkgPaths& paths, const StatusParagraph& p) { static int update_id = 0; + auto& fs = paths.get_filesystem(); + 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); + + fs.write_contents(tmp_update_filename, Strings::serialize(p)); + fs.rename(tmp_update_filename, update_filename); } - static void upgrade_to_slash_terminated_sorted_format(std::vector<std::string>* lines, const fs::path& listfile_path) + static void upgrade_to_slash_terminated_sorted_format(Files::Filesystem& fs, std::vector<std::string>* lines, const fs::path& listfile_path) { static bool was_tracked = false; @@ -168,8 +165,8 @@ namespace vcpkg // 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); + fs.write_lines(updated_listfile_path, *lines); + fs.rename(updated_listfile_path, listfile_path); } std::vector<StatusParagraph*> get_installed_ports(const StatusParagraphs& status_db) @@ -187,6 +184,8 @@ namespace vcpkg std::vector<StatusParagraphAndAssociatedFiles> get_installed_files(const VcpkgPaths& paths, const StatusParagraphs& status_db) { + auto& fs = paths.get_filesystem(); + std::vector<StatusParagraphAndAssociatedFiles> installed_files; for (const std::unique_ptr<StatusParagraph>& pgh : status_db) @@ -197,9 +196,9 @@ namespace vcpkg } 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).value_or_exit(VCPKG_LINE_INFO); + std::vector<std::string> installed_files_of_current_pgh = fs.read_lines(listfile_path).value_or_exit(VCPKG_LINE_INFO); 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); + upgrade_to_slash_terminated_sorted_format(fs, &installed_files_of_current_pgh, listfile_path); // Remove the directories Util::erase_remove_if(installed_files_of_current_pgh, [](const std::string& file) { return file.back() == '/'; }); |
