[vcpkg] Enable storing archives across filesystems

This commit is contained in:
Robert Schumacher 2018-06-27 17:40:51 -07:00
parent 898e3e3710
commit 0ad79a67c7
3 changed files with 55 additions and 3 deletions

View File

@ -37,6 +37,10 @@ namespace vcpkg::Files
virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) = 0;
virtual void rename(const fs::path& oldpath, const fs::path& newpath) = 0;
virtual void rename(const fs::path& oldpath, const fs::path& newpath, std::error_code& ec) = 0;
virtual void rename_or_copy(const fs::path& oldpath,
const fs::path& newpath,
StringLiteral temp_suffix,
std::error_code& ec) = 0;
virtual bool remove(const fs::path& path) = 0;
virtual bool remove(const fs::path& path, std::error_code& ec) = 0;
virtual std::uintmax_t remove_all(const fs::path& path, std::error_code& ec) = 0;

View File

@ -4,6 +4,14 @@
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#if defined(__linux__)
#include <fcntl.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
namespace vcpkg::Files
{
static const std::regex FILESYSTEM_INVALID_CHARACTERS_REGEX = std::regex(R"([\/:*?"<>|])");
@ -117,6 +125,42 @@ namespace vcpkg::Files
{
fs::stdfs::rename(oldpath, newpath);
}
virtual void rename_or_copy(const fs::path& oldpath,
const fs::path& newpath,
StringLiteral temp_suffix,
std::error_code& ec) override
{
this->rename(oldpath, newpath, ec);
#if defined(__linux__)
if (ec)
{
auto dst = newpath;
dst.replace_filename(dst.filename() + temp_suffix.c_str());
int i_fd = open(oldpath.c_str(), O_RDONLY);
if (i_fd == -1) return;
int o_fd = creat(dst.c_str(), 0664);
if (o_fd == -1)
{
close(i_fd);
return;
}
off_t bytes = 0;
struct stat info = {0};
fstat(i_fd, &info);
auto written_bytes = sendfile(o_fd, i_fd, &bytes, info.st_size);
close(i_fd);
close(o_fd);
if (written_bytes == -1) return;
this->rename(dst, newpath, ec);
if (ec) return;
this->remove(oldpath, ec);
}
#endif
}
virtual bool remove(const fs::path& path) override { return fs::stdfs::remove(path); }
virtual bool remove(const fs::path& path, std::error_code& ec) override { return fs::stdfs::remove(path, ec); }
virtual std::uintmax_t remove_all(const fs::path& path, std::error_code& ec) override

View File

@ -646,10 +646,14 @@ namespace vcpkg::Build
compress_archive(paths, spec, tmp_archive_path);
fs.create_directories(archive_path.parent_path(), ec);
fs.rename(tmp_archive_path, archive_path, ec);
fs.rename_or_copy(tmp_archive_path, archive_path, ".tmp", ec);
if (ec)
System::println(
System::Color::warning, "Failed to store binary cache: %s", archive_path.u8string());
{
System::println(System::Color::warning,
"Failed to store binary cache %s: %s",
archive_path.u8string(),
ec.message());
}
else
System::println("Stored binary cache: %s", archive_path.u8string());
}