Skip to content

Commit

Permalink
archive/tar: catch short writes.
Browse files Browse the repository at this point in the history
Also make error messages consistent throughout.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5777064
  • Loading branch information
dsymonds committed Mar 12, 2012
1 parent ac0789c commit d75abb7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/pkg/archive/tar/reader.go
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
ErrHeader = errors.New("invalid tar header")
ErrHeader = errors.New("archive/tar: invalid tar header")
)

// A Reader provides sequential access to the contents of a tar archive.
Expand Down
17 changes: 13 additions & 4 deletions src/pkg/archive/tar/writer.go
Expand Up @@ -5,18 +5,19 @@
package tar

// TODO(dsymonds):
// - catch more errors (no first header, write after close, etc.)
// - catch more errors (no first header, etc.)

import (
"errors"
"fmt"
"io"
"strconv"
)

var (
ErrWriteTooLong = errors.New("write too long")
ErrFieldTooLong = errors.New("header field too long")
ErrWriteAfterClose = errors.New("write after close")
ErrWriteTooLong = errors.New("archive/tar: write too long")
ErrFieldTooLong = errors.New("archive/tar: header field too long")
ErrWriteAfterClose = errors.New("archive/tar: write after close")
)

// A Writer provides sequential writing of a tar archive in POSIX.1 format.
Expand Down Expand Up @@ -48,6 +49,11 @@ func NewWriter(w io.Writer) *Writer { return &Writer{w: w} }

// Flush finishes writing the current file (optional).
func (tw *Writer) Flush() error {
if tw.nb > 0 {
tw.err = fmt.Errorf("archive/tar: missed writing %d bytes", tw.nb)
return tw.err
}

n := tw.nb + tw.pad
for n > 0 && tw.err == nil {
nr := n
Expand Down Expand Up @@ -193,6 +199,9 @@ func (tw *Writer) Close() error {
}
tw.Flush()
tw.closed = true
if tw.err != nil {
return tw.err
}

// trailer: two zero blocks
for i := 0; i < 2; i++ {
Expand Down
9 changes: 7 additions & 2 deletions src/pkg/archive/tar/writer_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"testing/iotest"
"time"
Expand Down Expand Up @@ -95,7 +96,8 @@ var writerTests = []*writerTest{
Uname: "dsymonds",
Gname: "eng",
},
// no contents
// fake contents
contents: strings.Repeat("\x00", 4<<10),
},
},
},
Expand Down Expand Up @@ -150,7 +152,9 @@ testLoop:

buf := new(bytes.Buffer)
tw := NewWriter(iotest.TruncateWriter(buf, 4<<10)) // only catch the first 4 KB
big := false
for j, entry := range test.entries {
big = big || entry.header.Size > 1<<10
if err := tw.WriteHeader(entry.header); err != nil {
t.Errorf("test %d, entry %d: Failed writing header: %v", i, j, err)
continue testLoop
Expand All @@ -160,7 +164,8 @@ testLoop:
continue testLoop
}
}
if err := tw.Close(); err != nil {
// Only interested in Close failures for the small tests.
if err := tw.Close(); err != nil && !big {
t.Errorf("test %d: Failed closing archive: %v", i, err)
continue testLoop
}
Expand Down

0 comments on commit d75abb7

Please sign in to comment.