Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ipfs/kubo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 26cdd9957fb5
Choose a base ref
...
head repository: ipfs/kubo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d9d9798876b9
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Oct 4, 2015

  1. Decompose maybeGzwriter

    License: MIT
    Signed-off-by: rht <rhtbot@gmail.com>
    rht committed Oct 4, 2015
    Copy the full SHA
    ff27c03 View commit details
  2. Merge pull request #1697 from rht/fix/tar

    Decompose gz writer from DagArchive
    jbenet committed Oct 4, 2015
    Copy the full SHA
    d9d9798 View commit details
Showing with 33 additions and 29 deletions.
  1. +33 −29 unixfs/archive/archive.go
62 changes: 33 additions & 29 deletions unixfs/archive/archive.go
Original file line number Diff line number Diff line change
@@ -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.Close() // everything seems to be ok.
closeGzwAndPipe() // 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
}