archive.go 2.41 KB
Newer Older
rht's avatar
rht committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package archive

import (
	"bufio"
	"compress/gzip"
	"io"
	"path"

	cxt "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

	mdag "github.com/ipfs/go-ipfs/merkledag"
	tar "github.com/ipfs/go-ipfs/unixfs/archive/tar"
	uio "github.com/ipfs/go-ipfs/unixfs/io"
)

// DefaultBufSize is the buffer size for gets. for now, 1MB, which is ~4 blocks.
// TODO: does this need to be configurable?
var DefaultBufSize = 1048576

rht's avatar
rht committed
20 21 22 23 24 25 26 27 28 29 30 31
type identityWriteCloser struct {
	w io.Writer
}

func (i *identityWriteCloser) Write(p []byte) (int, error) {
	return i.w.Write(p)
}

func (i *identityWriteCloser) Close() error {
	return nil
}

rht's avatar
rht committed
32 33 34 35 36 37 38 39 40 41 42 43
// DagArchive is equivalent to `ipfs getdag $hash | maybe_tar | maybe_gzip`
func DagArchive(ctx cxt.Context, nd *mdag.Node, name string, dag mdag.DAGService, archive bool, compression int) (io.Reader, error) {

	_, filename := path.Split(name)

	// need to connect a writer to a reader
	piper, pipew := io.Pipe()

	// use a buffered writer to parallelize task
	bufw := bufio.NewWriterSize(pipew, DefaultBufSize)

	// compression determines whether to use gzip compression.
rht's avatar
rht committed
44 45
	var maybeGzw io.WriteCloser
	var err error
rht's avatar
rht committed
46 47 48
	if compression != gzip.NoCompression {
		maybeGzw, err = gzip.NewWriterLevel(bufw, compression)
		if err != nil {
rht's avatar
rht committed
49
			pipew.CloseWithError(err)
rht's avatar
rht committed
50 51 52
			return nil, err
		}
	} else {
rht's avatar
rht committed
53
		maybeGzw = &identityWriteCloser{bufw}
rht's avatar
rht committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	}

	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)
			return nil, err
		}

		go func() {
			if _, err := dagr.WriteTo(maybeGzw); err != nil {
				pipew.CloseWithError(err)
				return
			}
rht's avatar
rht committed
69 70 71 72 73
			maybeGzw.Close()
			if err := bufw.Flush(); err != nil {
				pipew.CloseWithError(err)
				return
			}
rht's avatar
rht committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
			pipew.Close() // 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 {
			return nil, err
		}

		go func() {
			// write all the nodes recursively
			if err := w.WriteNode(nd, filename); err != nil {
				pipew.CloseWithError(err)
				return
			}
rht's avatar
rht committed
91 92
			w.Close()
			maybeGzw.Close()
rht's avatar
rht committed
93 94 95 96 97 98 99 100 101 102
			if err := bufw.Flush(); err != nil {
				pipew.CloseWithError(err)
				return
			}
			pipew.Close() // everything seems to be ok.
		}()
	}

	return piper, nil
}