-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathinstall-tar.R
More file actions
372 lines (340 loc) · 8.99 KB
/
Copy pathinstall-tar.R
File metadata and controls
372 lines (340 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#' Create a tar background process
#'
#' Use an external tar program, if there is a working one, otherwise use
#' the internal implementation.
#'
#' When using the internal implementation, we need to start another R
#' process.
#'
#' @param tarfile Tar file.
#' @param files Files or regular expressions to set what to extract. if
#' `NULL` then everything is extracted.
#' @param exdir Where to extract the archive. It must exist.
#' @param restore_times Whether to restore file modification times.
#' @param post_process Function to call after the extraction.
#' @param stdout Standard output of process.
#' @param stderr Standard error of process.
#' @param ... Extra arguments for the process constructor.
#' @return The [processx::process] object.
#' @noRd
make_untar_process <- function(
tarfile,
files = NULL,
exdir = ".",
restore_times = TRUE,
post_process = NULL,
stdout = "|",
stderr = "2>&1",
...
) {
internal <- need_internal_tar()
if (internal) {
r_untar_process$new(
tarfile,
files,
exdir,
restore_times,
post_process = post_process,
stdout = stdout,
stderr = stderr,
...
)
} else {
external_untar_process$new(
tarfile,
files,
exdir,
restore_times,
post_process = post_process,
stdout = stdout,
stderr = stderr,
...
)
}
}
#' Check if we need to use R's internal tar implementation
#'
#' This is slow, because we need to start an R child process, and the
#' implementation is also very slow. So it is better to use an external tar
#' program, if we can. We test this by trying to uncompress a `.tar.gz`
#' archive using the external program. The name of the tar program is
#' taken from the `TAR` environment variable, if this is unset then `tar`
#' is used.
#'
#' @return Whether we need to use the internal tar implementation.
#' @noRd
need_internal_tar <- local({
internal <- NULL
function() {
if (!is.null(internal)) {
return(internal)
}
mkdirp(tmp <- tempfile())
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
tarfile <- system.file(package = .packageName, "tools", "pkg_1.0.0.tgz")
tryCatch(
p <- external_untar_process$new(tarfile, exdir = tmp),
error = function(e) {
internal <<- TRUE
}
)
if (!is.null(internal)) {
return(internal)
}
p$wait(timeout = 2000)
p$kill()
internal <<- p$get_exit_status() != 0 ||
!file.exists(file.path(tmp, "pkg", "DESCRIPTION"))
internal
}
})
#' R6 class for an external un-tar process
#'
#' @description
#' Uses the system's `tar` program, in a background process.
#'
#' @noRd
external_untar_process <- R6::R6Class(
"external_untar_process",
inherit = processx::process,
public = list(
#' @details
#' Start running the background process that extracts the file.
#'
#' @param tarfile Path to the `.tar` or `.tar.gz`, etc. file to
#' uncompress.
#' @param files List of files to uncompress, see [utils::untar()].
#' @param exdir Directory to extract the files to.
#' @param restore_times Whether to restore modification files.
#' @param tar Name of the external `tar` program. Defaults to
#' `TAR` environment variable, or `tar` if unset.
#' @param stdout Standard output of process.
#' @param stderr Standard error of process.
#' @param post_process Function to call, once the extraction is
#' done, or `NULL`
#' @param ... Extra arguments for the process constructor.
#' @return New `r_untar_process` object.
initialize = function(
tarfile,
files = NULL,
exdir = ".",
restore_times = TRUE,
tar = Sys.getenv("TAR", "tar"),
stdout = "|",
stderr = "2>&1",
post_process = NULL,
...
) {
private$options <- list(
tarfile = normalizePath(tarfile),
files = files,
exdir = exdir,
restore_times = restore_times,
tar = tar
)
private$options$args <- eup_get_args(private$options)
super$initialize(
tar,
private$options$args,
post_process = post_process,
stdout = stdout,
stderr = stderr,
...
)
invisible(self)
}
),
private = list(
options = NULL
)
)
#' R6 class for an R un-tar process
#'
#' @description
#' Uses [utils::untar()], in a background process.
#'
#' @noRd
r_untar_process <- R6::R6Class(
"r_untar_process",
inherit = callr::r_process,
public = list(
#' @details
#' Start running the background R process that extracts the file.
#'
#' @param tarfile Path to the `.tar` or `.tar.gz`, etc. file to
#' uncompress.
#' @param files List of files to uncompress, see [utils::untar()].
#' @param exdir Directory to extract the files to.
#' @param restore_times Whether to restore modification files.
#' @param post_process Function to call, once the extraction is
#' done, or `NULL`
#' @param stdout Standard output of process.
#' @param stderr Standard error of process.
#' @param ... Extra arguments for the process cosntructor.
#' @return New `r_untar_process` object.
initialize = function(
tarfile,
files = NULL,
exdir = ".",
restore_times = TRUE,
post_process = NULL,
stdout = "|",
stderr = "2>&1",
...
) {
options <- list(
tarfile = normalizePath(tarfile),
files = files,
exdir = exdir,
restore_times = restore_times,
post_process = post_process
)
process_options <- callr::r_process_options(
stdout = stdout,
stderr = stderr
)
# nocov start
process_options$func <- function(options) {
ret <- utils::untar(
tarfile = options$tarfile,
files = options$files,
list = FALSE,
exdir = options$exdir,
compressed = NA,
restore_times = options$restore_times,
tar = "internal"
)
if (!is.null(options$post_process)) options$post_process() else ret
}
# nocov end
process_options$args <- list(options = options)
super$initialize(process_options)
}
),
private = list(
options = NULL
)
)
eup_get_args <- function(options) {
c(
"-x",
"-f",
path_norm(options$tarfile),
"-C",
path_norm(options$exdir),
# do not restore ownership, this is problematic on some mounts, e.f. sshfs
"-o",
get_untar_decompress_arg(options$tarfile),
if (!options$restore_times) "-m",
options$files
)
}
get_untar_decompress_arg <- function(tarfile) {
type <- detect_package_archive_type(tarfile)
switch(
type,
"gzip" = "-z",
"bzip2" = "-j",
"xz" = "-J",
"zip" = throw(pkg_error(
"{.path {tarfile}} is not a tar file, it looks like a zip file"
)),
"unknown" = character()
)
}
detect_package_archive_type <- function(file) {
buf <- readBin(file, what = "raw", n = 6)
if (is_gzip(buf)) {
"gzip"
} else if (is_zip(buf)) {
"zip"
} else if (is_bzip2(buf)) {
"bzip2"
} else if (is_xz(buf)) {
"xz"
} else {
"unknown"
}
}
is_gzip <- function(buf) {
if (!is.raw(buf)) {
buf <- readBin(buf, what = "raw", n = 3)
}
length(buf) >= 3 &&
buf[1] == 0x1f &&
buf[2] == 0x8b &&
buf[3] == 0x08
}
is_bzip2 <- function(buf) {
if (!is.raw(buf)) {
buf <- readBin(buf, what = "raw", n = 3)
}
length(buf) >= 3 &&
buf[1] == 0x42 &&
buf[2] == 0x5a &&
buf[3] == 0x68
}
is_xz <- function(buf) {
if (!is.raw(buf)) {
buf <- readBin(buf, what = "raw", n = 6)
}
length(buf) >= 6 &&
buf[1] == 0xFD &&
buf[2] == 0x37 &&
buf[3] == 0x7A &&
buf[4] == 0x58 &&
buf[5] == 0x5A &&
buf[6] == 0x00
}
is_zip <- function(buf) {
if (!is.raw(buf)) {
buf <- readBin(buf, what = "raw", n = 4)
}
length(buf) >= 4 &&
buf[1] == 0x50 &&
buf[2] == 0x4b &&
(buf[3] == 0x03 || buf[3] == 0x05 || buf[5] == 0x07) &&
(buf[4] == 0x04 || buf[4] == 0x06 || buf[4] == 0x08)
}
make_uncompress_process <- function(archive, exdir = ".", ...) {
type <- detect_package_archive_type(archive)
if (type == "unknown") {
throw(pkg_error(
"Cannot extract {.path {archive}}, unknown archive type.",
.class = "install_input_error"
))
}
if (type == "zip") {
make_unzip_process(archive, exdir = exdir)
} else {
make_untar_process(archive, exdir = exdir)
}
}
# This is similar but uses the async framework
run_uncompress_process <- function(archive, exdir = ".", ...) {
type <- detect_package_archive_type(archive)
if (type == "unknown") {
throw(pkg_error(
"Cannot extract {.path {archive}}, unknown archive type.",
.class = "install_input_error"
))
}
stdout <- tempfile()
if (type == "zip") {
external_process(
make_unzip_process,
zipfile = archive,
exdir = exdir,
stdout = stdout,
stderr = stdout
)
} else {
external_process(
make_untar_process,
tarfile = archive,
exdir = exdir,
stdout = stdout,
stderr = stdout
)
}
}