-
Notifications
You must be signed in to change notification settings - Fork 50
/
stream.go
79 lines (65 loc) · 1.27 KB
/
stream.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package libvirt
/*
#cgo LDFLAGS: -lvirt
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
*/
import "C"
import (
"io"
"unsafe"
)
type VirStream struct {
ptr C.virStreamPtr
}
func NewVirStream(c *VirConnection, flags uint) (*VirStream, error) {
virStream := C.virStreamNew(c.ptr, C.uint(flags))
if virStream == nil {
return nil, GetLastError()
}
return &VirStream{
ptr: virStream,
}, nil
}
func (v *VirStream) Abort() error {
result := C.virStreamAbort(v.ptr)
if result == -1 {
return GetLastError()
}
return nil
}
func (v *VirStream) Close() error {
result := C.virStreamFinish(v.ptr)
if result == -1 {
return GetLastError()
}
return nil
}
func (v *VirStream) Free() error {
result := C.virStreamFree(v.ptr)
if result == -1 {
return GetLastError()
}
return nil
}
func (v *VirStream) Read(p []byte) (int, error) {
n := C.virStreamRecv(v.ptr, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(len(p)))
if n < 0 {
return 0, GetLastError()
}
if n == 0 {
return 0, io.EOF
}
return int(n), nil
}
func (v *VirStream) Write(p []byte) (int, error) {
n := C.virStreamSend(v.ptr, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(len(p)))
if n < 0 {
return 0, GetLastError()
}
if n == 0 {
return 0, io.EOF
}
return int(n), nil
}