Skip to content

Commit

Permalink
refactor: simplify unixfsls
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 1, 2023
1 parent 973bd21 commit b2c10f9
Showing 1 changed file with 8 additions and 44 deletions.
52 changes: 8 additions & 44 deletions examples/gateway/car-file-gateway/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
uio "github.com/ipfs/go-unixfs/io"
"github.com/ipfs/go-unixfsnode"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
carblockstore "github.com/ipld/go-car/v2/blockstore"
dagpb "github.com/ipld/go-codec-dagpb"
Expand Down Expand Up @@ -115,31 +114,23 @@ func (api *carGateway) GetUnixFsNode(ctx context.Context, p path.Resolved) (file
}

func (api *carGateway) LsUnixFsDir(ctx context.Context, p path.Resolved) (<-chan iface.DirEntry, error) {
settings, err := options.UnixfsLsOptions(
options.Unixfs.ResolveChildren(false),
options.Unixfs.UseCumulativeSize(true),
)
if err != nil {
return nil, err
}

dagnode, err := api.resolveNode(ctx, p)
if err != nil {
return nil, err
}

dir, err := uio.NewDirectoryFromNode(api.dsrv, dagnode)
if err == uio.ErrNotADir {
return api.lsFromLinks(ctx, dagnode.Links(), settings)
return api.lsFromLinks(ctx, dagnode.Links())
}
if err != nil {
return nil, err
}

return api.lsFromLinksAsync(ctx, dir, settings)
return api.lsFromLinksAsync(ctx, dir)
}

func (api *carGateway) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) iface.DirEntry {
func (api *carGateway) processLink(ctx context.Context, linkres ft.LinkResult) iface.DirEntry {
if linkres.Err != nil {
return iface.DirEntry{Err: linkres.Err}
}
Expand All @@ -155,46 +146,20 @@ func (api *carGateway) processLink(ctx context.Context, linkres ft.LinkResult, s
lnk.Type = iface.TFile
lnk.Size = linkres.Link.Size
case cid.DagProtobuf:
if !settings.ResolveChildren {
break
}

linkNode, err := linkres.Link.GetNode(ctx, api.dsrv)
if err != nil {
lnk.Err = err
break
}

if pn, ok := linkNode.(*merkledag.ProtoNode); ok {
d, err := ft.FSNodeFromBytes(pn.Data())
if err != nil {
lnk.Err = err
break
}
switch d.Type() {
case ft.TFile, ft.TRaw:
lnk.Type = iface.TFile
case ft.THAMTShard, ft.TDirectory, ft.TMetadata:
lnk.Type = iface.TDirectory
case ft.TSymlink:
lnk.Type = iface.TSymlink
lnk.Target = string(d.Data())
}
lnk.Size = d.FileSize()
}
lnk.Size = linkres.Link.Size
}

return lnk
}

func (api *carGateway) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan iface.DirEntry, error) {
func (api *carGateway) lsFromLinksAsync(ctx context.Context, dir uio.Directory) (<-chan iface.DirEntry, error) {
out := make(chan iface.DirEntry, uio.DefaultShardWidth)

go func() {
defer close(out)
for l := range dir.EnumLinksAsync(ctx) {
select {
case out <- api.processLink(ctx, l, settings): // TODO: perf: processing can be done in background and in parallel
case out <- api.processLink(ctx, l):
case <-ctx.Done():
return
}
Expand All @@ -204,12 +169,11 @@ func (api *carGateway) lsFromLinksAsync(ctx context.Context, dir uio.Directory,
return out, nil
}

func (api *carGateway) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan iface.DirEntry, error) {
func (api *carGateway) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link) (<-chan iface.DirEntry, error) {
links := make(chan iface.DirEntry, len(ndlinks))
for _, l := range ndlinks {
lr := ft.LinkResult{Link: &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}}

links <- api.processLink(ctx, lr, settings) // TODO: can be parallel if settings.Async
links <- api.processLink(ctx, lr)
}
close(links)
return links, nil
Expand Down

0 comments on commit b2c10f9

Please sign in to comment.