| 1 |
|
| 2 |
renv_archive_type <- function(archive) { |
| 3 |
|
| 4 |
ext <- fileext(archive) |
| 5 |
|
| 6 |
if (ext %in% c(".tgz", ".tar", ".tar.gz")) |
| 7 |
return("tar") |
| 8 |
else if (ext %in% c(".zip")) |
| 9 |
return("zip") |
| 10 |
else |
| 11 |
return("unknown") |
| 12 |
|
| 13 |
} |
| 14 |
|
| 15 |
renv_archive_list <- function(archive) { |
| 16 |
suppressWarnings(renv_archive_list_impl(archive)) |
| 17 |
} |
| 18 |
|
| 19 |
renv_archive_list_impl <- function(archive) { |
| 20 |
|
| 21 |
switch( |
| 22 |
renv_archive_type(archive), |
| 23 |
tar = untar(archive, list = TRUE), |
| 24 |
zip = unzip(archive, list = TRUE)[["Name"]], |
| 25 |
stopf("don't know how to list files in archive '%s'", basename(archive)) |
| 26 |
) |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
renv_archive_decompress <- function(archive, files = NULL, exdir = ".", ...) { |
| 31 |
|
| 32 |
switch( |
| 33 |
renv_archive_type(archive), |
| 34 |
tar = renv_archive_decompress_tar(archive, files = files, exdir = exdir, ...), |
| 35 |
zip = renv_archive_decompress_zip(archive, files = files, exdir = exdir, ...), |
| 36 |
stopf("don't know how to decompress archive '%s'", basename(archive)) |
| 37 |
) |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
renv_archive_decompress_tar <- function(archive, files = NULL, exdir = ".", ...) { |
| 42 |
|
| 43 |
# if an appropriate system tar is available, use it |
| 44 |
tar <- renv_tar_exe() |
| 45 |
if (nzchar(tar)) |
| 46 |
return(renv_tar_decompress(tar, archive = archive, files = files, exdir = exdir, ...)) |
| 47 |
|
| 48 |
# when using internal TAR, we want to suppress warnings |
| 49 |
# (otherwise we get noise about global PAX headers) |
| 50 |
suppressWarnings(untar(archive, files = files, exdir = exdir, tar = "internal", ...)) |
| 51 |
return(TRUE) |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
renv_archive_decompress_zip <- function(archive, files = NULL, exdir = ".", ...) { |
| 56 |
|
| 57 |
# the default unzip tool will give warnings rather than |
| 58 |
# errors if R was unable to extract from a zip archive |
| 59 |
status <- tryCatch( |
| 60 |
unzip(archive, files = files, exdir = exdir, ...), |
| 61 |
condition = identity |
| 62 |
) |
| 63 |
|
| 64 |
if (inherits(status, "condition")) { |
| 65 |
fmt <- "failed to decompress '%s' [%s]" |
| 66 |
stopf(fmt, basename(archive), conditionMessage(status)) |
| 67 |
} |
| 68 |
|
| 69 |
TRUE |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
renv_archive_find <- function(archive, pattern) { |
| 74 |
files <- renv_archive_list(archive) |
| 75 |
grep(pattern, files, value = TRUE) |
| 76 |
} |
| 77 |
|
| 78 |
renv_archive_read <- function(archive, file) { |
| 79 |
|
| 80 |
type <- renv_archive_type(archive) |
| 81 |
case( |
| 82 |
type == "tar" ~ renv_archive_read_tar(archive, file), |
| 83 |
type == "zip" ~ renv_archive_read_zip(archive, file), |
| 84 |
~ stopf("don't know how to read file from archive %s", renv_path_pretty(archive)) |
| 85 |
) |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
renv_archive_read_tar <- function(archive, file) { |
| 90 |
|
| 91 |
# if an appropriate tar is available, use it |
| 92 |
tar <- renv_tar_exe() |
| 93 |
if (nzchar(tar)) { |
| 94 |
args <- c("xf", renv_shell_path(archive), "-O", renv_shell_path(file)) |
| 95 |
return(renv_system_exec(tar, args, action = "reading file from archive")) |
| 96 |
} |
| 97 |
|
| 98 |
# create extraction directory |
| 99 |
exdir <- renv_scope_tempfile("renv-archive-") |
| 100 |
ensure_directory(exdir) |
| 101 |
|
| 102 |
# unpack the requested file |
| 103 |
suppressWarnings(untar(archive, files = file, exdir = exdir, tar = "internal")) |
| 104 |
|
| 105 |
# and read it |
| 106 |
archive <- file.path(exdir, file) |
| 107 |
readLines(archive, warn = FALSE) |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
renv_archive_read_zip <- function(archive, file) { |
| 112 |
renv_scope_tempdir() |
| 113 |
conn <- unz(archive, file, encoding = "native.enc") |
| 114 |
defer(close(conn)) |
| 115 |
readLines(conn, warn = FALSE) |
| 116 |
} |