[vcpkg] Improve common case of ignoring filesystem errors (#10557)

This commit is contained in:
Robert Schumacher 2020-03-25 15:18:10 -07:00 committed by GitHub
parent 52273558f6
commit 42ad12f91d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 13 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <vcpkg/base/expected.h>
#include <vcpkg/base/ignore_errors.h>
#if USE_STD_FILESYSTEM
#include <filesystem>
@ -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<fs::path> find_from_PATH(const std::string& name) const = 0;
};

View File

@ -0,0 +1,10 @@
#pragma once
namespace vcpkg
{
struct ignore_errors_t
{
};
constexpr ignore_errors_t ignore_errors;
}

View File

@ -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;
// drop this on the floor, for compatibility with existing code
return exists(path, 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;
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<std::string>& 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)

View File

@ -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

View File

@ -152,6 +152,7 @@
<ClInclude Include="..\include\vcpkg\base\files.h" />
<ClInclude Include="..\include\vcpkg\base\graphs.h" />
<ClInclude Include="..\include\vcpkg\base\hash.h" />
<ClInclude Include="..\include\vcpkg\base\ignore_errors.h" />
<ClInclude Include="..\include\vcpkg\base\lazy.h" />
<ClInclude Include="..\include\vcpkg\base\lineinfo.h" />
<ClInclude Include="..\include\vcpkg\base\machinetype.h" />