-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
204 lines (176 loc) · 3.75 KB
/
reader.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package tar
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
gopath "path"
"time"
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
mdag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
uio "github.com/ipfs/go-ipfs/unixfs/io"
upb "github.com/ipfs/go-ipfs/unixfs/pb"
)
type Reader struct {
buf bytes.Buffer
closed bool
signalChan chan struct{}
dag mdag.DAGService
resolver *path.Resolver
writer *tar.Writer
gzipWriter *gzip.Writer
err error
}
func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compression int) (*Reader, error) {
reader := &Reader{
signalChan: make(chan struct{}),
dag: dag,
}
var err error
if compression != gzip.NoCompression {
reader.gzipWriter, err = gzip.NewWriterLevel(&reader.buf, compression)
if err != nil {
return nil, err
}
reader.writer = tar.NewWriter(reader.gzipWriter)
} else {
reader.writer = tar.NewWriter(&reader.buf)
}
// writeToBuf will write the data to the buffer, and will signal when there
// is new data to read
_, filename := gopath.Split(path.String())
go reader.writeToBuf(dagnode, filename, 0)
return reader, nil
}
func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
pb := new(upb.Data)
err := proto.Unmarshal(dagnode.Data, pb)
if err != nil {
r.emitError(err)
return
}
if depth == 0 {
defer r.close()
}
if pb.GetType() == upb.Data_Directory {
err = r.writer.WriteHeader(&tar.Header{
Name: path,
Typeflag: tar.TypeDir,
Mode: 0777,
ModTime: time.Now(),
// TODO: set mode, dates, etc. when added to unixFS
})
if err != nil {
r.emitError(err)
return
}
r.flush()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*60)
defer cancel()
for i, ng := range r.dag.GetDAG(ctx, dagnode) {
childNode, err := ng.Get(ctx)
if err != nil {
r.emitError(err)
return
}
r.writeToBuf(childNode, gopath.Join(path, dagnode.Links[i].Name), depth+1)
}
return
}
err = r.writer.WriteHeader(&tar.Header{
Name: path,
Size: int64(pb.GetFilesize()),
Typeflag: tar.TypeReg,
Mode: 0644,
ModTime: time.Now(),
// TODO: set mode, dates, etc. when added to unixFS
})
if err != nil {
r.emitError(err)
return
}
r.flush()
reader, err := uio.NewDagReader(context.TODO(), dagnode, r.dag)
if err != nil {
r.emitError(err)
return
}
err = r.syncCopy(reader)
if err != nil {
r.emitError(err)
return
}
}
func (r *Reader) Read(p []byte) (int, error) {
// wait for the goroutine that is writing data to the buffer to tell us
// there is something to read
if !r.closed {
<-r.signalChan
}
if r.err != nil {
return 0, r.err
}
if !r.closed {
defer r.signal()
}
if r.buf.Len() == 0 {
if r.closed {
return 0, io.EOF
}
return 0, nil
}
n, err := r.buf.Read(p)
if err == io.EOF && !r.closed || r.buf.Len() > 0 {
return n, nil
}
return n, err
}
func (r *Reader) signal() {
r.signalChan <- struct{}{}
}
func (r *Reader) flush() {
r.signal()
<-r.signalChan
}
func (r *Reader) emitError(err error) {
r.err = err
r.signal()
}
func (r *Reader) close() {
r.closed = true
defer r.signal()
err := r.writer.Close()
if err != nil {
r.emitError(err)
return
}
if r.gzipWriter != nil {
err = r.gzipWriter.Close()
if err != nil {
r.emitError(err)
return
}
}
}
func (r *Reader) syncCopy(reader io.Reader) error {
buf := make([]byte, 32*1024)
for {
nr, err := reader.Read(buf)
if nr > 0 {
_, err := r.writer.Write(buf[:nr])
if err != nil {
return err
}
r.flush()
}
if err == io.EOF {
break
}
if err != nil {
return err
}
}
return nil
}