tarutil: new package for working with tar archives

Will be used by the coordinator and gomote in future changes.

Change-Id: I57c8f268e60848c212831d14ba8b12b74a8f7a22
Reviewed-on: https://go-review.googlesource.com/3419
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-01-28 16:53:55 -06:00
Родитель 50ba0cb8f6
Коммит aef04bd899
2 изменённых файлов: 176 добавлений и 0 удалений

93
tarutil/tarutil.go Normal file
Просмотреть файл

@ -0,0 +1,93 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tarutil contains utilities for working with tar archives.
package tarutil
import (
"archive/tar"
"compress/gzip"
"errors"
"io"
)
// FileList is a list of entries in a tar archive which acts
// as a template to make .tar.gz io.Readers as needed.
//
// The zero value is a valid empty list.
//
// All entries must be added before calling OpenTarGz.
type FileList struct {
files []headerContent
}
type headerContent struct {
header *tar.Header
// For regularf files:
size int64
content io.ReaderAt
}
// Add adds a non-regular file to the FileList.
func (fl *FileList) Add(h *tar.Header) {
fl.files = append(fl.files, headerContent{header: h})
}
// AddRegular adds a regular file to the FileList.
func (fl *FileList) AddRegular(h *tar.Header, size int64, content io.ReaderAt) {
fl.files = append(fl.files, headerContent{
header: h,
size: size,
content: content,
})
}
// OpenTarGz returns an io.Reader of a gzip-compressed tar file
// containing the contents of the FileList.
// All Add calls must happen before OpenTarGz is called.
// Callers must call Close on the returned ReadCloser to release
// resources.
func (fl *FileList) OpenTarGz() io.ReadCloser {
pr, pw := io.Pipe()
go func() {
err := fl.writeTarGz(pw)
pw.CloseWithError(err)
}()
return struct {
io.Reader
io.Closer
}{
pr,
funcCloser(func() error {
pw.CloseWithError(errors.New("tarutil: .tar.gz generation aborted with Close"))
return nil
}),
}
}
func (fl *FileList) writeTarGz(w *io.PipeWriter) error {
zw := gzip.NewWriter(w)
tw := tar.NewWriter(zw)
for _, f := range fl.files {
if err := tw.WriteHeader(f.header); err != nil {
return err
}
if f.content != nil {
if _, err := io.CopyN(tw, io.NewSectionReader(f.content, 0, f.size), f.size); err != nil {
return err
}
}
}
if err := tw.Close(); err != nil {
return err
}
return zw.Close()
}
// funcCloser implements io.Closer with a function.
type funcCloser func() error
func (fn funcCloser) Close() error { return fn() }

83
tarutil/tarutil_test.go Normal file
Просмотреть файл

@ -0,0 +1,83 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tarutil
import (
"archive/tar"
"compress/gzip"
"io"
"io/ioutil"
"os"
"path"
"strings"
"testing"
"time"
)
// fileInfo is an os.FileInfo implementation for tarHeader.
type fileInfo struct {
name string
mode os.FileMode
size int64
target string // if symlink
}
func (fi fileInfo) IsDir() bool { return fi.mode.IsDir() }
func (fi fileInfo) ModTime() time.Time { return time.Time{} }
func (fi fileInfo) Mode() os.FileMode { return fi.mode }
func (fi fileInfo) Name() string { return path.Base(fi.name) }
func (fi fileInfo) Size() int64 { return fi.size }
func (fi fileInfo) Sys() interface{} { return nil }
func tarHeader(t *testing.T, fi fileInfo) *tar.Header {
h, err := tar.FileInfoHeader(fi, fi.target)
if err != nil {
t.Fatalf("tarHeader: %v", err)
}
h.Name = fi.name // see docs on tar.FileInfoHeader
return h
}
func TestFileList(t *testing.T) {
fl := new(FileList)
fl.Add(tarHeader(t, fileInfo{name: "symlink-file", target: "link-target", mode: 0644 | os.ModeSymlink}))
fl.AddRegular(tarHeader(t, fileInfo{name: "regular.txt", mode: 0644, size: 7}), 7, strings.NewReader("foo bar"))
tgz := fl.OpenTarGz()
defer tgz.Close()
zr, err := gzip.NewReader(tgz)
if err != nil {
t.Fatalf("gzip.NewReader: %v", err)
}
tr := tar.NewReader(zr)
saw := 0
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("tar.Reader.Next: %v", err)
}
saw++
switch h.Name {
case "symlink-file":
case "regular.txt":
all, err := ioutil.ReadAll(tr)
if err != nil {
t.Fatalf("Reading regular.txt: %v", err)
}
if string(all) != "foo bar" {
t.Errorf("regular.txt = %q; want \"foo bar\"", all)
}
default:
t.Fatalf("unknown name %q", h.Name)
}
}
if saw != 2 {
t.Errorf("number of entries = %d; want 2", saw)
}
}