@cryptotaxi247 / kubo / commits / ff27c03da

Decompose maybeGzwriter

License: MIT Signed-off-by: rht <rhtbot@gmail.com>

rht committed Sep 14, 2015 at 06:52 UTC ff27c03da0a4544d3eacc5e6b5c7172452ba7c8c
1 file changed +33 -29
unixfs/archive/archive.go
+33 -29
@@ -36,67 +36,71 @@ func DagArchive(ctx cxt.Context, nd *mdag.Node, name string, dag mdag.DAGService
36
37 // need to connect a writer to a reader
38 piper, pipew := io.Pipe()
39 + checkErrAndClosePipe := func(err error) bool {
40 + if err != nil {
41 + pipew.CloseWithError(err)
42 + return true
43 + }
44 + return false
45 + }
46
47 // use a buffered writer to parallelize task
48 bufw := bufio.NewWriterSize(pipew, DefaultBufSize)
49
50 // compression determines whether to use gzip compression.
44 - var maybeGzw io.WriteCloser
45 - var err error
46 - if compression != gzip.NoCompression {
47 - maybeGzw, err = gzip.NewWriterLevel(bufw, compression)
48 - if err != nil {
49 - pipew.CloseWithError(err)
50 - return nil, err
51 + maybeGzw, err := newMaybeGzWriter(bufw, compression)
52 + if checkErrAndClosePipe(err) {
53 + return nil, err
54 + }
55 +
56 + closeGzwAndPipe := func() {
57 + if err := maybeGzw.Close(); checkErrAndClosePipe(err) {
58 + return
59 }
52 - } else {
53 - maybeGzw = &identityWriteCloser{bufw}
60 + if err := bufw.Flush(); checkErrAndClosePipe(err) {
61 + return
62 + }
63 + pipew.Close() // everything seems to be ok.
64 }
65
66 if !archive && compression != gzip.NoCompression {
67 // the case when the node is a file
68 dagr, err := uio.NewDagReader(ctx, nd, dag)
59 - if err != nil {
60 - pipew.CloseWithError(err)
69 + if checkErrAndClosePipe(err) {
70 return nil, err
71 }
72
73 go func() {
65 - if _, err := dagr.WriteTo(maybeGzw); err != nil {
66 - pipew.CloseWithError(err)
67 - return
68 - }
69 - maybeGzw.Close()
70 - if err := bufw.Flush(); err != nil {
71 - pipew.CloseWithError(err)
74 + if _, err := dagr.WriteTo(maybeGzw); checkErrAndClosePipe(err) {
75 return
76 }
74 - pipew.Close() // everything seems to be ok.
77 + closeGzwAndPipe() // everything seems to be ok
78 }()
79 } else {
80 // the case for 1. archive, and 2. not archived and not compressed, in which tar is used anyway as a transport format
81
82 // construct the tar writer
83 w, err := tar.NewWriter(ctx, dag, archive, compression, maybeGzw)
81 - if err != nil {
84 + if checkErrAndClosePipe(err) {
85 return nil, err
86 }
87
88 go func() {
89 // write all the nodes recursively
87 - if err := w.WriteNode(nd, filename); err != nil {
88 - pipew.CloseWithError(err)
90 + if err := w.WriteNode(nd, filename); checkErrAndClosePipe(err) {
91 return
92 }
91 - w.Close()
92 - maybeGzw.Close()
93 - if err := bufw.Flush(); err != nil {
94 - pipew.CloseWithError(err)
95 - return
96 - }
97 - pipew.Close() // everything seems to be ok.
93 + w.Close() // close tar writer
94 + closeGzwAndPipe() // everything seems to be ok
95 }()
96 }
97
98 return piper, nil
99 }
100 +
101 +func newMaybeGzWriter(w io.Writer, compression int) (io.WriteCloser, error) {
102 + if compression != gzip.NoCompression {
103 + return gzip.NewWriterLevel(w, compression)
104 + }
105 + return &identityWriteCloser{w}, nil
106 +}