Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #878 from obedmr/issue-661
Browse files Browse the repository at this point in the history
ciao-image: Fix open file issue on posix write ops
  • Loading branch information
markdryan authored Nov 30, 2016
2 parents ecd62f5 + bc0b292 commit f97db51
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
16 changes: 8 additions & 8 deletions ciao-image/datastore/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion ciao-image/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 12 additions & 4 deletions ciao-image/datastore/posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ciao-image/datastore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f97db51

Please sign in to comment.