// 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 (fi *File) Open(flags int, sync bool) (FileDescriptor, error) { fi.nodelk.Lock() node := fi.node fi.nodelk.Unlock() switch node := node.(type) { case *dag.ProtoNode: fsn, err := ft.FSNodeFromBytes(node.Data()) if err != nil { return nil, err } switch fsn.Type { default: return nil, fmt.Errorf("unsupported fsnode type for 'file'") case ft.TSymlink: return nil, fmt.Errorf("symlinks not yet supported") case ft.TFile, ft.TRaw: // OK case } case *dag.RawNode: // Ok as well. } switch flags { case OpenReadOnly: fi.desclock.RLock() case OpenWriteOnly, OpenReadWrite: fi.desclock.Lock() default: // TODO: support other modes return nil, fmt.Errorf("mode not supported") } dmod, err := mod.NewDagModifier(context.TODO(), node, fi.dserv, chunk.DefaultSplitter) if err != nil { return nil, err } return &fileDescriptor{ inode: fi, perms: flags, sync: sync, mod: dmod, }, nil }
func TestFlushing(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() _, rt := setupRoot(ctx, t) dir := rt.GetValue().(*Directory) c := mkdirP(t, dir, "a/b/c") d := mkdirP(t, dir, "a/b/d") e := mkdirP(t, dir, "a/b/e") data := []byte("this is a test\n") nd1 := &dag.Node{Data: ft.FilePBData(data, uint64(len(data)))} if err := c.AddChild("TEST", nd1); err != nil { t.Fatal(err) } if err := d.AddChild("TEST", nd1); err != nil { t.Fatal(err) } if err := e.AddChild("TEST", nd1); err != nil { t.Fatal(err) } if err := dir.AddChild("FILE", nd1); err != nil { t.Fatal(err) } if err := FlushPath(rt, "/a/b/c/TEST"); err != nil { t.Fatal(err) } if err := FlushPath(rt, "/a/b/d/TEST"); err != nil { t.Fatal(err) } if err := FlushPath(rt, "/a/b/e/TEST"); err != nil { t.Fatal(err) } if err := FlushPath(rt, "/FILE"); err != nil { t.Fatal(err) } rnd, err := dir.GetNode() if err != nil { t.Fatal(err) } fsnode, err := ft.FSNodeFromBytes(rnd.Data) if err != nil { t.Fatal(err) } if fsnode.Type != ft.TDirectory { t.Fatal("root wasnt a directory") } rnk, err := rnd.Key() if err != nil { t.Fatal(err) } exp := "QmWMVyhTuyxUrXX3ynz171jq76yY3PktfY9Bxiph7b9ikr" if rnk.B58String() != exp { t.Fatalf("dag looks wrong, expected %s, but got %s", exp, rnk.B58String()) } }