Debian Codegraph @

R/tar.R

raw
1 # This file was pulled from the R code base as of
2 # Thursday, November 22, 2012 at 6:24:55 AM UTC
3 # and edited to remove everything but the copyright
4 # header and untar2, and to make untar2 more tolerant
5 # of the 'x' and 'g' extended block indicators, the
6 # latter of which is used in tar files generated by
7 # GitHub.
8
9
10 # File src/library/utils/R/tar.R
11 # Part of the R package, http://www.R-project.org
12 #
13 # Copyright (C) 1995-2012 The R Core Team
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # A copy of the GNU General Public License is available at
26 # http://www.r-project.org/Licenses/
27
28 untar2 <- function(tarfile, files = NULL, list = FALSE, exdir = ".")
29 {
30 getOct <- function(x, offset, len)
31 {
32 x <- 0L
33 for(i in offset + seq_len(len)) {
34 z <- block[i]
35 if(!as.integer(z)) break; # terminate on nul
36 switch(rawToChar(z),
37 " " = {},
38 "0"=,"1"=,"2"=,"3"=,"4"=,"5"=,"6"=,"7"=
39 {x <- 8*x + (as.integer(z)-48)},
40 stop("invalid octal digit")
41 )
42 }
43 x
44 }
45
46 mydir.create <- function(path, ...) {
47 ## for Windows' sake
48 path <- sub("[\\/]$", "", path)
49 if(utils::file_test("-d", path)) return()
50 if(!dir.create(path, showWarnings = TRUE, recursive = TRUE, ...))
51 stop(gettextf("failed to create directory %s", sQuote(path)),
52 domain = NA)
53 }
54
55 warn1 <- character()
56
57 ## A tar file is a set of 512 byte records,
58 ## a header record followed by file contents (zero-padded).
59 ## See http://en.wikipedia.org/wiki/Tar_%28file_format%29
60 if(is.character(tarfile) && length(tarfile) == 1L) {
61 con <- gzfile(path.expand(tarfile), "rb") # reads compressed formats
62 on.exit(close(con))
63 } else if(inherits(tarfile, "connection")) con <- tarfile
64 else stop("'tarfile' must be a character string or a connection")
65 if (!missing(exdir)) {
66 mydir.create(exdir)
67 od <- setwd(exdir)
68 on.exit(setwd(od), add = TRUE)
69 }
70 contents <- character()
71 llink <- lname <- NULL
72 repeat{
73 block <- readBin(con, "raw", n = 512L)
74 if(!length(block)) break
75 if(length(block) < 512L) stop("incomplete block on file")
76 if(all(block == 0)) break
77 ns <- max(which(block[1:100] > 0))
78 name <- rawToChar(block[seq_len(ns)])
79 magic <- rawToChar(block[258:262])
80 if ((magic == "ustar") && block[346] > 0) {
81 ns <- max(which(block[346:500] > 0))
82 prefix <- rawToChar(block[345+seq_len(ns)])
83 name <- file.path(prefix, name)
84 }
85 ## mode zero-padded 8 bytes (including nul) at 101
86 ## Aargh: bsdtar has this one incorrectly with 6 bytes+space
87 mode <- as.octmode(getOct(block, 100, 8))
88 size <- getOct(block, 124, 12)
89 ts <- getOct(block, 136, 12)
90 ft <- as.POSIXct(as.numeric(ts), origin="1970-01-01", tz="UTC")
91 csum <- getOct(block, 148, 8)
92 block[149:156] <- charToRaw(" ")
93 xx <- as.integer(block)
94 checksum <- sum(xx) %% 2^24 # 6 bytes
95 if(csum != checksum) {
96 ## try it with signed bytes.
97 checksum <- sum(ifelse(xx > 127, xx - 128, xx)) %% 2^24 # 6 bytes
98 if(csum != checksum)
99 warning(gettextf("checksum error for entry '%s'", name),
100 domain = NA)
101 }
102 type <- block[157L]
103 ctype <- rawToChar(type)
104 if(type == 0L || ctype == "0") {
105 if(!is.null(lname)) {name <- lname; lname <- NULL}
106 contents <- c(contents, name)
107 remain <- size
108 dothis <- !list
109 if(dothis && length(files)) dothis <- name %in% files
110 if(dothis) {
111 mydir.create(dirname(name))
112 out <- file(name, "wb")
113 }
114 for(i in seq_len(ceiling(size/512L))) {
115 block <- readBin(con, "raw", n = 512L)
116 if(length(block) < 512L)
117 stop("incomplete block on file")
118 if (dothis) {
119 writeBin(block[seq_len(min(512L, remain))], out)
120 remain <- remain - 512L
121 }
122 }
123 if(dothis) {
124 close(out)
125 Sys.chmod(name, mode, FALSE) # override umask
126 Sys.setFileTime(name, ft)
127 }
128 } else if(ctype %in% c("1", "2")) { # hard and symbolic links
129 contents <- c(contents, name)
130 ns <- max(which(block[158:257] > 0))
131 name2 <- rawToChar(block[157L + seq_len(ns)])
132 if(!is.null(lname)) {name <- lname; lname <- NULL}
133 if(!is.null(llink)) {name2 <- llink; llink <- NULL}
134 if(!list) {
135 if(ctype == "1") {
136 if (!file.link(name2, name)) { # will give a warning
137 ## link failed, so try a file copy
138 if(file.copy(name2, name))
139 warn1 <- c(warn1, "restoring hard link as a file copy")
140 else
141 warning(gettextf("failed to copy %s to %s", sQuote(name2), sQuote(name)), domain = NA)
142 }
143 } else {
144 if(isWindows()) {
145 ## this will not work for links to dirs
146 from <- file.path(dirname(name), name2)
147 if (!file.copy(from, name))
148 warning(gettextf("failed to copy %s to %s", sQuote(from), sQuote(name)), domain = NA)
149 else
150 warn1 <- c(warn1, "restoring symbolic link as a file copy")
151 } else {
152 if(!file.symlink(name2, name)) { # will give a warning
153 ## so try a file copy: will not work for links to dirs
154 from <- file.path(dirname(name), name2)
155 if (file.copy(from, name))
156 warn1 <- c(warn1, "restoring symbolic link as a file copy")
157 else
158 warning(gettextf("failed to copy %s to %s", sQuote(from), sQuote(name)), domain = NA)
159 }
160 }
161 }
162 }
163 } else if(ctype == "5") {
164 contents <- c(contents, name)
165 if(!list) {
166 mydir.create(name)
167 Sys.chmod(name, mode, TRUE) # FIXME: check result
168 ## no point is setting time, as dir will be populated later.
169 }
170 } else if(ctype %in% c("L", "K")) {
171 ## This is a GNU extension that should no longer be
172 ## in use, but it is.
173 name_size <- 512L * ceiling(size/512L)
174 block <- readBin(con, "raw", n = name_size)
175 if(length(block) < name_size)
176 stop("incomplete block on file")
177 ns <- max(which(block > 0)) # size on file may or may not include final nul
178 if(ctype == "L")
179 lname <- rawToChar(block[seq_len(ns)])
180 else
181 llink <- rawToChar(block[seq_len(ns)])
182 } else if(ctype %in% c("x", "g")) {
183 readBin(con, "raw", n = 512L*ceiling(size/512L))
184 } else stop("unsupported entry type ", sQuote(ctype))
185 }
186 if(length(warn1)) {
187 warn1 <- unique(warn1)
188 for (w in warn1) warning(w, domain = NA)
189 }
190 if(list) contents else invisible(0L)
191 }