mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-24 20:07:45 +08:00
mfs: inherit CID prefix from from parent directory
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
This commit is contained in:
parent
77e9b8ddf9
commit
f2fbfdf291
@ -890,8 +890,10 @@ func getFileHandle(r *mfs.Root, path string, create bool) (*mfs.File, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s was not a directory", dirname)
|
||||
}
|
||||
prefix := pdir.GetPrefix()
|
||||
|
||||
nd := dag.NodeWithData(ft.FilePBData(nil, 0))
|
||||
nd.SetPrefix(prefix)
|
||||
err = pdir.AddChild(fname, nd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -58,6 +58,11 @@ func NewDirectory(ctx context.Context, name string, node node.Node, parent child
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetPrefix gets the CID prefix of the root node
|
||||
func (d *Directory) GetPrefix() *cid.Prefix {
|
||||
return d.dirbuilder.GetPrefix()
|
||||
}
|
||||
|
||||
// SetPrefix sets the CID prefix
|
||||
func (d *Directory) SetPrefix(prefix *cid.Prefix) {
|
||||
d.dirbuilder.SetPrefix(prefix)
|
||||
@ -299,6 +304,7 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
|
||||
}
|
||||
|
||||
ndir := ft.EmptyDirNode()
|
||||
ndir.SetPrefix(d.GetPrefix())
|
||||
|
||||
_, err = d.dserv.Add(ndir)
|
||||
if err != nil {
|
||||
|
||||
11
mfs/file.go
11
mfs/file.go
@ -27,14 +27,19 @@ type File struct {
|
||||
RawLeaves bool
|
||||
}
|
||||
|
||||
// NewFile returns a NewFile object with the given parameters
|
||||
// NewFile returns a NewFile object with the given parameters. If the
|
||||
// Cid version is non-zero RawLeaves will be enabled.
|
||||
func NewFile(name string, node node.Node, parent childCloser, dserv dag.DAGService) (*File, error) {
|
||||
return &File{
|
||||
fi := &File{
|
||||
dserv: dserv,
|
||||
parent: parent,
|
||||
name: name,
|
||||
node: node,
|
||||
}, nil
|
||||
}
|
||||
if node.Cid().Prefix().Version > 0 {
|
||||
fi.RawLeaves = true
|
||||
}
|
||||
return fi, nil
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@ -129,7 +129,9 @@ func Mkdir(r *Root, pth string, mkparents bool, flush bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mkd.SetPrefix(r.Prefix)
|
||||
if r.Prefix != nil {
|
||||
mkd.SetPrefix(r.Prefix)
|
||||
}
|
||||
fsn = mkd
|
||||
} else if err != nil {
|
||||
return err
|
||||
@ -148,7 +150,9 @@ func Mkdir(r *Root, pth string, mkparents bool, flush bool) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
final.SetPrefix(r.Prefix)
|
||||
if r.Prefix != nil {
|
||||
final.SetPrefix(r.Prefix)
|
||||
}
|
||||
|
||||
if flush {
|
||||
err := final.Flush()
|
||||
|
||||
@ -75,8 +75,8 @@ test_add_large_dir_v1() {
|
||||
'
|
||||
}
|
||||
|
||||
# this hash implies both the directory and the leaf entries are CIDv1
|
||||
SHARDEDV1="zdj7WX91spg4DsnNpvoBLjyjXUGgcTTWavygBbSifpmJdgPUA"
|
||||
# this hash implies the directory is CIDv1 and leaf entries are CIDv1 and raw
|
||||
SHARDEDV1="zdj7WY8aNcxF49q1ZpFXfchNmbswnUxiVDVjmrHb53xRM8W4C"
|
||||
test_add_large_dir_v1 "$SHARDEDV1"
|
||||
|
||||
test_launch_ipfs_daemon
|
||||
|
||||
@ -121,6 +121,7 @@ func NewHamtFromDag(dserv dag.DAGService, nd node.Node) (*HamtShard, error) {
|
||||
ds.children = make([]child, len(pbnd.Links()))
|
||||
ds.bitfield = new(big.Int).SetBytes(pbd.GetData())
|
||||
ds.hashFunc = pbd.GetHashType()
|
||||
ds.prefix = &ds.nd.Prefix
|
||||
|
||||
return ds, nil
|
||||
}
|
||||
@ -130,6 +131,11 @@ func (ds *HamtShard) SetPrefix(prefix *cid.Prefix) {
|
||||
ds.prefix = prefix
|
||||
}
|
||||
|
||||
// GetPrefix gets the CID Prefix, may be nil if unset
|
||||
func (ds *HamtShard) Prefix() *cid.Prefix {
|
||||
return ds.prefix
|
||||
}
|
||||
|
||||
// Node serializes the HAMT structure into a merkledag node with unixfs formatting
|
||||
func (ds *HamtShard) Node() (node.Node, error) {
|
||||
out := new(dag.ProtoNode)
|
||||
@ -500,6 +506,7 @@ func (ds *HamtShard) modifyValue(ctx context.Context, hv *hashBits, key string,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ns.prefix = ds.prefix
|
||||
chhv := &hashBits{
|
||||
b: hash([]byte(child.key)),
|
||||
consumed: hv.consumed,
|
||||
|
||||
@ -115,6 +115,7 @@ func (d *Directory) switchToSharding(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.SetPrefix(&d.dirnode.Prefix)
|
||||
|
||||
d.shard = s
|
||||
for _, lnk := range d.dirnode.Links() {
|
||||
@ -192,3 +193,11 @@ func (d *Directory) GetNode() (node.Node, error) {
|
||||
|
||||
return d.shard.Node()
|
||||
}
|
||||
|
||||
func (d *Directory) GetPrefix() *cid.Prefix {
|
||||
if d.shard == nil {
|
||||
return &d.dirnode.Prefix
|
||||
}
|
||||
|
||||
return d.shard.Prefix()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user