writer.go 2.7 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10 11 12
package tar

import (
	"archive/tar"
	"io"
	"path"
	"time"

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

	mdag "github.com/ipfs/go-ipfs/merkledag"
rht's avatar
rht committed
13
	ft "github.com/ipfs/go-ipfs/unixfs"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18 19 20 21 22 23
	uio "github.com/ipfs/go-ipfs/unixfs/io"
	upb "github.com/ipfs/go-ipfs/unixfs/pb"
)

// Writer is a utility structure that helps to write
// unixfs merkledag nodes as a tar archive format.
// It wraps any io.Writer.
type Writer struct {
	Dag  mdag.DAGService
	TarW *tar.Writer
rht's avatar
rht committed
24 25

	ctx cxt.Context
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28
}

// NewWriter wraps given io.Writer.
rht's avatar
rht committed
29
func NewWriter(ctx cxt.Context, dag mdag.DAGService, archive bool, compression int, w io.Writer) (*Writer, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32
	return &Writer{
		Dag:  dag,
		TarW: tar.NewWriter(w),
rht's avatar
rht committed
33
		ctx:  ctx,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36
	}, nil
}

rht's avatar
rht committed
37
func (w *Writer) writeDir(nd *mdag.Node, fpath string) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40 41
	if err := writeDirHeader(w.TarW, fpath); err != nil {
		return err
	}

rht's avatar
rht committed
42 43
	for i, ng := range w.Dag.GetDAG(w.ctx, nd) {
		child, err := ng.Get(w.ctx)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47 48
		if err != nil {
			return err
		}

		npath := path.Join(fpath, nd.Links[i].Name)
rht's avatar
rht committed
49
		if err := w.WriteNode(child, npath); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51 52 53 54 55 56
			return err
		}
	}

	return nil
}

rht's avatar
rht committed
57
func (w *Writer) writeFile(nd *mdag.Node, pb *upb.Data, fpath string) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59 60 61
	if err := writeFileHeader(w.TarW, fpath, pb.GetFilesize()); err != nil {
		return err
	}

rht's avatar
rht committed
62 63 64
	dagr := uio.NewDataFileReader(w.ctx, nd, pb, w.Dag)
	_, err := dagr.WriteTo(w.TarW)
	return err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65 66
}

rht's avatar
rht committed
67
func (w *Writer) WriteNode(nd *mdag.Node, fpath string) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69 70 71 72 73
	pb := new(upb.Data)
	if err := proto.Unmarshal(nd.Data, pb); err != nil {
		return err
	}

	switch pb.GetType() {
rht's avatar
rht committed
74 75
	case upb.Data_Metadata:
		fallthrough
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	case upb.Data_Directory:
rht's avatar
rht committed
77 78 79
		return w.writeDir(nd, fpath)
	case upb.Data_Raw:
		fallthrough
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
	case upb.Data_File:
rht's avatar
rht committed
81
		return w.writeFile(nd, pb, fpath)
Jeromy's avatar
Jeromy committed
82 83
	case upb.Data_Symlink:
		return writeSymlinkHeader(w.TarW, string(pb.GetData()), fpath)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
	default:
rht's avatar
rht committed
85
		return ft.ErrUnrecognizedType
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
	}
}

func (w *Writer) Close() error {
	return w.TarW.Close()
}

func writeDirHeader(w *tar.Writer, fpath string) error {
	return w.WriteHeader(&tar.Header{
		Name:     fpath,
		Typeflag: tar.TypeDir,
		Mode:     0777,
		ModTime:  time.Now(),
		// TODO: set mode, dates, etc. when added to unixFS
	})
}

func writeFileHeader(w *tar.Writer, fpath string, size uint64) error {
	return w.WriteHeader(&tar.Header{
		Name:     fpath,
		Size:     int64(size),
		Typeflag: tar.TypeReg,
		Mode:     0644,
		ModTime:  time.Now(),
		// TODO: set mode, dates, etc. when added to unixFS
	})
}
Jeromy's avatar
Jeromy committed
113 114 115 116 117 118 119 120 121

func writeSymlinkHeader(w *tar.Writer, target, fpath string) error {
	return w.WriteHeader(&tar.Header{
		Name:     fpath,
		Linkname: target,
		Mode:     0777,
		Typeflag: tar.TypeSymlink,
	})
}