Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(136)

Delta Between Two Patch Sets: src/pkg/archive/tar/writer.go

Issue 6700047: code review 6700047: archive/tar: read/write extended pax/gnu tar archives (Closed)
Left Patch Set: diff -r 93dc7f0e302b https://code.google.com/p/go Created 11 years, 5 months ago
Right Patch Set: diff -r 439cb8bad388 https://code.google.com/p/go Created 11 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/archive/tar/testdata/ustar.tar ('k') | src/pkg/archive/tar/writer_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package tar 5 package tar
6 6
7 // TODO(dsymonds): 7 // TODO(dsymonds):
8 // - catch more errors (no first header, etc.) 8 // - catch more errors (no first header, etc.)
9 9
10 import ( 10 import (
11 "bytes" 11 "bytes"
12 "errors" 12 "errors"
13 "fmt" 13 "fmt"
14 "io" 14 "io"
15 "os" 15 "os"
16 » "path/filepath" 16 » "path"
17 "strconv" 17 "strconv"
18 "strings"
19 "time"
18 ) 20 )
19 21
20 var ( 22 var (
21 ErrWriteTooLong = errors.New("archive/tar: write too long") 23 ErrWriteTooLong = errors.New("archive/tar: write too long")
22 ErrFieldTooLong = errors.New("archive/tar: header field too long") 24 ErrFieldTooLong = errors.New("archive/tar: header field too long")
23 ErrWriteAfterClose = errors.New("archive/tar: write after close") 25 ErrWriteAfterClose = errors.New("archive/tar: write after close")
26 errNameTooLong = errors.New("archive/tar: name too long")
24 ) 27 )
25 28
26 // A Writer provides sequential writing of a tar archive in POSIX.1 format. 29 // A Writer provides sequential writing of a tar archive in POSIX.1 format.
27 // A tar archive consists of a sequence of files. 30 // A tar archive consists of a sequence of files.
28 // Call WriteHeader to begin a new file, and then call Write to supply that file 's data, 31 // Call WriteHeader to begin a new file, and then call Write to supply that file 's data,
29 // writing at most hdr.Size bytes in total. 32 // writing at most hdr.Size bytes in total.
30 //
31 // Example:
32 // tw := tar.NewWriter(w)
33 // hdr := new(tar.Header)
34 // hdr.Size = length of data in bytes
35 // // populate other hdr fields as desired
36 // if err := tw.WriteHeader(hdr); err != nil {
37 // // handle error
38 // }
39 // io.Copy(tw, data)
40 // tw.Close()
41 type Writer struct { 33 type Writer struct {
42 w io.Writer 34 w io.Writer
43 err error 35 err error
44 nb int64 // number of unwritten bytes for current file entry 36 nb int64 // number of unwritten bytes for current file entry
45 pad int64 // amount of padding to write after current file entry 37 pad int64 // amount of padding to write after current file entry
46 closed bool 38 closed bool
47 usedBinary bool // whether the binary numeric field extension was used 39 usedBinary bool // whether the binary numeric field extension was used
48 } 40 }
49 41
50 // NewWriter creates a new Writer writing to w. 42 // NewWriter creates a new Writer writing to w.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 } 98 }
107 // Too big: use binary (big-endian). 99 // Too big: use binary (big-endian).
108 tw.usedBinary = true 100 tw.usedBinary = true
109 for i := len(b) - 1; x > 0 && i >= 0; i-- { 101 for i := len(b) - 1; x > 0 && i >= 0; i-- {
110 b[i] = byte(x) 102 b[i] = byte(x)
111 x >>= 8 103 x >>= 8
112 } 104 }
113 b[0] |= 0x80 // highest bit indicates binary format 105 b[0] |= 0x80 // highest bit indicates binary format
114 } 106 }
115 107
108 var (
109 minTime = time.Unix(0, 0)
110 // There is room for 11 octal digits (33 bits) of mtime.
111 maxTime = minTime.Add((1<<33 - 1) * time.Second)
112 )
113
116 // WriteHeader writes hdr and prepares to accept the file's contents. 114 // WriteHeader writes hdr and prepares to accept the file's contents.
117 // WriteHeader calls Flush if it is not the first header. 115 // WriteHeader calls Flush if it is not the first header.
118 // Calling after a Close will return ErrWriteAfterClose. 116 // Calling after a Close will return ErrWriteAfterClose.
119 func (tw *Writer) WriteHeader(hdr *Header) error { 117 func (tw *Writer) WriteHeader(hdr *Header) error {
120 if tw.closed { 118 if tw.closed {
121 return ErrWriteAfterClose 119 return ErrWriteAfterClose
122 } 120 }
123 if tw.err == nil { 121 if tw.err == nil {
124 tw.Flush() 122 tw.Flush()
125 } 123 }
126 if tw.err != nil { 124 if tw.err != nil {
127 return tw.err 125 return tw.err
128 } 126 }
129 // Decide whether or not to use PAX extensions 127 // Decide whether or not to use PAX extensions
130 // TODO(shanemhansen): we might want to use PAX headers for 128 // TODO(shanemhansen): we might want to use PAX headers for
131 // subsecond time resolution, but for now let's just capture 129 // subsecond time resolution, but for now let's just capture
132 // the long name/long symlink use case. 130 // the long name/long symlink use case.
133 » if len(hdr.Name) > 100 || len(hdr.Linkname) > 100 { 131 » suffix := hdr.Name
134 » » if err := tw.writePAXHeader(hdr); err != nil { 132 » prefix := ""
133 » if len(hdr.Name) > fileNameSize || len(hdr.Linkname) > fileNameSize {
134 » » var err error
135 » » prefix, suffix, err = tw.splitUSTARLongName(hdr.Name)
136 » » // Either we were unable to pack the long name into ustar format
137 » » // or the link name is too long; use PAX headers.
138 » » if err == errNameTooLong || len(hdr.Linkname) > fileNameSize {
139 » » » if err := tw.writePAXHeader(hdr); err != nil {
140 » » » » return err
141 » » » }
142 » » } else if err != nil {
135 return err 143 return err
136 } 144 }
137 } 145 }
138 tw.nb = int64(hdr.Size) 146 tw.nb = int64(hdr.Size)
139 tw.pad = -tw.nb & (blockSize - 1) // blockSize is a power of two 147 tw.pad = -tw.nb & (blockSize - 1) // blockSize is a power of two
140 148
141 header := make([]byte, blockSize) 149 header := make([]byte, blockSize)
142 s := slicer(header) 150 s := slicer(header)
143 » copy(s.next(100), []byte(hdr.Name)) 151 » tw.cString(s.next(fileNameSize), suffix)
144 152
145 » tw.octal(s.next(8), hdr.Mode) // 100:108 153 » // Handle out of range ModTime carefully.
146 » tw.numeric(s.next(8), int64(hdr.Uid)) // 108:116 154 » var modTime int64
147 » tw.numeric(s.next(8), int64(hdr.Gid)) // 116:124 155 » if !hdr.ModTime.Before(minTime) && !hdr.ModTime.After(maxTime) {
148 » tw.numeric(s.next(12), hdr.Size) // 124:136 156 » » modTime = hdr.ModTime.Unix()
149 » tw.numeric(s.next(12), hdr.ModTime.Unix()) // 136:148 157 » }
150 » s.next(8) // chksum (148:156) 158
151 » s.next(1)[0] = hdr.Typeflag // 156:157 159 » tw.octal(s.next(8), hdr.Mode) // 100:108
152 » tw.cString(s.next(100), hdr.Linkname) // linkname (157:257) 160 » tw.numeric(s.next(8), int64(hdr.Uid)) // 108:116
153 » copy(s.next(8), []byte("ustar\x0000")) // 257:265 161 » tw.numeric(s.next(8), int64(hdr.Gid)) // 116:124
154 » tw.cString(s.next(32), hdr.Uname) // 265:297 162 » tw.numeric(s.next(12), hdr.Size) // 124:136
155 » tw.cString(s.next(32), hdr.Gname) // 297:329 163 » tw.numeric(s.next(12), modTime) // 136:148
156 » tw.numeric(s.next(8), hdr.Devmajor) // 329:337 164 » s.next(8) // chksum (148:156)
157 » tw.numeric(s.next(8), hdr.Devminor) // 337:345 165 » s.next(1)[0] = hdr.Typeflag // 156:157
158 166 » tw.cString(s.next(100), hdr.Linkname) // linkname (157:257)
167 » copy(s.next(8), []byte("ustar\x0000")) // 257:265
168 » tw.cString(s.next(32), hdr.Uname) // 265:297
169 » tw.cString(s.next(32), hdr.Gname) // 297:329
170 » tw.numeric(s.next(8), hdr.Devmajor) // 329:337
171 » tw.numeric(s.next(8), hdr.Devminor) // 337:345
172 » tw.cString(s.next(155), prefix) // 345:500
159 // Use the GNU magic instead of POSIX magic if we used any GNU extension s. 173 // Use the GNU magic instead of POSIX magic if we used any GNU extension s.
160 if tw.usedBinary { 174 if tw.usedBinary {
161 copy(header[257:265], []byte("ustar \x00")) 175 copy(header[257:265], []byte("ustar \x00"))
176 }
177 // Use the ustar magic if we used ustar long names.
178 if len(prefix) > 0 {
179 copy(header[257:265], []byte("ustar\000"))
162 } 180 }
163 181
164 // The chksum field is terminated by a NUL and a space. 182 // The chksum field is terminated by a NUL and a space.
165 // This is different from the other octal fields. 183 // This is different from the other octal fields.
166 chksum, _ := checksum(header) 184 chksum, _ := checksum(header)
167 tw.octal(header[148:155], chksum) 185 tw.octal(header[148:155], chksum)
168 header[155] = ' ' 186 header[155] = ' '
169 187
170 if tw.err != nil { 188 if tw.err != nil {
171 // problem with header; probably integer too big for a field. 189 // problem with header; probably integer too big for a field.
172 return tw.err 190 return tw.err
173 } 191 }
174 192
175 _, tw.err = tw.w.Write(header) 193 _, tw.err = tw.w.Write(header)
176 194
177 return tw.err 195 return tw.err
178 } 196 }
179 197
198 // writeUSTARLongName splits a USTAR long name hdr.Name.
199 // name must be < 256 characters. errNameTooLong is returned
200 // if hdr.Name can't be split. The splitting heuristic
201 // is compatible with gnu tar.
202 func (tw *Writer) splitUSTARLongName(name string) (prefix, suffix string, err er ror) {
203 length := len(name)
204 if length > fileNamePrefixSize+1 {
205 length = fileNamePrefixSize + 1
206 } else if name[length-1] == '/' {
207 length--
208 }
209 i := strings.LastIndex(name[:length], "/")
210 nlen := length - i - 1
211 if i <= 0 || nlen > fileNameSize || nlen == 0 {
212 err = errNameTooLong
213 return
214 }
215 prefix, suffix = name[:i], name[i+1:]
216 return
217 }
218
180 // writePaxHeader writes an extended pax header to the 219 // writePaxHeader writes an extended pax header to the
181 // archive. 220 // archive.
182 func (tw *Writer) writePAXHeader(hdr *Header) error { 221 func (tw *Writer) writePAXHeader(hdr *Header) error {
183 // Prepare extended header 222 // Prepare extended header
184 » extendedHdr := new(Header) 223 » ext := new(Header)
dsymonds 2012/10/31 12:16:25 extendedHdr -> ext
185 » extendedHdr.Typeflag = TypeXHeader 224 » ext.Typeflag = TypeXHeader
186 // Setting ModTime is required for reader parsing to 225 // Setting ModTime is required for reader parsing to
187 // succeed, and seems harmless enough. 226 // succeed, and seems harmless enough.
188 » extendedHdr.ModTime = hdr.ModTime 227 » ext.ModTime = hdr.ModTime
189 » // The spec asks that we namespace our psuedo files 228 » // The spec asks that we namespace our pseudo files
dsymonds 2012/10/31 12:16:25 "pseudo"
190 // with the current pid. 229 // with the current pid.
191 pid := os.Getpid() 230 pid := os.Getpid()
192 » dir, file := filepath.Split(hdr.Name) 231 » dir, file := path.Split(hdr.Name)
193 » extendedHdr.Name = filepath.Join(dir, 232 » ext.Name = path.Join(dir,
194 fmt.Sprintf("PaxHeaders.%d", pid), file)[0:100] 233 fmt.Sprintf("PaxHeaders.%d", pid), file)[0:100]
195 // Construct the body 234 // Construct the body
196 var buf bytes.Buffer 235 var buf bytes.Buffer
197 » if len(hdr.Name) > 100 { 236 » if len(hdr.Name) > fileNameSize {
198 » » msg := fmt.Sprintf(" path=%s\n", hdr.Name) 237 » » fmt.Fprint(&buf, paxHeader("path="+hdr.Name))
199 » » size := len(msg) + 1 238 » }
200 » » length := string(size) 239 » if len(hdr.Linkname) > fileNameSize {
201 » » size += len(length) 240 » » fmt.Fprint(&buf, paxHeader("linkpath="+hdr.Linkname))
202 » » fmt.Fprintf(&buf, "%d%s", size, msg) 241 » }
dsymonds 2012/10/31 12:16:25 I am confused by this construction. Shouldn't it b
shanemhansen 2012/11/01 02:40:57 The pax standard is weird here. It states that the
203 » } 242 » ext.Size = int64(len(buf.Bytes()))
204 » if len(hdr.Linkname) > 100 { 243 » if err := tw.WriteHeader(ext); err != nil {
205 » » msg := fmt.Sprintf(" linkpath=%s\n", hdr.Name)
206 » » size := len(msg) + 1
207 » » length := string(size)
208 » » size += len(length)
209 » » fmt.Fprintf(&buf, "%d%s", size, msg)
210 » }
211 » extendedHdr.Size = int64(len(buf.Bytes()))
212 » err := tw.WriteHeader(extendedHdr)
dsymonds 2012/10/31 12:16:25 snuggle these where possible. if err := tw.Write
shanemhansen 2012/11/01 02:40:57 Done.
213 » if err != nil {
214 return err 244 return err
215 } 245 }
216 » _, err = tw.Write(buf.Bytes()) 246 » if _, err := tw.Write(buf.Bytes()); err != nil {
217 » if err != nil {
218 return err 247 return err
219 } 248 }
220 » err = tw.Flush() 249 » if err := tw.Flush(); err != nil {
221 » if err != nil {
222 return err 250 return err
223 } 251 }
224 return nil 252 return nil
253 }
254
255 // paxHeader formats a single pax record, prefixing it with the appropriate leng th
256 func paxHeader(msg string) string {
257 const padding = 2 // Extra padding for space and newline
258 size := len(msg) + padding
259 size += len(strconv.Itoa(size))
260 record := fmt.Sprintf("%d %s\n", size, msg)
261 if len(record) != size {
262 // Final adjustment if adding size increased
263 // the number of digits in size
264 size = len(record)
265 record = fmt.Sprintf("%d %s\n", size, msg)
266 }
267 return record
225 } 268 }
226 269
227 // Write writes to the current entry in the tar archive. 270 // Write writes to the current entry in the tar archive.
228 // Write returns the error ErrWriteTooLong if more than 271 // Write returns the error ErrWriteTooLong if more than
229 // hdr.Size bytes are written after WriteHeader. 272 // hdr.Size bytes are written after WriteHeader.
230 func (tw *Writer) Write(b []byte) (n int, err error) { 273 func (tw *Writer) Write(b []byte) (n int, err error) {
231 if tw.closed { 274 if tw.closed {
232 err = ErrWriteTooLong 275 err = ErrWriteTooLong
233 return 276 return
234 } 277 }
(...skipping 26 matching lines...) Expand all
261 304
262 // trailer: two zero blocks 305 // trailer: two zero blocks
263 for i := 0; i < 2; i++ { 306 for i := 0; i < 2; i++ {
264 _, tw.err = tw.w.Write(zeroBlock) 307 _, tw.err = tw.w.Write(zeroBlock)
265 if tw.err != nil { 308 if tw.err != nil {
266 break 309 break
267 } 310 }
268 } 311 }
269 return tw.err 312 return tw.err
270 } 313 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b