-
Notifications
You must be signed in to change notification settings - Fork 4
/
volume_upload.go
61 lines (48 loc) · 1.25 KB
/
volume_upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package volumes
import (
"io"
"libvirt.org/go/libvirt"
)
// VolumeUpload contains source and destination for the upload operation
type VolumeUpload struct {
streamSrc io.ReadCloser
streamDst *libvirt.Stream
}
// NewVolumeUploadFromReader creates a VolumeUpload instance, allowing to upload
// a file to a libvirt storage pool
func NewVolumeUploadFromReader(streamSrc io.ReadCloser, connDst *libvirt.Connect, volDst *libvirt.StorageVol) (instance *VolumeUpload, err error) {
streamDst, err := connDst.NewStream(0)
if err != nil {
return nil, err
}
err = volDst.Upload(streamDst, 0, 0, 0)
if err != nil {
return nil, err
}
instance = &VolumeUpload{
streamSrc: streamSrc,
streamDst: streamDst,
}
return instance, nil
}
func (v VolumeUpload) Write(p []byte) (n int, e error) {
return v.streamDst.Send(p)
}
// Copy do the actual upload
func (v *VolumeUpload) Copy() (written int64, err error) {
defer v.streamDst.Free()
buf := make([]byte, VolumeCopyBufferSize)
written, err = io.CopyBuffer(v, v.streamSrc, buf)
if err != nil {
v.streamSrc.Close()
v.streamDst.Abort()
return written, err
}
if e := v.streamSrc.Close(); e != nil {
return written, e
}
if e := v.streamDst.Finish(); e != nil {
return written, e
}
return written, err
}