@cryptotaxi247 / kubo / commits / 987a46a69

tweak the Ls interface

1. Avoid `ipld.Link`. This is a protodag specific thing that will go away in future IPLD versions. 2. Avoid exposing the underlying file types. The user shouldn't care if they're dealing with a hamt, etc. 3. Add a field for a symlink's target. 4. Rename LsLink to DirEntry to better this type's role. This commit was moved from ipfs/interface-go-ipfs-core@dbee8cc1adb3b53a10ea33add0584b030f92106a This commit was moved from ipfs/boxo@9c3cf70c5f23696257374f5f42212364d706d427

Steven Allen committed Mar 4, 2019 at 20:11 UTC 987a46a697747db37898d501e7efeed72e4989c4
2 files changed +33 -26
core/coreiface/tests/unixfs.go
+11 -12
@@ -750,26 +750,25 @@ func (tp *provider) TestLs(t *testing.T) {
750 t.Error(err)
751 }
752
753 - links, err := api.Unixfs().Ls(ctx, p)
753 + entries, err := api.Unixfs().Ls(ctx, p)
754 if err != nil {
755 t.Error(err)
756 }
757
758 - linkRes := <-links
759 - if linkRes.Err != nil {
760 - t.Fatal(linkRes.Err)
758 + entry := <-entries
759 + if entry.Err != nil {
760 + t.Fatal(entry.Err)
761 }
762 - link := linkRes.Link
763 - if linkRes.Size != 15 {
764 - t.Fatalf("expected size = 15, got %d", link.Size)
762 + if entry.Size != 15 {
763 + t.Fatalf("expected size = 15, got %d", entry.Size)
764 }
766 - if link.Name != "name-of-file" {
767 - t.Fatalf("expected name = name-of-file, got %s", link.Name)
765 + if entry.Name != "name-of-file" {
766 + t.Fatalf("expected name = name-of-file, got %s", entry.Name)
767 }
769 - if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
770 - t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid)
768 + if entry.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
769 + t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", entry.Cid)
770 }
772 - if l, ok := <-links; ok {
771 + if l, ok := <-entries; ok {
772 t.Errorf("didn't expect a second link")
773 if l.Err != nil {
774 t.Error(l.Err)
core/coreiface/unixfs.go
+22 -14
@@ -4,9 +4,8 @@ import (
4 "context"
5 "github.com/ipfs/interface-go-ipfs-core/options"
6
7 - "github.com/ipfs/go-ipfs-files"
8 - ipld "github.com/ipfs/go-ipld-format"
9 - "github.com/ipfs/go-unixfs"
7 + cid "github.com/ipfs/go-cid"
8 + files "github.com/ipfs/go-ipfs-files"
9 )
10
11 type AddEvent struct {
@@ -16,21 +15,30 @@ type AddEvent struct {
15 Size string `json:",omitempty"`
16 }
17
18 +// FileType is an enum of possible UnixFS file types.
19 type FileType int32
20
21 const (
22 - TRaw = FileType(unixfs.TRaw)
23 - TFile = FileType(unixfs.TFile)
24 - TDirectory = FileType(unixfs.TDirectory)
25 - TMetadata = FileType(unixfs.TMetadata)
26 - TSymlink = FileType(unixfs.TSymlink)
27 - THAMTShard = FileType(unixfs.THAMTShard)
22 + // TUnknown means the file type isn't known (e.g., it hasn't been
23 + // resolved).
24 + TUnknown FileType = iota
25 + // TFile is a regular file.
26 + TFile
27 + // TDirectory is a directory.
28 + TDirectory
29 + // TSymlink is a symlink.
30 + TSymlink
31 )
32
30 -type LsLink struct {
31 - Link *ipld.Link
32 - Size uint64
33 - Type FileType
33 +// DirEntry is a directory entry returned by `Ls`.
34 +type DirEntry struct {
35 + Name string
36 + Cid cid.Cid
37 +
38 + // Only filled when asked to resolve the directory entry.
39 + Size uint64 // The size of the file in bytes (or the size of the symlink).
40 + Type FileType // The type of the file.
41 + Target Path // The symlink target (if a symlink).
42
43 Err error
44 }
@@ -51,5 +59,5 @@ type UnixfsAPI interface {
59
60 // Ls returns the list of links in a directory. Links aren't guaranteed to be
61 // returned in order
54 - Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan LsLink, error)
62 + Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan DirEntry, error)
63 }