Skip to content

Commit 9d16a8a

Browse files
author
ripley
committed
updates and clarifications, including to which extracted paths have been altered
git-svn-id: https://svn.r-project.org/R/trunk@88347 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent d91ba03 commit 9d16a8a

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

src/library/utils/R/tar.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ untar <- function(tarfile, files = NULL, list = FALSE, exdir = ".",
4141

4242
## The ability of external tar commands to handle compressed tarfiles
4343
## automagically varies and is poorly documented.
44-
## E.g. macOS says its tar handles bzip2 but does not mention xz nor lzma.
45-
## (And it supports -J and --lzma flags not mentioned by man tar.)
44+
## E.g. macOS used to say its tar handles bzip2 but did not mention xz nor lzma.
4645
##
4746
## But as all commonly-used tars do (some commercial Unix do not,
4847
## but GNU tar is commonly used there).
48+
##
49+
## OTOH some (e.g. macOS) need external commands which may not be present.
4950
cflag <- ""
5051
if (!missing(compressed))
5152
warning("untar(compressed=) is deprecated", call. = FALSE, domain = NA)
@@ -124,7 +125,7 @@ untar <- function(tarfile, files = NULL, list = FALSE, exdir = ".",
124125
}
125126
}
126127

127-
##' R's "internal" untar() -- called from of untar(), *not* exported
128+
##' R's "internal" untar() -- called from untar(), *not* exported
128129
untar2 <- function(tarfile, files = NULL, list = FALSE, exdir = ".",
129130
restore_times = TRUE)
130131
{
@@ -151,7 +152,8 @@ untar2 <- function(tarfile, files = NULL, list = FALSE, exdir = ".",
151152
path <- gsub("\\\\", "/", path)
152153
while(grepl("^/", path) || grepl("^[a-zA-Z]:", path)) {
153154
if (grepl("^/", path)) {
154-
warning("removing leading '/'")
155+
warning(gettextf("removing leading '/' from '%s'", path),
156+
call. = FALSE, domain = NA)
155157
path <- sub("^/+", "", path)
156158
}
157159
if (grepl("^[a-zA-Z]:", path)) {
@@ -162,7 +164,8 @@ untar2 <- function(tarfile, files = NULL, list = FALSE, exdir = ".",
162164
}
163165
} else {
164166
if (grepl("^/", path)) {
165-
warning("removing leading '/'")
167+
warning(gettextf("removing leading '/' from '%s'", path),
168+
call. = FALSE, domain = NA)
166169
path <- sub("^/+", "", path)
167170
}
168171
}
@@ -175,11 +178,8 @@ untar2 <- function(tarfile, files = NULL, list = FALSE, exdir = ".",
175178
return(".")
176179
p <- ""
177180
for(el in parts) {
178-
if (nzchar(p))
179-
p <- file.path(p, el)
180-
else
181-
p <- el
182-
if(isTRUE(nzchar(Sys.readlink(p), keepNA=TRUE)))
181+
p <- if (nzchar(p)) file.path(p, el) else el
182+
if(isTRUE(nzchar(Sys.readlink(p), keepNA = TRUE)))
183183
stop("cannot extract through symlink")
184184
}
185185
p
@@ -433,7 +433,7 @@ tar <- function(tarfile, files = NULL,
433433
## Could pipe through gzip etc: might be safer for xz
434434
## as -J was lzma in GNU tar 1.20:21
435435
## NetBSD < 8 used --xz not -J
436-
## OpenBSD and Heirloom Toolchest have no support for xz
436+
## OpenBSD and Heirloom Toolchest had no support for xz
437437
flags <- switch(match.arg(compression),
438438
"none" = "-cf",
439439
"gzip" = "-zcf",
@@ -449,7 +449,7 @@ tar <- function(tarfile, files = NULL,
449449
if (is.null(extra_flags)) extra_flags <- ""
450450
## precaution added in R 3.5.0 for over-long command lines
451451
nc <- nchar(ff <- paste(shQuote(files), collapse=" "))
452-
## -T is not supported by Solaris nor Heirloom Toolchest's tar
452+
## -T was not supported by Solaris nor Heirloom Toolchest's tar
453453
if(nc > 1000 &&
454454
any(grepl("(GNU tar|libarchive)",
455455
tryCatch(system(paste(tar, "--version"), intern = TRUE),

src/library/utils/man/untar.Rd

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ untar(tarfile, files = NULL, list = FALSE, exdir = ".",
5252
}
5353

5454
\item{extras}{\code{NULL} or a character string: further command-line
55-
flags such as \option{-p} or \option{-P} to be passed to an external
56-
\command{tar} program.}
55+
flags such as \option{-p}, \option{-P} or \option{-k} to be passed
56+
to an external \command{tar} program.}
5757

5858
\item{verbose}{logical: if true echo the command used for an external
5959
\command{tar} program.}
@@ -112,36 +112,36 @@ untar(tarfile, files = NULL, list = FALSE, exdir = ".",
112112
compression.
113113

114114
On a Unix-alike, \command{configure} will set environment variable
115-
\env{TAR}, preferring GNU tar if found.}
115+
\env{TAR}, preferring GNU tar if found as \command{gtar} or
116+
\command{gnutar}.}
116117

117118
%% bsdtar had it in FreeBSB 5.3 (2004)
118-
\item{\code{bsdtar}:}{macOS 10.6 and later (and FreeBSD and some
119-
other OSes) have a \command{tar} from the \I{libarchive} project
120-
which detects known-to-it forms of compression automagically.
121-
However, this may rely on an external command being available: macOS
122-
has a tar which knows about \code{zstd} compression, but relies
123-
on a \command{zstd} command which it does not supply.
119+
\item{\code{bsdtar}:}{macOS 10.6 and later (and FreeBSD, NetBSD >= 9
120+
and some other OSes) have a \command{tar} from the \I{libarchive}
121+
project which detects known-to-it forms of compression
122+
automagically. However, this may rely on an external command
123+
being available: macOS has a tar which knows about \code{zstd}
124+
compression, but relies on a \command{zstd} command which it does
125+
not supply.
124126

125127
This added support for \command{xz} in 2019 and for \command{zstd}
126128
in 2020 (if the appropriate library or external program is
127129
available).
128-
}
129130

130-
\item{NetBSD:}{It is undocumented if \I{NetBSD}'s \command{tar} can
131-
detect compression automagically: for versions before 8 the flag
132-
for \command{xz} compression was \option{--xz} not \option{-J}.
133-
So \code{support_old_tars = TRUE} is recommended (or use
134-
\command{bsdtar} if installed).}
131+
Recent versions of Windows supply a build of \code{bsdtar} as
132+
\code{tar.exe}, but with compiled-in support only for \code{gzip}
133+
compresssion.
134+
}
135135

136136
%% https://news.ycombinator.com/item?id=10722697
137137
\item{OpenBSD:}{\I{OpenBSD}'s \command{tar} does not detect compression
138138
automagically. It has no support for \command{xz} beyond reporting
139139
that the file is \command{xz}-compressed. So \code{support_old_tars
140140
= TRUE} is recommended.}
141141
142-
\item{Heirloom Toolchest:}{This \command{tar} does automagically
143-
detect \command{gzip} and \command{bzip2} compression (undocumented)
144-
but had no support for \command{xz} nor \command{zstd} compression.}
142+
%% \item{Heirloom Toolchest:}{This \command{tar} does automagically
143+
%% detect \command{gzip} and \command{bzip2} compression (undocumented)
144+
%% but had no support for \command{xz} nor \command{zstd} compression.}
145145
146146
\item{Older support:}{Environment variable \env{R_GZIPCMD} gives the
147147
command to decompress \command{gzip} files, and
@@ -151,8 +151,8 @@ untar(tarfile, files = NULL, list = FALSE, exdir = ".",
151151
decompression is expected to fail.}
152152
}
153153
154-
Arguments \code{compressed}, \code{extras} and \code{verbose} are only
155-
used when an external \command{tar} is used.
154+
Arguments \code{compressed} and \code{verbose} are only used for an
155+
external \command{tar}.
156156
157157
Some external \command{tar} commands will detect some of
158158
\command{lrzip}, \command{lzma}, \command{lz4} and \command{lzop}
@@ -174,12 +174,11 @@ untar(tarfile, files = NULL, list = FALSE, exdir = ".",
174174
handle files compressed by any of the methods that function can
175175
handle: at least \command{compress}, \command{gzip}, \command{bzip2},
176176
\command{xz} and \command{zstd} compression, and some types of
177-
\command{lzma} compression. It does not guard against restoring
178-
absolute file paths, as some \command{tar} implementations do. It
179-
will create the parent directories for directories or files in the
180-
archive if necessary. It handles the \I{USTAR}/POSIX, GNU and
181-
\command{pax} ways of handling file paths of more than 100 bytes, and
182-
the GNU way of handling link targets of more than 100 bytes.
177+
\command{lzma} compression. It will create the parent directories for
178+
directories or files in the archive if necessary. It handles the
179+
\I{USTAR}/POSIX, GNU and \command{pax} ways of handling file paths of
180+
more than 100 bytes, and the GNU way of handling link targets of more
181+
than 100 bytes.
183182
184183
You may see warnings from the internal implementation such
185184
as \preformatted{ unsupported entry type 'x'}
@@ -206,7 +205,12 @@ untar(tarfile, files = NULL, list = FALSE, exdir = ".",
206205
\command{bsdtar} remove the leading slash on absolute filepaths:
207206
specify \code{extras = "-P"} to override this. \command{bsdtar}
208207
refuses to extract paths containing \code{".."} with the same
209-
workaround.
208+
workaround. The internal implementation removes leading slashes (with
209+
a warning) and stops with an error for paths starting with \code{"~"}
210+
or containing \code{".."}.
211+
212+
Extracted files will overwrite any existing file unless flag \option{-k}
213+
is included in \code{extras}.
210214
}
211215

212216
\value{

0 commit comments

Comments
 (0)