diff options
author | Flavio Castelli <fcastelli@suse.com> | 2017-08-11 16:46:21 +0200 |
---|---|---|
committer | Flavio Castelli <fcastelli@suse.com> | 2017-08-11 16:46:21 +0200 |
commit | 47fa4efff290518b756a489da7be556d58d25688 (patch) | |
tree | 28c05a1240eb88d778484f166cf3c48c8e9162aa /libvirt | |
parent | 7fbe405ecb15e4ab22da4a74368aafcc6b0c2ea8 (diff) | |
download | terraform-provider-libvirt-47fa4efff290518b756a489da7be556d58d25688.tar terraform-provider-libvirt-47fa4efff290518b756a489da7be556d58d25688.tar.gz |
Ensure libvirt stream are cleaned up properly
We have to `free()` the stream once they are no longer used.
Diffstat (limited to 'libvirt')
-rw-r--r-- | libvirt/utils_volume.go | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/libvirt/utils_volume.go b/libvirt/utils_volume.go index 289ded4e..1c97cff7 100644 --- a/libvirt/utils_volume.go +++ b/libvirt/utils_volume.go @@ -135,21 +135,31 @@ func newImage(source string) (image, error) { func newCopier(virConn *libvirt.Connect, volume *libvirt.StorageVol, size uint64) func(src io.Reader) error { copier := func(src io.Reader) error { + var bytesCopied int64 + stream, err := virConn.NewStream(0) if err != nil { return err } - defer stream.Finish() + + defer func() { + if uint64(bytesCopied) != size { + stream.Abort() + } else { + stream.Finish() + } + stream.Free() + }() volume.Upload(stream, 0, size, 0) sio := NewStreamIO(*stream) - n, err := io.Copy(sio, src) + bytesCopied, err = io.Copy(sio, src) if err != nil { return err } - log.Printf("%d bytes uploaded\n", n) + log.Printf("%d bytes uploaded\n", bytesCopied) return nil } return copier |