Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 7b30fd0e1d8bc77f0556181c82f85d046b058f27)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -41,6 +41,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 41 | 41 |
# IMPORTANT: If the version of Go is updated, the Windows to Linux CI machines |
| 42 | 42 |
# will need updating, to avoid errors. Ping #docker-maintainers on IRC |
| 43 | 43 |
# with a heads-up. |
| 44 |
+# IMPORTANT: When updating this please note that stdlib archive/tar pkg is vendored |
|
| 44 | 45 |
ENV GO_VERSION 1.10.1 |
| 45 | 46 |
RUN curl -fsSL "https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz" \
|
| 46 | 47 |
| tar -xzC /usr/local |
| ... | ... |
@@ -150,3 +150,9 @@ github.com/Nvveen/Gotty a8b993ba6abdb0e0c12b0125c603323a71c7790c https://github. |
| 150 | 150 |
github.com/docker/go-metrics d466d4f6fd960e01820085bd7e1a24426ee7ef18 |
| 151 | 151 |
|
| 152 | 152 |
github.com/opencontainers/selinux b29023b86e4a69d1b46b7e7b4e2b6fda03f0b9cd |
| 153 |
+ |
|
| 154 |
+ |
|
| 155 |
+# archive/tar (for Go 1.10, see https://github.com/golang/go/issues/24787) |
|
| 156 |
+# mkdir -p ./vendor/archive |
|
| 157 |
+# git clone -b go-1.10 --depth=1 git@github.com:kolyshkin/go-tar.git ./vendor/archive/tar |
|
| 158 |
+# vndr # to clean up test files |
| 153 | 159 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,27 @@ |
| 0 |
+Copyright (c) 2009 The Go Authors. All rights reserved. |
|
| 1 |
+ |
|
| 2 |
+Redistribution and use in source and binary forms, with or without |
|
| 3 |
+modification, are permitted provided that the following conditions are |
|
| 4 |
+met: |
|
| 5 |
+ |
|
| 6 |
+ * Redistributions of source code must retain the above copyright |
|
| 7 |
+notice, this list of conditions and the following disclaimer. |
|
| 8 |
+ * Redistributions in binary form must reproduce the above |
|
| 9 |
+copyright notice, this list of conditions and the following disclaimer |
|
| 10 |
+in the documentation and/or other materials provided with the |
|
| 11 |
+distribution. |
|
| 12 |
+ * Neither the name of Google Inc. nor the names of its |
|
| 13 |
+contributors may be used to endorse or promote products derived from |
|
| 14 |
+this software without specific prior written permission. |
|
| 15 |
+ |
|
| 16 |
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
| 17 |
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
| 18 |
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
| 19 |
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
| 20 |
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
| 21 |
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
| 22 |
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
| 23 |
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
| 24 |
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
| 25 |
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
| 26 |
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 0 | 27 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,27 @@ |
| 0 |
+This is a fork of Go 1.10 `archive/tar` package from the official |
|
| 1 |
+[repo](https://github.com/golang/go/tree/release-branch.go1.10/src/archive/tar), |
|
| 2 |
+with a partial [revert](https://github.com/kolyshkin/go-tar/commit/d651d6e45972363e9bb62b8e9d876df440b31628) |
|
| 3 |
+of upstream [commit 0564e304a6ea](https://github.com/golang/go/commit/0564e304a6ea394a42929060c588469dbd6f32af). |
|
| 4 |
+It is suggested as a replacement to the original package included with Go 1.10 |
|
| 5 |
+in case you want to build a static Linux/glibc binary that works, and |
|
| 6 |
+can't afford to use `CGO_ENABLED=0`. |
|
| 7 |
+ |
|
| 8 |
+## Details |
|
| 9 |
+ |
|
| 10 |
+Using Go 1.10 [archive/tar](https://golang.org/pkg/archive/tar/) from a static binary |
|
| 11 |
+compiled with glibc on Linux can result in a panic upon calling |
|
| 12 |
+[`tar.FileInfoHeader()`](https://golang.org/pkg/archive/tar/#FileInfoHeader). |
|
| 13 |
+This is a major regression in Go 1.10, filed as |
|
| 14 |
+[Go issue #24787](https://github.com/golang/go/issues/24787). |
|
| 15 |
+ |
|
| 16 |
+The above issue is caused by an unfortunate combination of: |
|
| 17 |
+1. glibc way of dynamic loading of nss libraries even for a static build; |
|
| 18 |
+2. Go `os/user` package hard-coded reliance on libc to resolve user/group IDs to names (unless CGO is disabled). |
|
| 19 |
+ |
|
| 20 |
+While glibc can probably not be fixed and is not considered a bug per se, |
|
| 21 |
+the `os/user` issue is documented (see [Go issue #23265](https://github.com/golang/go/issues/23265)) |
|
| 22 |
+and already fixed by [Go commit 62f0127d81](https://github.com/golang/go/commit/62f0127d8104d8266d9a3fb5a87e2f09ec8b6f5b). |
|
| 23 |
+The fix is expected to make its way to Go 1.11, and requires `osusergo` build tag |
|
| 24 |
+to be used for a static build. |
|
| 25 |
+ |
|
| 26 |
+This repository serves as a temporary workaround until the above fix is available. |
| 0 | 27 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,720 @@ |
| 0 |
+// Copyright 2009 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// Package tar implements access to tar archives. |
|
| 5 |
+// |
|
| 6 |
+// Tape archives (tar) are a file format for storing a sequence of files that |
|
| 7 |
+// can be read and written in a streaming manner. |
|
| 8 |
+// This package aims to cover most variations of the format, |
|
| 9 |
+// including those produced by GNU and BSD tar tools. |
|
| 10 |
+package tar |
|
| 11 |
+ |
|
| 12 |
+import ( |
|
| 13 |
+ "errors" |
|
| 14 |
+ "fmt" |
|
| 15 |
+ "math" |
|
| 16 |
+ "os" |
|
| 17 |
+ "path" |
|
| 18 |
+ "reflect" |
|
| 19 |
+ "strconv" |
|
| 20 |
+ "strings" |
|
| 21 |
+ "time" |
|
| 22 |
+) |
|
| 23 |
+ |
|
| 24 |
+// BUG: Use of the Uid and Gid fields in Header could overflow on 32-bit |
|
| 25 |
+// architectures. If a large value is encountered when decoding, the result |
|
| 26 |
+// stored in Header will be the truncated version. |
|
| 27 |
+ |
|
| 28 |
+var ( |
|
| 29 |
+ ErrHeader = errors.New("archive/tar: invalid tar header")
|
|
| 30 |
+ ErrWriteTooLong = errors.New("archive/tar: write too long")
|
|
| 31 |
+ ErrFieldTooLong = errors.New("archive/tar: header field too long")
|
|
| 32 |
+ ErrWriteAfterClose = errors.New("archive/tar: write after close")
|
|
| 33 |
+ errMissData = errors.New("archive/tar: sparse file references non-existent data")
|
|
| 34 |
+ errUnrefData = errors.New("archive/tar: sparse file contains unreferenced data")
|
|
| 35 |
+ errWriteHole = errors.New("archive/tar: write non-NUL byte in sparse hole")
|
|
| 36 |
+) |
|
| 37 |
+ |
|
| 38 |
+type headerError []string |
|
| 39 |
+ |
|
| 40 |
+func (he headerError) Error() string {
|
|
| 41 |
+ const prefix = "archive/tar: cannot encode header" |
|
| 42 |
+ var ss []string |
|
| 43 |
+ for _, s := range he {
|
|
| 44 |
+ if s != "" {
|
|
| 45 |
+ ss = append(ss, s) |
|
| 46 |
+ } |
|
| 47 |
+ } |
|
| 48 |
+ if len(ss) == 0 {
|
|
| 49 |
+ return prefix |
|
| 50 |
+ } |
|
| 51 |
+ return fmt.Sprintf("%s: %v", prefix, strings.Join(ss, "; and "))
|
|
| 52 |
+} |
|
| 53 |
+ |
|
| 54 |
+// Type flags for Header.Typeflag. |
|
| 55 |
+const ( |
|
| 56 |
+ // Type '0' indicates a regular file. |
|
| 57 |
+ TypeReg = '0' |
|
| 58 |
+ TypeRegA = '\x00' // For legacy support; use TypeReg instead |
|
| 59 |
+ |
|
| 60 |
+ // Type '1' to '6' are header-only flags and may not have a data body. |
|
| 61 |
+ TypeLink = '1' // Hard link |
|
| 62 |
+ TypeSymlink = '2' // Symbolic link |
|
| 63 |
+ TypeChar = '3' // Character device node |
|
| 64 |
+ TypeBlock = '4' // Block device node |
|
| 65 |
+ TypeDir = '5' // Directory |
|
| 66 |
+ TypeFifo = '6' // FIFO node |
|
| 67 |
+ |
|
| 68 |
+ // Type '7' is reserved. |
|
| 69 |
+ TypeCont = '7' |
|
| 70 |
+ |
|
| 71 |
+ // Type 'x' is used by the PAX format to store key-value records that |
|
| 72 |
+ // are only relevant to the next file. |
|
| 73 |
+ // This package transparently handles these types. |
|
| 74 |
+ TypeXHeader = 'x' |
|
| 75 |
+ |
|
| 76 |
+ // Type 'g' is used by the PAX format to store key-value records that |
|
| 77 |
+ // are relevant to all subsequent files. |
|
| 78 |
+ // This package only supports parsing and composing such headers, |
|
| 79 |
+ // but does not currently support persisting the global state across files. |
|
| 80 |
+ TypeXGlobalHeader = 'g' |
|
| 81 |
+ |
|
| 82 |
+ // Type 'S' indicates a sparse file in the GNU format. |
|
| 83 |
+ TypeGNUSparse = 'S' |
|
| 84 |
+ |
|
| 85 |
+ // Types 'L' and 'K' are used by the GNU format for a meta file |
|
| 86 |
+ // used to store the path or link name for the next file. |
|
| 87 |
+ // This package transparently handles these types. |
|
| 88 |
+ TypeGNULongName = 'L' |
|
| 89 |
+ TypeGNULongLink = 'K' |
|
| 90 |
+) |
|
| 91 |
+ |
|
| 92 |
+// Keywords for PAX extended header records. |
|
| 93 |
+const ( |
|
| 94 |
+ paxNone = "" // Indicates that no PAX key is suitable |
|
| 95 |
+ paxPath = "path" |
|
| 96 |
+ paxLinkpath = "linkpath" |
|
| 97 |
+ paxSize = "size" |
|
| 98 |
+ paxUid = "uid" |
|
| 99 |
+ paxGid = "gid" |
|
| 100 |
+ paxUname = "uname" |
|
| 101 |
+ paxGname = "gname" |
|
| 102 |
+ paxMtime = "mtime" |
|
| 103 |
+ paxAtime = "atime" |
|
| 104 |
+ paxCtime = "ctime" // Removed from later revision of PAX spec, but was valid |
|
| 105 |
+ paxCharset = "charset" // Currently unused |
|
| 106 |
+ paxComment = "comment" // Currently unused |
|
| 107 |
+ |
|
| 108 |
+ paxSchilyXattr = "SCHILY.xattr." |
|
| 109 |
+ |
|
| 110 |
+ // Keywords for GNU sparse files in a PAX extended header. |
|
| 111 |
+ paxGNUSparse = "GNU.sparse." |
|
| 112 |
+ paxGNUSparseNumBlocks = "GNU.sparse.numblocks" |
|
| 113 |
+ paxGNUSparseOffset = "GNU.sparse.offset" |
|
| 114 |
+ paxGNUSparseNumBytes = "GNU.sparse.numbytes" |
|
| 115 |
+ paxGNUSparseMap = "GNU.sparse.map" |
|
| 116 |
+ paxGNUSparseName = "GNU.sparse.name" |
|
| 117 |
+ paxGNUSparseMajor = "GNU.sparse.major" |
|
| 118 |
+ paxGNUSparseMinor = "GNU.sparse.minor" |
|
| 119 |
+ paxGNUSparseSize = "GNU.sparse.size" |
|
| 120 |
+ paxGNUSparseRealSize = "GNU.sparse.realsize" |
|
| 121 |
+) |
|
| 122 |
+ |
|
| 123 |
+// basicKeys is a set of the PAX keys for which we have built-in support. |
|
| 124 |
+// This does not contain "charset" or "comment", which are both PAX-specific, |
|
| 125 |
+// so adding them as first-class features of Header is unlikely. |
|
| 126 |
+// Users can use the PAXRecords field to set it themselves. |
|
| 127 |
+var basicKeys = map[string]bool{
|
|
| 128 |
+ paxPath: true, paxLinkpath: true, paxSize: true, paxUid: true, paxGid: true, |
|
| 129 |
+ paxUname: true, paxGname: true, paxMtime: true, paxAtime: true, paxCtime: true, |
|
| 130 |
+} |
|
| 131 |
+ |
|
| 132 |
+// A Header represents a single header in a tar archive. |
|
| 133 |
+// Some fields may not be populated. |
|
| 134 |
+// |
|
| 135 |
+// For forward compatibility, users that retrieve a Header from Reader.Next, |
|
| 136 |
+// mutate it in some ways, and then pass it back to Writer.WriteHeader |
|
| 137 |
+// should do so by creating a new Header and copying the fields |
|
| 138 |
+// that they are interested in preserving. |
|
| 139 |
+type Header struct {
|
|
| 140 |
+ Typeflag byte // Type of header entry (should be TypeReg for most files) |
|
| 141 |
+ |
|
| 142 |
+ Name string // Name of file entry |
|
| 143 |
+ Linkname string // Target name of link (valid for TypeLink or TypeSymlink) |
|
| 144 |
+ |
|
| 145 |
+ Size int64 // Logical file size in bytes |
|
| 146 |
+ Mode int64 // Permission and mode bits |
|
| 147 |
+ Uid int // User ID of owner |
|
| 148 |
+ Gid int // Group ID of owner |
|
| 149 |
+ Uname string // User name of owner |
|
| 150 |
+ Gname string // Group name of owner |
|
| 151 |
+ |
|
| 152 |
+ // If the Format is unspecified, then Writer.WriteHeader rounds ModTime |
|
| 153 |
+ // to the nearest second and ignores the AccessTime and ChangeTime fields. |
|
| 154 |
+ // |
|
| 155 |
+ // To use AccessTime or ChangeTime, specify the Format as PAX or GNU. |
|
| 156 |
+ // To use sub-second resolution, specify the Format as PAX. |
|
| 157 |
+ ModTime time.Time // Modification time |
|
| 158 |
+ AccessTime time.Time // Access time (requires either PAX or GNU support) |
|
| 159 |
+ ChangeTime time.Time // Change time (requires either PAX or GNU support) |
|
| 160 |
+ |
|
| 161 |
+ Devmajor int64 // Major device number (valid for TypeChar or TypeBlock) |
|
| 162 |
+ Devminor int64 // Minor device number (valid for TypeChar or TypeBlock) |
|
| 163 |
+ |
|
| 164 |
+ // Xattrs stores extended attributes as PAX records under the |
|
| 165 |
+ // "SCHILY.xattr." namespace. |
|
| 166 |
+ // |
|
| 167 |
+ // The following are semantically equivalent: |
|
| 168 |
+ // h.Xattrs[key] = value |
|
| 169 |
+ // h.PAXRecords["SCHILY.xattr."+key] = value |
|
| 170 |
+ // |
|
| 171 |
+ // When Writer.WriteHeader is called, the contents of Xattrs will take |
|
| 172 |
+ // precedence over those in PAXRecords. |
|
| 173 |
+ // |
|
| 174 |
+ // Deprecated: Use PAXRecords instead. |
|
| 175 |
+ Xattrs map[string]string |
|
| 176 |
+ |
|
| 177 |
+ // PAXRecords is a map of PAX extended header records. |
|
| 178 |
+ // |
|
| 179 |
+ // User-defined records should have keys of the following form: |
|
| 180 |
+ // VENDOR.keyword |
|
| 181 |
+ // Where VENDOR is some namespace in all uppercase, and keyword may |
|
| 182 |
+ // not contain the '=' character (e.g., "GOLANG.pkg.version"). |
|
| 183 |
+ // The key and value should be non-empty UTF-8 strings. |
|
| 184 |
+ // |
|
| 185 |
+ // When Writer.WriteHeader is called, PAX records derived from the |
|
| 186 |
+ // the other fields in Header take precedence over PAXRecords. |
|
| 187 |
+ PAXRecords map[string]string |
|
| 188 |
+ |
|
| 189 |
+ // Format specifies the format of the tar header. |
|
| 190 |
+ // |
|
| 191 |
+ // This is set by Reader.Next as a best-effort guess at the format. |
|
| 192 |
+ // Since the Reader liberally reads some non-compliant files, |
|
| 193 |
+ // it is possible for this to be FormatUnknown. |
|
| 194 |
+ // |
|
| 195 |
+ // If the format is unspecified when Writer.WriteHeader is called, |
|
| 196 |
+ // then it uses the first format (in the order of USTAR, PAX, GNU) |
|
| 197 |
+ // capable of encoding this Header (see Format). |
|
| 198 |
+ Format Format |
|
| 199 |
+} |
|
| 200 |
+ |
|
| 201 |
+// sparseEntry represents a Length-sized fragment at Offset in the file. |
|
| 202 |
+type sparseEntry struct{ Offset, Length int64 }
|
|
| 203 |
+ |
|
| 204 |
+func (s sparseEntry) endOffset() int64 { return s.Offset + s.Length }
|
|
| 205 |
+ |
|
| 206 |
+// A sparse file can be represented as either a sparseDatas or a sparseHoles. |
|
| 207 |
+// As long as the total size is known, they are equivalent and one can be |
|
| 208 |
+// converted to the other form and back. The various tar formats with sparse |
|
| 209 |
+// file support represent sparse files in the sparseDatas form. That is, they |
|
| 210 |
+// specify the fragments in the file that has data, and treat everything else as |
|
| 211 |
+// having zero bytes. As such, the encoding and decoding logic in this package |
|
| 212 |
+// deals with sparseDatas. |
|
| 213 |
+// |
|
| 214 |
+// However, the external API uses sparseHoles instead of sparseDatas because the |
|
| 215 |
+// zero value of sparseHoles logically represents a normal file (i.e., there are |
|
| 216 |
+// no holes in it). On the other hand, the zero value of sparseDatas implies |
|
| 217 |
+// that the file has no data in it, which is rather odd. |
|
| 218 |
+// |
|
| 219 |
+// As an example, if the underlying raw file contains the 10-byte data: |
|
| 220 |
+// var compactFile = "abcdefgh" |
|
| 221 |
+// |
|
| 222 |
+// And the sparse map has the following entries: |
|
| 223 |
+// var spd sparseDatas = []sparseEntry{
|
|
| 224 |
+// {Offset: 2, Length: 5}, // Data fragment for 2..6
|
|
| 225 |
+// {Offset: 18, Length: 3}, // Data fragment for 18..20
|
|
| 226 |
+// } |
|
| 227 |
+// var sph sparseHoles = []sparseEntry{
|
|
| 228 |
+// {Offset: 0, Length: 2}, // Hole fragment for 0..1
|
|
| 229 |
+// {Offset: 7, Length: 11}, // Hole fragment for 7..17
|
|
| 230 |
+// {Offset: 21, Length: 4}, // Hole fragment for 21..24
|
|
| 231 |
+// } |
|
| 232 |
+// |
|
| 233 |
+// Then the content of the resulting sparse file with a Header.Size of 25 is: |
|
| 234 |
+// var sparseFile = "\x00"*2 + "abcde" + "\x00"*11 + "fgh" + "\x00"*4 |
|
| 235 |
+type ( |
|
| 236 |
+ sparseDatas []sparseEntry |
|
| 237 |
+ sparseHoles []sparseEntry |
|
| 238 |
+) |
|
| 239 |
+ |
|
| 240 |
+// validateSparseEntries reports whether sp is a valid sparse map. |
|
| 241 |
+// It does not matter whether sp represents data fragments or hole fragments. |
|
| 242 |
+func validateSparseEntries(sp []sparseEntry, size int64) bool {
|
|
| 243 |
+ // Validate all sparse entries. These are the same checks as performed by |
|
| 244 |
+ // the BSD tar utility. |
|
| 245 |
+ if size < 0 {
|
|
| 246 |
+ return false |
|
| 247 |
+ } |
|
| 248 |
+ var pre sparseEntry |
|
| 249 |
+ for _, cur := range sp {
|
|
| 250 |
+ switch {
|
|
| 251 |
+ case cur.Offset < 0 || cur.Length < 0: |
|
| 252 |
+ return false // Negative values are never okay |
|
| 253 |
+ case cur.Offset > math.MaxInt64-cur.Length: |
|
| 254 |
+ return false // Integer overflow with large length |
|
| 255 |
+ case cur.endOffset() > size: |
|
| 256 |
+ return false // Region extends beyond the actual size |
|
| 257 |
+ case pre.endOffset() > cur.Offset: |
|
| 258 |
+ return false // Regions cannot overlap and must be in order |
|
| 259 |
+ } |
|
| 260 |
+ pre = cur |
|
| 261 |
+ } |
|
| 262 |
+ return true |
|
| 263 |
+} |
|
| 264 |
+ |
|
| 265 |
+// alignSparseEntries mutates src and returns dst where each fragment's |
|
| 266 |
+// starting offset is aligned up to the nearest block edge, and each |
|
| 267 |
+// ending offset is aligned down to the nearest block edge. |
|
| 268 |
+// |
|
| 269 |
+// Even though the Go tar Reader and the BSD tar utility can handle entries |
|
| 270 |
+// with arbitrary offsets and lengths, the GNU tar utility can only handle |
|
| 271 |
+// offsets and lengths that are multiples of blockSize. |
|
| 272 |
+func alignSparseEntries(src []sparseEntry, size int64) []sparseEntry {
|
|
| 273 |
+ dst := src[:0] |
|
| 274 |
+ for _, s := range src {
|
|
| 275 |
+ pos, end := s.Offset, s.endOffset() |
|
| 276 |
+ pos += blockPadding(+pos) // Round-up to nearest blockSize |
|
| 277 |
+ if end != size {
|
|
| 278 |
+ end -= blockPadding(-end) // Round-down to nearest blockSize |
|
| 279 |
+ } |
|
| 280 |
+ if pos < end {
|
|
| 281 |
+ dst = append(dst, sparseEntry{Offset: pos, Length: end - pos})
|
|
| 282 |
+ } |
|
| 283 |
+ } |
|
| 284 |
+ return dst |
|
| 285 |
+} |
|
| 286 |
+ |
|
| 287 |
+// invertSparseEntries converts a sparse map from one form to the other. |
|
| 288 |
+// If the input is sparseHoles, then it will output sparseDatas and vice-versa. |
|
| 289 |
+// The input must have been already validated. |
|
| 290 |
+// |
|
| 291 |
+// This function mutates src and returns a normalized map where: |
|
| 292 |
+// * adjacent fragments are coalesced together |
|
| 293 |
+// * only the last fragment may be empty |
|
| 294 |
+// * the endOffset of the last fragment is the total size |
|
| 295 |
+func invertSparseEntries(src []sparseEntry, size int64) []sparseEntry {
|
|
| 296 |
+ dst := src[:0] |
|
| 297 |
+ var pre sparseEntry |
|
| 298 |
+ for _, cur := range src {
|
|
| 299 |
+ if cur.Length == 0 {
|
|
| 300 |
+ continue // Skip empty fragments |
|
| 301 |
+ } |
|
| 302 |
+ pre.Length = cur.Offset - pre.Offset |
|
| 303 |
+ if pre.Length > 0 {
|
|
| 304 |
+ dst = append(dst, pre) // Only add non-empty fragments |
|
| 305 |
+ } |
|
| 306 |
+ pre.Offset = cur.endOffset() |
|
| 307 |
+ } |
|
| 308 |
+ pre.Length = size - pre.Offset // Possibly the only empty fragment |
|
| 309 |
+ return append(dst, pre) |
|
| 310 |
+} |
|
| 311 |
+ |
|
| 312 |
+// fileState tracks the number of logical (includes sparse holes) and physical |
|
| 313 |
+// (actual in tar archive) bytes remaining for the current file. |
|
| 314 |
+// |
|
| 315 |
+// Invariant: LogicalRemaining >= PhysicalRemaining |
|
| 316 |
+type fileState interface {
|
|
| 317 |
+ LogicalRemaining() int64 |
|
| 318 |
+ PhysicalRemaining() int64 |
|
| 319 |
+} |
|
| 320 |
+ |
|
| 321 |
+// allowedFormats determines which formats can be used. |
|
| 322 |
+// The value returned is the logical OR of multiple possible formats. |
|
| 323 |
+// If the value is FormatUnknown, then the input Header cannot be encoded |
|
| 324 |
+// and an error is returned explaining why. |
|
| 325 |
+// |
|
| 326 |
+// As a by-product of checking the fields, this function returns paxHdrs, which |
|
| 327 |
+// contain all fields that could not be directly encoded. |
|
| 328 |
+// A value receiver ensures that this method does not mutate the source Header. |
|
| 329 |
+func (h Header) allowedFormats() (format Format, paxHdrs map[string]string, err error) {
|
|
| 330 |
+ format = FormatUSTAR | FormatPAX | FormatGNU |
|
| 331 |
+ paxHdrs = make(map[string]string) |
|
| 332 |
+ |
|
| 333 |
+ var whyNoUSTAR, whyNoPAX, whyNoGNU string |
|
| 334 |
+ var preferPAX bool // Prefer PAX over USTAR |
|
| 335 |
+ verifyString := func(s string, size int, name, paxKey string) {
|
|
| 336 |
+ // NUL-terminator is optional for path and linkpath. |
|
| 337 |
+ // Technically, it is required for uname and gname, |
|
| 338 |
+ // but neither GNU nor BSD tar checks for it. |
|
| 339 |
+ tooLong := len(s) > size |
|
| 340 |
+ allowLongGNU := paxKey == paxPath || paxKey == paxLinkpath |
|
| 341 |
+ if hasNUL(s) || (tooLong && !allowLongGNU) {
|
|
| 342 |
+ whyNoGNU = fmt.Sprintf("GNU cannot encode %s=%q", name, s)
|
|
| 343 |
+ format.mustNotBe(FormatGNU) |
|
| 344 |
+ } |
|
| 345 |
+ if !isASCII(s) || tooLong {
|
|
| 346 |
+ canSplitUSTAR := paxKey == paxPath |
|
| 347 |
+ if _, _, ok := splitUSTARPath(s); !canSplitUSTAR || !ok {
|
|
| 348 |
+ whyNoUSTAR = fmt.Sprintf("USTAR cannot encode %s=%q", name, s)
|
|
| 349 |
+ format.mustNotBe(FormatUSTAR) |
|
| 350 |
+ } |
|
| 351 |
+ if paxKey == paxNone {
|
|
| 352 |
+ whyNoPAX = fmt.Sprintf("PAX cannot encode %s=%q", name, s)
|
|
| 353 |
+ format.mustNotBe(FormatPAX) |
|
| 354 |
+ } else {
|
|
| 355 |
+ paxHdrs[paxKey] = s |
|
| 356 |
+ } |
|
| 357 |
+ } |
|
| 358 |
+ if v, ok := h.PAXRecords[paxKey]; ok && v == s {
|
|
| 359 |
+ paxHdrs[paxKey] = v |
|
| 360 |
+ } |
|
| 361 |
+ } |
|
| 362 |
+ verifyNumeric := func(n int64, size int, name, paxKey string) {
|
|
| 363 |
+ if !fitsInBase256(size, n) {
|
|
| 364 |
+ whyNoGNU = fmt.Sprintf("GNU cannot encode %s=%d", name, n)
|
|
| 365 |
+ format.mustNotBe(FormatGNU) |
|
| 366 |
+ } |
|
| 367 |
+ if !fitsInOctal(size, n) {
|
|
| 368 |
+ whyNoUSTAR = fmt.Sprintf("USTAR cannot encode %s=%d", name, n)
|
|
| 369 |
+ format.mustNotBe(FormatUSTAR) |
|
| 370 |
+ if paxKey == paxNone {
|
|
| 371 |
+ whyNoPAX = fmt.Sprintf("PAX cannot encode %s=%d", name, n)
|
|
| 372 |
+ format.mustNotBe(FormatPAX) |
|
| 373 |
+ } else {
|
|
| 374 |
+ paxHdrs[paxKey] = strconv.FormatInt(n, 10) |
|
| 375 |
+ } |
|
| 376 |
+ } |
|
| 377 |
+ if v, ok := h.PAXRecords[paxKey]; ok && v == strconv.FormatInt(n, 10) {
|
|
| 378 |
+ paxHdrs[paxKey] = v |
|
| 379 |
+ } |
|
| 380 |
+ } |
|
| 381 |
+ verifyTime := func(ts time.Time, size int, name, paxKey string) {
|
|
| 382 |
+ if ts.IsZero() {
|
|
| 383 |
+ return // Always okay |
|
| 384 |
+ } |
|
| 385 |
+ if !fitsInBase256(size, ts.Unix()) {
|
|
| 386 |
+ whyNoGNU = fmt.Sprintf("GNU cannot encode %s=%v", name, ts)
|
|
| 387 |
+ format.mustNotBe(FormatGNU) |
|
| 388 |
+ } |
|
| 389 |
+ isMtime := paxKey == paxMtime |
|
| 390 |
+ fitsOctal := fitsInOctal(size, ts.Unix()) |
|
| 391 |
+ if (isMtime && !fitsOctal) || !isMtime {
|
|
| 392 |
+ whyNoUSTAR = fmt.Sprintf("USTAR cannot encode %s=%v", name, ts)
|
|
| 393 |
+ format.mustNotBe(FormatUSTAR) |
|
| 394 |
+ } |
|
| 395 |
+ needsNano := ts.Nanosecond() != 0 |
|
| 396 |
+ if !isMtime || !fitsOctal || needsNano {
|
|
| 397 |
+ preferPAX = true // USTAR may truncate sub-second measurements |
|
| 398 |
+ if paxKey == paxNone {
|
|
| 399 |
+ whyNoPAX = fmt.Sprintf("PAX cannot encode %s=%v", name, ts)
|
|
| 400 |
+ format.mustNotBe(FormatPAX) |
|
| 401 |
+ } else {
|
|
| 402 |
+ paxHdrs[paxKey] = formatPAXTime(ts) |
|
| 403 |
+ } |
|
| 404 |
+ } |
|
| 405 |
+ if v, ok := h.PAXRecords[paxKey]; ok && v == formatPAXTime(ts) {
|
|
| 406 |
+ paxHdrs[paxKey] = v |
|
| 407 |
+ } |
|
| 408 |
+ } |
|
| 409 |
+ |
|
| 410 |
+ // Check basic fields. |
|
| 411 |
+ var blk block |
|
| 412 |
+ v7 := blk.V7() |
|
| 413 |
+ ustar := blk.USTAR() |
|
| 414 |
+ gnu := blk.GNU() |
|
| 415 |
+ verifyString(h.Name, len(v7.Name()), "Name", paxPath) |
|
| 416 |
+ verifyString(h.Linkname, len(v7.LinkName()), "Linkname", paxLinkpath) |
|
| 417 |
+ verifyString(h.Uname, len(ustar.UserName()), "Uname", paxUname) |
|
| 418 |
+ verifyString(h.Gname, len(ustar.GroupName()), "Gname", paxGname) |
|
| 419 |
+ verifyNumeric(h.Mode, len(v7.Mode()), "Mode", paxNone) |
|
| 420 |
+ verifyNumeric(int64(h.Uid), len(v7.UID()), "Uid", paxUid) |
|
| 421 |
+ verifyNumeric(int64(h.Gid), len(v7.GID()), "Gid", paxGid) |
|
| 422 |
+ verifyNumeric(h.Size, len(v7.Size()), "Size", paxSize) |
|
| 423 |
+ verifyNumeric(h.Devmajor, len(ustar.DevMajor()), "Devmajor", paxNone) |
|
| 424 |
+ verifyNumeric(h.Devminor, len(ustar.DevMinor()), "Devminor", paxNone) |
|
| 425 |
+ verifyTime(h.ModTime, len(v7.ModTime()), "ModTime", paxMtime) |
|
| 426 |
+ verifyTime(h.AccessTime, len(gnu.AccessTime()), "AccessTime", paxAtime) |
|
| 427 |
+ verifyTime(h.ChangeTime, len(gnu.ChangeTime()), "ChangeTime", paxCtime) |
|
| 428 |
+ |
|
| 429 |
+ // Check for header-only types. |
|
| 430 |
+ var whyOnlyPAX, whyOnlyGNU string |
|
| 431 |
+ switch h.Typeflag {
|
|
| 432 |
+ case TypeReg, TypeChar, TypeBlock, TypeFifo, TypeGNUSparse: |
|
| 433 |
+ // Exclude TypeLink and TypeSymlink, since they may reference directories. |
|
| 434 |
+ if strings.HasSuffix(h.Name, "/") {
|
|
| 435 |
+ return FormatUnknown, nil, headerError{"filename may not have trailing slash"}
|
|
| 436 |
+ } |
|
| 437 |
+ case TypeXHeader, TypeGNULongName, TypeGNULongLink: |
|
| 438 |
+ return FormatUnknown, nil, headerError{"cannot manually encode TypeXHeader, TypeGNULongName, or TypeGNULongLink headers"}
|
|
| 439 |
+ case TypeXGlobalHeader: |
|
| 440 |
+ h2 := Header{Name: h.Name, Typeflag: h.Typeflag, Xattrs: h.Xattrs, PAXRecords: h.PAXRecords, Format: h.Format}
|
|
| 441 |
+ if !reflect.DeepEqual(h, h2) {
|
|
| 442 |
+ return FormatUnknown, nil, headerError{"only PAXRecords should be set for TypeXGlobalHeader"}
|
|
| 443 |
+ } |
|
| 444 |
+ whyOnlyPAX = "only PAX supports TypeXGlobalHeader" |
|
| 445 |
+ format.mayOnlyBe(FormatPAX) |
|
| 446 |
+ } |
|
| 447 |
+ if !isHeaderOnlyType(h.Typeflag) && h.Size < 0 {
|
|
| 448 |
+ return FormatUnknown, nil, headerError{"negative size on header-only type"}
|
|
| 449 |
+ } |
|
| 450 |
+ |
|
| 451 |
+ // Check PAX records. |
|
| 452 |
+ if len(h.Xattrs) > 0 {
|
|
| 453 |
+ for k, v := range h.Xattrs {
|
|
| 454 |
+ paxHdrs[paxSchilyXattr+k] = v |
|
| 455 |
+ } |
|
| 456 |
+ whyOnlyPAX = "only PAX supports Xattrs" |
|
| 457 |
+ format.mayOnlyBe(FormatPAX) |
|
| 458 |
+ } |
|
| 459 |
+ if len(h.PAXRecords) > 0 {
|
|
| 460 |
+ for k, v := range h.PAXRecords {
|
|
| 461 |
+ switch _, exists := paxHdrs[k]; {
|
|
| 462 |
+ case exists: |
|
| 463 |
+ continue // Do not overwrite existing records |
|
| 464 |
+ case h.Typeflag == TypeXGlobalHeader: |
|
| 465 |
+ paxHdrs[k] = v // Copy all records |
|
| 466 |
+ case !basicKeys[k] && !strings.HasPrefix(k, paxGNUSparse): |
|
| 467 |
+ paxHdrs[k] = v // Ignore local records that may conflict |
|
| 468 |
+ } |
|
| 469 |
+ } |
|
| 470 |
+ whyOnlyPAX = "only PAX supports PAXRecords" |
|
| 471 |
+ format.mayOnlyBe(FormatPAX) |
|
| 472 |
+ } |
|
| 473 |
+ for k, v := range paxHdrs {
|
|
| 474 |
+ if !validPAXRecord(k, v) {
|
|
| 475 |
+ return FormatUnknown, nil, headerError{fmt.Sprintf("invalid PAX record: %q", k+" = "+v)}
|
|
| 476 |
+ } |
|
| 477 |
+ } |
|
| 478 |
+ |
|
| 479 |
+ // TODO(dsnet): Re-enable this when adding sparse support. |
|
| 480 |
+ // See https://golang.org/issue/22735 |
|
| 481 |
+ /* |
|
| 482 |
+ // Check sparse files. |
|
| 483 |
+ if len(h.SparseHoles) > 0 || h.Typeflag == TypeGNUSparse {
|
|
| 484 |
+ if isHeaderOnlyType(h.Typeflag) {
|
|
| 485 |
+ return FormatUnknown, nil, headerError{"header-only type cannot be sparse"}
|
|
| 486 |
+ } |
|
| 487 |
+ if !validateSparseEntries(h.SparseHoles, h.Size) {
|
|
| 488 |
+ return FormatUnknown, nil, headerError{"invalid sparse holes"}
|
|
| 489 |
+ } |
|
| 490 |
+ if h.Typeflag == TypeGNUSparse {
|
|
| 491 |
+ whyOnlyGNU = "only GNU supports TypeGNUSparse" |
|
| 492 |
+ format.mayOnlyBe(FormatGNU) |
|
| 493 |
+ } else {
|
|
| 494 |
+ whyNoGNU = "GNU supports sparse files only with TypeGNUSparse" |
|
| 495 |
+ format.mustNotBe(FormatGNU) |
|
| 496 |
+ } |
|
| 497 |
+ whyNoUSTAR = "USTAR does not support sparse files" |
|
| 498 |
+ format.mustNotBe(FormatUSTAR) |
|
| 499 |
+ } |
|
| 500 |
+ */ |
|
| 501 |
+ |
|
| 502 |
+ // Check desired format. |
|
| 503 |
+ if wantFormat := h.Format; wantFormat != FormatUnknown {
|
|
| 504 |
+ if wantFormat.has(FormatPAX) && !preferPAX {
|
|
| 505 |
+ wantFormat.mayBe(FormatUSTAR) // PAX implies USTAR allowed too |
|
| 506 |
+ } |
|
| 507 |
+ format.mayOnlyBe(wantFormat) // Set union of formats allowed and format wanted |
|
| 508 |
+ } |
|
| 509 |
+ if format == FormatUnknown {
|
|
| 510 |
+ switch h.Format {
|
|
| 511 |
+ case FormatUSTAR: |
|
| 512 |
+ err = headerError{"Format specifies USTAR", whyNoUSTAR, whyOnlyPAX, whyOnlyGNU}
|
|
| 513 |
+ case FormatPAX: |
|
| 514 |
+ err = headerError{"Format specifies PAX", whyNoPAX, whyOnlyGNU}
|
|
| 515 |
+ case FormatGNU: |
|
| 516 |
+ err = headerError{"Format specifies GNU", whyNoGNU, whyOnlyPAX}
|
|
| 517 |
+ default: |
|
| 518 |
+ err = headerError{whyNoUSTAR, whyNoPAX, whyNoGNU, whyOnlyPAX, whyOnlyGNU}
|
|
| 519 |
+ } |
|
| 520 |
+ } |
|
| 521 |
+ return format, paxHdrs, err |
|
| 522 |
+} |
|
| 523 |
+ |
|
| 524 |
+// FileInfo returns an os.FileInfo for the Header. |
|
| 525 |
+func (h *Header) FileInfo() os.FileInfo {
|
|
| 526 |
+ return headerFileInfo{h}
|
|
| 527 |
+} |
|
| 528 |
+ |
|
| 529 |
+// headerFileInfo implements os.FileInfo. |
|
| 530 |
+type headerFileInfo struct {
|
|
| 531 |
+ h *Header |
|
| 532 |
+} |
|
| 533 |
+ |
|
| 534 |
+func (fi headerFileInfo) Size() int64 { return fi.h.Size }
|
|
| 535 |
+func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
|
|
| 536 |
+func (fi headerFileInfo) ModTime() time.Time { return fi.h.ModTime }
|
|
| 537 |
+func (fi headerFileInfo) Sys() interface{} { return fi.h }
|
|
| 538 |
+ |
|
| 539 |
+// Name returns the base name of the file. |
|
| 540 |
+func (fi headerFileInfo) Name() string {
|
|
| 541 |
+ if fi.IsDir() {
|
|
| 542 |
+ return path.Base(path.Clean(fi.h.Name)) |
|
| 543 |
+ } |
|
| 544 |
+ return path.Base(fi.h.Name) |
|
| 545 |
+} |
|
| 546 |
+ |
|
| 547 |
+// Mode returns the permission and mode bits for the headerFileInfo. |
|
| 548 |
+func (fi headerFileInfo) Mode() (mode os.FileMode) {
|
|
| 549 |
+ // Set file permission bits. |
|
| 550 |
+ mode = os.FileMode(fi.h.Mode).Perm() |
|
| 551 |
+ |
|
| 552 |
+ // Set setuid, setgid and sticky bits. |
|
| 553 |
+ if fi.h.Mode&c_ISUID != 0 {
|
|
| 554 |
+ mode |= os.ModeSetuid |
|
| 555 |
+ } |
|
| 556 |
+ if fi.h.Mode&c_ISGID != 0 {
|
|
| 557 |
+ mode |= os.ModeSetgid |
|
| 558 |
+ } |
|
| 559 |
+ if fi.h.Mode&c_ISVTX != 0 {
|
|
| 560 |
+ mode |= os.ModeSticky |
|
| 561 |
+ } |
|
| 562 |
+ |
|
| 563 |
+ // Set file mode bits; clear perm, setuid, setgid, and sticky bits. |
|
| 564 |
+ switch m := os.FileMode(fi.h.Mode) &^ 07777; m {
|
|
| 565 |
+ case c_ISDIR: |
|
| 566 |
+ mode |= os.ModeDir |
|
| 567 |
+ case c_ISFIFO: |
|
| 568 |
+ mode |= os.ModeNamedPipe |
|
| 569 |
+ case c_ISLNK: |
|
| 570 |
+ mode |= os.ModeSymlink |
|
| 571 |
+ case c_ISBLK: |
|
| 572 |
+ mode |= os.ModeDevice |
|
| 573 |
+ case c_ISCHR: |
|
| 574 |
+ mode |= os.ModeDevice |
|
| 575 |
+ mode |= os.ModeCharDevice |
|
| 576 |
+ case c_ISSOCK: |
|
| 577 |
+ mode |= os.ModeSocket |
|
| 578 |
+ } |
|
| 579 |
+ |
|
| 580 |
+ switch fi.h.Typeflag {
|
|
| 581 |
+ case TypeSymlink: |
|
| 582 |
+ mode |= os.ModeSymlink |
|
| 583 |
+ case TypeChar: |
|
| 584 |
+ mode |= os.ModeDevice |
|
| 585 |
+ mode |= os.ModeCharDevice |
|
| 586 |
+ case TypeBlock: |
|
| 587 |
+ mode |= os.ModeDevice |
|
| 588 |
+ case TypeDir: |
|
| 589 |
+ mode |= os.ModeDir |
|
| 590 |
+ case TypeFifo: |
|
| 591 |
+ mode |= os.ModeNamedPipe |
|
| 592 |
+ } |
|
| 593 |
+ |
|
| 594 |
+ return mode |
|
| 595 |
+} |
|
| 596 |
+ |
|
| 597 |
+// sysStat, if non-nil, populates h from system-dependent fields of fi. |
|
| 598 |
+var sysStat func(fi os.FileInfo, h *Header) error |
|
| 599 |
+ |
|
| 600 |
+const ( |
|
| 601 |
+ // Mode constants from the USTAR spec: |
|
| 602 |
+ // See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06 |
|
| 603 |
+ c_ISUID = 04000 // Set uid |
|
| 604 |
+ c_ISGID = 02000 // Set gid |
|
| 605 |
+ c_ISVTX = 01000 // Save text (sticky bit) |
|
| 606 |
+ |
|
| 607 |
+ // Common Unix mode constants; these are not defined in any common tar standard. |
|
| 608 |
+ // Header.FileInfo understands these, but FileInfoHeader will never produce these. |
|
| 609 |
+ c_ISDIR = 040000 // Directory |
|
| 610 |
+ c_ISFIFO = 010000 // FIFO |
|
| 611 |
+ c_ISREG = 0100000 // Regular file |
|
| 612 |
+ c_ISLNK = 0120000 // Symbolic link |
|
| 613 |
+ c_ISBLK = 060000 // Block special file |
|
| 614 |
+ c_ISCHR = 020000 // Character special file |
|
| 615 |
+ c_ISSOCK = 0140000 // Socket |
|
| 616 |
+) |
|
| 617 |
+ |
|
| 618 |
+// FileInfoHeader creates a partially-populated Header from fi. |
|
| 619 |
+// If fi describes a symlink, FileInfoHeader records link as the link target. |
|
| 620 |
+// If fi describes a directory, a slash is appended to the name. |
|
| 621 |
+// |
|
| 622 |
+// Since os.FileInfo's Name method only returns the base name of |
|
| 623 |
+// the file it describes, it may be necessary to modify Header.Name |
|
| 624 |
+// to provide the full path name of the file. |
|
| 625 |
+func FileInfoHeader(fi os.FileInfo, link string) (*Header, error) {
|
|
| 626 |
+ if fi == nil {
|
|
| 627 |
+ return nil, errors.New("archive/tar: FileInfo is nil")
|
|
| 628 |
+ } |
|
| 629 |
+ fm := fi.Mode() |
|
| 630 |
+ h := &Header{
|
|
| 631 |
+ Name: fi.Name(), |
|
| 632 |
+ ModTime: fi.ModTime(), |
|
| 633 |
+ Mode: int64(fm.Perm()), // or'd with c_IS* constants later |
|
| 634 |
+ } |
|
| 635 |
+ switch {
|
|
| 636 |
+ case fm.IsRegular(): |
|
| 637 |
+ h.Typeflag = TypeReg |
|
| 638 |
+ h.Size = fi.Size() |
|
| 639 |
+ case fi.IsDir(): |
|
| 640 |
+ h.Typeflag = TypeDir |
|
| 641 |
+ h.Name += "/" |
|
| 642 |
+ case fm&os.ModeSymlink != 0: |
|
| 643 |
+ h.Typeflag = TypeSymlink |
|
| 644 |
+ h.Linkname = link |
|
| 645 |
+ case fm&os.ModeDevice != 0: |
|
| 646 |
+ if fm&os.ModeCharDevice != 0 {
|
|
| 647 |
+ h.Typeflag = TypeChar |
|
| 648 |
+ } else {
|
|
| 649 |
+ h.Typeflag = TypeBlock |
|
| 650 |
+ } |
|
| 651 |
+ case fm&os.ModeNamedPipe != 0: |
|
| 652 |
+ h.Typeflag = TypeFifo |
|
| 653 |
+ case fm&os.ModeSocket != 0: |
|
| 654 |
+ return nil, fmt.Errorf("archive/tar: sockets not supported")
|
|
| 655 |
+ default: |
|
| 656 |
+ return nil, fmt.Errorf("archive/tar: unknown file mode %v", fm)
|
|
| 657 |
+ } |
|
| 658 |
+ if fm&os.ModeSetuid != 0 {
|
|
| 659 |
+ h.Mode |= c_ISUID |
|
| 660 |
+ } |
|
| 661 |
+ if fm&os.ModeSetgid != 0 {
|
|
| 662 |
+ h.Mode |= c_ISGID |
|
| 663 |
+ } |
|
| 664 |
+ if fm&os.ModeSticky != 0 {
|
|
| 665 |
+ h.Mode |= c_ISVTX |
|
| 666 |
+ } |
|
| 667 |
+ // If possible, populate additional fields from OS-specific |
|
| 668 |
+ // FileInfo fields. |
|
| 669 |
+ if sys, ok := fi.Sys().(*Header); ok {
|
|
| 670 |
+ // This FileInfo came from a Header (not the OS). Use the |
|
| 671 |
+ // original Header to populate all remaining fields. |
|
| 672 |
+ h.Uid = sys.Uid |
|
| 673 |
+ h.Gid = sys.Gid |
|
| 674 |
+ h.Uname = sys.Uname |
|
| 675 |
+ h.Gname = sys.Gname |
|
| 676 |
+ h.AccessTime = sys.AccessTime |
|
| 677 |
+ h.ChangeTime = sys.ChangeTime |
|
| 678 |
+ if sys.Xattrs != nil {
|
|
| 679 |
+ h.Xattrs = make(map[string]string) |
|
| 680 |
+ for k, v := range sys.Xattrs {
|
|
| 681 |
+ h.Xattrs[k] = v |
|
| 682 |
+ } |
|
| 683 |
+ } |
|
| 684 |
+ if sys.Typeflag == TypeLink {
|
|
| 685 |
+ // hard link |
|
| 686 |
+ h.Typeflag = TypeLink |
|
| 687 |
+ h.Size = 0 |
|
| 688 |
+ h.Linkname = sys.Linkname |
|
| 689 |
+ } |
|
| 690 |
+ if sys.PAXRecords != nil {
|
|
| 691 |
+ h.PAXRecords = make(map[string]string) |
|
| 692 |
+ for k, v := range sys.PAXRecords {
|
|
| 693 |
+ h.PAXRecords[k] = v |
|
| 694 |
+ } |
|
| 695 |
+ } |
|
| 696 |
+ } |
|
| 697 |
+ if sysStat != nil {
|
|
| 698 |
+ return h, sysStat(fi, h) |
|
| 699 |
+ } |
|
| 700 |
+ return h, nil |
|
| 701 |
+} |
|
| 702 |
+ |
|
| 703 |
+// isHeaderOnlyType checks if the given type flag is of the type that has no |
|
| 704 |
+// data section even if a size is specified. |
|
| 705 |
+func isHeaderOnlyType(flag byte) bool {
|
|
| 706 |
+ switch flag {
|
|
| 707 |
+ case TypeLink, TypeSymlink, TypeChar, TypeBlock, TypeDir, TypeFifo: |
|
| 708 |
+ return true |
|
| 709 |
+ default: |
|
| 710 |
+ return false |
|
| 711 |
+ } |
|
| 712 |
+} |
|
| 713 |
+ |
|
| 714 |
+func min(a, b int64) int64 {
|
|
| 715 |
+ if a < b {
|
|
| 716 |
+ return a |
|
| 717 |
+ } |
|
| 718 |
+ return b |
|
| 719 |
+} |
| 0 | 720 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,303 @@ |
| 0 |
+// Copyright 2016 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package tar |
|
| 5 |
+ |
|
| 6 |
+import "strings" |
|
| 7 |
+ |
|
| 8 |
+// Format represents the tar archive format. |
|
| 9 |
+// |
|
| 10 |
+// The original tar format was introduced in Unix V7. |
|
| 11 |
+// Since then, there have been multiple competing formats attempting to |
|
| 12 |
+// standardize or extend the V7 format to overcome its limitations. |
|
| 13 |
+// The most common formats are the USTAR, PAX, and GNU formats, |
|
| 14 |
+// each with their own advantages and limitations. |
|
| 15 |
+// |
|
| 16 |
+// The following table captures the capabilities of each format: |
|
| 17 |
+// |
|
| 18 |
+// | USTAR | PAX | GNU |
|
| 19 |
+// ------------------+--------+-----------+---------- |
|
| 20 |
+// Name | 256B | unlimited | unlimited |
|
| 21 |
+// Linkname | 100B | unlimited | unlimited |
|
| 22 |
+// Size | uint33 | unlimited | uint89 |
|
| 23 |
+// Mode | uint21 | uint21 | uint57 |
|
| 24 |
+// Uid/Gid | uint21 | unlimited | uint57 |
|
| 25 |
+// Uname/Gname | 32B | unlimited | 32B |
|
| 26 |
+// ModTime | uint33 | unlimited | int89 |
|
| 27 |
+// AccessTime | n/a | unlimited | int89 |
|
| 28 |
+// ChangeTime | n/a | unlimited | int89 |
|
| 29 |
+// Devmajor/Devminor | uint21 | uint21 | uint57 |
|
| 30 |
+// ------------------+--------+-----------+---------- |
|
| 31 |
+// string encoding | ASCII | UTF-8 | binary |
|
| 32 |
+// sub-second times | no | yes | no |
|
| 33 |
+// sparse files | no | yes | yes |
|
| 34 |
+// |
|
| 35 |
+// The table's upper portion shows the Header fields, where each format reports |
|
| 36 |
+// the maximum number of bytes allowed for each string field and |
|
| 37 |
+// the integer type used to store each numeric field |
|
| 38 |
+// (where timestamps are stored as the number of seconds since the Unix epoch). |
|
| 39 |
+// |
|
| 40 |
+// The table's lower portion shows specialized features of each format, |
|
| 41 |
+// such as supported string encodings, support for sub-second timestamps, |
|
| 42 |
+// or support for sparse files. |
|
| 43 |
+// |
|
| 44 |
+// The Writer currently provides no support for sparse files. |
|
| 45 |
+type Format int |
|
| 46 |
+ |
|
| 47 |
+// Constants to identify various tar formats. |
|
| 48 |
+const ( |
|
| 49 |
+ // Deliberately hide the meaning of constants from public API. |
|
| 50 |
+ _ Format = (1 << iota) / 4 // Sequence of 0, 0, 1, 2, 4, 8, etc... |
|
| 51 |
+ |
|
| 52 |
+ // FormatUnknown indicates that the format is unknown. |
|
| 53 |
+ FormatUnknown |
|
| 54 |
+ |
|
| 55 |
+ // The format of the original Unix V7 tar tool prior to standardization. |
|
| 56 |
+ formatV7 |
|
| 57 |
+ |
|
| 58 |
+ // FormatUSTAR represents the USTAR header format defined in POSIX.1-1988. |
|
| 59 |
+ // |
|
| 60 |
+ // While this format is compatible with most tar readers, |
|
| 61 |
+ // the format has several limitations making it unsuitable for some usages. |
|
| 62 |
+ // Most notably, it cannot support sparse files, files larger than 8GiB, |
|
| 63 |
+ // filenames larger than 256 characters, and non-ASCII filenames. |
|
| 64 |
+ // |
|
| 65 |
+ // Reference: |
|
| 66 |
+ // http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06 |
|
| 67 |
+ FormatUSTAR |
|
| 68 |
+ |
|
| 69 |
+ // FormatPAX represents the PAX header format defined in POSIX.1-2001. |
|
| 70 |
+ // |
|
| 71 |
+ // PAX extends USTAR by writing a special file with Typeflag TypeXHeader |
|
| 72 |
+ // preceding the original header. This file contains a set of key-value |
|
| 73 |
+ // records, which are used to overcome USTAR's shortcomings, in addition to |
|
| 74 |
+ // providing the ability to have sub-second resolution for timestamps. |
|
| 75 |
+ // |
|
| 76 |
+ // Some newer formats add their own extensions to PAX by defining their |
|
| 77 |
+ // own keys and assigning certain semantic meaning to the associated values. |
|
| 78 |
+ // For example, sparse file support in PAX is implemented using keys |
|
| 79 |
+ // defined by the GNU manual (e.g., "GNU.sparse.map"). |
|
| 80 |
+ // |
|
| 81 |
+ // Reference: |
|
| 82 |
+ // http://pubs.opengroup.org/onlinepubs/009695399/utilities/pax.html |
|
| 83 |
+ FormatPAX |
|
| 84 |
+ |
|
| 85 |
+ // FormatGNU represents the GNU header format. |
|
| 86 |
+ // |
|
| 87 |
+ // The GNU header format is older than the USTAR and PAX standards and |
|
| 88 |
+ // is not compatible with them. The GNU format supports |
|
| 89 |
+ // arbitrary file sizes, filenames of arbitrary encoding and length, |
|
| 90 |
+ // sparse files, and other features. |
|
| 91 |
+ // |
|
| 92 |
+ // It is recommended that PAX be chosen over GNU unless the target |
|
| 93 |
+ // application can only parse GNU formatted archives. |
|
| 94 |
+ // |
|
| 95 |
+ // Reference: |
|
| 96 |
+ // http://www.gnu.org/software/tar/manual/html_node/Standard.html |
|
| 97 |
+ FormatGNU |
|
| 98 |
+ |
|
| 99 |
+ // Schily's tar format, which is incompatible with USTAR. |
|
| 100 |
+ // This does not cover STAR extensions to the PAX format; these fall under |
|
| 101 |
+ // the PAX format. |
|
| 102 |
+ formatSTAR |
|
| 103 |
+ |
|
| 104 |
+ formatMax |
|
| 105 |
+) |
|
| 106 |
+ |
|
| 107 |
+func (f Format) has(f2 Format) bool { return f&f2 != 0 }
|
|
| 108 |
+func (f *Format) mayBe(f2 Format) { *f |= f2 }
|
|
| 109 |
+func (f *Format) mayOnlyBe(f2 Format) { *f &= f2 }
|
|
| 110 |
+func (f *Format) mustNotBe(f2 Format) { *f &^= f2 }
|
|
| 111 |
+ |
|
| 112 |
+var formatNames = map[Format]string{
|
|
| 113 |
+ formatV7: "V7", FormatUSTAR: "USTAR", FormatPAX: "PAX", FormatGNU: "GNU", formatSTAR: "STAR", |
|
| 114 |
+} |
|
| 115 |
+ |
|
| 116 |
+func (f Format) String() string {
|
|
| 117 |
+ var ss []string |
|
| 118 |
+ for f2 := Format(1); f2 < formatMax; f2 <<= 1 {
|
|
| 119 |
+ if f.has(f2) {
|
|
| 120 |
+ ss = append(ss, formatNames[f2]) |
|
| 121 |
+ } |
|
| 122 |
+ } |
|
| 123 |
+ switch len(ss) {
|
|
| 124 |
+ case 0: |
|
| 125 |
+ return "<unknown>" |
|
| 126 |
+ case 1: |
|
| 127 |
+ return ss[0] |
|
| 128 |
+ default: |
|
| 129 |
+ return "(" + strings.Join(ss, " | ") + ")"
|
|
| 130 |
+ } |
|
| 131 |
+} |
|
| 132 |
+ |
|
| 133 |
+// Magics used to identify various formats. |
|
| 134 |
+const ( |
|
| 135 |
+ magicGNU, versionGNU = "ustar ", " \x00" |
|
| 136 |
+ magicUSTAR, versionUSTAR = "ustar\x00", "00" |
|
| 137 |
+ trailerSTAR = "tar\x00" |
|
| 138 |
+) |
|
| 139 |
+ |
|
| 140 |
+// Size constants from various tar specifications. |
|
| 141 |
+const ( |
|
| 142 |
+ blockSize = 512 // Size of each block in a tar stream |
|
| 143 |
+ nameSize = 100 // Max length of the name field in USTAR format |
|
| 144 |
+ prefixSize = 155 // Max length of the prefix field in USTAR format |
|
| 145 |
+) |
|
| 146 |
+ |
|
| 147 |
+// blockPadding computes the number of bytes needed to pad offset up to the |
|
| 148 |
+// nearest block edge where 0 <= n < blockSize. |
|
| 149 |
+func blockPadding(offset int64) (n int64) {
|
|
| 150 |
+ return -offset & (blockSize - 1) |
|
| 151 |
+} |
|
| 152 |
+ |
|
| 153 |
+var zeroBlock block |
|
| 154 |
+ |
|
| 155 |
+type block [blockSize]byte |
|
| 156 |
+ |
|
| 157 |
+// Convert block to any number of formats. |
|
| 158 |
+func (b *block) V7() *headerV7 { return (*headerV7)(b) }
|
|
| 159 |
+func (b *block) GNU() *headerGNU { return (*headerGNU)(b) }
|
|
| 160 |
+func (b *block) STAR() *headerSTAR { return (*headerSTAR)(b) }
|
|
| 161 |
+func (b *block) USTAR() *headerUSTAR { return (*headerUSTAR)(b) }
|
|
| 162 |
+func (b *block) Sparse() sparseArray { return (sparseArray)(b[:]) }
|
|
| 163 |
+ |
|
| 164 |
+// GetFormat checks that the block is a valid tar header based on the checksum. |
|
| 165 |
+// It then attempts to guess the specific format based on magic values. |
|
| 166 |
+// If the checksum fails, then FormatUnknown is returned. |
|
| 167 |
+func (b *block) GetFormat() Format {
|
|
| 168 |
+ // Verify checksum. |
|
| 169 |
+ var p parser |
|
| 170 |
+ value := p.parseOctal(b.V7().Chksum()) |
|
| 171 |
+ chksum1, chksum2 := b.ComputeChecksum() |
|
| 172 |
+ if p.err != nil || (value != chksum1 && value != chksum2) {
|
|
| 173 |
+ return FormatUnknown |
|
| 174 |
+ } |
|
| 175 |
+ |
|
| 176 |
+ // Guess the magic values. |
|
| 177 |
+ magic := string(b.USTAR().Magic()) |
|
| 178 |
+ version := string(b.USTAR().Version()) |
|
| 179 |
+ trailer := string(b.STAR().Trailer()) |
|
| 180 |
+ switch {
|
|
| 181 |
+ case magic == magicUSTAR && trailer == trailerSTAR: |
|
| 182 |
+ return formatSTAR |
|
| 183 |
+ case magic == magicUSTAR: |
|
| 184 |
+ return FormatUSTAR | FormatPAX |
|
| 185 |
+ case magic == magicGNU && version == versionGNU: |
|
| 186 |
+ return FormatGNU |
|
| 187 |
+ default: |
|
| 188 |
+ return formatV7 |
|
| 189 |
+ } |
|
| 190 |
+} |
|
| 191 |
+ |
|
| 192 |
+// SetFormat writes the magic values necessary for specified format |
|
| 193 |
+// and then updates the checksum accordingly. |
|
| 194 |
+func (b *block) SetFormat(format Format) {
|
|
| 195 |
+ // Set the magic values. |
|
| 196 |
+ switch {
|
|
| 197 |
+ case format.has(formatV7): |
|
| 198 |
+ // Do nothing. |
|
| 199 |
+ case format.has(FormatGNU): |
|
| 200 |
+ copy(b.GNU().Magic(), magicGNU) |
|
| 201 |
+ copy(b.GNU().Version(), versionGNU) |
|
| 202 |
+ case format.has(formatSTAR): |
|
| 203 |
+ copy(b.STAR().Magic(), magicUSTAR) |
|
| 204 |
+ copy(b.STAR().Version(), versionUSTAR) |
|
| 205 |
+ copy(b.STAR().Trailer(), trailerSTAR) |
|
| 206 |
+ case format.has(FormatUSTAR | FormatPAX): |
|
| 207 |
+ copy(b.USTAR().Magic(), magicUSTAR) |
|
| 208 |
+ copy(b.USTAR().Version(), versionUSTAR) |
|
| 209 |
+ default: |
|
| 210 |
+ panic("invalid format")
|
|
| 211 |
+ } |
|
| 212 |
+ |
|
| 213 |
+ // Update checksum. |
|
| 214 |
+ // This field is special in that it is terminated by a NULL then space. |
|
| 215 |
+ var f formatter |
|
| 216 |
+ field := b.V7().Chksum() |
|
| 217 |
+ chksum, _ := b.ComputeChecksum() // Possible values are 256..128776 |
|
| 218 |
+ f.formatOctal(field[:7], chksum) // Never fails since 128776 < 262143 |
|
| 219 |
+ field[7] = ' ' |
|
| 220 |
+} |
|
| 221 |
+ |
|
| 222 |
+// ComputeChecksum computes the checksum for the header block. |
|
| 223 |
+// POSIX specifies a sum of the unsigned byte values, but the Sun tar used |
|
| 224 |
+// signed byte values. |
|
| 225 |
+// We compute and return both. |
|
| 226 |
+func (b *block) ComputeChecksum() (unsigned, signed int64) {
|
|
| 227 |
+ for i, c := range b {
|
|
| 228 |
+ if 148 <= i && i < 156 {
|
|
| 229 |
+ c = ' ' // Treat the checksum field itself as all spaces. |
|
| 230 |
+ } |
|
| 231 |
+ unsigned += int64(c) |
|
| 232 |
+ signed += int64(int8(c)) |
|
| 233 |
+ } |
|
| 234 |
+ return unsigned, signed |
|
| 235 |
+} |
|
| 236 |
+ |
|
| 237 |
+// Reset clears the block with all zeros. |
|
| 238 |
+func (b *block) Reset() {
|
|
| 239 |
+ *b = block{}
|
|
| 240 |
+} |
|
| 241 |
+ |
|
| 242 |
+type headerV7 [blockSize]byte |
|
| 243 |
+ |
|
| 244 |
+func (h *headerV7) Name() []byte { return h[000:][:100] }
|
|
| 245 |
+func (h *headerV7) Mode() []byte { return h[100:][:8] }
|
|
| 246 |
+func (h *headerV7) UID() []byte { return h[108:][:8] }
|
|
| 247 |
+func (h *headerV7) GID() []byte { return h[116:][:8] }
|
|
| 248 |
+func (h *headerV7) Size() []byte { return h[124:][:12] }
|
|
| 249 |
+func (h *headerV7) ModTime() []byte { return h[136:][:12] }
|
|
| 250 |
+func (h *headerV7) Chksum() []byte { return h[148:][:8] }
|
|
| 251 |
+func (h *headerV7) TypeFlag() []byte { return h[156:][:1] }
|
|
| 252 |
+func (h *headerV7) LinkName() []byte { return h[157:][:100] }
|
|
| 253 |
+ |
|
| 254 |
+type headerGNU [blockSize]byte |
|
| 255 |
+ |
|
| 256 |
+func (h *headerGNU) V7() *headerV7 { return (*headerV7)(h) }
|
|
| 257 |
+func (h *headerGNU) Magic() []byte { return h[257:][:6] }
|
|
| 258 |
+func (h *headerGNU) Version() []byte { return h[263:][:2] }
|
|
| 259 |
+func (h *headerGNU) UserName() []byte { return h[265:][:32] }
|
|
| 260 |
+func (h *headerGNU) GroupName() []byte { return h[297:][:32] }
|
|
| 261 |
+func (h *headerGNU) DevMajor() []byte { return h[329:][:8] }
|
|
| 262 |
+func (h *headerGNU) DevMinor() []byte { return h[337:][:8] }
|
|
| 263 |
+func (h *headerGNU) AccessTime() []byte { return h[345:][:12] }
|
|
| 264 |
+func (h *headerGNU) ChangeTime() []byte { return h[357:][:12] }
|
|
| 265 |
+func (h *headerGNU) Sparse() sparseArray { return (sparseArray)(h[386:][:24*4+1]) }
|
|
| 266 |
+func (h *headerGNU) RealSize() []byte { return h[483:][:12] }
|
|
| 267 |
+ |
|
| 268 |
+type headerSTAR [blockSize]byte |
|
| 269 |
+ |
|
| 270 |
+func (h *headerSTAR) V7() *headerV7 { return (*headerV7)(h) }
|
|
| 271 |
+func (h *headerSTAR) Magic() []byte { return h[257:][:6] }
|
|
| 272 |
+func (h *headerSTAR) Version() []byte { return h[263:][:2] }
|
|
| 273 |
+func (h *headerSTAR) UserName() []byte { return h[265:][:32] }
|
|
| 274 |
+func (h *headerSTAR) GroupName() []byte { return h[297:][:32] }
|
|
| 275 |
+func (h *headerSTAR) DevMajor() []byte { return h[329:][:8] }
|
|
| 276 |
+func (h *headerSTAR) DevMinor() []byte { return h[337:][:8] }
|
|
| 277 |
+func (h *headerSTAR) Prefix() []byte { return h[345:][:131] }
|
|
| 278 |
+func (h *headerSTAR) AccessTime() []byte { return h[476:][:12] }
|
|
| 279 |
+func (h *headerSTAR) ChangeTime() []byte { return h[488:][:12] }
|
|
| 280 |
+func (h *headerSTAR) Trailer() []byte { return h[508:][:4] }
|
|
| 281 |
+ |
|
| 282 |
+type headerUSTAR [blockSize]byte |
|
| 283 |
+ |
|
| 284 |
+func (h *headerUSTAR) V7() *headerV7 { return (*headerV7)(h) }
|
|
| 285 |
+func (h *headerUSTAR) Magic() []byte { return h[257:][:6] }
|
|
| 286 |
+func (h *headerUSTAR) Version() []byte { return h[263:][:2] }
|
|
| 287 |
+func (h *headerUSTAR) UserName() []byte { return h[265:][:32] }
|
|
| 288 |
+func (h *headerUSTAR) GroupName() []byte { return h[297:][:32] }
|
|
| 289 |
+func (h *headerUSTAR) DevMajor() []byte { return h[329:][:8] }
|
|
| 290 |
+func (h *headerUSTAR) DevMinor() []byte { return h[337:][:8] }
|
|
| 291 |
+func (h *headerUSTAR) Prefix() []byte { return h[345:][:155] }
|
|
| 292 |
+ |
|
| 293 |
+type sparseArray []byte |
|
| 294 |
+ |
|
| 295 |
+func (s sparseArray) Entry(i int) sparseElem { return (sparseElem)(s[i*24:]) }
|
|
| 296 |
+func (s sparseArray) IsExtended() []byte { return s[24*s.MaxEntries():][:1] }
|
|
| 297 |
+func (s sparseArray) MaxEntries() int { return len(s) / 24 }
|
|
| 298 |
+ |
|
| 299 |
+type sparseElem []byte |
|
| 300 |
+ |
|
| 301 |
+func (s sparseElem) Offset() []byte { return s[00:][:12] }
|
|
| 302 |
+func (s sparseElem) Length() []byte { return s[12:][:12] }
|
| 0 | 303 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,855 @@ |
| 0 |
+// Copyright 2009 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package tar |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "bytes" |
|
| 8 |
+ "io" |
|
| 9 |
+ "io/ioutil" |
|
| 10 |
+ "strconv" |
|
| 11 |
+ "strings" |
|
| 12 |
+ "time" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+// Reader provides sequential access to the contents of a tar archive. |
|
| 16 |
+// Reader.Next advances to the next file in the archive (including the first), |
|
| 17 |
+// and then Reader can be treated as an io.Reader to access the file's data. |
|
| 18 |
+type Reader struct {
|
|
| 19 |
+ r io.Reader |
|
| 20 |
+ pad int64 // Amount of padding (ignored) after current file entry |
|
| 21 |
+ curr fileReader // Reader for current file entry |
|
| 22 |
+ blk block // Buffer to use as temporary local storage |
|
| 23 |
+ |
|
| 24 |
+ // err is a persistent error. |
|
| 25 |
+ // It is only the responsibility of every exported method of Reader to |
|
| 26 |
+ // ensure that this error is sticky. |
|
| 27 |
+ err error |
|
| 28 |
+} |
|
| 29 |
+ |
|
| 30 |
+type fileReader interface {
|
|
| 31 |
+ io.Reader |
|
| 32 |
+ fileState |
|
| 33 |
+ |
|
| 34 |
+ WriteTo(io.Writer) (int64, error) |
|
| 35 |
+} |
|
| 36 |
+ |
|
| 37 |
+// NewReader creates a new Reader reading from r. |
|
| 38 |
+func NewReader(r io.Reader) *Reader {
|
|
| 39 |
+ return &Reader{r: r, curr: ®FileReader{r, 0}}
|
|
| 40 |
+} |
|
| 41 |
+ |
|
| 42 |
+// Next advances to the next entry in the tar archive. |
|
| 43 |
+// The Header.Size determines how many bytes can be read for the next file. |
|
| 44 |
+// Any remaining data in the current file is automatically discarded. |
|
| 45 |
+// |
|
| 46 |
+// io.EOF is returned at the end of the input. |
|
| 47 |
+func (tr *Reader) Next() (*Header, error) {
|
|
| 48 |
+ if tr.err != nil {
|
|
| 49 |
+ return nil, tr.err |
|
| 50 |
+ } |
|
| 51 |
+ hdr, err := tr.next() |
|
| 52 |
+ tr.err = err |
|
| 53 |
+ return hdr, err |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 56 |
+func (tr *Reader) next() (*Header, error) {
|
|
| 57 |
+ var paxHdrs map[string]string |
|
| 58 |
+ var gnuLongName, gnuLongLink string |
|
| 59 |
+ |
|
| 60 |
+ // Externally, Next iterates through the tar archive as if it is a series of |
|
| 61 |
+ // files. Internally, the tar format often uses fake "files" to add meta |
|
| 62 |
+ // data that describes the next file. These meta data "files" should not |
|
| 63 |
+ // normally be visible to the outside. As such, this loop iterates through |
|
| 64 |
+ // one or more "header files" until it finds a "normal file". |
|
| 65 |
+ format := FormatUSTAR | FormatPAX | FormatGNU |
|
| 66 |
+loop: |
|
| 67 |
+ for {
|
|
| 68 |
+ // Discard the remainder of the file and any padding. |
|
| 69 |
+ if err := discard(tr.r, tr.curr.PhysicalRemaining()); err != nil {
|
|
| 70 |
+ return nil, err |
|
| 71 |
+ } |
|
| 72 |
+ if _, err := tryReadFull(tr.r, tr.blk[:tr.pad]); err != nil {
|
|
| 73 |
+ return nil, err |
|
| 74 |
+ } |
|
| 75 |
+ tr.pad = 0 |
|
| 76 |
+ |
|
| 77 |
+ hdr, rawHdr, err := tr.readHeader() |
|
| 78 |
+ if err != nil {
|
|
| 79 |
+ return nil, err |
|
| 80 |
+ } |
|
| 81 |
+ if err := tr.handleRegularFile(hdr); err != nil {
|
|
| 82 |
+ return nil, err |
|
| 83 |
+ } |
|
| 84 |
+ format.mayOnlyBe(hdr.Format) |
|
| 85 |
+ |
|
| 86 |
+ // Check for PAX/GNU special headers and files. |
|
| 87 |
+ switch hdr.Typeflag {
|
|
| 88 |
+ case TypeXHeader, TypeXGlobalHeader: |
|
| 89 |
+ format.mayOnlyBe(FormatPAX) |
|
| 90 |
+ paxHdrs, err = parsePAX(tr) |
|
| 91 |
+ if err != nil {
|
|
| 92 |
+ return nil, err |
|
| 93 |
+ } |
|
| 94 |
+ if hdr.Typeflag == TypeXGlobalHeader {
|
|
| 95 |
+ mergePAX(hdr, paxHdrs) |
|
| 96 |
+ return &Header{
|
|
| 97 |
+ Name: hdr.Name, |
|
| 98 |
+ Typeflag: hdr.Typeflag, |
|
| 99 |
+ Xattrs: hdr.Xattrs, |
|
| 100 |
+ PAXRecords: hdr.PAXRecords, |
|
| 101 |
+ Format: format, |
|
| 102 |
+ }, nil |
|
| 103 |
+ } |
|
| 104 |
+ continue loop // This is a meta header affecting the next header |
|
| 105 |
+ case TypeGNULongName, TypeGNULongLink: |
|
| 106 |
+ format.mayOnlyBe(FormatGNU) |
|
| 107 |
+ realname, err := ioutil.ReadAll(tr) |
|
| 108 |
+ if err != nil {
|
|
| 109 |
+ return nil, err |
|
| 110 |
+ } |
|
| 111 |
+ |
|
| 112 |
+ var p parser |
|
| 113 |
+ switch hdr.Typeflag {
|
|
| 114 |
+ case TypeGNULongName: |
|
| 115 |
+ gnuLongName = p.parseString(realname) |
|
| 116 |
+ case TypeGNULongLink: |
|
| 117 |
+ gnuLongLink = p.parseString(realname) |
|
| 118 |
+ } |
|
| 119 |
+ continue loop // This is a meta header affecting the next header |
|
| 120 |
+ default: |
|
| 121 |
+ // The old GNU sparse format is handled here since it is technically |
|
| 122 |
+ // just a regular file with additional attributes. |
|
| 123 |
+ |
|
| 124 |
+ if err := mergePAX(hdr, paxHdrs); err != nil {
|
|
| 125 |
+ return nil, err |
|
| 126 |
+ } |
|
| 127 |
+ if gnuLongName != "" {
|
|
| 128 |
+ hdr.Name = gnuLongName |
|
| 129 |
+ } |
|
| 130 |
+ if gnuLongLink != "" {
|
|
| 131 |
+ hdr.Linkname = gnuLongLink |
|
| 132 |
+ } |
|
| 133 |
+ if hdr.Typeflag == TypeRegA && strings.HasSuffix(hdr.Name, "/") {
|
|
| 134 |
+ hdr.Typeflag = TypeDir // Legacy archives use trailing slash for directories |
|
| 135 |
+ } |
|
| 136 |
+ |
|
| 137 |
+ // The extended headers may have updated the size. |
|
| 138 |
+ // Thus, setup the regFileReader again after merging PAX headers. |
|
| 139 |
+ if err := tr.handleRegularFile(hdr); err != nil {
|
|
| 140 |
+ return nil, err |
|
| 141 |
+ } |
|
| 142 |
+ |
|
| 143 |
+ // Sparse formats rely on being able to read from the logical data |
|
| 144 |
+ // section; there must be a preceding call to handleRegularFile. |
|
| 145 |
+ if err := tr.handleSparseFile(hdr, rawHdr); err != nil {
|
|
| 146 |
+ return nil, err |
|
| 147 |
+ } |
|
| 148 |
+ |
|
| 149 |
+ // Set the final guess at the format. |
|
| 150 |
+ if format.has(FormatUSTAR) && format.has(FormatPAX) {
|
|
| 151 |
+ format.mayOnlyBe(FormatUSTAR) |
|
| 152 |
+ } |
|
| 153 |
+ hdr.Format = format |
|
| 154 |
+ return hdr, nil // This is a file, so stop |
|
| 155 |
+ } |
|
| 156 |
+ } |
|
| 157 |
+} |
|
| 158 |
+ |
|
| 159 |
+// handleRegularFile sets up the current file reader and padding such that it |
|
| 160 |
+// can only read the following logical data section. It will properly handle |
|
| 161 |
+// special headers that contain no data section. |
|
| 162 |
+func (tr *Reader) handleRegularFile(hdr *Header) error {
|
|
| 163 |
+ nb := hdr.Size |
|
| 164 |
+ if isHeaderOnlyType(hdr.Typeflag) {
|
|
| 165 |
+ nb = 0 |
|
| 166 |
+ } |
|
| 167 |
+ if nb < 0 {
|
|
| 168 |
+ return ErrHeader |
|
| 169 |
+ } |
|
| 170 |
+ |
|
| 171 |
+ tr.pad = blockPadding(nb) |
|
| 172 |
+ tr.curr = ®FileReader{r: tr.r, nb: nb}
|
|
| 173 |
+ return nil |
|
| 174 |
+} |
|
| 175 |
+ |
|
| 176 |
+// handleSparseFile checks if the current file is a sparse format of any type |
|
| 177 |
+// and sets the curr reader appropriately. |
|
| 178 |
+func (tr *Reader) handleSparseFile(hdr *Header, rawHdr *block) error {
|
|
| 179 |
+ var spd sparseDatas |
|
| 180 |
+ var err error |
|
| 181 |
+ if hdr.Typeflag == TypeGNUSparse {
|
|
| 182 |
+ spd, err = tr.readOldGNUSparseMap(hdr, rawHdr) |
|
| 183 |
+ } else {
|
|
| 184 |
+ spd, err = tr.readGNUSparsePAXHeaders(hdr) |
|
| 185 |
+ } |
|
| 186 |
+ |
|
| 187 |
+ // If sp is non-nil, then this is a sparse file. |
|
| 188 |
+ // Note that it is possible for len(sp) == 0. |
|
| 189 |
+ if err == nil && spd != nil {
|
|
| 190 |
+ if isHeaderOnlyType(hdr.Typeflag) || !validateSparseEntries(spd, hdr.Size) {
|
|
| 191 |
+ return ErrHeader |
|
| 192 |
+ } |
|
| 193 |
+ sph := invertSparseEntries(spd, hdr.Size) |
|
| 194 |
+ tr.curr = &sparseFileReader{tr.curr, sph, 0}
|
|
| 195 |
+ } |
|
| 196 |
+ return err |
|
| 197 |
+} |
|
| 198 |
+ |
|
| 199 |
+// readGNUSparsePAXHeaders checks the PAX headers for GNU sparse headers. |
|
| 200 |
+// If they are found, then this function reads the sparse map and returns it. |
|
| 201 |
+// This assumes that 0.0 headers have already been converted to 0.1 headers |
|
| 202 |
+// by the the PAX header parsing logic. |
|
| 203 |
+func (tr *Reader) readGNUSparsePAXHeaders(hdr *Header) (sparseDatas, error) {
|
|
| 204 |
+ // Identify the version of GNU headers. |
|
| 205 |
+ var is1x0 bool |
|
| 206 |
+ major, minor := hdr.PAXRecords[paxGNUSparseMajor], hdr.PAXRecords[paxGNUSparseMinor] |
|
| 207 |
+ switch {
|
|
| 208 |
+ case major == "0" && (minor == "0" || minor == "1"): |
|
| 209 |
+ is1x0 = false |
|
| 210 |
+ case major == "1" && minor == "0": |
|
| 211 |
+ is1x0 = true |
|
| 212 |
+ case major != "" || minor != "": |
|
| 213 |
+ return nil, nil // Unknown GNU sparse PAX version |
|
| 214 |
+ case hdr.PAXRecords[paxGNUSparseMap] != "": |
|
| 215 |
+ is1x0 = false // 0.0 and 0.1 did not have explicit version records, so guess |
|
| 216 |
+ default: |
|
| 217 |
+ return nil, nil // Not a PAX format GNU sparse file. |
|
| 218 |
+ } |
|
| 219 |
+ hdr.Format.mayOnlyBe(FormatPAX) |
|
| 220 |
+ |
|
| 221 |
+ // Update hdr from GNU sparse PAX headers. |
|
| 222 |
+ if name := hdr.PAXRecords[paxGNUSparseName]; name != "" {
|
|
| 223 |
+ hdr.Name = name |
|
| 224 |
+ } |
|
| 225 |
+ size := hdr.PAXRecords[paxGNUSparseSize] |
|
| 226 |
+ if size == "" {
|
|
| 227 |
+ size = hdr.PAXRecords[paxGNUSparseRealSize] |
|
| 228 |
+ } |
|
| 229 |
+ if size != "" {
|
|
| 230 |
+ n, err := strconv.ParseInt(size, 10, 64) |
|
| 231 |
+ if err != nil {
|
|
| 232 |
+ return nil, ErrHeader |
|
| 233 |
+ } |
|
| 234 |
+ hdr.Size = n |
|
| 235 |
+ } |
|
| 236 |
+ |
|
| 237 |
+ // Read the sparse map according to the appropriate format. |
|
| 238 |
+ if is1x0 {
|
|
| 239 |
+ return readGNUSparseMap1x0(tr.curr) |
|
| 240 |
+ } |
|
| 241 |
+ return readGNUSparseMap0x1(hdr.PAXRecords) |
|
| 242 |
+} |
|
| 243 |
+ |
|
| 244 |
+// mergePAX merges paxHdrs into hdr for all relevant fields of Header. |
|
| 245 |
+func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {
|
|
| 246 |
+ for k, v := range paxHdrs {
|
|
| 247 |
+ if v == "" {
|
|
| 248 |
+ continue // Keep the original USTAR value |
|
| 249 |
+ } |
|
| 250 |
+ var id64 int64 |
|
| 251 |
+ switch k {
|
|
| 252 |
+ case paxPath: |
|
| 253 |
+ hdr.Name = v |
|
| 254 |
+ case paxLinkpath: |
|
| 255 |
+ hdr.Linkname = v |
|
| 256 |
+ case paxUname: |
|
| 257 |
+ hdr.Uname = v |
|
| 258 |
+ case paxGname: |
|
| 259 |
+ hdr.Gname = v |
|
| 260 |
+ case paxUid: |
|
| 261 |
+ id64, err = strconv.ParseInt(v, 10, 64) |
|
| 262 |
+ hdr.Uid = int(id64) // Integer overflow possible |
|
| 263 |
+ case paxGid: |
|
| 264 |
+ id64, err = strconv.ParseInt(v, 10, 64) |
|
| 265 |
+ hdr.Gid = int(id64) // Integer overflow possible |
|
| 266 |
+ case paxAtime: |
|
| 267 |
+ hdr.AccessTime, err = parsePAXTime(v) |
|
| 268 |
+ case paxMtime: |
|
| 269 |
+ hdr.ModTime, err = parsePAXTime(v) |
|
| 270 |
+ case paxCtime: |
|
| 271 |
+ hdr.ChangeTime, err = parsePAXTime(v) |
|
| 272 |
+ case paxSize: |
|
| 273 |
+ hdr.Size, err = strconv.ParseInt(v, 10, 64) |
|
| 274 |
+ default: |
|
| 275 |
+ if strings.HasPrefix(k, paxSchilyXattr) {
|
|
| 276 |
+ if hdr.Xattrs == nil {
|
|
| 277 |
+ hdr.Xattrs = make(map[string]string) |
|
| 278 |
+ } |
|
| 279 |
+ hdr.Xattrs[k[len(paxSchilyXattr):]] = v |
|
| 280 |
+ } |
|
| 281 |
+ } |
|
| 282 |
+ if err != nil {
|
|
| 283 |
+ return ErrHeader |
|
| 284 |
+ } |
|
| 285 |
+ } |
|
| 286 |
+ hdr.PAXRecords = paxHdrs |
|
| 287 |
+ return nil |
|
| 288 |
+} |
|
| 289 |
+ |
|
| 290 |
+// parsePAX parses PAX headers. |
|
| 291 |
+// If an extended header (type 'x') is invalid, ErrHeader is returned |
|
| 292 |
+func parsePAX(r io.Reader) (map[string]string, error) {
|
|
| 293 |
+ buf, err := ioutil.ReadAll(r) |
|
| 294 |
+ if err != nil {
|
|
| 295 |
+ return nil, err |
|
| 296 |
+ } |
|
| 297 |
+ sbuf := string(buf) |
|
| 298 |
+ |
|
| 299 |
+ // For GNU PAX sparse format 0.0 support. |
|
| 300 |
+ // This function transforms the sparse format 0.0 headers into format 0.1 |
|
| 301 |
+ // headers since 0.0 headers were not PAX compliant. |
|
| 302 |
+ var sparseMap []string |
|
| 303 |
+ |
|
| 304 |
+ paxHdrs := make(map[string]string) |
|
| 305 |
+ for len(sbuf) > 0 {
|
|
| 306 |
+ key, value, residual, err := parsePAXRecord(sbuf) |
|
| 307 |
+ if err != nil {
|
|
| 308 |
+ return nil, ErrHeader |
|
| 309 |
+ } |
|
| 310 |
+ sbuf = residual |
|
| 311 |
+ |
|
| 312 |
+ switch key {
|
|
| 313 |
+ case paxGNUSparseOffset, paxGNUSparseNumBytes: |
|
| 314 |
+ // Validate sparse header order and value. |
|
| 315 |
+ if (len(sparseMap)%2 == 0 && key != paxGNUSparseOffset) || |
|
| 316 |
+ (len(sparseMap)%2 == 1 && key != paxGNUSparseNumBytes) || |
|
| 317 |
+ strings.Contains(value, ",") {
|
|
| 318 |
+ return nil, ErrHeader |
|
| 319 |
+ } |
|
| 320 |
+ sparseMap = append(sparseMap, value) |
|
| 321 |
+ default: |
|
| 322 |
+ paxHdrs[key] = value |
|
| 323 |
+ } |
|
| 324 |
+ } |
|
| 325 |
+ if len(sparseMap) > 0 {
|
|
| 326 |
+ paxHdrs[paxGNUSparseMap] = strings.Join(sparseMap, ",") |
|
| 327 |
+ } |
|
| 328 |
+ return paxHdrs, nil |
|
| 329 |
+} |
|
| 330 |
+ |
|
| 331 |
+// readHeader reads the next block header and assumes that the underlying reader |
|
| 332 |
+// is already aligned to a block boundary. It returns the raw block of the |
|
| 333 |
+// header in case further processing is required. |
|
| 334 |
+// |
|
| 335 |
+// The err will be set to io.EOF only when one of the following occurs: |
|
| 336 |
+// * Exactly 0 bytes are read and EOF is hit. |
|
| 337 |
+// * Exactly 1 block of zeros is read and EOF is hit. |
|
| 338 |
+// * At least 2 blocks of zeros are read. |
|
| 339 |
+func (tr *Reader) readHeader() (*Header, *block, error) {
|
|
| 340 |
+ // Two blocks of zero bytes marks the end of the archive. |
|
| 341 |
+ if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
|
|
| 342 |
+ return nil, nil, err // EOF is okay here; exactly 0 bytes read |
|
| 343 |
+ } |
|
| 344 |
+ if bytes.Equal(tr.blk[:], zeroBlock[:]) {
|
|
| 345 |
+ if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
|
|
| 346 |
+ return nil, nil, err // EOF is okay here; exactly 1 block of zeros read |
|
| 347 |
+ } |
|
| 348 |
+ if bytes.Equal(tr.blk[:], zeroBlock[:]) {
|
|
| 349 |
+ return nil, nil, io.EOF // normal EOF; exactly 2 block of zeros read |
|
| 350 |
+ } |
|
| 351 |
+ return nil, nil, ErrHeader // Zero block and then non-zero block |
|
| 352 |
+ } |
|
| 353 |
+ |
|
| 354 |
+ // Verify the header matches a known format. |
|
| 355 |
+ format := tr.blk.GetFormat() |
|
| 356 |
+ if format == FormatUnknown {
|
|
| 357 |
+ return nil, nil, ErrHeader |
|
| 358 |
+ } |
|
| 359 |
+ |
|
| 360 |
+ var p parser |
|
| 361 |
+ hdr := new(Header) |
|
| 362 |
+ |
|
| 363 |
+ // Unpack the V7 header. |
|
| 364 |
+ v7 := tr.blk.V7() |
|
| 365 |
+ hdr.Typeflag = v7.TypeFlag()[0] |
|
| 366 |
+ hdr.Name = p.parseString(v7.Name()) |
|
| 367 |
+ hdr.Linkname = p.parseString(v7.LinkName()) |
|
| 368 |
+ hdr.Size = p.parseNumeric(v7.Size()) |
|
| 369 |
+ hdr.Mode = p.parseNumeric(v7.Mode()) |
|
| 370 |
+ hdr.Uid = int(p.parseNumeric(v7.UID())) |
|
| 371 |
+ hdr.Gid = int(p.parseNumeric(v7.GID())) |
|
| 372 |
+ hdr.ModTime = time.Unix(p.parseNumeric(v7.ModTime()), 0) |
|
| 373 |
+ |
|
| 374 |
+ // Unpack format specific fields. |
|
| 375 |
+ if format > formatV7 {
|
|
| 376 |
+ ustar := tr.blk.USTAR() |
|
| 377 |
+ hdr.Uname = p.parseString(ustar.UserName()) |
|
| 378 |
+ hdr.Gname = p.parseString(ustar.GroupName()) |
|
| 379 |
+ hdr.Devmajor = p.parseNumeric(ustar.DevMajor()) |
|
| 380 |
+ hdr.Devminor = p.parseNumeric(ustar.DevMinor()) |
|
| 381 |
+ |
|
| 382 |
+ var prefix string |
|
| 383 |
+ switch {
|
|
| 384 |
+ case format.has(FormatUSTAR | FormatPAX): |
|
| 385 |
+ hdr.Format = format |
|
| 386 |
+ ustar := tr.blk.USTAR() |
|
| 387 |
+ prefix = p.parseString(ustar.Prefix()) |
|
| 388 |
+ |
|
| 389 |
+ // For Format detection, check if block is properly formatted since |
|
| 390 |
+ // the parser is more liberal than what USTAR actually permits. |
|
| 391 |
+ notASCII := func(r rune) bool { return r >= 0x80 }
|
|
| 392 |
+ if bytes.IndexFunc(tr.blk[:], notASCII) >= 0 {
|
|
| 393 |
+ hdr.Format = FormatUnknown // Non-ASCII characters in block. |
|
| 394 |
+ } |
|
| 395 |
+ nul := func(b []byte) bool { return int(b[len(b)-1]) == 0 }
|
|
| 396 |
+ if !(nul(v7.Size()) && nul(v7.Mode()) && nul(v7.UID()) && nul(v7.GID()) && |
|
| 397 |
+ nul(v7.ModTime()) && nul(ustar.DevMajor()) && nul(ustar.DevMinor())) {
|
|
| 398 |
+ hdr.Format = FormatUnknown // Numeric fields must end in NUL |
|
| 399 |
+ } |
|
| 400 |
+ case format.has(formatSTAR): |
|
| 401 |
+ star := tr.blk.STAR() |
|
| 402 |
+ prefix = p.parseString(star.Prefix()) |
|
| 403 |
+ hdr.AccessTime = time.Unix(p.parseNumeric(star.AccessTime()), 0) |
|
| 404 |
+ hdr.ChangeTime = time.Unix(p.parseNumeric(star.ChangeTime()), 0) |
|
| 405 |
+ case format.has(FormatGNU): |
|
| 406 |
+ hdr.Format = format |
|
| 407 |
+ var p2 parser |
|
| 408 |
+ gnu := tr.blk.GNU() |
|
| 409 |
+ if b := gnu.AccessTime(); b[0] != 0 {
|
|
| 410 |
+ hdr.AccessTime = time.Unix(p2.parseNumeric(b), 0) |
|
| 411 |
+ } |
|
| 412 |
+ if b := gnu.ChangeTime(); b[0] != 0 {
|
|
| 413 |
+ hdr.ChangeTime = time.Unix(p2.parseNumeric(b), 0) |
|
| 414 |
+ } |
|
| 415 |
+ |
|
| 416 |
+ // Prior to Go1.8, the Writer had a bug where it would output |
|
| 417 |
+ // an invalid tar file in certain rare situations because the logic |
|
| 418 |
+ // incorrectly believed that the old GNU format had a prefix field. |
|
| 419 |
+ // This is wrong and leads to an output file that mangles the |
|
| 420 |
+ // atime and ctime fields, which are often left unused. |
|
| 421 |
+ // |
|
| 422 |
+ // In order to continue reading tar files created by former, buggy |
|
| 423 |
+ // versions of Go, we skeptically parse the atime and ctime fields. |
|
| 424 |
+ // If we are unable to parse them and the prefix field looks like |
|
| 425 |
+ // an ASCII string, then we fallback on the pre-Go1.8 behavior |
|
| 426 |
+ // of treating these fields as the USTAR prefix field. |
|
| 427 |
+ // |
|
| 428 |
+ // Note that this will not use the fallback logic for all possible |
|
| 429 |
+ // files generated by a pre-Go1.8 toolchain. If the generated file |
|
| 430 |
+ // happened to have a prefix field that parses as valid |
|
| 431 |
+ // atime and ctime fields (e.g., when they are valid octal strings), |
|
| 432 |
+ // then it is impossible to distinguish between an valid GNU file |
|
| 433 |
+ // and an invalid pre-Go1.8 file. |
|
| 434 |
+ // |
|
| 435 |
+ // See https://golang.org/issues/12594 |
|
| 436 |
+ // See https://golang.org/issues/21005 |
|
| 437 |
+ if p2.err != nil {
|
|
| 438 |
+ hdr.AccessTime, hdr.ChangeTime = time.Time{}, time.Time{}
|
|
| 439 |
+ ustar := tr.blk.USTAR() |
|
| 440 |
+ if s := p.parseString(ustar.Prefix()); isASCII(s) {
|
|
| 441 |
+ prefix = s |
|
| 442 |
+ } |
|
| 443 |
+ hdr.Format = FormatUnknown // Buggy file is not GNU |
|
| 444 |
+ } |
|
| 445 |
+ } |
|
| 446 |
+ if len(prefix) > 0 {
|
|
| 447 |
+ hdr.Name = prefix + "/" + hdr.Name |
|
| 448 |
+ } |
|
| 449 |
+ } |
|
| 450 |
+ return hdr, &tr.blk, p.err |
|
| 451 |
+} |
|
| 452 |
+ |
|
| 453 |
+// readOldGNUSparseMap reads the sparse map from the old GNU sparse format. |
|
| 454 |
+// The sparse map is stored in the tar header if it's small enough. |
|
| 455 |
+// If it's larger than four entries, then one or more extension headers are used |
|
| 456 |
+// to store the rest of the sparse map. |
|
| 457 |
+// |
|
| 458 |
+// The Header.Size does not reflect the size of any extended headers used. |
|
| 459 |
+// Thus, this function will read from the raw io.Reader to fetch extra headers. |
|
| 460 |
+// This method mutates blk in the process. |
|
| 461 |
+func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, error) {
|
|
| 462 |
+ // Make sure that the input format is GNU. |
|
| 463 |
+ // Unfortunately, the STAR format also has a sparse header format that uses |
|
| 464 |
+ // the same type flag but has a completely different layout. |
|
| 465 |
+ if blk.GetFormat() != FormatGNU {
|
|
| 466 |
+ return nil, ErrHeader |
|
| 467 |
+ } |
|
| 468 |
+ hdr.Format.mayOnlyBe(FormatGNU) |
|
| 469 |
+ |
|
| 470 |
+ var p parser |
|
| 471 |
+ hdr.Size = p.parseNumeric(blk.GNU().RealSize()) |
|
| 472 |
+ if p.err != nil {
|
|
| 473 |
+ return nil, p.err |
|
| 474 |
+ } |
|
| 475 |
+ s := blk.GNU().Sparse() |
|
| 476 |
+ spd := make(sparseDatas, 0, s.MaxEntries()) |
|
| 477 |
+ for {
|
|
| 478 |
+ for i := 0; i < s.MaxEntries(); i++ {
|
|
| 479 |
+ // This termination condition is identical to GNU and BSD tar. |
|
| 480 |
+ if s.Entry(i).Offset()[0] == 0x00 {
|
|
| 481 |
+ break // Don't return, need to process extended headers (even if empty) |
|
| 482 |
+ } |
|
| 483 |
+ offset := p.parseNumeric(s.Entry(i).Offset()) |
|
| 484 |
+ length := p.parseNumeric(s.Entry(i).Length()) |
|
| 485 |
+ if p.err != nil {
|
|
| 486 |
+ return nil, p.err |
|
| 487 |
+ } |
|
| 488 |
+ spd = append(spd, sparseEntry{Offset: offset, Length: length})
|
|
| 489 |
+ } |
|
| 490 |
+ |
|
| 491 |
+ if s.IsExtended()[0] > 0 {
|
|
| 492 |
+ // There are more entries. Read an extension header and parse its entries. |
|
| 493 |
+ if _, err := mustReadFull(tr.r, blk[:]); err != nil {
|
|
| 494 |
+ return nil, err |
|
| 495 |
+ } |
|
| 496 |
+ s = blk.Sparse() |
|
| 497 |
+ continue |
|
| 498 |
+ } |
|
| 499 |
+ return spd, nil // Done |
|
| 500 |
+ } |
|
| 501 |
+} |
|
| 502 |
+ |
|
| 503 |
+// readGNUSparseMap1x0 reads the sparse map as stored in GNU's PAX sparse format |
|
| 504 |
+// version 1.0. The format of the sparse map consists of a series of |
|
| 505 |
+// newline-terminated numeric fields. The first field is the number of entries |
|
| 506 |
+// and is always present. Following this are the entries, consisting of two |
|
| 507 |
+// fields (offset, length). This function must stop reading at the end |
|
| 508 |
+// boundary of the block containing the last newline. |
|
| 509 |
+// |
|
| 510 |
+// Note that the GNU manual says that numeric values should be encoded in octal |
|
| 511 |
+// format. However, the GNU tar utility itself outputs these values in decimal. |
|
| 512 |
+// As such, this library treats values as being encoded in decimal. |
|
| 513 |
+func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
|
|
| 514 |
+ var ( |
|
| 515 |
+ cntNewline int64 |
|
| 516 |
+ buf bytes.Buffer |
|
| 517 |
+ blk block |
|
| 518 |
+ ) |
|
| 519 |
+ |
|
| 520 |
+ // feedTokens copies data in blocks from r into buf until there are |
|
| 521 |
+ // at least cnt newlines in buf. It will not read more blocks than needed. |
|
| 522 |
+ feedTokens := func(n int64) error {
|
|
| 523 |
+ for cntNewline < n {
|
|
| 524 |
+ if _, err := mustReadFull(r, blk[:]); err != nil {
|
|
| 525 |
+ return err |
|
| 526 |
+ } |
|
| 527 |
+ buf.Write(blk[:]) |
|
| 528 |
+ for _, c := range blk {
|
|
| 529 |
+ if c == '\n' {
|
|
| 530 |
+ cntNewline++ |
|
| 531 |
+ } |
|
| 532 |
+ } |
|
| 533 |
+ } |
|
| 534 |
+ return nil |
|
| 535 |
+ } |
|
| 536 |
+ |
|
| 537 |
+ // nextToken gets the next token delimited by a newline. This assumes that |
|
| 538 |
+ // at least one newline exists in the buffer. |
|
| 539 |
+ nextToken := func() string {
|
|
| 540 |
+ cntNewline-- |
|
| 541 |
+ tok, _ := buf.ReadString('\n')
|
|
| 542 |
+ return strings.TrimRight(tok, "\n") |
|
| 543 |
+ } |
|
| 544 |
+ |
|
| 545 |
+ // Parse for the number of entries. |
|
| 546 |
+ // Use integer overflow resistant math to check this. |
|
| 547 |
+ if err := feedTokens(1); err != nil {
|
|
| 548 |
+ return nil, err |
|
| 549 |
+ } |
|
| 550 |
+ numEntries, err := strconv.ParseInt(nextToken(), 10, 0) // Intentionally parse as native int |
|
| 551 |
+ if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
|
|
| 552 |
+ return nil, ErrHeader |
|
| 553 |
+ } |
|
| 554 |
+ |
|
| 555 |
+ // Parse for all member entries. |
|
| 556 |
+ // numEntries is trusted after this since a potential attacker must have |
|
| 557 |
+ // committed resources proportional to what this library used. |
|
| 558 |
+ if err := feedTokens(2 * numEntries); err != nil {
|
|
| 559 |
+ return nil, err |
|
| 560 |
+ } |
|
| 561 |
+ spd := make(sparseDatas, 0, numEntries) |
|
| 562 |
+ for i := int64(0); i < numEntries; i++ {
|
|
| 563 |
+ offset, err1 := strconv.ParseInt(nextToken(), 10, 64) |
|
| 564 |
+ length, err2 := strconv.ParseInt(nextToken(), 10, 64) |
|
| 565 |
+ if err1 != nil || err2 != nil {
|
|
| 566 |
+ return nil, ErrHeader |
|
| 567 |
+ } |
|
| 568 |
+ spd = append(spd, sparseEntry{Offset: offset, Length: length})
|
|
| 569 |
+ } |
|
| 570 |
+ return spd, nil |
|
| 571 |
+} |
|
| 572 |
+ |
|
| 573 |
+// readGNUSparseMap0x1 reads the sparse map as stored in GNU's PAX sparse format |
|
| 574 |
+// version 0.1. The sparse map is stored in the PAX headers. |
|
| 575 |
+func readGNUSparseMap0x1(paxHdrs map[string]string) (sparseDatas, error) {
|
|
| 576 |
+ // Get number of entries. |
|
| 577 |
+ // Use integer overflow resistant math to check this. |
|
| 578 |
+ numEntriesStr := paxHdrs[paxGNUSparseNumBlocks] |
|
| 579 |
+ numEntries, err := strconv.ParseInt(numEntriesStr, 10, 0) // Intentionally parse as native int |
|
| 580 |
+ if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
|
|
| 581 |
+ return nil, ErrHeader |
|
| 582 |
+ } |
|
| 583 |
+ |
|
| 584 |
+ // There should be two numbers in sparseMap for each entry. |
|
| 585 |
+ sparseMap := strings.Split(paxHdrs[paxGNUSparseMap], ",") |
|
| 586 |
+ if len(sparseMap) == 1 && sparseMap[0] == "" {
|
|
| 587 |
+ sparseMap = sparseMap[:0] |
|
| 588 |
+ } |
|
| 589 |
+ if int64(len(sparseMap)) != 2*numEntries {
|
|
| 590 |
+ return nil, ErrHeader |
|
| 591 |
+ } |
|
| 592 |
+ |
|
| 593 |
+ // Loop through the entries in the sparse map. |
|
| 594 |
+ // numEntries is trusted now. |
|
| 595 |
+ spd := make(sparseDatas, 0, numEntries) |
|
| 596 |
+ for len(sparseMap) >= 2 {
|
|
| 597 |
+ offset, err1 := strconv.ParseInt(sparseMap[0], 10, 64) |
|
| 598 |
+ length, err2 := strconv.ParseInt(sparseMap[1], 10, 64) |
|
| 599 |
+ if err1 != nil || err2 != nil {
|
|
| 600 |
+ return nil, ErrHeader |
|
| 601 |
+ } |
|
| 602 |
+ spd = append(spd, sparseEntry{Offset: offset, Length: length})
|
|
| 603 |
+ sparseMap = sparseMap[2:] |
|
| 604 |
+ } |
|
| 605 |
+ return spd, nil |
|
| 606 |
+} |
|
| 607 |
+ |
|
| 608 |
+// Read reads from the current file in the tar archive. |
|
| 609 |
+// It returns (0, io.EOF) when it reaches the end of that file, |
|
| 610 |
+// until Next is called to advance to the next file. |
|
| 611 |
+// |
|
| 612 |
+// If the current file is sparse, then the regions marked as a hole |
|
| 613 |
+// are read back as NUL-bytes. |
|
| 614 |
+// |
|
| 615 |
+// Calling Read on special types like TypeLink, TypeSymlink, TypeChar, |
|
| 616 |
+// TypeBlock, TypeDir, and TypeFifo returns (0, io.EOF) regardless of what |
|
| 617 |
+// the Header.Size claims. |
|
| 618 |
+func (tr *Reader) Read(b []byte) (int, error) {
|
|
| 619 |
+ if tr.err != nil {
|
|
| 620 |
+ return 0, tr.err |
|
| 621 |
+ } |
|
| 622 |
+ n, err := tr.curr.Read(b) |
|
| 623 |
+ if err != nil && err != io.EOF {
|
|
| 624 |
+ tr.err = err |
|
| 625 |
+ } |
|
| 626 |
+ return n, err |
|
| 627 |
+} |
|
| 628 |
+ |
|
| 629 |
+// writeTo writes the content of the current file to w. |
|
| 630 |
+// The bytes written matches the number of remaining bytes in the current file. |
|
| 631 |
+// |
|
| 632 |
+// If the current file is sparse and w is an io.WriteSeeker, |
|
| 633 |
+// then writeTo uses Seek to skip past holes defined in Header.SparseHoles, |
|
| 634 |
+// assuming that skipped regions are filled with NULs. |
|
| 635 |
+// This always writes the last byte to ensure w is the right size. |
|
| 636 |
+// |
|
| 637 |
+// TODO(dsnet): Re-export this when adding sparse file support. |
|
| 638 |
+// See https://golang.org/issue/22735 |
|
| 639 |
+func (tr *Reader) writeTo(w io.Writer) (int64, error) {
|
|
| 640 |
+ if tr.err != nil {
|
|
| 641 |
+ return 0, tr.err |
|
| 642 |
+ } |
|
| 643 |
+ n, err := tr.curr.WriteTo(w) |
|
| 644 |
+ if err != nil {
|
|
| 645 |
+ tr.err = err |
|
| 646 |
+ } |
|
| 647 |
+ return n, err |
|
| 648 |
+} |
|
| 649 |
+ |
|
| 650 |
+// regFileReader is a fileReader for reading data from a regular file entry. |
|
| 651 |
+type regFileReader struct {
|
|
| 652 |
+ r io.Reader // Underlying Reader |
|
| 653 |
+ nb int64 // Number of remaining bytes to read |
|
| 654 |
+} |
|
| 655 |
+ |
|
| 656 |
+func (fr *regFileReader) Read(b []byte) (n int, err error) {
|
|
| 657 |
+ if int64(len(b)) > fr.nb {
|
|
| 658 |
+ b = b[:fr.nb] |
|
| 659 |
+ } |
|
| 660 |
+ if len(b) > 0 {
|
|
| 661 |
+ n, err = fr.r.Read(b) |
|
| 662 |
+ fr.nb -= int64(n) |
|
| 663 |
+ } |
|
| 664 |
+ switch {
|
|
| 665 |
+ case err == io.EOF && fr.nb > 0: |
|
| 666 |
+ return n, io.ErrUnexpectedEOF |
|
| 667 |
+ case err == nil && fr.nb == 0: |
|
| 668 |
+ return n, io.EOF |
|
| 669 |
+ default: |
|
| 670 |
+ return n, err |
|
| 671 |
+ } |
|
| 672 |
+} |
|
| 673 |
+ |
|
| 674 |
+func (fr *regFileReader) WriteTo(w io.Writer) (int64, error) {
|
|
| 675 |
+ return io.Copy(w, struct{ io.Reader }{fr})
|
|
| 676 |
+} |
|
| 677 |
+ |
|
| 678 |
+func (fr regFileReader) LogicalRemaining() int64 {
|
|
| 679 |
+ return fr.nb |
|
| 680 |
+} |
|
| 681 |
+ |
|
| 682 |
+func (fr regFileReader) PhysicalRemaining() int64 {
|
|
| 683 |
+ return fr.nb |
|
| 684 |
+} |
|
| 685 |
+ |
|
| 686 |
+// sparseFileReader is a fileReader for reading data from a sparse file entry. |
|
| 687 |
+type sparseFileReader struct {
|
|
| 688 |
+ fr fileReader // Underlying fileReader |
|
| 689 |
+ sp sparseHoles // Normalized list of sparse holes |
|
| 690 |
+ pos int64 // Current position in sparse file |
|
| 691 |
+} |
|
| 692 |
+ |
|
| 693 |
+func (sr *sparseFileReader) Read(b []byte) (n int, err error) {
|
|
| 694 |
+ finished := int64(len(b)) >= sr.LogicalRemaining() |
|
| 695 |
+ if finished {
|
|
| 696 |
+ b = b[:sr.LogicalRemaining()] |
|
| 697 |
+ } |
|
| 698 |
+ |
|
| 699 |
+ b0 := b |
|
| 700 |
+ endPos := sr.pos + int64(len(b)) |
|
| 701 |
+ for endPos > sr.pos && err == nil {
|
|
| 702 |
+ var nf int // Bytes read in fragment |
|
| 703 |
+ holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset() |
|
| 704 |
+ if sr.pos < holeStart { // In a data fragment
|
|
| 705 |
+ bf := b[:min(int64(len(b)), holeStart-sr.pos)] |
|
| 706 |
+ nf, err = tryReadFull(sr.fr, bf) |
|
| 707 |
+ } else { // In a hole fragment
|
|
| 708 |
+ bf := b[:min(int64(len(b)), holeEnd-sr.pos)] |
|
| 709 |
+ nf, err = tryReadFull(zeroReader{}, bf)
|
|
| 710 |
+ } |
|
| 711 |
+ b = b[nf:] |
|
| 712 |
+ sr.pos += int64(nf) |
|
| 713 |
+ if sr.pos >= holeEnd && len(sr.sp) > 1 {
|
|
| 714 |
+ sr.sp = sr.sp[1:] // Ensure last fragment always remains |
|
| 715 |
+ } |
|
| 716 |
+ } |
|
| 717 |
+ |
|
| 718 |
+ n = len(b0) - len(b) |
|
| 719 |
+ switch {
|
|
| 720 |
+ case err == io.EOF: |
|
| 721 |
+ return n, errMissData // Less data in dense file than sparse file |
|
| 722 |
+ case err != nil: |
|
| 723 |
+ return n, err |
|
| 724 |
+ case sr.LogicalRemaining() == 0 && sr.PhysicalRemaining() > 0: |
|
| 725 |
+ return n, errUnrefData // More data in dense file than sparse file |
|
| 726 |
+ case finished: |
|
| 727 |
+ return n, io.EOF |
|
| 728 |
+ default: |
|
| 729 |
+ return n, nil |
|
| 730 |
+ } |
|
| 731 |
+} |
|
| 732 |
+ |
|
| 733 |
+func (sr *sparseFileReader) WriteTo(w io.Writer) (n int64, err error) {
|
|
| 734 |
+ ws, ok := w.(io.WriteSeeker) |
|
| 735 |
+ if ok {
|
|
| 736 |
+ if _, err := ws.Seek(0, io.SeekCurrent); err != nil {
|
|
| 737 |
+ ok = false // Not all io.Seeker can really seek |
|
| 738 |
+ } |
|
| 739 |
+ } |
|
| 740 |
+ if !ok {
|
|
| 741 |
+ return io.Copy(w, struct{ io.Reader }{sr})
|
|
| 742 |
+ } |
|
| 743 |
+ |
|
| 744 |
+ var writeLastByte bool |
|
| 745 |
+ pos0 := sr.pos |
|
| 746 |
+ for sr.LogicalRemaining() > 0 && !writeLastByte && err == nil {
|
|
| 747 |
+ var nf int64 // Size of fragment |
|
| 748 |
+ holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset() |
|
| 749 |
+ if sr.pos < holeStart { // In a data fragment
|
|
| 750 |
+ nf = holeStart - sr.pos |
|
| 751 |
+ nf, err = io.CopyN(ws, sr.fr, nf) |
|
| 752 |
+ } else { // In a hole fragment
|
|
| 753 |
+ nf = holeEnd - sr.pos |
|
| 754 |
+ if sr.PhysicalRemaining() == 0 {
|
|
| 755 |
+ writeLastByte = true |
|
| 756 |
+ nf-- |
|
| 757 |
+ } |
|
| 758 |
+ _, err = ws.Seek(nf, io.SeekCurrent) |
|
| 759 |
+ } |
|
| 760 |
+ sr.pos += nf |
|
| 761 |
+ if sr.pos >= holeEnd && len(sr.sp) > 1 {
|
|
| 762 |
+ sr.sp = sr.sp[1:] // Ensure last fragment always remains |
|
| 763 |
+ } |
|
| 764 |
+ } |
|
| 765 |
+ |
|
| 766 |
+ // If the last fragment is a hole, then seek to 1-byte before EOF, and |
|
| 767 |
+ // write a single byte to ensure the file is the right size. |
|
| 768 |
+ if writeLastByte && err == nil {
|
|
| 769 |
+ _, err = ws.Write([]byte{0})
|
|
| 770 |
+ sr.pos++ |
|
| 771 |
+ } |
|
| 772 |
+ |
|
| 773 |
+ n = sr.pos - pos0 |
|
| 774 |
+ switch {
|
|
| 775 |
+ case err == io.EOF: |
|
| 776 |
+ return n, errMissData // Less data in dense file than sparse file |
|
| 777 |
+ case err != nil: |
|
| 778 |
+ return n, err |
|
| 779 |
+ case sr.LogicalRemaining() == 0 && sr.PhysicalRemaining() > 0: |
|
| 780 |
+ return n, errUnrefData // More data in dense file than sparse file |
|
| 781 |
+ default: |
|
| 782 |
+ return n, nil |
|
| 783 |
+ } |
|
| 784 |
+} |
|
| 785 |
+ |
|
| 786 |
+func (sr sparseFileReader) LogicalRemaining() int64 {
|
|
| 787 |
+ return sr.sp[len(sr.sp)-1].endOffset() - sr.pos |
|
| 788 |
+} |
|
| 789 |
+func (sr sparseFileReader) PhysicalRemaining() int64 {
|
|
| 790 |
+ return sr.fr.PhysicalRemaining() |
|
| 791 |
+} |
|
| 792 |
+ |
|
| 793 |
+type zeroReader struct{}
|
|
| 794 |
+ |
|
| 795 |
+func (zeroReader) Read(b []byte) (int, error) {
|
|
| 796 |
+ for i := range b {
|
|
| 797 |
+ b[i] = 0 |
|
| 798 |
+ } |
|
| 799 |
+ return len(b), nil |
|
| 800 |
+} |
|
| 801 |
+ |
|
| 802 |
+// mustReadFull is like io.ReadFull except it returns |
|
| 803 |
+// io.ErrUnexpectedEOF when io.EOF is hit before len(b) bytes are read. |
|
| 804 |
+func mustReadFull(r io.Reader, b []byte) (int, error) {
|
|
| 805 |
+ n, err := tryReadFull(r, b) |
|
| 806 |
+ if err == io.EOF {
|
|
| 807 |
+ err = io.ErrUnexpectedEOF |
|
| 808 |
+ } |
|
| 809 |
+ return n, err |
|
| 810 |
+} |
|
| 811 |
+ |
|
| 812 |
+// tryReadFull is like io.ReadFull except it returns |
|
| 813 |
+// io.EOF when it is hit before len(b) bytes are read. |
|
| 814 |
+func tryReadFull(r io.Reader, b []byte) (n int, err error) {
|
|
| 815 |
+ for len(b) > n && err == nil {
|
|
| 816 |
+ var nn int |
|
| 817 |
+ nn, err = r.Read(b[n:]) |
|
| 818 |
+ n += nn |
|
| 819 |
+ } |
|
| 820 |
+ if len(b) == n && err == io.EOF {
|
|
| 821 |
+ err = nil |
|
| 822 |
+ } |
|
| 823 |
+ return n, err |
|
| 824 |
+} |
|
| 825 |
+ |
|
| 826 |
+// discard skips n bytes in r, reporting an error if unable to do so. |
|
| 827 |
+func discard(r io.Reader, n int64) error {
|
|
| 828 |
+ // If possible, Seek to the last byte before the end of the data section. |
|
| 829 |
+ // Do this because Seek is often lazy about reporting errors; this will mask |
|
| 830 |
+ // the fact that the stream may be truncated. We can rely on the |
|
| 831 |
+ // io.CopyN done shortly afterwards to trigger any IO errors. |
|
| 832 |
+ var seekSkipped int64 // Number of bytes skipped via Seek |
|
| 833 |
+ if sr, ok := r.(io.Seeker); ok && n > 1 {
|
|
| 834 |
+ // Not all io.Seeker can actually Seek. For example, os.Stdin implements |
|
| 835 |
+ // io.Seeker, but calling Seek always returns an error and performs |
|
| 836 |
+ // no action. Thus, we try an innocent seek to the current position |
|
| 837 |
+ // to see if Seek is really supported. |
|
| 838 |
+ pos1, err := sr.Seek(0, io.SeekCurrent) |
|
| 839 |
+ if pos1 >= 0 && err == nil {
|
|
| 840 |
+ // Seek seems supported, so perform the real Seek. |
|
| 841 |
+ pos2, err := sr.Seek(n-1, io.SeekCurrent) |
|
| 842 |
+ if pos2 < 0 || err != nil {
|
|
| 843 |
+ return err |
|
| 844 |
+ } |
|
| 845 |
+ seekSkipped = pos2 - pos1 |
|
| 846 |
+ } |
|
| 847 |
+ } |
|
| 848 |
+ |
|
| 849 |
+ copySkipped, err := io.CopyN(ioutil.Discard, r, n-seekSkipped) |
|
| 850 |
+ if err == io.EOF && seekSkipped+copySkipped < n {
|
|
| 851 |
+ err = io.ErrUnexpectedEOF |
|
| 852 |
+ } |
|
| 853 |
+ return err |
|
| 854 |
+} |
| 0 | 855 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,20 @@ |
| 0 |
+// Copyright 2012 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// +build linux dragonfly openbsd solaris |
|
| 5 |
+ |
|
| 6 |
+package tar |
|
| 7 |
+ |
|
| 8 |
+import ( |
|
| 9 |
+ "syscall" |
|
| 10 |
+ "time" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+func statAtime(st *syscall.Stat_t) time.Time {
|
|
| 14 |
+ return time.Unix(st.Atim.Unix()) |
|
| 15 |
+} |
|
| 16 |
+ |
|
| 17 |
+func statCtime(st *syscall.Stat_t) time.Time {
|
|
| 18 |
+ return time.Unix(st.Ctim.Unix()) |
|
| 19 |
+} |
| 0 | 20 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,20 @@ |
| 0 |
+// Copyright 2012 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// +build darwin freebsd netbsd |
|
| 5 |
+ |
|
| 6 |
+package tar |
|
| 7 |
+ |
|
| 8 |
+import ( |
|
| 9 |
+ "syscall" |
|
| 10 |
+ "time" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+func statAtime(st *syscall.Stat_t) time.Time {
|
|
| 14 |
+ return time.Unix(st.Atimespec.Unix()) |
|
| 15 |
+} |
|
| 16 |
+ |
|
| 17 |
+func statCtime(st *syscall.Stat_t) time.Time {
|
|
| 18 |
+ return time.Unix(st.Ctimespec.Unix()) |
|
| 19 |
+} |
| 0 | 20 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,76 @@ |
| 0 |
+// Copyright 2012 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// +build linux darwin dragonfly freebsd openbsd netbsd solaris |
|
| 5 |
+ |
|
| 6 |
+package tar |
|
| 7 |
+ |
|
| 8 |
+import ( |
|
| 9 |
+ "os" |
|
| 10 |
+ "runtime" |
|
| 11 |
+ "syscall" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+func init() {
|
|
| 15 |
+ sysStat = statUnix |
|
| 16 |
+} |
|
| 17 |
+ |
|
| 18 |
+func statUnix(fi os.FileInfo, h *Header) error {
|
|
| 19 |
+ sys, ok := fi.Sys().(*syscall.Stat_t) |
|
| 20 |
+ if !ok {
|
|
| 21 |
+ return nil |
|
| 22 |
+ } |
|
| 23 |
+ h.Uid = int(sys.Uid) |
|
| 24 |
+ h.Gid = int(sys.Gid) |
|
| 25 |
+ |
|
| 26 |
+ // TODO(bradfitz): populate username & group. os/user |
|
| 27 |
+ // doesn't cache LookupId lookups, and lacks group |
|
| 28 |
+ // lookup functions. |
|
| 29 |
+ h.AccessTime = statAtime(sys) |
|
| 30 |
+ h.ChangeTime = statCtime(sys) |
|
| 31 |
+ |
|
| 32 |
+ // Best effort at populating Devmajor and Devminor. |
|
| 33 |
+ if h.Typeflag == TypeChar || h.Typeflag == TypeBlock {
|
|
| 34 |
+ dev := uint64(sys.Rdev) // May be int32 or uint32 |
|
| 35 |
+ switch runtime.GOOS {
|
|
| 36 |
+ case "linux": |
|
| 37 |
+ // Copied from golang.org/x/sys/unix/dev_linux.go. |
|
| 38 |
+ major := uint32((dev & 0x00000000000fff00) >> 8) |
|
| 39 |
+ major |= uint32((dev & 0xfffff00000000000) >> 32) |
|
| 40 |
+ minor := uint32((dev & 0x00000000000000ff) >> 0) |
|
| 41 |
+ minor |= uint32((dev & 0x00000ffffff00000) >> 12) |
|
| 42 |
+ h.Devmajor, h.Devminor = int64(major), int64(minor) |
|
| 43 |
+ case "darwin": |
|
| 44 |
+ // Copied from golang.org/x/sys/unix/dev_darwin.go. |
|
| 45 |
+ major := uint32((dev >> 24) & 0xff) |
|
| 46 |
+ minor := uint32(dev & 0xffffff) |
|
| 47 |
+ h.Devmajor, h.Devminor = int64(major), int64(minor) |
|
| 48 |
+ case "dragonfly": |
|
| 49 |
+ // Copied from golang.org/x/sys/unix/dev_dragonfly.go. |
|
| 50 |
+ major := uint32((dev >> 8) & 0xff) |
|
| 51 |
+ minor := uint32(dev & 0xffff00ff) |
|
| 52 |
+ h.Devmajor, h.Devminor = int64(major), int64(minor) |
|
| 53 |
+ case "freebsd": |
|
| 54 |
+ // Copied from golang.org/x/sys/unix/dev_freebsd.go. |
|
| 55 |
+ major := uint32((dev >> 8) & 0xff) |
|
| 56 |
+ minor := uint32(dev & 0xffff00ff) |
|
| 57 |
+ h.Devmajor, h.Devminor = int64(major), int64(minor) |
|
| 58 |
+ case "netbsd": |
|
| 59 |
+ // Copied from golang.org/x/sys/unix/dev_netbsd.go. |
|
| 60 |
+ major := uint32((dev & 0x000fff00) >> 8) |
|
| 61 |
+ minor := uint32((dev & 0x000000ff) >> 0) |
|
| 62 |
+ minor |= uint32((dev & 0xfff00000) >> 12) |
|
| 63 |
+ h.Devmajor, h.Devminor = int64(major), int64(minor) |
|
| 64 |
+ case "openbsd": |
|
| 65 |
+ // Copied from golang.org/x/sys/unix/dev_openbsd.go. |
|
| 66 |
+ major := uint32((dev & 0x0000ff00) >> 8) |
|
| 67 |
+ minor := uint32((dev & 0x000000ff) >> 0) |
|
| 68 |
+ minor |= uint32((dev & 0xffff0000) >> 8) |
|
| 69 |
+ h.Devmajor, h.Devminor = int64(major), int64(minor) |
|
| 70 |
+ default: |
|
| 71 |
+ // TODO: Implement solaris (see https://golang.org/issue/8106) |
|
| 72 |
+ } |
|
| 73 |
+ } |
|
| 74 |
+ return nil |
|
| 75 |
+} |
| 0 | 76 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,326 @@ |
| 0 |
+// Copyright 2016 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package tar |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "bytes" |
|
| 8 |
+ "fmt" |
|
| 9 |
+ "strconv" |
|
| 10 |
+ "strings" |
|
| 11 |
+ "time" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+// hasNUL reports whether the NUL character exists within s. |
|
| 15 |
+func hasNUL(s string) bool {
|
|
| 16 |
+ return strings.IndexByte(s, 0) >= 0 |
|
| 17 |
+} |
|
| 18 |
+ |
|
| 19 |
+// isASCII reports whether the input is an ASCII C-style string. |
|
| 20 |
+func isASCII(s string) bool {
|
|
| 21 |
+ for _, c := range s {
|
|
| 22 |
+ if c >= 0x80 || c == 0x00 {
|
|
| 23 |
+ return false |
|
| 24 |
+ } |
|
| 25 |
+ } |
|
| 26 |
+ return true |
|
| 27 |
+} |
|
| 28 |
+ |
|
| 29 |
+// toASCII converts the input to an ASCII C-style string. |
|
| 30 |
+// This a best effort conversion, so invalid characters are dropped. |
|
| 31 |
+func toASCII(s string) string {
|
|
| 32 |
+ if isASCII(s) {
|
|
| 33 |
+ return s |
|
| 34 |
+ } |
|
| 35 |
+ b := make([]byte, 0, len(s)) |
|
| 36 |
+ for _, c := range s {
|
|
| 37 |
+ if c < 0x80 && c != 0x00 {
|
|
| 38 |
+ b = append(b, byte(c)) |
|
| 39 |
+ } |
|
| 40 |
+ } |
|
| 41 |
+ return string(b) |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+type parser struct {
|
|
| 45 |
+ err error // Last error seen |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+type formatter struct {
|
|
| 49 |
+ err error // Last error seen |
|
| 50 |
+} |
|
| 51 |
+ |
|
| 52 |
+// parseString parses bytes as a NUL-terminated C-style string. |
|
| 53 |
+// If a NUL byte is not found then the whole slice is returned as a string. |
|
| 54 |
+func (*parser) parseString(b []byte) string {
|
|
| 55 |
+ if i := bytes.IndexByte(b, 0); i >= 0 {
|
|
| 56 |
+ return string(b[:i]) |
|
| 57 |
+ } |
|
| 58 |
+ return string(b) |
|
| 59 |
+} |
|
| 60 |
+ |
|
| 61 |
+// formatString copies s into b, NUL-terminating if possible. |
|
| 62 |
+func (f *formatter) formatString(b []byte, s string) {
|
|
| 63 |
+ if len(s) > len(b) {
|
|
| 64 |
+ f.err = ErrFieldTooLong |
|
| 65 |
+ } |
|
| 66 |
+ copy(b, s) |
|
| 67 |
+ if len(s) < len(b) {
|
|
| 68 |
+ b[len(s)] = 0 |
|
| 69 |
+ } |
|
| 70 |
+ |
|
| 71 |
+ // Some buggy readers treat regular files with a trailing slash |
|
| 72 |
+ // in the V7 path field as a directory even though the full path |
|
| 73 |
+ // recorded elsewhere (e.g., via PAX record) contains no trailing slash. |
|
| 74 |
+ if len(s) > len(b) && b[len(b)-1] == '/' {
|
|
| 75 |
+ n := len(strings.TrimRight(s[:len(b)], "/")) |
|
| 76 |
+ b[n] = 0 // Replace trailing slash with NUL terminator |
|
| 77 |
+ } |
|
| 78 |
+} |
|
| 79 |
+ |
|
| 80 |
+// fitsInBase256 reports whether x can be encoded into n bytes using base-256 |
|
| 81 |
+// encoding. Unlike octal encoding, base-256 encoding does not require that the |
|
| 82 |
+// string ends with a NUL character. Thus, all n bytes are available for output. |
|
| 83 |
+// |
|
| 84 |
+// If operating in binary mode, this assumes strict GNU binary mode; which means |
|
| 85 |
+// that the first byte can only be either 0x80 or 0xff. Thus, the first byte is |
|
| 86 |
+// equivalent to the sign bit in two's complement form. |
|
| 87 |
+func fitsInBase256(n int, x int64) bool {
|
|
| 88 |
+ binBits := uint(n-1) * 8 |
|
| 89 |
+ return n >= 9 || (x >= -1<<binBits && x < 1<<binBits) |
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+// parseNumeric parses the input as being encoded in either base-256 or octal. |
|
| 93 |
+// This function may return negative numbers. |
|
| 94 |
+// If parsing fails or an integer overflow occurs, err will be set. |
|
| 95 |
+func (p *parser) parseNumeric(b []byte) int64 {
|
|
| 96 |
+ // Check for base-256 (binary) format first. |
|
| 97 |
+ // If the first bit is set, then all following bits constitute a two's |
|
| 98 |
+ // complement encoded number in big-endian byte order. |
|
| 99 |
+ if len(b) > 0 && b[0]&0x80 != 0 {
|
|
| 100 |
+ // Handling negative numbers relies on the following identity: |
|
| 101 |
+ // -a-1 == ^a |
|
| 102 |
+ // |
|
| 103 |
+ // If the number is negative, we use an inversion mask to invert the |
|
| 104 |
+ // data bytes and treat the value as an unsigned number. |
|
| 105 |
+ var inv byte // 0x00 if positive or zero, 0xff if negative |
|
| 106 |
+ if b[0]&0x40 != 0 {
|
|
| 107 |
+ inv = 0xff |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ var x uint64 |
|
| 111 |
+ for i, c := range b {
|
|
| 112 |
+ c ^= inv // Inverts c only if inv is 0xff, otherwise does nothing |
|
| 113 |
+ if i == 0 {
|
|
| 114 |
+ c &= 0x7f // Ignore signal bit in first byte |
|
| 115 |
+ } |
|
| 116 |
+ if (x >> 56) > 0 {
|
|
| 117 |
+ p.err = ErrHeader // Integer overflow |
|
| 118 |
+ return 0 |
|
| 119 |
+ } |
|
| 120 |
+ x = x<<8 | uint64(c) |
|
| 121 |
+ } |
|
| 122 |
+ if (x >> 63) > 0 {
|
|
| 123 |
+ p.err = ErrHeader // Integer overflow |
|
| 124 |
+ return 0 |
|
| 125 |
+ } |
|
| 126 |
+ if inv == 0xff {
|
|
| 127 |
+ return ^int64(x) |
|
| 128 |
+ } |
|
| 129 |
+ return int64(x) |
|
| 130 |
+ } |
|
| 131 |
+ |
|
| 132 |
+ // Normal case is base-8 (octal) format. |
|
| 133 |
+ return p.parseOctal(b) |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+// formatNumeric encodes x into b using base-8 (octal) encoding if possible. |
|
| 137 |
+// Otherwise it will attempt to use base-256 (binary) encoding. |
|
| 138 |
+func (f *formatter) formatNumeric(b []byte, x int64) {
|
|
| 139 |
+ if fitsInOctal(len(b), x) {
|
|
| 140 |
+ f.formatOctal(b, x) |
|
| 141 |
+ return |
|
| 142 |
+ } |
|
| 143 |
+ |
|
| 144 |
+ if fitsInBase256(len(b), x) {
|
|
| 145 |
+ for i := len(b) - 1; i >= 0; i-- {
|
|
| 146 |
+ b[i] = byte(x) |
|
| 147 |
+ x >>= 8 |
|
| 148 |
+ } |
|
| 149 |
+ b[0] |= 0x80 // Highest bit indicates binary format |
|
| 150 |
+ return |
|
| 151 |
+ } |
|
| 152 |
+ |
|
| 153 |
+ f.formatOctal(b, 0) // Last resort, just write zero |
|
| 154 |
+ f.err = ErrFieldTooLong |
|
| 155 |
+} |
|
| 156 |
+ |
|
| 157 |
+func (p *parser) parseOctal(b []byte) int64 {
|
|
| 158 |
+ // Because unused fields are filled with NULs, we need |
|
| 159 |
+ // to skip leading NULs. Fields may also be padded with |
|
| 160 |
+ // spaces or NULs. |
|
| 161 |
+ // So we remove leading and trailing NULs and spaces to |
|
| 162 |
+ // be sure. |
|
| 163 |
+ b = bytes.Trim(b, " \x00") |
|
| 164 |
+ |
|
| 165 |
+ if len(b) == 0 {
|
|
| 166 |
+ return 0 |
|
| 167 |
+ } |
|
| 168 |
+ x, perr := strconv.ParseUint(p.parseString(b), 8, 64) |
|
| 169 |
+ if perr != nil {
|
|
| 170 |
+ p.err = ErrHeader |
|
| 171 |
+ } |
|
| 172 |
+ return int64(x) |
|
| 173 |
+} |
|
| 174 |
+ |
|
| 175 |
+func (f *formatter) formatOctal(b []byte, x int64) {
|
|
| 176 |
+ if !fitsInOctal(len(b), x) {
|
|
| 177 |
+ x = 0 // Last resort, just write zero |
|
| 178 |
+ f.err = ErrFieldTooLong |
|
| 179 |
+ } |
|
| 180 |
+ |
|
| 181 |
+ s := strconv.FormatInt(x, 8) |
|
| 182 |
+ // Add leading zeros, but leave room for a NUL. |
|
| 183 |
+ if n := len(b) - len(s) - 1; n > 0 {
|
|
| 184 |
+ s = strings.Repeat("0", n) + s
|
|
| 185 |
+ } |
|
| 186 |
+ f.formatString(b, s) |
|
| 187 |
+} |
|
| 188 |
+ |
|
| 189 |
+// fitsInOctal reports whether the integer x fits in a field n-bytes long |
|
| 190 |
+// using octal encoding with the appropriate NUL terminator. |
|
| 191 |
+func fitsInOctal(n int, x int64) bool {
|
|
| 192 |
+ octBits := uint(n-1) * 3 |
|
| 193 |
+ return x >= 0 && (n >= 22 || x < 1<<octBits) |
|
| 194 |
+} |
|
| 195 |
+ |
|
| 196 |
+// parsePAXTime takes a string of the form %d.%d as described in the PAX |
|
| 197 |
+// specification. Note that this implementation allows for negative timestamps, |
|
| 198 |
+// which is allowed for by the PAX specification, but not always portable. |
|
| 199 |
+func parsePAXTime(s string) (time.Time, error) {
|
|
| 200 |
+ const maxNanoSecondDigits = 9 |
|
| 201 |
+ |
|
| 202 |
+ // Split string into seconds and sub-seconds parts. |
|
| 203 |
+ ss, sn := s, "" |
|
| 204 |
+ if pos := strings.IndexByte(s, '.'); pos >= 0 {
|
|
| 205 |
+ ss, sn = s[:pos], s[pos+1:] |
|
| 206 |
+ } |
|
| 207 |
+ |
|
| 208 |
+ // Parse the seconds. |
|
| 209 |
+ secs, err := strconv.ParseInt(ss, 10, 64) |
|
| 210 |
+ if err != nil {
|
|
| 211 |
+ return time.Time{}, ErrHeader
|
|
| 212 |
+ } |
|
| 213 |
+ if len(sn) == 0 {
|
|
| 214 |
+ return time.Unix(secs, 0), nil // No sub-second values |
|
| 215 |
+ } |
|
| 216 |
+ |
|
| 217 |
+ // Parse the nanoseconds. |
|
| 218 |
+ if strings.Trim(sn, "0123456789") != "" {
|
|
| 219 |
+ return time.Time{}, ErrHeader
|
|
| 220 |
+ } |
|
| 221 |
+ if len(sn) < maxNanoSecondDigits {
|
|
| 222 |
+ sn += strings.Repeat("0", maxNanoSecondDigits-len(sn)) // Right pad
|
|
| 223 |
+ } else {
|
|
| 224 |
+ sn = sn[:maxNanoSecondDigits] // Right truncate |
|
| 225 |
+ } |
|
| 226 |
+ nsecs, _ := strconv.ParseInt(sn, 10, 64) // Must succeed |
|
| 227 |
+ if len(ss) > 0 && ss[0] == '-' {
|
|
| 228 |
+ return time.Unix(secs, -1*nsecs), nil // Negative correction |
|
| 229 |
+ } |
|
| 230 |
+ return time.Unix(secs, nsecs), nil |
|
| 231 |
+} |
|
| 232 |
+ |
|
| 233 |
+// formatPAXTime converts ts into a time of the form %d.%d as described in the |
|
| 234 |
+// PAX specification. This function is capable of negative timestamps. |
|
| 235 |
+func formatPAXTime(ts time.Time) (s string) {
|
|
| 236 |
+ secs, nsecs := ts.Unix(), ts.Nanosecond() |
|
| 237 |
+ if nsecs == 0 {
|
|
| 238 |
+ return strconv.FormatInt(secs, 10) |
|
| 239 |
+ } |
|
| 240 |
+ |
|
| 241 |
+ // If seconds is negative, then perform correction. |
|
| 242 |
+ sign := "" |
|
| 243 |
+ if secs < 0 {
|
|
| 244 |
+ sign = "-" // Remember sign |
|
| 245 |
+ secs = -(secs + 1) // Add a second to secs |
|
| 246 |
+ nsecs = -(nsecs - 1E9) // Take that second away from nsecs |
|
| 247 |
+ } |
|
| 248 |
+ return strings.TrimRight(fmt.Sprintf("%s%d.%09d", sign, secs, nsecs), "0")
|
|
| 249 |
+} |
|
| 250 |
+ |
|
| 251 |
+// parsePAXRecord parses the input PAX record string into a key-value pair. |
|
| 252 |
+// If parsing is successful, it will slice off the currently read record and |
|
| 253 |
+// return the remainder as r. |
|
| 254 |
+func parsePAXRecord(s string) (k, v, r string, err error) {
|
|
| 255 |
+ // The size field ends at the first space. |
|
| 256 |
+ sp := strings.IndexByte(s, ' ') |
|
| 257 |
+ if sp == -1 {
|
|
| 258 |
+ return "", "", s, ErrHeader |
|
| 259 |
+ } |
|
| 260 |
+ |
|
| 261 |
+ // Parse the first token as a decimal integer. |
|
| 262 |
+ n, perr := strconv.ParseInt(s[:sp], 10, 0) // Intentionally parse as native int |
|
| 263 |
+ if perr != nil || n < 5 || int64(len(s)) < n {
|
|
| 264 |
+ return "", "", s, ErrHeader |
|
| 265 |
+ } |
|
| 266 |
+ |
|
| 267 |
+ // Extract everything between the space and the final newline. |
|
| 268 |
+ rec, nl, rem := s[sp+1:n-1], s[n-1:n], s[n:] |
|
| 269 |
+ if nl != "\n" {
|
|
| 270 |
+ return "", "", s, ErrHeader |
|
| 271 |
+ } |
|
| 272 |
+ |
|
| 273 |
+ // The first equals separates the key from the value. |
|
| 274 |
+ eq := strings.IndexByte(rec, '=') |
|
| 275 |
+ if eq == -1 {
|
|
| 276 |
+ return "", "", s, ErrHeader |
|
| 277 |
+ } |
|
| 278 |
+ k, v = rec[:eq], rec[eq+1:] |
|
| 279 |
+ |
|
| 280 |
+ if !validPAXRecord(k, v) {
|
|
| 281 |
+ return "", "", s, ErrHeader |
|
| 282 |
+ } |
|
| 283 |
+ return k, v, rem, nil |
|
| 284 |
+} |
|
| 285 |
+ |
|
| 286 |
+// formatPAXRecord formats a single PAX record, prefixing it with the |
|
| 287 |
+// appropriate length. |
|
| 288 |
+func formatPAXRecord(k, v string) (string, error) {
|
|
| 289 |
+ if !validPAXRecord(k, v) {
|
|
| 290 |
+ return "", ErrHeader |
|
| 291 |
+ } |
|
| 292 |
+ |
|
| 293 |
+ const padding = 3 // Extra padding for ' ', '=', and '\n' |
|
| 294 |
+ size := len(k) + len(v) + padding |
|
| 295 |
+ size += len(strconv.Itoa(size)) |
|
| 296 |
+ record := strconv.Itoa(size) + " " + k + "=" + v + "\n" |
|
| 297 |
+ |
|
| 298 |
+ // Final adjustment if adding size field increased the record size. |
|
| 299 |
+ if len(record) != size {
|
|
| 300 |
+ size = len(record) |
|
| 301 |
+ record = strconv.Itoa(size) + " " + k + "=" + v + "\n" |
|
| 302 |
+ } |
|
| 303 |
+ return record, nil |
|
| 304 |
+} |
|
| 305 |
+ |
|
| 306 |
+// validPAXRecord reports whether the key-value pair is valid where each |
|
| 307 |
+// record is formatted as: |
|
| 308 |
+// "%d %s=%s\n" % (size, key, value) |
|
| 309 |
+// |
|
| 310 |
+// Keys and values should be UTF-8, but the number of bad writers out there |
|
| 311 |
+// forces us to be a more liberal. |
|
| 312 |
+// Thus, we only reject all keys with NUL, and only reject NULs in values |
|
| 313 |
+// for the PAX version of the USTAR string fields. |
|
| 314 |
+// The key must not contain an '=' character. |
|
| 315 |
+func validPAXRecord(k, v string) bool {
|
|
| 316 |
+ if k == "" || strings.IndexByte(k, '=') >= 0 {
|
|
| 317 |
+ return false |
|
| 318 |
+ } |
|
| 319 |
+ switch k {
|
|
| 320 |
+ case paxPath, paxLinkpath, paxUname, paxGname: |
|
| 321 |
+ return !hasNUL(v) |
|
| 322 |
+ default: |
|
| 323 |
+ return !hasNUL(k) |
|
| 324 |
+ } |
|
| 325 |
+} |
| 0 | 326 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,644 @@ |
| 0 |
+// Copyright 2009 The Go Authors. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package tar |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "bytes" |
|
| 8 |
+ "fmt" |
|
| 9 |
+ "io" |
|
| 10 |
+ "path" |
|
| 11 |
+ "sort" |
|
| 12 |
+ "strings" |
|
| 13 |
+ "time" |
|
| 14 |
+) |
|
| 15 |
+ |
|
| 16 |
+// Writer provides sequential writing of a tar archive. |
|
| 17 |
+// Write.WriteHeader begins a new file with the provided Header, |
|
| 18 |
+// and then Writer can be treated as an io.Writer to supply that file's data. |
|
| 19 |
+type Writer struct {
|
|
| 20 |
+ w io.Writer |
|
| 21 |
+ pad int64 // Amount of padding to write after current file entry |
|
| 22 |
+ curr fileWriter // Writer for current file entry |
|
| 23 |
+ hdr Header // Shallow copy of Header that is safe for mutations |
|
| 24 |
+ blk block // Buffer to use as temporary local storage |
|
| 25 |
+ |
|
| 26 |
+ // err is a persistent error. |
|
| 27 |
+ // It is only the responsibility of every exported method of Writer to |
|
| 28 |
+ // ensure that this error is sticky. |
|
| 29 |
+ err error |
|
| 30 |
+} |
|
| 31 |
+ |
|
| 32 |
+// NewWriter creates a new Writer writing to w. |
|
| 33 |
+func NewWriter(w io.Writer) *Writer {
|
|
| 34 |
+ return &Writer{w: w, curr: ®FileWriter{w, 0}}
|
|
| 35 |
+} |
|
| 36 |
+ |
|
| 37 |
+type fileWriter interface {
|
|
| 38 |
+ io.Writer |
|
| 39 |
+ fileState |
|
| 40 |
+ |
|
| 41 |
+ ReadFrom(io.Reader) (int64, error) |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+// Flush finishes writing the current file's block padding. |
|
| 45 |
+// The current file must be fully written before Flush can be called. |
|
| 46 |
+// |
|
| 47 |
+// This is unnecessary as the next call to WriteHeader or Close |
|
| 48 |
+// will implicitly flush out the file's padding. |
|
| 49 |
+func (tw *Writer) Flush() error {
|
|
| 50 |
+ if tw.err != nil {
|
|
| 51 |
+ return tw.err |
|
| 52 |
+ } |
|
| 53 |
+ if nb := tw.curr.LogicalRemaining(); nb > 0 {
|
|
| 54 |
+ return fmt.Errorf("archive/tar: missed writing %d bytes", nb)
|
|
| 55 |
+ } |
|
| 56 |
+ if _, tw.err = tw.w.Write(zeroBlock[:tw.pad]); tw.err != nil {
|
|
| 57 |
+ return tw.err |
|
| 58 |
+ } |
|
| 59 |
+ tw.pad = 0 |
|
| 60 |
+ return nil |
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+// WriteHeader writes hdr and prepares to accept the file's contents. |
|
| 64 |
+// The Header.Size determines how many bytes can be written for the next file. |
|
| 65 |
+// If the current file is not fully written, then this returns an error. |
|
| 66 |
+// This implicitly flushes any padding necessary before writing the header. |
|
| 67 |
+func (tw *Writer) WriteHeader(hdr *Header) error {
|
|
| 68 |
+ if err := tw.Flush(); err != nil {
|
|
| 69 |
+ return err |
|
| 70 |
+ } |
|
| 71 |
+ tw.hdr = *hdr // Shallow copy of Header |
|
| 72 |
+ |
|
| 73 |
+ // Round ModTime and ignore AccessTime and ChangeTime unless |
|
| 74 |
+ // the format is explicitly chosen. |
|
| 75 |
+ // This ensures nominal usage of WriteHeader (without specifying the format) |
|
| 76 |
+ // does not always result in the PAX format being chosen, which |
|
| 77 |
+ // causes a 1KiB increase to every header. |
|
| 78 |
+ if tw.hdr.Format == FormatUnknown {
|
|
| 79 |
+ tw.hdr.ModTime = tw.hdr.ModTime.Round(time.Second) |
|
| 80 |
+ tw.hdr.AccessTime = time.Time{}
|
|
| 81 |
+ tw.hdr.ChangeTime = time.Time{}
|
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ allowedFormats, paxHdrs, err := tw.hdr.allowedFormats() |
|
| 85 |
+ switch {
|
|
| 86 |
+ case allowedFormats.has(FormatUSTAR): |
|
| 87 |
+ tw.err = tw.writeUSTARHeader(&tw.hdr) |
|
| 88 |
+ return tw.err |
|
| 89 |
+ case allowedFormats.has(FormatPAX): |
|
| 90 |
+ tw.err = tw.writePAXHeader(&tw.hdr, paxHdrs) |
|
| 91 |
+ return tw.err |
|
| 92 |
+ case allowedFormats.has(FormatGNU): |
|
| 93 |
+ tw.err = tw.writeGNUHeader(&tw.hdr) |
|
| 94 |
+ return tw.err |
|
| 95 |
+ default: |
|
| 96 |
+ return err // Non-fatal error |
|
| 97 |
+ } |
|
| 98 |
+} |
|
| 99 |
+ |
|
| 100 |
+func (tw *Writer) writeUSTARHeader(hdr *Header) error {
|
|
| 101 |
+ // Check if we can use USTAR prefix/suffix splitting. |
|
| 102 |
+ var namePrefix string |
|
| 103 |
+ if prefix, suffix, ok := splitUSTARPath(hdr.Name); ok {
|
|
| 104 |
+ namePrefix, hdr.Name = prefix, suffix |
|
| 105 |
+ } |
|
| 106 |
+ |
|
| 107 |
+ // Pack the main header. |
|
| 108 |
+ var f formatter |
|
| 109 |
+ blk := tw.templateV7Plus(hdr, f.formatString, f.formatOctal) |
|
| 110 |
+ f.formatString(blk.USTAR().Prefix(), namePrefix) |
|
| 111 |
+ blk.SetFormat(FormatUSTAR) |
|
| 112 |
+ if f.err != nil {
|
|
| 113 |
+ return f.err // Should never happen since header is validated |
|
| 114 |
+ } |
|
| 115 |
+ return tw.writeRawHeader(blk, hdr.Size, hdr.Typeflag) |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 118 |
+func (tw *Writer) writePAXHeader(hdr *Header, paxHdrs map[string]string) error {
|
|
| 119 |
+ realName, realSize := hdr.Name, hdr.Size |
|
| 120 |
+ |
|
| 121 |
+ // TODO(dsnet): Re-enable this when adding sparse support. |
|
| 122 |
+ // See https://golang.org/issue/22735 |
|
| 123 |
+ /* |
|
| 124 |
+ // Handle sparse files. |
|
| 125 |
+ var spd sparseDatas |
|
| 126 |
+ var spb []byte |
|
| 127 |
+ if len(hdr.SparseHoles) > 0 {
|
|
| 128 |
+ sph := append([]sparseEntry{}, hdr.SparseHoles...) // Copy sparse map
|
|
| 129 |
+ sph = alignSparseEntries(sph, hdr.Size) |
|
| 130 |
+ spd = invertSparseEntries(sph, hdr.Size) |
|
| 131 |
+ |
|
| 132 |
+ // Format the sparse map. |
|
| 133 |
+ hdr.Size = 0 // Replace with encoded size |
|
| 134 |
+ spb = append(strconv.AppendInt(spb, int64(len(spd)), 10), '\n') |
|
| 135 |
+ for _, s := range spd {
|
|
| 136 |
+ hdr.Size += s.Length |
|
| 137 |
+ spb = append(strconv.AppendInt(spb, s.Offset, 10), '\n') |
|
| 138 |
+ spb = append(strconv.AppendInt(spb, s.Length, 10), '\n') |
|
| 139 |
+ } |
|
| 140 |
+ pad := blockPadding(int64(len(spb))) |
|
| 141 |
+ spb = append(spb, zeroBlock[:pad]...) |
|
| 142 |
+ hdr.Size += int64(len(spb)) // Accounts for encoded sparse map |
|
| 143 |
+ |
|
| 144 |
+ // Add and modify appropriate PAX records. |
|
| 145 |
+ dir, file := path.Split(realName) |
|
| 146 |
+ hdr.Name = path.Join(dir, "GNUSparseFile.0", file) |
|
| 147 |
+ paxHdrs[paxGNUSparseMajor] = "1" |
|
| 148 |
+ paxHdrs[paxGNUSparseMinor] = "0" |
|
| 149 |
+ paxHdrs[paxGNUSparseName] = realName |
|
| 150 |
+ paxHdrs[paxGNUSparseRealSize] = strconv.FormatInt(realSize, 10) |
|
| 151 |
+ paxHdrs[paxSize] = strconv.FormatInt(hdr.Size, 10) |
|
| 152 |
+ delete(paxHdrs, paxPath) // Recorded by paxGNUSparseName |
|
| 153 |
+ } |
|
| 154 |
+ */ |
|
| 155 |
+ _ = realSize |
|
| 156 |
+ |
|
| 157 |
+ // Write PAX records to the output. |
|
| 158 |
+ isGlobal := hdr.Typeflag == TypeXGlobalHeader |
|
| 159 |
+ if len(paxHdrs) > 0 || isGlobal {
|
|
| 160 |
+ // Sort keys for deterministic ordering. |
|
| 161 |
+ var keys []string |
|
| 162 |
+ for k := range paxHdrs {
|
|
| 163 |
+ keys = append(keys, k) |
|
| 164 |
+ } |
|
| 165 |
+ sort.Strings(keys) |
|
| 166 |
+ |
|
| 167 |
+ // Write each record to a buffer. |
|
| 168 |
+ var buf bytes.Buffer |
|
| 169 |
+ for _, k := range keys {
|
|
| 170 |
+ rec, err := formatPAXRecord(k, paxHdrs[k]) |
|
| 171 |
+ if err != nil {
|
|
| 172 |
+ return err |
|
| 173 |
+ } |
|
| 174 |
+ buf.WriteString(rec) |
|
| 175 |
+ } |
|
| 176 |
+ |
|
| 177 |
+ // Write the extended header file. |
|
| 178 |
+ var name string |
|
| 179 |
+ var flag byte |
|
| 180 |
+ if isGlobal {
|
|
| 181 |
+ name = realName |
|
| 182 |
+ if name == "" {
|
|
| 183 |
+ name = "GlobalHead.0.0" |
|
| 184 |
+ } |
|
| 185 |
+ flag = TypeXGlobalHeader |
|
| 186 |
+ } else {
|
|
| 187 |
+ dir, file := path.Split(realName) |
|
| 188 |
+ name = path.Join(dir, "PaxHeaders.0", file) |
|
| 189 |
+ flag = TypeXHeader |
|
| 190 |
+ } |
|
| 191 |
+ data := buf.String() |
|
| 192 |
+ if err := tw.writeRawFile(name, data, flag, FormatPAX); err != nil || isGlobal {
|
|
| 193 |
+ return err // Global headers return here |
|
| 194 |
+ } |
|
| 195 |
+ } |
|
| 196 |
+ |
|
| 197 |
+ // Pack the main header. |
|
| 198 |
+ var f formatter // Ignore errors since they are expected |
|
| 199 |
+ fmtStr := func(b []byte, s string) { f.formatString(b, toASCII(s)) }
|
|
| 200 |
+ blk := tw.templateV7Plus(hdr, fmtStr, f.formatOctal) |
|
| 201 |
+ blk.SetFormat(FormatPAX) |
|
| 202 |
+ if err := tw.writeRawHeader(blk, hdr.Size, hdr.Typeflag); err != nil {
|
|
| 203 |
+ return err |
|
| 204 |
+ } |
|
| 205 |
+ |
|
| 206 |
+ // TODO(dsnet): Re-enable this when adding sparse support. |
|
| 207 |
+ // See https://golang.org/issue/22735 |
|
| 208 |
+ /* |
|
| 209 |
+ // Write the sparse map and setup the sparse writer if necessary. |
|
| 210 |
+ if len(spd) > 0 {
|
|
| 211 |
+ // Use tw.curr since the sparse map is accounted for in hdr.Size. |
|
| 212 |
+ if _, err := tw.curr.Write(spb); err != nil {
|
|
| 213 |
+ return err |
|
| 214 |
+ } |
|
| 215 |
+ tw.curr = &sparseFileWriter{tw.curr, spd, 0}
|
|
| 216 |
+ } |
|
| 217 |
+ */ |
|
| 218 |
+ return nil |
|
| 219 |
+} |
|
| 220 |
+ |
|
| 221 |
+func (tw *Writer) writeGNUHeader(hdr *Header) error {
|
|
| 222 |
+ // Use long-link files if Name or Linkname exceeds the field size. |
|
| 223 |
+ const longName = "././@LongLink" |
|
| 224 |
+ if len(hdr.Name) > nameSize {
|
|
| 225 |
+ data := hdr.Name + "\x00" |
|
| 226 |
+ if err := tw.writeRawFile(longName, data, TypeGNULongName, FormatGNU); err != nil {
|
|
| 227 |
+ return err |
|
| 228 |
+ } |
|
| 229 |
+ } |
|
| 230 |
+ if len(hdr.Linkname) > nameSize {
|
|
| 231 |
+ data := hdr.Linkname + "\x00" |
|
| 232 |
+ if err := tw.writeRawFile(longName, data, TypeGNULongLink, FormatGNU); err != nil {
|
|
| 233 |
+ return err |
|
| 234 |
+ } |
|
| 235 |
+ } |
|
| 236 |
+ |
|
| 237 |
+ // Pack the main header. |
|
| 238 |
+ var f formatter // Ignore errors since they are expected |
|
| 239 |
+ var spd sparseDatas |
|
| 240 |
+ var spb []byte |
|
| 241 |
+ blk := tw.templateV7Plus(hdr, f.formatString, f.formatNumeric) |
|
| 242 |
+ if !hdr.AccessTime.IsZero() {
|
|
| 243 |
+ f.formatNumeric(blk.GNU().AccessTime(), hdr.AccessTime.Unix()) |
|
| 244 |
+ } |
|
| 245 |
+ if !hdr.ChangeTime.IsZero() {
|
|
| 246 |
+ f.formatNumeric(blk.GNU().ChangeTime(), hdr.ChangeTime.Unix()) |
|
| 247 |
+ } |
|
| 248 |
+ // TODO(dsnet): Re-enable this when adding sparse support. |
|
| 249 |
+ // See https://golang.org/issue/22735 |
|
| 250 |
+ /* |
|
| 251 |
+ if hdr.Typeflag == TypeGNUSparse {
|
|
| 252 |
+ sph := append([]sparseEntry{}, hdr.SparseHoles...) // Copy sparse map
|
|
| 253 |
+ sph = alignSparseEntries(sph, hdr.Size) |
|
| 254 |
+ spd = invertSparseEntries(sph, hdr.Size) |
|
| 255 |
+ |
|
| 256 |
+ // Format the sparse map. |
|
| 257 |
+ formatSPD := func(sp sparseDatas, sa sparseArray) sparseDatas {
|
|
| 258 |
+ for i := 0; len(sp) > 0 && i < sa.MaxEntries(); i++ {
|
|
| 259 |
+ f.formatNumeric(sa.Entry(i).Offset(), sp[0].Offset) |
|
| 260 |
+ f.formatNumeric(sa.Entry(i).Length(), sp[0].Length) |
|
| 261 |
+ sp = sp[1:] |
|
| 262 |
+ } |
|
| 263 |
+ if len(sp) > 0 {
|
|
| 264 |
+ sa.IsExtended()[0] = 1 |
|
| 265 |
+ } |
|
| 266 |
+ return sp |
|
| 267 |
+ } |
|
| 268 |
+ sp2 := formatSPD(spd, blk.GNU().Sparse()) |
|
| 269 |
+ for len(sp2) > 0 {
|
|
| 270 |
+ var spHdr block |
|
| 271 |
+ sp2 = formatSPD(sp2, spHdr.Sparse()) |
|
| 272 |
+ spb = append(spb, spHdr[:]...) |
|
| 273 |
+ } |
|
| 274 |
+ |
|
| 275 |
+ // Update size fields in the header block. |
|
| 276 |
+ realSize := hdr.Size |
|
| 277 |
+ hdr.Size = 0 // Encoded size; does not account for encoded sparse map |
|
| 278 |
+ for _, s := range spd {
|
|
| 279 |
+ hdr.Size += s.Length |
|
| 280 |
+ } |
|
| 281 |
+ copy(blk.V7().Size(), zeroBlock[:]) // Reset field |
|
| 282 |
+ f.formatNumeric(blk.V7().Size(), hdr.Size) |
|
| 283 |
+ f.formatNumeric(blk.GNU().RealSize(), realSize) |
|
| 284 |
+ } |
|
| 285 |
+ */ |
|
| 286 |
+ blk.SetFormat(FormatGNU) |
|
| 287 |
+ if err := tw.writeRawHeader(blk, hdr.Size, hdr.Typeflag); err != nil {
|
|
| 288 |
+ return err |
|
| 289 |
+ } |
|
| 290 |
+ |
|
| 291 |
+ // Write the extended sparse map and setup the sparse writer if necessary. |
|
| 292 |
+ if len(spd) > 0 {
|
|
| 293 |
+ // Use tw.w since the sparse map is not accounted for in hdr.Size. |
|
| 294 |
+ if _, err := tw.w.Write(spb); err != nil {
|
|
| 295 |
+ return err |
|
| 296 |
+ } |
|
| 297 |
+ tw.curr = &sparseFileWriter{tw.curr, spd, 0}
|
|
| 298 |
+ } |
|
| 299 |
+ return nil |
|
| 300 |
+} |
|
| 301 |
+ |
|
| 302 |
+type ( |
|
| 303 |
+ stringFormatter func([]byte, string) |
|
| 304 |
+ numberFormatter func([]byte, int64) |
|
| 305 |
+) |
|
| 306 |
+ |
|
| 307 |
+// templateV7Plus fills out the V7 fields of a block using values from hdr. |
|
| 308 |
+// It also fills out fields (uname, gname, devmajor, devminor) that are |
|
| 309 |
+// shared in the USTAR, PAX, and GNU formats using the provided formatters. |
|
| 310 |
+// |
|
| 311 |
+// The block returned is only valid until the next call to |
|
| 312 |
+// templateV7Plus or writeRawFile. |
|
| 313 |
+func (tw *Writer) templateV7Plus(hdr *Header, fmtStr stringFormatter, fmtNum numberFormatter) *block {
|
|
| 314 |
+ tw.blk.Reset() |
|
| 315 |
+ |
|
| 316 |
+ modTime := hdr.ModTime |
|
| 317 |
+ if modTime.IsZero() {
|
|
| 318 |
+ modTime = time.Unix(0, 0) |
|
| 319 |
+ } |
|
| 320 |
+ |
|
| 321 |
+ v7 := tw.blk.V7() |
|
| 322 |
+ v7.TypeFlag()[0] = hdr.Typeflag |
|
| 323 |
+ fmtStr(v7.Name(), hdr.Name) |
|
| 324 |
+ fmtStr(v7.LinkName(), hdr.Linkname) |
|
| 325 |
+ fmtNum(v7.Mode(), hdr.Mode) |
|
| 326 |
+ fmtNum(v7.UID(), int64(hdr.Uid)) |
|
| 327 |
+ fmtNum(v7.GID(), int64(hdr.Gid)) |
|
| 328 |
+ fmtNum(v7.Size(), hdr.Size) |
|
| 329 |
+ fmtNum(v7.ModTime(), modTime.Unix()) |
|
| 330 |
+ |
|
| 331 |
+ ustar := tw.blk.USTAR() |
|
| 332 |
+ fmtStr(ustar.UserName(), hdr.Uname) |
|
| 333 |
+ fmtStr(ustar.GroupName(), hdr.Gname) |
|
| 334 |
+ fmtNum(ustar.DevMajor(), hdr.Devmajor) |
|
| 335 |
+ fmtNum(ustar.DevMinor(), hdr.Devminor) |
|
| 336 |
+ |
|
| 337 |
+ return &tw.blk |
|
| 338 |
+} |
|
| 339 |
+ |
|
| 340 |
+// writeRawFile writes a minimal file with the given name and flag type. |
|
| 341 |
+// It uses format to encode the header format and will write data as the body. |
|
| 342 |
+// It uses default values for all of the other fields (as BSD and GNU tar does). |
|
| 343 |
+func (tw *Writer) writeRawFile(name, data string, flag byte, format Format) error {
|
|
| 344 |
+ tw.blk.Reset() |
|
| 345 |
+ |
|
| 346 |
+ // Best effort for the filename. |
|
| 347 |
+ name = toASCII(name) |
|
| 348 |
+ if len(name) > nameSize {
|
|
| 349 |
+ name = name[:nameSize] |
|
| 350 |
+ } |
|
| 351 |
+ name = strings.TrimRight(name, "/") |
|
| 352 |
+ |
|
| 353 |
+ var f formatter |
|
| 354 |
+ v7 := tw.blk.V7() |
|
| 355 |
+ v7.TypeFlag()[0] = flag |
|
| 356 |
+ f.formatString(v7.Name(), name) |
|
| 357 |
+ f.formatOctal(v7.Mode(), 0) |
|
| 358 |
+ f.formatOctal(v7.UID(), 0) |
|
| 359 |
+ f.formatOctal(v7.GID(), 0) |
|
| 360 |
+ f.formatOctal(v7.Size(), int64(len(data))) // Must be < 8GiB |
|
| 361 |
+ f.formatOctal(v7.ModTime(), 0) |
|
| 362 |
+ tw.blk.SetFormat(format) |
|
| 363 |
+ if f.err != nil {
|
|
| 364 |
+ return f.err // Only occurs if size condition is violated |
|
| 365 |
+ } |
|
| 366 |
+ |
|
| 367 |
+ // Write the header and data. |
|
| 368 |
+ if err := tw.writeRawHeader(&tw.blk, int64(len(data)), flag); err != nil {
|
|
| 369 |
+ return err |
|
| 370 |
+ } |
|
| 371 |
+ _, err := io.WriteString(tw, data) |
|
| 372 |
+ return err |
|
| 373 |
+} |
|
| 374 |
+ |
|
| 375 |
+// writeRawHeader writes the value of blk, regardless of its value. |
|
| 376 |
+// It sets up the Writer such that it can accept a file of the given size. |
|
| 377 |
+// If the flag is a special header-only flag, then the size is treated as zero. |
|
| 378 |
+func (tw *Writer) writeRawHeader(blk *block, size int64, flag byte) error {
|
|
| 379 |
+ if err := tw.Flush(); err != nil {
|
|
| 380 |
+ return err |
|
| 381 |
+ } |
|
| 382 |
+ if _, err := tw.w.Write(blk[:]); err != nil {
|
|
| 383 |
+ return err |
|
| 384 |
+ } |
|
| 385 |
+ if isHeaderOnlyType(flag) {
|
|
| 386 |
+ size = 0 |
|
| 387 |
+ } |
|
| 388 |
+ tw.curr = ®FileWriter{tw.w, size}
|
|
| 389 |
+ tw.pad = blockPadding(size) |
|
| 390 |
+ return nil |
|
| 391 |
+} |
|
| 392 |
+ |
|
| 393 |
+// splitUSTARPath splits a path according to USTAR prefix and suffix rules. |
|
| 394 |
+// If the path is not splittable, then it will return ("", "", false).
|
|
| 395 |
+func splitUSTARPath(name string) (prefix, suffix string, ok bool) {
|
|
| 396 |
+ length := len(name) |
|
| 397 |
+ if length <= nameSize || !isASCII(name) {
|
|
| 398 |
+ return "", "", false |
|
| 399 |
+ } else if length > prefixSize+1 {
|
|
| 400 |
+ length = prefixSize + 1 |
|
| 401 |
+ } else if name[length-1] == '/' {
|
|
| 402 |
+ length-- |
|
| 403 |
+ } |
|
| 404 |
+ |
|
| 405 |
+ i := strings.LastIndex(name[:length], "/") |
|
| 406 |
+ nlen := len(name) - i - 1 // nlen is length of suffix |
|
| 407 |
+ plen := i // plen is length of prefix |
|
| 408 |
+ if i <= 0 || nlen > nameSize || nlen == 0 || plen > prefixSize {
|
|
| 409 |
+ return "", "", false |
|
| 410 |
+ } |
|
| 411 |
+ return name[:i], name[i+1:], true |
|
| 412 |
+} |
|
| 413 |
+ |
|
| 414 |
+// Write writes to the current file in the tar archive. |
|
| 415 |
+// Write returns the error ErrWriteTooLong if more than |
|
| 416 |
+// Header.Size bytes are written after WriteHeader. |
|
| 417 |
+// |
|
| 418 |
+// Calling Write on special types like TypeLink, TypeSymlink, TypeChar, |
|
| 419 |
+// TypeBlock, TypeDir, and TypeFifo returns (0, ErrWriteTooLong) regardless |
|
| 420 |
+// of what the Header.Size claims. |
|
| 421 |
+func (tw *Writer) Write(b []byte) (int, error) {
|
|
| 422 |
+ if tw.err != nil {
|
|
| 423 |
+ return 0, tw.err |
|
| 424 |
+ } |
|
| 425 |
+ n, err := tw.curr.Write(b) |
|
| 426 |
+ if err != nil && err != ErrWriteTooLong {
|
|
| 427 |
+ tw.err = err |
|
| 428 |
+ } |
|
| 429 |
+ return n, err |
|
| 430 |
+} |
|
| 431 |
+ |
|
| 432 |
+// readFrom populates the content of the current file by reading from r. |
|
| 433 |
+// The bytes read must match the number of remaining bytes in the current file. |
|
| 434 |
+// |
|
| 435 |
+// If the current file is sparse and r is an io.ReadSeeker, |
|
| 436 |
+// then readFrom uses Seek to skip past holes defined in Header.SparseHoles, |
|
| 437 |
+// assuming that skipped regions are all NULs. |
|
| 438 |
+// This always reads the last byte to ensure r is the right size. |
|
| 439 |
+// |
|
| 440 |
+// TODO(dsnet): Re-export this when adding sparse file support. |
|
| 441 |
+// See https://golang.org/issue/22735 |
|
| 442 |
+func (tw *Writer) readFrom(r io.Reader) (int64, error) {
|
|
| 443 |
+ if tw.err != nil {
|
|
| 444 |
+ return 0, tw.err |
|
| 445 |
+ } |
|
| 446 |
+ n, err := tw.curr.ReadFrom(r) |
|
| 447 |
+ if err != nil && err != ErrWriteTooLong {
|
|
| 448 |
+ tw.err = err |
|
| 449 |
+ } |
|
| 450 |
+ return n, err |
|
| 451 |
+} |
|
| 452 |
+ |
|
| 453 |
+// Close closes the tar archive by flushing the padding, and writing the footer. |
|
| 454 |
+// If the current file (from a prior call to WriteHeader) is not fully written, |
|
| 455 |
+// then this returns an error. |
|
| 456 |
+func (tw *Writer) Close() error {
|
|
| 457 |
+ if tw.err == ErrWriteAfterClose {
|
|
| 458 |
+ return nil |
|
| 459 |
+ } |
|
| 460 |
+ if tw.err != nil {
|
|
| 461 |
+ return tw.err |
|
| 462 |
+ } |
|
| 463 |
+ |
|
| 464 |
+ // Trailer: two zero blocks. |
|
| 465 |
+ err := tw.Flush() |
|
| 466 |
+ for i := 0; i < 2 && err == nil; i++ {
|
|
| 467 |
+ _, err = tw.w.Write(zeroBlock[:]) |
|
| 468 |
+ } |
|
| 469 |
+ |
|
| 470 |
+ // Ensure all future actions are invalid. |
|
| 471 |
+ tw.err = ErrWriteAfterClose |
|
| 472 |
+ return err // Report IO errors |
|
| 473 |
+} |
|
| 474 |
+ |
|
| 475 |
+// regFileWriter is a fileWriter for writing data to a regular file entry. |
|
| 476 |
+type regFileWriter struct {
|
|
| 477 |
+ w io.Writer // Underlying Writer |
|
| 478 |
+ nb int64 // Number of remaining bytes to write |
|
| 479 |
+} |
|
| 480 |
+ |
|
| 481 |
+func (fw *regFileWriter) Write(b []byte) (n int, err error) {
|
|
| 482 |
+ overwrite := int64(len(b)) > fw.nb |
|
| 483 |
+ if overwrite {
|
|
| 484 |
+ b = b[:fw.nb] |
|
| 485 |
+ } |
|
| 486 |
+ if len(b) > 0 {
|
|
| 487 |
+ n, err = fw.w.Write(b) |
|
| 488 |
+ fw.nb -= int64(n) |
|
| 489 |
+ } |
|
| 490 |
+ switch {
|
|
| 491 |
+ case err != nil: |
|
| 492 |
+ return n, err |
|
| 493 |
+ case overwrite: |
|
| 494 |
+ return n, ErrWriteTooLong |
|
| 495 |
+ default: |
|
| 496 |
+ return n, nil |
|
| 497 |
+ } |
|
| 498 |
+} |
|
| 499 |
+ |
|
| 500 |
+func (fw *regFileWriter) ReadFrom(r io.Reader) (int64, error) {
|
|
| 501 |
+ return io.Copy(struct{ io.Writer }{fw}, r)
|
|
| 502 |
+} |
|
| 503 |
+ |
|
| 504 |
+func (fw regFileWriter) LogicalRemaining() int64 {
|
|
| 505 |
+ return fw.nb |
|
| 506 |
+} |
|
| 507 |
+func (fw regFileWriter) PhysicalRemaining() int64 {
|
|
| 508 |
+ return fw.nb |
|
| 509 |
+} |
|
| 510 |
+ |
|
| 511 |
+// sparseFileWriter is a fileWriter for writing data to a sparse file entry. |
|
| 512 |
+type sparseFileWriter struct {
|
|
| 513 |
+ fw fileWriter // Underlying fileWriter |
|
| 514 |
+ sp sparseDatas // Normalized list of data fragments |
|
| 515 |
+ pos int64 // Current position in sparse file |
|
| 516 |
+} |
|
| 517 |
+ |
|
| 518 |
+func (sw *sparseFileWriter) Write(b []byte) (n int, err error) {
|
|
| 519 |
+ overwrite := int64(len(b)) > sw.LogicalRemaining() |
|
| 520 |
+ if overwrite {
|
|
| 521 |
+ b = b[:sw.LogicalRemaining()] |
|
| 522 |
+ } |
|
| 523 |
+ |
|
| 524 |
+ b0 := b |
|
| 525 |
+ endPos := sw.pos + int64(len(b)) |
|
| 526 |
+ for endPos > sw.pos && err == nil {
|
|
| 527 |
+ var nf int // Bytes written in fragment |
|
| 528 |
+ dataStart, dataEnd := sw.sp[0].Offset, sw.sp[0].endOffset() |
|
| 529 |
+ if sw.pos < dataStart { // In a hole fragment
|
|
| 530 |
+ bf := b[:min(int64(len(b)), dataStart-sw.pos)] |
|
| 531 |
+ nf, err = zeroWriter{}.Write(bf)
|
|
| 532 |
+ } else { // In a data fragment
|
|
| 533 |
+ bf := b[:min(int64(len(b)), dataEnd-sw.pos)] |
|
| 534 |
+ nf, err = sw.fw.Write(bf) |
|
| 535 |
+ } |
|
| 536 |
+ b = b[nf:] |
|
| 537 |
+ sw.pos += int64(nf) |
|
| 538 |
+ if sw.pos >= dataEnd && len(sw.sp) > 1 {
|
|
| 539 |
+ sw.sp = sw.sp[1:] // Ensure last fragment always remains |
|
| 540 |
+ } |
|
| 541 |
+ } |
|
| 542 |
+ |
|
| 543 |
+ n = len(b0) - len(b) |
|
| 544 |
+ switch {
|
|
| 545 |
+ case err == ErrWriteTooLong: |
|
| 546 |
+ return n, errMissData // Not possible; implies bug in validation logic |
|
| 547 |
+ case err != nil: |
|
| 548 |
+ return n, err |
|
| 549 |
+ case sw.LogicalRemaining() == 0 && sw.PhysicalRemaining() > 0: |
|
| 550 |
+ return n, errUnrefData // Not possible; implies bug in validation logic |
|
| 551 |
+ case overwrite: |
|
| 552 |
+ return n, ErrWriteTooLong |
|
| 553 |
+ default: |
|
| 554 |
+ return n, nil |
|
| 555 |
+ } |
|
| 556 |
+} |
|
| 557 |
+ |
|
| 558 |
+func (sw *sparseFileWriter) ReadFrom(r io.Reader) (n int64, err error) {
|
|
| 559 |
+ rs, ok := r.(io.ReadSeeker) |
|
| 560 |
+ if ok {
|
|
| 561 |
+ if _, err := rs.Seek(0, io.SeekCurrent); err != nil {
|
|
| 562 |
+ ok = false // Not all io.Seeker can really seek |
|
| 563 |
+ } |
|
| 564 |
+ } |
|
| 565 |
+ if !ok {
|
|
| 566 |
+ return io.Copy(struct{ io.Writer }{sw}, r)
|
|
| 567 |
+ } |
|
| 568 |
+ |
|
| 569 |
+ var readLastByte bool |
|
| 570 |
+ pos0 := sw.pos |
|
| 571 |
+ for sw.LogicalRemaining() > 0 && !readLastByte && err == nil {
|
|
| 572 |
+ var nf int64 // Size of fragment |
|
| 573 |
+ dataStart, dataEnd := sw.sp[0].Offset, sw.sp[0].endOffset() |
|
| 574 |
+ if sw.pos < dataStart { // In a hole fragment
|
|
| 575 |
+ nf = dataStart - sw.pos |
|
| 576 |
+ if sw.PhysicalRemaining() == 0 {
|
|
| 577 |
+ readLastByte = true |
|
| 578 |
+ nf-- |
|
| 579 |
+ } |
|
| 580 |
+ _, err = rs.Seek(nf, io.SeekCurrent) |
|
| 581 |
+ } else { // In a data fragment
|
|
| 582 |
+ nf = dataEnd - sw.pos |
|
| 583 |
+ nf, err = io.CopyN(sw.fw, rs, nf) |
|
| 584 |
+ } |
|
| 585 |
+ sw.pos += nf |
|
| 586 |
+ if sw.pos >= dataEnd && len(sw.sp) > 1 {
|
|
| 587 |
+ sw.sp = sw.sp[1:] // Ensure last fragment always remains |
|
| 588 |
+ } |
|
| 589 |
+ } |
|
| 590 |
+ |
|
| 591 |
+ // If the last fragment is a hole, then seek to 1-byte before EOF, and |
|
| 592 |
+ // read a single byte to ensure the file is the right size. |
|
| 593 |
+ if readLastByte && err == nil {
|
|
| 594 |
+ _, err = mustReadFull(rs, []byte{0})
|
|
| 595 |
+ sw.pos++ |
|
| 596 |
+ } |
|
| 597 |
+ |
|
| 598 |
+ n = sw.pos - pos0 |
|
| 599 |
+ switch {
|
|
| 600 |
+ case err == io.EOF: |
|
| 601 |
+ return n, io.ErrUnexpectedEOF |
|
| 602 |
+ case err == ErrWriteTooLong: |
|
| 603 |
+ return n, errMissData // Not possible; implies bug in validation logic |
|
| 604 |
+ case err != nil: |
|
| 605 |
+ return n, err |
|
| 606 |
+ case sw.LogicalRemaining() == 0 && sw.PhysicalRemaining() > 0: |
|
| 607 |
+ return n, errUnrefData // Not possible; implies bug in validation logic |
|
| 608 |
+ default: |
|
| 609 |
+ return n, ensureEOF(rs) |
|
| 610 |
+ } |
|
| 611 |
+} |
|
| 612 |
+ |
|
| 613 |
+func (sw sparseFileWriter) LogicalRemaining() int64 {
|
|
| 614 |
+ return sw.sp[len(sw.sp)-1].endOffset() - sw.pos |
|
| 615 |
+} |
|
| 616 |
+func (sw sparseFileWriter) PhysicalRemaining() int64 {
|
|
| 617 |
+ return sw.fw.PhysicalRemaining() |
|
| 618 |
+} |
|
| 619 |
+ |
|
| 620 |
+// zeroWriter may only be written with NULs, otherwise it returns errWriteHole. |
|
| 621 |
+type zeroWriter struct{}
|
|
| 622 |
+ |
|
| 623 |
+func (zeroWriter) Write(b []byte) (int, error) {
|
|
| 624 |
+ for i, c := range b {
|
|
| 625 |
+ if c != 0 {
|
|
| 626 |
+ return i, errWriteHole |
|
| 627 |
+ } |
|
| 628 |
+ } |
|
| 629 |
+ return len(b), nil |
|
| 630 |
+} |
|
| 631 |
+ |
|
| 632 |
+// ensureEOF checks whether r is at EOF, reporting ErrWriteTooLong if not so. |
|
| 633 |
+func ensureEOF(r io.Reader) error {
|
|
| 634 |
+ n, err := tryReadFull(r, []byte{0})
|
|
| 635 |
+ switch {
|
|
| 636 |
+ case n > 0: |
|
| 637 |
+ return ErrWriteTooLong |
|
| 638 |
+ case err == io.EOF: |
|
| 639 |
+ return nil |
|
| 640 |
+ default: |
|
| 641 |
+ return err |
|
| 642 |
+ } |
|
| 643 |
+} |