diff --git a/ciao-image/datastore/ceph.go b/ciao-image/datastore/ceph.go index 2c44f94c5..e414179d5 100644 --- a/ciao-image/datastore/ceph.go +++ b/ciao-image/datastore/ceph.go @@ -31,38 +31,38 @@ type Ceph struct { // Write copies an image onto the fileystem into a tempory location and uploads // it into ceph, snapshots. -func (c *Ceph) Write(ID string, body io.Reader) (int64, error) { +func (c *Ceph) Write(ID string, body io.Reader) error { image, err := ioutil.TempFile("", "ciao-image") if err != nil { - return 0, fmt.Errorf("Error creating temporary image file: %v", err) + return fmt.Errorf("Error creating temporary image file: %v", err) } defer os.Remove(image.Name()) // TODO(rbradford): Is there a better way buf := make([]byte, 1<<16) - res, err := io.CopyBuffer(image, body, buf) + _, err = io.CopyBuffer(image, body, buf) if err != nil { image.Close() - return 0, fmt.Errorf("Error writing to temporary image file: %v", err) + return fmt.Errorf("Error writing to temporary image file: %v", err) } err = image.Close() if err != nil { - return 0, fmt.Errorf("Error closing temporary image file: %v", err) + return fmt.Errorf("Error closing temporary image file: %v", err) } _, err = c.BlockDriver.CreateBlockDevice(ID, image.Name(), 0) if err != nil { - return 0, fmt.Errorf("Error creating block device: %v", err) + return fmt.Errorf("Error creating block device: %v", err) } err = c.BlockDriver.CreateBlockDeviceSnapshot(ID, "ciao-image") if err != nil { c.BlockDriver.DeleteBlockDevice(ID) - return 0, fmt.Errorf("Unable to create snapshot: %v", err) + return fmt.Errorf("Unable to create snapshot: %v", err) } - return res, nil + return nil } // Delete removes an image from ceph after deleting the snapshot. diff --git a/ciao-image/datastore/datastore.go b/ciao-image/datastore/datastore.go index 943a7c5cc..fc2f8b732 100644 --- a/ciao-image/datastore/datastore.go +++ b/ciao-image/datastore/datastore.go @@ -104,6 +104,6 @@ type MetaDataStore interface { // RawDataStore is the raw data storage interface that's used by the // image cache implementation. type RawDataStore interface { - Write(ID string, body io.Reader) (int64, error) + Write(ID string, body io.Reader) error Delete(ID string) error } diff --git a/ciao-image/datastore/posix.go b/ciao-image/datastore/posix.go index 4c976b728..ba3902873 100644 --- a/ciao-image/datastore/posix.go +++ b/ciao-image/datastore/posix.go @@ -27,20 +27,28 @@ type Posix struct { } // Write copies an image into the posix filesystem. -func (p *Posix) Write(ID string, body io.Reader) (int64, error) { +func (p *Posix) Write(ID string, body io.Reader) (err error) { imageName := path.Join(p.MountPoint, ID) if _, err := os.Stat(imageName); !os.IsNotExist(err) { - return 0, fmt.Errorf("image already uploaded with that ID") + return fmt.Errorf("image already uploaded with that ID") } image, err := os.Create(imageName) if err != nil { - return 0, err + return err } buf := make([]byte, 1<<16) - return io.CopyBuffer(image, body, buf) + _, err = io.CopyBuffer(image, body, buf) + defer func() { + err1 := image.Close() + if err == nil { + err = err1 + } + }() + + return err } // Delete removes an image from the posix filesystem diff --git a/ciao-image/datastore/store.go b/ciao-image/datastore/store.go index de5c263a1..a6896df32 100644 --- a/ciao-image/datastore/store.go +++ b/ciao-image/datastore/store.go @@ -159,7 +159,7 @@ func (s *ImageStore) UploadImage(ID string, body io.Reader) error { img.State = Saving if s.rawDs != nil { - _, err := s.rawDs.Write(ID, body) + err := s.rawDs.Write(ID, body) if err != nil { return err }