format.go 3.78 KB
Newer Older
1 2
// Package format implements a data format for files in the ipfs filesystem
// It is not the only format in ipfs, but it is the one that the filesystem assumes
3
package unixfs
4 5 6 7

import (
	"errors"

8 9
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
	pb "github.com/jbenet/go-ipfs/unixfs/pb"
10 11
)

Jeromy's avatar
Jeromy committed
12 13 14 15
var ErrMalformedFileFormat = errors.New("malformed data in file format")
var ErrInvalidDirLocation = errors.New("found directory node in unexpected place")
var ErrUnrecognizedType = errors.New("unrecognized node type")

16 17
func FromBytes(data []byte) (*pb.Data, error) {
	pbdata := new(pb.Data)
Jeromy's avatar
Jeromy committed
18 19 20 21 22 23 24
	err := proto.Unmarshal(data, pbdata)
	if err != nil {
		return nil, err
	}
	return pbdata, nil
}

25
func FilePBData(data []byte, totalsize uint64) []byte {
26 27
	pbfile := new(pb.Data)
	typ := pb.Data_File
28 29 30 31 32 33
	pbfile.Type = &typ
	pbfile.Data = data
	pbfile.Filesize = proto.Uint64(totalsize)

	data, err := proto.Marshal(pbfile)
	if err != nil {
Jeromy's avatar
Jeromy committed
34 35 36 37 38
		// This really shouldnt happen, i promise
		// The only failure case for marshal is if required fields
		// are not filled out, and they all are. If the proto object
		// gets changed and nobody updates this function, the code
		// should panic due to programmer error
39 40 41 42 43
		panic(err)
	}
	return data
}

Jeromy's avatar
Jeromy committed
44
// Returns Bytes that represent a Directory
45
func FolderPBData() []byte {
46 47
	pbfile := new(pb.Data)
	typ := pb.Data_Directory
48 49 50 51 52 53 54 55 56 57 58
	pbfile.Type = &typ

	data, err := proto.Marshal(pbfile)
	if err != nil {
		//this really shouldnt happen, i promise
		panic(err)
	}
	return data
}

func WrapData(b []byte) []byte {
59 60
	pbdata := new(pb.Data)
	typ := pb.Data_Raw
61 62 63 64 65 66 67 68 69 70 71 72
	pbdata.Data = b
	pbdata.Type = &typ

	out, err := proto.Marshal(pbdata)
	if err != nil {
		// This shouldnt happen. seriously.
		panic(err)
	}

	return out
}

Jeromy's avatar
Jeromy committed
73
func UnwrapData(data []byte) ([]byte, error) {
74
	pbdata := new(pb.Data)
Jeromy's avatar
Jeromy committed
75 76 77 78 79 80 81
	err := proto.Unmarshal(data, pbdata)
	if err != nil {
		return nil, err
	}
	return pbdata.GetData(), nil
}

82
func DataSize(data []byte) (uint64, error) {
83
	pbdata := new(pb.Data)
84 85 86 87 88 89
	err := proto.Unmarshal(data, pbdata)
	if err != nil {
		return 0, err
	}

	switch pbdata.GetType() {
90
	case pb.Data_Directory:
91
		return 0, errors.New("Cant get data size of directory!")
92
	case pb.Data_File:
93
		return pbdata.GetFilesize(), nil
94
	case pb.Data_Raw:
95 96 97 98 99
		return uint64(len(pbdata.GetData())), nil
	default:
		return 0, errors.New("Unrecognized node data type!")
	}
}
Jeromy's avatar
Jeromy committed
100 101 102 103 104 105 106 107 108 109 110 111 112

type MultiBlock struct {
	Data       []byte
	blocksizes []uint64
	subtotal   uint64
}

func (mb *MultiBlock) AddBlockSize(s uint64) {
	mb.subtotal += s
	mb.blocksizes = append(mb.blocksizes, s)
}

func (mb *MultiBlock) GetBytes() ([]byte, error) {
113 114
	pbn := new(pb.Data)
	t := pb.Data_File
Jeromy's avatar
Jeromy committed
115 116 117 118 119 120
	pbn.Type = &t
	pbn.Filesize = proto.Uint64(uint64(len(mb.Data)) + mb.subtotal)
	pbn.Blocksizes = mb.blocksizes
	pbn.Data = mb.Data
	return proto.Marshal(pbn)
}
Jeromy's avatar
Jeromy committed
121 122 123 124 125 126 127 128

func (mb *MultiBlock) FileSize() uint64 {
	return uint64(len(mb.Data)) + mb.subtotal
}

func (mb *MultiBlock) NumChildren() int {
	return len(mb.blocksizes)
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

type Metadata struct {
	MimeType string
	Size     uint64
}

func MetadataFromBytes(b []byte) (*Metadata, error) {
	pbd := new(pb.Data)
	err := proto.Unmarshal(b, pbd)
	if err != nil {
		return nil, err
	}
	if pbd.GetType() != pb.Data_Metadata {
		return nil, errors.New("incorrect node type")
	}

	pbm := new(pb.Metadata)
	err = proto.Unmarshal(pbd.Data, pbm)
	if err != nil {
		return nil, err
	}
	md := new(Metadata)
	md.MimeType = pbm.GetMimeType()
	return md, nil
}

func (m *Metadata) Bytes() ([]byte, error) {
	pbm := new(pb.Metadata)
	pbm.MimeType = &m.MimeType
	return proto.Marshal(pbm)
}

func BytesForMetadata(m *Metadata) ([]byte, error) {
	pbd := new(pb.Data)
	pbd.Filesize = proto.Uint64(m.Size)
	typ := pb.Data_Metadata
	pbd.Type = &typ
	mdd, err := m.Bytes()
	if err != nil {
		return nil, err
	}

	pbd.Data = mdd
	return proto.Marshal(pbd)
}