-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Ipns/refactor unixfs #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ipns/refactor unixfs #865
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,20 +38,51 @@ var ErrSizeLimitExceeded = fmt.Errorf("object size limit exceeded") | |
// of unixfs DAG trees | ||
type UnixfsNode struct { | ||
node *dag.Node | ||
ufmt *ft.MultiBlock | ||
ufmt *ft.FSNode | ||
} | ||
|
||
// NewUnixfsNode creates a new Unixfs node to represent a file | ||
func NewUnixfsNode() *UnixfsNode { | ||
return &UnixfsNode{ | ||
node: new(dag.Node), | ||
ufmt: new(ft.MultiBlock), | ||
ufmt: &ft.FSNode{Type: ft.TFile}, | ||
} | ||
} | ||
|
||
// NewUnixfsBlock creates a new Unixfs node to represent a raw data block | ||
func NewUnixfsBlock() *UnixfsNode { | ||
return &UnixfsNode{ | ||
node: new(dag.Node), | ||
ufmt: &ft.FSNode{Type: ft.TRaw}, | ||
} | ||
} | ||
|
||
// NewUnixfsNodeFromDag reconstructs a Unixfs node from a given dag node | ||
func NewUnixfsNodeFromDag(nd *dag.Node) (*UnixfsNode, error) { | ||
mb, err := ft.FSNodeFromBytes(nd.Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &UnixfsNode{ | ||
node: nd, | ||
ufmt: mb, | ||
}, nil | ||
} | ||
|
||
func (n *UnixfsNode) NumChildren() int { | ||
return n.ufmt.NumChildren() | ||
} | ||
|
||
func (n *UnixfsNode) GetChild(i int, ds dag.DAGService) (*UnixfsNode, error) { | ||
nd, err := n.node.Links[i].GetNode(ds) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewUnixfsNodeFromDag(nd) | ||
} | ||
|
||
// addChild will add the given UnixfsNode as a child of the receiver. | ||
// the passed in DagBuilderHelper is used to store the child node an | ||
// pin it locally so it doesnt get lost | ||
|
@@ -83,7 +114,13 @@ func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error { | |
return nil | ||
} | ||
|
||
func (n *UnixfsNode) setData(data []byte) { | ||
// Removes the child node at the given index | ||
func (n *UnixfsNode) RemoveChild(index int) { | ||
n.ufmt.RemoveBlockSize(index) | ||
n.node.Links = append(n.node.Links[:index], n.node.Links[index+1:]...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should check index is in the right range or this will panic-- maybe that's ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think of it as a normal array access, you wouldnt try doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
func (n *UnixfsNode) SetData(data []byte) { | ||
n.ufmt.Data = data | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should check index is in the right range or this will panic-- maybe that's ok