Skip to content

Commit bc45cb5

Browse files
Download and check MD5 regardless of quite and force values
1 parent 982c5f3 commit bc45cb5

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

R/java_download.R

+29-23
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ java_download <- function(
3232
temp_dir = FALSE
3333
) {
3434

35+
# Download distribution and check MD5 checksum
36+
download_dist_check_md5 <- function(url, dest_file, md5sum, md5sum_expectedm, quiet) {
37+
38+
curl::curl_download(url, dest_file, quiet = FALSE)
39+
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
40+
41+
if (!quiet) {
42+
cli::cli_inform("Download completed.", .envir = environment())
43+
}
44+
md5sum <- tools::md5sum(dest_file)
45+
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
46+
47+
if (md5sum != md5sum_expected) {
48+
cli::cli_alert_danger("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
49+
unlink(dest_file)
50+
return(NULL)
51+
} else {
52+
if (!quiet) {
53+
cli::cli_inform("MD5 checksum verified.", .envir = environment())
54+
}
55+
}
56+
}
57+
3558
# override cache_path if temp_dir is set to TRUE
3659
if (temp_dir) {
3760
temp_dir <- tempdir()
@@ -96,7 +119,6 @@ java_download <- function(
96119
dest_file <- file.path(cache_path, basename(url))
97120
dest_file_md5 <- paste0(file.path(cache_path, basename(url_md5)), ".md5")
98121

99-
100122
if (!quiet) {
101123
cli::cli_inform("Downloading Java {version} ({distribution}) for {platform} {arch} to {dest_file}", .envir = environment())
102124
}
@@ -105,30 +127,14 @@ java_download <- function(
105127
if (!quiet) {
106128
cli::cli_inform("File already exists. Skipping download.", .envir = environment())
107129
}
108-
} else if(file.exists(dest_file)) {
109-
if (force) {
110-
if (!quiet) {
111-
cli::cli_inform("Removing existing installation.", .envir = environment())
112-
}
113-
file.remove(dest_file)
114-
curl::curl_download(url, dest_file, quiet = FALSE)
115-
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
116-
}
130+
} else if(file.exists(dest_file) & force) {
117131
if (!quiet) {
118-
cli::cli_inform("Download completed.", .envir = environment())
119-
}
120-
md5sum <- tools::md5sum(dest_file)
121-
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
122-
123-
if (md5sum != md5sum_expected) {
124-
cli::cli_alert_danger("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
125-
unlink(dest_file)
126-
return(NULL)
127-
} else {
128-
if (!quiet) {
129-
cli::cli_inform("MD5 checksum verified.", .envir = environment())
132+
cli::cli_inform("Removing existing installation.", .envir = environment())
130133
}
131-
}
134+
file.remove(dest_file)
135+
download_dist_check_md5(url, dest_file, md5sum, md5sum_expectedm, quiet)
136+
} else if (!file.exists(dest_file)) {
137+
download_dist_check_md5(url, dest_file, md5sum, md5sum_expectedm, quiet)
132138
}
133139

134140
return(dest_file)

0 commit comments

Comments
 (0)