Writing data out with error handling

master
Frederick F. Kautz IV 10 years ago
parent 97b2e7fb3b
commit 8129d8fd0b
  1. 54
      pkg/storage/donut/v1/donut.go

@ -18,6 +18,7 @@ package v1
import ( import (
"encoding/binary" "encoding/binary"
"errors"
"io" "io"
) )
@ -97,18 +98,45 @@ func (donut *Donut) Write(gobHeader GobHeader, object io.Reader) error {
} }
func (donut *Donut) WriteFormat(target io.Writer, donutFormat DonutFormat) error { func (donut *Donut) WriteFormat(target io.Writer, donutFormat DonutFormat) error {
err := binary.Write(target, binary.LittleEndian, donutFormat.BlockStart) if err := binary.Write(target, binary.LittleEndian, donutFormat.BlockStart); err != nil {
err = binary.Write(target, binary.LittleEndian, donutFormat.VersionMajor) return err
err = binary.Write(target, binary.LittleEndian, donutFormat.VersionMinor) }
err = binary.Write(target, binary.LittleEndian, donutFormat.VersionPatch) if err := binary.Write(target, binary.LittleEndian, donutFormat.VersionMajor); err != nil {
err = binary.Write(target, binary.LittleEndian, donutFormat.VersionReserved) return err
err = binary.Write(target, binary.LittleEndian, donutFormat.Reserved) }
err = binary.Write(target, binary.LittleEndian, donutFormat.GobHeaderLen) if err := binary.Write(target, binary.LittleEndian, donutFormat.VersionMinor); err != nil {
err = binary.Write(target, binary.LittleEndian, donutFormat.GobHeader) return err
err = binary.Write(target, binary.LittleEndian, donutFormat.BlockData) }
err = binary.Write(target, binary.LittleEndian, donutFormat.Data) if err := binary.Write(target, binary.LittleEndian, donutFormat.VersionPatch); err != nil {
err = binary.Write(target, binary.LittleEndian, donutFormat.BlockLen) return err
err = binary.Write(target, binary.LittleEndian, donutFormat.BlockEnd) }
if err := binary.Write(target, binary.LittleEndian, donutFormat.VersionReserved); err != nil {
return err
}
if err := binary.Write(target, binary.LittleEndian, donutFormat.Reserved); err != nil {
return err
}
if err := binary.Write(target, binary.LittleEndian, donutFormat.GobHeaderLen); err != nil {
return err
}
if err := binary.Write(target, binary.LittleEndian, donutFormat.GobHeader); err != nil {
return err
}
if err := binary.Write(target, binary.LittleEndian, donutFormat.BlockData); err != nil {
return err
}
if count, err := io.Copy(target, donutFormat.Data); uint64(count) != donutFormat.BlockLen || err != nil {
if err == nil {
return err
}
return errors.New("Copy failed, count incorrect.")
}
if err := binary.Write(target, binary.LittleEndian, donutFormat.BlockLen); err != nil {
return err
}
if err := binary.Write(target, binary.LittleEndian, donutFormat.BlockEnd); err != nil {
return err
}
return err return nil
} }

Loading…
Cancel
Save