Skip to content

Commit 570a5d2

Browse files
committed
Merge remote-tracking branch 'quyykk/gha-upload-fixes' into gha-cache
2 parents c681dfe + 608a72b commit 570a5d2

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

include/vcpkg/base/downloads.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ namespace vcpkg
4343
StringView url,
4444
const std::vector<std::string>& secrets,
4545
View<std::string> headers,
46-
const Path& file,
47-
StringView method = "PUT");
46+
const Path& file);
47+
ExpectedL<Unit> patch_file(const Filesystem& fs,
48+
StringView url,
49+
View<std::string> headers,
50+
const Path& file,
51+
std::size_t file_size,
52+
std::size_t chunk_size = 450 * 1024 * 1024);
4853

4954
ExpectedL<std::string> invoke_http_request(StringView method,
5055
View<std::string> headers,

src/vcpkg/base/downloads.cpp

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ namespace vcpkg
527527
StringView url,
528528
const std::vector<std::string>& secrets,
529529
View<std::string> headers,
530-
const Path& file,
531-
StringView method)
530+
const Path& file)
532531
{
533532
static constexpr StringLiteral guid_marker = "9a1db05f-a65d-419b-aa72-037fb4d0672e";
534533

@@ -555,7 +554,7 @@ namespace vcpkg
555554
return std::move(maybe_res).error();
556555
}
557556

558-
auto http_cmd = Command{"curl"}.string_arg("-X").string_arg(method);
557+
auto http_cmd = Command{"curl"}.string_arg("-X").string_arg("PUT");
559558
for (auto&& header : headers)
560559
{
561560
http_cmd.string_arg("-H").string_arg(header);
@@ -586,6 +585,61 @@ namespace vcpkg
586585
return 0;
587586
}
588587

588+
ExpectedL<Unit> patch_file(const Filesystem& fs,
589+
StringView url,
590+
View<std::string> headers,
591+
const Path& file,
592+
std::size_t file_size,
593+
std::size_t chunk_size)
594+
{
595+
static constexpr StringLiteral guid_marker = "9a1db05f-a65d-419b-aa72-037fb4d0672e";
596+
597+
Command base_cmd;
598+
base_cmd.string_arg("curl").string_arg("-X").string_arg("PATCH").string_arg("-w").string_arg(
599+
"\\n" + guid_marker.to_string() + "%{http_code}\n");
600+
for (auto&& header : headers)
601+
{
602+
base_cmd.string_arg("-H").string_arg(header);
603+
}
604+
base_cmd.string_arg(url);
605+
606+
auto file_ptr = fs.open_for_read(file, VCPKG_LINE_INFO);
607+
std::vector<char> buffer(chunk_size);
608+
std::size_t bytes_read = 0;
609+
for (std::size_t i = 0; i < file_size; i += bytes_read)
610+
{
611+
bytes_read = file_ptr.read(buffer.data(), sizeof(decltype(buffer)::value_type), chunk_size);
612+
if (!bytes_read)
613+
{
614+
return msg::format_error(
615+
msgFileReadFailed, msg::path = file, msg::byte_offset = i, msg::count = chunk_size);
616+
}
617+
618+
auto cmd = base_cmd;
619+
cmd.string_arg("-H")
620+
.string_arg(fmt::format("Content-Range: bytes {}-{}/{}", i, i + bytes_read - 1, file_size))
621+
.string_arg("--data-binary")
622+
.string_arg("@-");
623+
624+
int code = 0;
625+
RedirectedProcessLaunchSettings launch_settings;
626+
launch_settings.stdin_content = {buffer.data(), bytes_read};
627+
auto res = cmd_execute_and_stream_lines(cmd, launch_settings, [&code](StringView line) {
628+
if (Strings::starts_with(line, guid_marker))
629+
{
630+
code = std::strtol(line.data() + guid_marker.size(), nullptr, 10);
631+
}
632+
});
633+
if (!res.get() || *res.get() != 0 || (code >= 100 && code < 200) || code >= 300)
634+
{
635+
return msg::format_error(
636+
msgCurlFailedToPutHttp, msg::exit_code = res.value_or(-1), msg::url = url, msg::value = code);
637+
}
638+
}
639+
640+
return Unit{};
641+
}
642+
589643
std::string format_url_query(StringView base_url, View<std::string> query_params)
590644
{
591645
auto url = base_url.to_string();

src/vcpkg/binarycaching.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,11 +918,10 @@ namespace
918918
m_token_header,
919919
m_accept_header.to_string(),
920920
"Content-Type: application/octet-stream",
921-
"Content-Range: bytes 0-" + std::to_string(cache_size) + "/*",
922921
};
922+
auto url = m_url + "/" + std::to_string(*cacheId.get());
923923

924-
const auto url = m_url + "/" + std::to_string(*cacheId.get());
925-
if (put_file(m_fs, url, {}, custom_headers, zip_path, "PATCH"))
924+
if (patch_file(m_fs, url, custom_headers, zip_path, cache_size))
926925
{
927926
Json::Object commit;
928927
commit.insert("size", std::to_string(cache_size));

0 commit comments

Comments
 (0)