forked from odeke-em/drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_closer.go
50 lines (43 loc) · 868 Bytes
/
write_closer.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
package statos
import (
"io"
"syscall"
)
// WriteCloserStatos implements the Write() interface
type WriteCloserStatos struct {
commChan chan int
commClosed bool
iterator io.WriteCloser
}
func NewWriteCloser(w io.WriteCloser) *WriteCloserStatos {
return &WriteCloserStatos{
commChan: make(chan int),
iterator: w,
commClosed: false,
}
}
func (w *WriteCloserStatos) Write(p []byte) (n int, err error) {
n, err = w.iterator.Write(p)
if err != nil && err != syscall.EINTR {
if !w.commClosed {
close(w.commChan)
w.commClosed = true
}
} else if n >= 0 {
w.commChan <- n
}
return
}
func (w *WriteCloserStatos) ProgressChan() chan int {
return w.commChan
}
func (w *WriteCloserStatos) Close() error {
err := w.iterator.Close()
if err == nil {
if !w.commClosed {
close(w.commChan)
w.commClosed = true
}
}
return err
}