From 42ad12f91d62a6533d909d8b5ee3a46009c2a10a Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Wed, 25 Mar 2020 15:18:10 -0700 Subject: [vcpkg] Improve common case of ignoring filesystem errors (#10557) --- toolsrc/include/vcpkg/base/files.h | 12 +++++-- toolsrc/include/vcpkg/base/ignore_errors.h | 10 ++++++ toolsrc/src/vcpkg/base/files.cpp | 55 ++++++++++++++++++++++++++---- toolsrc/src/vcpkg/binarycaching.cpp | 8 ++--- toolsrc/vcpkglib/vcpkglib.vcxproj | 1 + 5 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 toolsrc/include/vcpkg/base/ignore_errors.h diff --git a/toolsrc/include/vcpkg/base/files.h b/toolsrc/include/vcpkg/base/files.h index bb4a10d49..3b92feabe 100644 --- a/toolsrc/include/vcpkg/base/files.h +++ b/toolsrc/include/vcpkg/base/files.h @@ -1,6 +1,7 @@ #pragma once #include +#include #if USE_STD_FILESYSTEM #include @@ -126,20 +127,22 @@ namespace vcpkg::Files StringLiteral temp_suffix, std::error_code& ec) = 0; bool remove(const fs::path& path, LineInfo linfo); + bool remove(const fs::path& path, ignore_errors_t); virtual bool remove(const fs::path& path, std::error_code& ec) = 0; virtual void remove_all(const fs::path& path, std::error_code& ec, fs::path& failure_point) = 0; void remove_all(const fs::path& path, LineInfo li); + void remove_all(const fs::path& path, ignore_errors_t); bool exists(const fs::path& path, std::error_code& ec) const; bool exists(LineInfo li, const fs::path& path) const; - // this should probably not exist, but would require a pass through of - // existing code to fix - bool exists(const fs::path& path) const; + bool exists(const fs::path& path, ignore_errors_t = ignore_errors) const; virtual bool is_directory(const fs::path& path) const = 0; virtual bool is_regular_file(const fs::path& path) const = 0; virtual bool is_empty(const fs::path& path) const = 0; virtual bool create_directory(const fs::path& path, std::error_code& ec) = 0; + bool create_directory(const fs::path& path, ignore_errors_t); virtual bool create_directories(const fs::path& path, std::error_code& ec) = 0; + bool create_directories(const fs::path& path, ignore_errors_t); virtual void copy(const fs::path& oldpath, const fs::path& newpath, fs::copy_options opts) = 0; virtual bool copy_file(const fs::path& oldpath, const fs::path& newpath, @@ -149,9 +152,12 @@ namespace vcpkg::Files virtual fs::file_status status(const fs::path& path, std::error_code& ec) const = 0; virtual fs::file_status symlink_status(const fs::path& path, std::error_code& ec) const = 0; fs::file_status status(LineInfo li, const fs::path& p) const noexcept; + fs::file_status status(const fs::path& p, ignore_errors_t) const noexcept; fs::file_status symlink_status(LineInfo li, const fs::path& p) const noexcept; + fs::file_status symlink_status(const fs::path& p, ignore_errors_t) const noexcept; virtual fs::path canonical(const fs::path& path, std::error_code& ec) const = 0; fs::path canonical(LineInfo li, const fs::path& path) const; + fs::path canonical(const fs::path& path, ignore_errors_t) const; virtual std::vector find_from_PATH(const std::string& name) const = 0; }; diff --git a/toolsrc/include/vcpkg/base/ignore_errors.h b/toolsrc/include/vcpkg/base/ignore_errors.h new file mode 100644 index 000000000..44077059a --- /dev/null +++ b/toolsrc/include/vcpkg/base/ignore_errors.h @@ -0,0 +1,10 @@ +#pragma once + +namespace vcpkg +{ + struct ignore_errors_t + { + }; + + constexpr ignore_errors_t ignore_errors; +} diff --git a/toolsrc/src/vcpkg/base/files.cpp b/toolsrc/src/vcpkg/base/files.cpp index cd8fba129..0bd1cb893 100644 --- a/toolsrc/src/vcpkg/base/files.cpp +++ b/toolsrc/src/vcpkg/base/files.cpp @@ -173,6 +173,12 @@ namespace vcpkg::Files return r; } + bool Filesystem::remove(const fs::path& path, ignore_errors_t) + { + std::error_code ec; + return this->remove(path, ec); + } + bool Filesystem::exists(const fs::path& path, std::error_code& ec) const { return fs::exists(this->symlink_status(path, ec)); @@ -185,12 +191,23 @@ namespace vcpkg::Files if (ec) Checks::exit_with_message(li, "error checking existence of file %s: %s", path.u8string(), ec.message()); return result; } - - bool Filesystem::exists(const fs::path& path) const + + bool Filesystem::exists(const fs::path& path, ignore_errors_t) const + { + std::error_code ec; + return this->exists(path, ec); + } + + bool Filesystem::create_directory(const fs::path& path, ignore_errors_t) + { + std::error_code ec; + return this->create_directory(path, ec); + } + + bool Filesystem::create_directories(const fs::path& path, ignore_errors_t) { std::error_code ec; - // drop this on the floor, for compatibility with existing code - return exists(path, ec); + return this->create_directories(path, ec); } fs::file_status Filesystem::status(vcpkg::LineInfo li, const fs::path& p) const noexcept @@ -202,6 +219,12 @@ namespace vcpkg::Files return result; } + fs::file_status Filesystem::status(const fs::path& p, ignore_errors_t) const noexcept + { + std::error_code ec; + return this->status(p, ec); + } + fs::file_status Filesystem::symlink_status(vcpkg::LineInfo li, const fs::path& p) const noexcept { std::error_code ec; @@ -211,6 +234,12 @@ namespace vcpkg::Files return result; } + fs::file_status Filesystem::symlink_status(const fs::path& p, ignore_errors_t) const noexcept + { + std::error_code ec; + return this->symlink_status(p, ec); + } + void Filesystem::write_lines(const fs::path& path, const std::vector& lines, LineInfo linfo) { std::error_code ec; @@ -235,6 +264,14 @@ namespace vcpkg::Files } } + void Filesystem::remove_all(const fs::path& path, ignore_errors_t) + { + std::error_code ec; + fs::path failure_point; + + this->remove_all(path, ec, failure_point); + } + fs::path Filesystem::canonical(LineInfo li, const fs::path& path) const { std::error_code ec; @@ -244,6 +281,11 @@ namespace vcpkg::Files if (ec) Checks::exit_with_message(li, "Error getting canonicalization of %s: %s", path.string(), ec.message()); return result; } + fs::path Filesystem::canonical(const fs::path& path, ignore_errors_t) const + { + std::error_code ec; + return this->canonical(path, ec); + } struct RealFilesystem final : Filesystem { @@ -432,7 +474,8 @@ namespace vcpkg::Files break; } auto remaining = read_bytes; - while (remaining > 0) { + while (remaining > 0) + { auto read_result = write(o_fd, buffer.get(), remaining); if (read_result == -1) { @@ -444,7 +487,7 @@ namespace vcpkg::Files } } - copy_failure: ; + copy_failure:; } #endif if (written_bytes == -1) diff --git a/toolsrc/src/vcpkg/binarycaching.cpp b/toolsrc/src/vcpkg/binarycaching.cpp index af4a879ca..0750366ff 100644 --- a/toolsrc/src/vcpkg/binarycaching.cpp +++ b/toolsrc/src/vcpkg/binarycaching.cpp @@ -123,7 +123,6 @@ namespace const auto& abi_tag = action.package_abi.value_or_exit(VCPKG_LINE_INFO); auto& spec = action.spec; auto& fs = paths.get_filesystem(); - std::error_code ec; const fs::path archives_root_dir = paths.root / "archives"; const std::string archive_name = abi_tag + ".zip"; const fs::path archive_subpath = fs::u8path(abi_tag.substr(0, 2)) / archive_name; @@ -133,7 +132,8 @@ namespace compress_directory(paths, paths.package_dir(spec), tmp_archive_path); - fs.create_directories(archive_path.parent_path(), ec); + fs.create_directories(archive_path.parent_path(), ignore_errors); + std::error_code ec; fs.rename_or_copy(tmp_archive_path, archive_path, ".tmp", ec); if (ec) { @@ -163,7 +163,7 @@ namespace const auto tmp_log_path = paths.buildtrees / spec.name() / "tmp_failure_logs"; const auto tmp_log_path_destination = tmp_log_path / spec.name(); const auto tmp_failure_zip = paths.buildtrees / spec.name() / "failure_logs.zip"; - fs.create_directories(tmp_log_path_destination, ec); + fs.create_directories(tmp_log_path_destination, ignore_errors); for (auto& log_file : fs::stdfs::directory_iterator(paths.buildtrees / spec.name())) { @@ -178,7 +178,7 @@ namespace compress_directory(paths, tmp_log_path, paths.buildtrees / spec.name() / "failure_logs.zip"); - fs.create_directories(archive_tombstone_path.parent_path(), ec); + fs.create_directories(archive_tombstone_path.parent_path(), ignore_errors); fs.rename_or_copy(tmp_failure_zip, archive_tombstone_path, ".tmp", ec); // clean up temporary directory diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj b/toolsrc/vcpkglib/vcpkglib.vcxproj index 2fb776640..fc6d37de7 100644 --- a/toolsrc/vcpkglib/vcpkglib.vcxproj +++ b/toolsrc/vcpkglib/vcpkglib.vcxproj @@ -152,6 +152,7 @@ + -- cgit v1.2.3