From 75b6191dffe3043d99eed1a60ce0c612125ff39c Mon Sep 17 00:00:00 2001 From: Victor Romero Date: Wed, 27 Oct 2021 10:36:59 -0700 Subject: [PATCH] [vcpkg_download_distfile] Check SHA512 for already downloaded files (#20988) * [vcpkg_download_distfile] Check SHA512 for already downloaded files * [vcpkg_download_distfile] Respect SKIP_SHA512 to enable --head --- scripts/cmake/vcpkg_download_distfile.cmake | 39 +++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/scripts/cmake/vcpkg_download_distfile.cmake b/scripts/cmake/vcpkg_download_distfile.cmake index 8bbe9e3b574..08a039c0518 100644 --- a/scripts/cmake/vcpkg_download_distfile.cmake +++ b/scripts/cmake/vcpkg_download_distfile.cmake @@ -58,7 +58,7 @@ The helper [`vcpkg_from_github`](vcpkg_from_github.md) should be used for downlo * [freetype](https://github.com/Microsoft/vcpkg/blob/master/ports/freetype/portfile.cmake) #]===] -function(z_vcpkg_download_distfile_test_hash path kind error_advice sha512 skip_sha512) +function(z_vcpkg_download_distfile_test_hash file_path kind error_advice sha512 skip_sha512) if(_VCPKG_INTERNAL_NO_HASH_CHECK) # When using the internal hash skip, do not output an explicit message. return() @@ -68,14 +68,14 @@ function(z_vcpkg_download_distfile_test_hash path kind error_advice sha512 skip_ return() endif() - file(SHA512 "${path}" file_hash) + file(SHA512 "${file_path}" file_hash) if(NOT "${file_hash}" STREQUAL "${sha512}") message(FATAL_ERROR "\nFile does not have expected hash:\n" " File path: [ ${file_path} ]\n" " Expected hash: [ ${sha512} ]\n" " Actual hash: [ ${file_hash} ]\n" - "${CUSTOM_ERROR_ADVICE}\n") + "${error_advice}\n") endif() endfunction() @@ -204,21 +204,38 @@ If you do not know the SHA512, add it as 'SHA512 0' and re-run this command.") file(REMOVE_RECURSE "${DOWNLOADS}/temp") file(MAKE_DIRECTORY "${DOWNLOADS}/temp") + # check if file with same name already exists in downloads + if(EXISTS "${downloaded_file_path}") + set(advice_message "The cached file SHA512 doesn't match. The file may have been corrupted.") + if(_VCPKG_NO_DOWNLOADS) + string(APPEND advice_message " Downloads are disabled please provide a valid file at path ${downloaded_file_path} and retry.") + else() + string(APPEND advice_message " To re-download this file please delete cached file at path ${downloaded_file_path} and retry.") + endif() + + z_vcpkg_download_distfile_test_hash( + "${downloaded_file_path}" + "cached file" + "${advice_message}" + "${arg_SHA512}" + "${arg_SKIP_SHA512}" + ) + + if(NOT vcpkg_download_distfile_QUIET) + message(STATUS "Using cached ${arg_FILENAME}.") + endif() + + # Suppress the "Downloading ${arg_URLS} -> ${arg_FILENAME}..." message + set(vcpkg_download_distfile_QUIET TRUE) + endif() + # vcpkg_download_distfile_ALWAYS_REDOWNLOAD only triggers when NOT _VCPKG_NO_DOWNLOADS # this could be de-morgan'd out but it's more clear this way if(_VCPKG_NO_DOWNLOADS) if(NOT EXISTS "${downloaded_file_path}") message(FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist.") endif() - if(NOT arg_QUIET) - message(STATUS "Using ${downloaded_file_path}") - endif() - z_vcpkg_download_distfile_test_hash( - "${downloaded_file_path}" - "cached file" - "Please delete the file and retry if this file should be downloaded again." - ) set("${out_var}" "${downloaded_file_path}" PARENT_SCOPE) return() endif()