writer.go 3.31 KB
Newer Older
Hector Sanjuan's avatar
Hector Sanjuan committed
1 2
// Package tar provides functionality to write a unixfs merkledag
// as a tar archive.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3 4 5 6
package tar

import (
	"archive/tar"
Jeromy's avatar
Jeromy committed
7 8
	"context"
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10 11 12 13
	"io"
	"path"
	"time"

	mdag "github.com/ipfs/go-ipfs/merkledag"
rht's avatar
rht committed
14
	ft "github.com/ipfs/go-ipfs/unixfs"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16
	uio "github.com/ipfs/go-ipfs/unixfs/io"
	upb "github.com/ipfs/go-ipfs/unixfs/pb"
Jeromy's avatar
Jeromy committed
17 18

	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
19
	ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22 23 24 25
)

// 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 {
26
	Dag  ipld.DAGService
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
	TarW *tar.Writer
rht's avatar
rht committed
28

Jeromy's avatar
Jeromy committed
29
	ctx context.Context
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32
}

// NewWriter wraps given io.Writer.
33
func NewWriter(ctx context.Context, dag ipld.DAGService, archive bool, compression int, w io.Writer) (*Writer, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36
	return &Writer{
		Dag:  dag,
		TarW: tar.NewWriter(w),
rht's avatar
rht committed
37
		ctx:  ctx,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40
	}, nil
}

41
func (w *Writer) writeDir(nd *mdag.ProtoNode, fpath string) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43 44 45
	if err := writeDirHeader(w.TarW, fpath); err != nil {
		return err
	}

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

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

	return nil
}

61
func (w *Writer) writeFile(nd *mdag.ProtoNode, pb *upb.Data, fpath string) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65
	if err := writeFileHeader(w.TarW, fpath, pb.GetFilesize()); err != nil {
		return err
	}

66
	dagr := uio.NewPBFileReader(w.ctx, nd, pb, w.Dag)
rht's avatar
rht committed
67 68 69 70 71
	if _, err := dagr.WriteTo(w.TarW); err != nil {
		return err
	}
	w.TarW.Flush()
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73
}

Hector Sanjuan's avatar
Hector Sanjuan committed
74
// WriteNode adds a node to the archive.
75
func (w *Writer) WriteNode(nd ipld.Node, fpath string) error {
Jeromy's avatar
Jeromy committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	switch nd := nd.(type) {
	case *mdag.ProtoNode:
		pb := new(upb.Data)
		if err := proto.Unmarshal(nd.Data(), pb); err != nil {
			return err
		}

		switch pb.GetType() {
		case upb.Data_Metadata:
			fallthrough
		case upb.Data_Directory:
			return w.writeDir(nd, fpath)
		case upb.Data_Raw:
			fallthrough
		case upb.Data_File:
			return w.writeFile(nd, pb, fpath)
		case upb.Data_Symlink:
			return writeSymlinkHeader(w.TarW, string(pb.GetData()), fpath)
		default:
			return ft.ErrUnrecognizedType
		}
	case *mdag.RawNode:
		if err := writeFileHeader(w.TarW, fpath, uint64(len(nd.RawData()))); err != nil {
			return err
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101

Jeromy's avatar
Jeromy committed
102 103 104 105 106
		if _, err := w.TarW.Write(nd.RawData()); err != nil {
			return err
		}
		w.TarW.Flush()
		return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107
	default:
Jeromy's avatar
Jeromy committed
108
		return fmt.Errorf("nodes of type %T are not supported in unixfs", nd)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109 110 111
	}
}

Hector Sanjuan's avatar
Hector Sanjuan committed
112
// Close closes the tar writer.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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
137 138 139 140 141 142 143 144 145

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