Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
ff27c03d
Commit
ff27c03d
authored
Sep 14, 2015
by
rht
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Decompose maybeGzwriter
License: MIT Signed-off-by:
rht
<
rhtbot@gmail.com
>
parent
7e691469
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
29 deletions
+33
-29
unixfs/archive/archive.go
unixfs/archive/archive.go
+33
-29
No files found.
unixfs/archive/archive.go
View file @
ff27c03d
...
...
@@ -36,67 +36,71 @@ func DagArchive(ctx cxt.Context, nd *mdag.Node, name string, dag mdag.DAGService
// need to connect a writer to a reader
piper
,
pipew
:=
io
.
Pipe
()
checkErrAndClosePipe
:=
func
(
err
error
)
bool
{
if
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
return
true
}
return
false
}
// use a buffered writer to parallelize task
bufw
:=
bufio
.
NewWriterSize
(
pipew
,
DefaultBufSize
)
// compression determines whether to use gzip compression.
var
maybeGzw
io
.
WriteCloser
var
err
error
if
compression
!=
gzip
.
NoCompression
{
maybeGzw
,
err
=
gzip
.
NewWriterLevel
(
bufw
,
compression
)
if
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
return
nil
,
err
maybeGzw
,
err
:=
newMaybeGzWriter
(
bufw
,
compression
)
if
checkErrAndClosePipe
(
err
)
{
return
nil
,
err
}
closeGzwAndPipe
:=
func
()
{
if
err
:=
maybeGzw
.
Close
();
checkErrAndClosePipe
(
err
)
{
return
}
}
else
{
maybeGzw
=
&
identityWriteCloser
{
bufw
}
if
err
:=
bufw
.
Flush
();
checkErrAndClosePipe
(
err
)
{
return
}
pipew
.
Close
()
// everything seems to be ok.
}
if
!
archive
&&
compression
!=
gzip
.
NoCompression
{
// the case when the node is a file
dagr
,
err
:=
uio
.
NewDagReader
(
ctx
,
nd
,
dag
)
if
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
if
checkErrAndClosePipe
(
err
)
{
return
nil
,
err
}
go
func
()
{
if
_
,
err
:=
dagr
.
WriteTo
(
maybeGzw
);
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
return
}
maybeGzw
.
Close
()
if
err
:=
bufw
.
Flush
();
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
if
_
,
err
:=
dagr
.
WriteTo
(
maybeGzw
);
checkErrAndClosePipe
(
err
)
{
return
}
pipew
.
Clos
e
()
// everything seems to be ok
.
closeGzwAndPip
e
()
// everything seems to be ok
}()
}
else
{
// the case for 1. archive, and 2. not archived and not compressed, in which tar is used anyway as a transport format
// construct the tar writer
w
,
err
:=
tar
.
NewWriter
(
ctx
,
dag
,
archive
,
compression
,
maybeGzw
)
if
err
!=
nil
{
if
checkErrAndClosePipe
(
err
)
{
return
nil
,
err
}
go
func
()
{
// write all the nodes recursively
if
err
:=
w
.
WriteNode
(
nd
,
filename
);
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
if
err
:=
w
.
WriteNode
(
nd
,
filename
);
checkErrAndClosePipe
(
err
)
{
return
}
w
.
Close
()
maybeGzw
.
Close
()
if
err
:=
bufw
.
Flush
();
err
!=
nil
{
pipew
.
CloseWithError
(
err
)
return
}
pipew
.
Close
()
// everything seems to be ok.
w
.
Close
()
// close tar writer
closeGzwAndPipe
()
// everything seems to be ok
}()
}
return
piper
,
nil
}
func
newMaybeGzWriter
(
w
io
.
Writer
,
compression
int
)
(
io
.
WriteCloser
,
error
)
{
if
compression
!=
gzip
.
NoCompression
{
return
gzip
.
NewWriterLevel
(
w
,
compression
)
}
return
&
identityWriteCloser
{
w
},
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment