Seek to beginning for tempbuffer

This commit is contained in:
Frederick F. Kautz IV 2015-02-27 12:18:08 -08:00
parent da10fd5a83
commit be768d01a2
2 changed files with 14 additions and 8 deletions

View File

@ -22,6 +22,8 @@ import (
"encoding/gob" "encoding/gob"
"errors" "errors"
"io" "io"
"io/ioutil"
"os"
"sync" "sync"
"github.com/minio-io/minio/pkg/storage/erasure" "github.com/minio-io/minio/pkg/storage/erasure"
@ -206,26 +208,30 @@ func (donut *Donut) Write(gobHeader GobHeader, object io.Reader) error {
BlockEnd: MagicINIM, BlockEnd: MagicINIM,
} }
var tempBuffer bytes.Buffer tempBuffer, err := ioutil.TempFile(os.TempDir(), "minio-staging")
if err != nil {
return err
}
defer os.Remove(tempBuffer.Name())
// write header // write header
if err := donut.WriteBegin(&tempBuffer, donutFormat); err != nil { if err := donut.WriteBegin(tempBuffer, donutFormat); err != nil {
return err return err
} }
// write data // write data
if err := donut.WriteData(&tempBuffer, donutFormat); err != nil { if err := donut.WriteData(tempBuffer, donutFormat); err != nil {
return err return err
} }
// write footer crc // write footer crc
if err := donut.WriteEnd(&tempBuffer, donutFormat); err != nil { if err := donut.WriteEnd(tempBuffer, donutFormat); err != nil {
return err return err
} }
// write footer // write footer
donut.file.Seek(0, 2) donut.file.Seek(0, 2)
io.Copy(donut.file, &tempBuffer) tempBuffer.Seek(0, 0)
io.Copy(donut.file, tempBuffer)
return nil return nil
} }

View File

@ -19,7 +19,6 @@ package v1
import ( import (
"bytes" "bytes"
. "gopkg.in/check.v1" . "gopkg.in/check.v1"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
@ -32,10 +31,11 @@ type MySuite struct{}
var _ = Suite(&MySuite{}) var _ = Suite(&MySuite{})
func (s *MySuite) TestAPISuite(c *C) { func (s *MySuite) TestAPISuite(c *C) {
var b io.ReadWriteSeeker //var b io.ReadWriteSeeker
var o bytes.Buffer var o bytes.Buffer
b, err := ioutil.TempFile(os.TempDir(), "minio-donut-test") b, err := ioutil.TempFile(os.TempDir(), "minio-donut-test")
defer os.RemoveAll(b.Name())
c.Assert(err, IsNil) c.Assert(err, IsNil)
donut := New(b) donut := New(b)