func WriteHeader(w io.Writer, src, dst peer.ID) error { // write header to w. mhw := mh.NewWriter(w) if err := mhw.WriteMultihash(mh.Multihash(src)); err != nil { return fmt.Errorf("failed to write relay header: %s -- %s", dst, err) } if err := mhw.WriteMultihash(mh.Multihash(dst)); err != nil { return fmt.Errorf("failed to write relay header: %s -- %s", dst, err) } return nil }
// modifyDag writes the data in 'data' over the data in 'node' starting at 'offset' // returns the new key of the passed in node and whether or not all the data in the reader // has been consumed. func (dm *DagModifier) modifyDag(node *mdag.Node, offset uint64, data io.Reader) (key.Key, bool, error) { f, err := ft.FromBytes(node.Data) if err != nil { return "", false, err } // If we've reached a leaf node. if len(node.Links) == 0 { n, err := data.Read(f.Data[offset:]) if err != nil && err != io.EOF { return "", false, err } // Update newly written node.. b, err := proto.Marshal(f) if err != nil { return "", false, err } nd := &mdag.Node{Data: b} k, err := dm.dagserv.Add(nd) if err != nil { return "", false, err } // Hey look! we're done! var done bool if n < len(f.Data[offset:]) { done = true } return k, done, nil } var cur uint64 var done bool for i, bs := range f.GetBlocksizes() { // We found the correct child to write into if cur+bs > offset { // Unpin block ckey := key.Key(node.Links[i].Hash) dm.mp.RemovePinWithMode(ckey, pin.Indirect) child, err := node.Links[i].GetNode(dm.ctx, dm.dagserv) if err != nil { return "", false, err } k, sdone, err := dm.modifyDag(child, offset-cur, data) if err != nil { return "", false, err } // pin the new node dm.mp.PinWithMode(k, pin.Indirect) offset += bs node.Links[i].Hash = mh.Multihash(k) // Recache serialized node _, err = node.Encoded(true) if err != nil { return "", false, err } if sdone { // No more bytes to write! done = true break } offset = cur + bs } cur += bs } k, err := dm.dagserv.Add(node) return k, done, err }
func (k Key) ToMultihash() mh.Multihash { return mh.Multihash(k) }