提交 e734b5be 编写于 作者: T Thomas Stromberg

Avoid calling Read() on 0-byte resources, and add copious debugging.

上级 d46decfb
......@@ -18,11 +18,13 @@ package assets
import (
"bytes"
"fmt"
"html/template"
"io"
"os"
"path"
"github.com/golang/glog"
"github.com/pkg/errors"
)
......@@ -197,6 +199,10 @@ func (m *BinDataAsset) loadData(isTemplate bool) error {
m.data = contents
m.Length = len(contents)
m.reader = bytes.NewReader(m.data)
glog.Infof("Created asset %s with %d bytes", m.AssetName, m.Length)
if m.Length == 0 {
return fmt.Errorf("%s is an empty asset", m.AssetName)
}
return nil
}
......@@ -227,5 +233,8 @@ func (m *BinDataAsset) GetLength() int {
// Read reads the asset
func (m *BinDataAsset) Read(p []byte) (int, error) {
if m.Length == 0 {
return 0, fmt.Errorf("attempted read from a 0 length asset")
}
return m.reader.Read(p)
}
......@@ -173,12 +173,30 @@ func (s *SSHRunner) Copy(f assets.CopyableFile) error {
// StdinPipe is closed. But let's use a WaitGroup to make it expicit.
var wg sync.WaitGroup
wg.Add(1)
var ierr error
var copied int64
go func() {
defer wg.Done()
defer w.Close()
glog.Infof("Transferring %d bytes to %s", f.GetLength(), f.GetTargetName())
header := fmt.Sprintf("C%s %d %s\n", f.GetPermissions(), f.GetLength(), f.GetTargetName())
fmt.Fprint(w, header)
io.Copy(w, f)
if f.GetLength() == 0 {
glog.Warningf("%s is a 0 byte asset!", f.GetTargetName())
fmt.Fprint(w, "\x00")
return
}
copied, ierr = io.Copy(w, f)
if copied != int64(f.GetLength()) {
glog.Warningf("%s: expected to copy %d bytes, but copied %d instead", f.GetTargetName(), f.GetLength(), copied)
} else {
glog.Infof("%s: copied %d bytes", f.GetTargetName(), copied)
}
if ierr != nil {
glog.Errorf("io.Copy failed: %v", ierr)
}
fmt.Fprint(w, "\x00")
}()
......@@ -187,5 +205,5 @@ func (s *SSHRunner) Copy(f assets.CopyableFile) error {
return err
}
wg.Wait()
return nil
return ierr
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册