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

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

Issue 162062: code review 162062: Robustness fixes for tar/archive. (Closed)
Left Patch Set: code review 162062: Robustness fixes for tar/archive. Created 15 years, 3 months ago
Right Patch Set: code review 162062: Robustness fixes for tar/archive. Created 15 years, 3 months 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/reader_test.go ('k') | no next file » | 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, write after close, etc.) 8 // - catch more errors (no first header, write after close, etc.)
9 9
10 import ( 10 import (
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 return tw.err 160 return tw.err
161 } 161 }
162 162
163 _, tw.err = tw.w.Write(header); 163 _, tw.err = tw.w.Write(header);
164 164
165 return tw.err; 165 return tw.err;
166 } 166 }
167 167
168 // Write writes to the current entry in the tar archive. 168 // Write writes to the current entry in the tar archive.
169 // Write returns the error ErrWriteTooLong if more than 169 // Write returns the error ErrWriteTooLong if more than
170 // hdr.Size bytes are written after WriteHeader. Writes 170 // hdr.Size bytes are written after WriteHeader.
171 // after Close will return ErrWriteTooLong.
rsc 2009/12/11 20:48:31 drop this sentence - it's wrong and can be left un
172 func (tw *Writer) Write(b []byte) (n int, err os.Error) { 171 func (tw *Writer) Write(b []byte) (n int, err os.Error) {
173 if tw.closed { 172 if tw.closed {
174 err = ErrWriteTooLong; 173 err = ErrWriteTooLong;
175 return; 174 return;
176 } 175 }
177 overwrite := false; 176 overwrite := false;
178 if int64(len(b)) > tw.nb { 177 if int64(len(b)) > tw.nb {
179 b = b[0:tw.nb]; 178 b = b[0:tw.nb];
180 overwrite = true; 179 overwrite = true;
181 } 180 }
182 n, err = tw.w.Write(b); 181 n, err = tw.w.Write(b);
183 tw.nb -= int64(n); 182 tw.nb -= int64(n);
184 if err == nil && overwrite { 183 if err == nil && overwrite {
185 err = ErrWriteTooLong; 184 err = ErrWriteTooLong;
186 return; 185 return;
187 } 186 }
188 tw.err = err; 187 tw.err = err;
189 return; 188 return;
190 } 189 }
191 190
192 // Close finishes writing to the tar archive. Trailing blocks are 191 // Close closes the tar archive, flushing any unwritten
rsc 2009/12/11 20:48:31 // Close closes the tar archive, flushing any unwr
193 // written out and any further writes will fail. 192 // data to the underlying writer.
194 func (tw *Writer) Close() os.Error { 193 func (tw *Writer) Close() os.Error {
195 if tw.err != nil || tw.closed { 194 if tw.err != nil || tw.closed {
196 return tw.err 195 return tw.err
197 } 196 }
198 tw.Flush(); 197 tw.Flush();
199 tw.closed = true; 198 tw.closed = true;
200 199
201 // trailer: two zero blocks 200 // trailer: two zero blocks
202 for i := 0; i < 2; i++ { 201 for i := 0; i < 2; i++ {
203 _, tw.err = tw.w.Write(zeroBlock); 202 _, tw.err = tw.w.Write(zeroBlock);
204 if tw.err != nil { 203 if tw.err != nil {
205 break 204 break
206 } 205 }
207 } 206 }
208 return tw.err; 207 return tw.err;
209 } 208 }
LEFTRIGHT

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