From dbee8cc1adb3b53a10ea33add0584b030f92106a Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 4 Mar 2019 20:11:38 -0800 Subject: [PATCH 1/6] 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. --- tests/unixfs.go | 23 +++++++++++------------ unixfs.go | 36 ++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/tests/unixfs.go b/tests/unixfs.go index bcb5331..a0c33c0 100644 --- a/tests/unixfs.go +++ b/tests/unixfs.go @@ -750,26 +750,25 @@ func (tp *provider) TestLs(t *testing.T) { t.Error(err) } - links, err := api.Unixfs().Ls(ctx, p) + entries, err := api.Unixfs().Ls(ctx, p) if err != nil { t.Error(err) } - linkRes := <-links - if linkRes.Err != nil { - t.Fatal(linkRes.Err) + entry := <-entries + if entry.Err != nil { + t.Fatal(entry.Err) } - link := linkRes.Link - if linkRes.Size != 15 { - t.Fatalf("expected size = 15, got %d", link.Size) + if entry.Size != 15 { + t.Fatalf("expected size = 15, got %d", entry.Size) } - if link.Name != "name-of-file" { - t.Fatalf("expected name = name-of-file, got %s", link.Name) + if entry.Name != "name-of-file" { + t.Fatalf("expected name = name-of-file, got %s", entry.Name) } - if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" { - t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid) + if entry.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" { + t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", entry.Cid) } - if l, ok := <-links; ok { + if l, ok := <-entries; ok { t.Errorf("didn't expect a second link") if l.Err != nil { t.Error(l.Err) diff --git a/unixfs.go b/unixfs.go index 5aae00d..bdf08b5 100644 --- a/unixfs.go +++ b/unixfs.go @@ -4,9 +4,8 @@ import ( "context" "github.com/ipfs/interface-go-ipfs-core/options" - "github.com/ipfs/go-ipfs-files" - ipld "github.com/ipfs/go-ipld-format" - "github.com/ipfs/go-unixfs" + cid "github.com/ipfs/go-cid" + files "github.com/ipfs/go-ipfs-files" ) type AddEvent struct { @@ -16,21 +15,30 @@ type AddEvent struct { Size string `json:",omitempty"` } +// FileType is an enum of possible UnixFS file types. type FileType int32 const ( - TRaw = FileType(unixfs.TRaw) - TFile = FileType(unixfs.TFile) - TDirectory = FileType(unixfs.TDirectory) - TMetadata = FileType(unixfs.TMetadata) - TSymlink = FileType(unixfs.TSymlink) - THAMTShard = FileType(unixfs.THAMTShard) + // TUnknown means the file type isn't known (e.g., it hasn't been + // resolved). + TUnknown FileType = iota + // TFile is a regular file. + TFile + // TDirectory is a directory. + TDirectory + // TSymlink is a symlink. + TSymlink ) -type LsLink struct { - Link *ipld.Link - Size uint64 - Type FileType +// DirEntry is a directory entry returned by `Ls`. +type DirEntry struct { + Name string + Cid cid.Cid + + // Only filled when asked to resolve the directory entry. + Size uint64 // The size of the file in bytes (or the size of the symlink). + Type FileType // The type of the file. + Target Path // The symlink target (if a symlink). Err error } @@ -51,5 +59,5 @@ type UnixfsAPI interface { // Ls returns the list of links in a directory. Links aren't guaranteed to be // returned in order - Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan LsLink, error) + Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan DirEntry, error) } From 4e99a8e9250040b9cfc9600641d138fca8ff01f9 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 5 Mar 2019 09:36:58 -0800 Subject: [PATCH 2/6] file type: add stringer --- unixfs.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/unixfs.go b/unixfs.go index bdf08b5..d0e3ec5 100644 --- a/unixfs.go +++ b/unixfs.go @@ -30,6 +30,21 @@ const ( TSymlink ) +func (t FileType) String() string { + switch t { + case TUnknown: + return "unknown" + case TFile: + return "file" + case TDirectory: + return "directory" + case TSymlink: + return "symlink" + default: + return "" + } +} + // DirEntry is a directory entry returned by `Ls`. type DirEntry struct { Name string From 5c6a751986f6d5fe1174819442fcd5f60e0a6f7d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 5 Mar 2019 09:37:40 -0800 Subject: [PATCH 3/6] tests: add symlink target test (also, fix some error versus fatal nits) --- tests/unixfs.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/unixfs.go b/tests/unixfs.go index a0c33c0..b8b22e5 100644 --- a/tests/unixfs.go +++ b/tests/unixfs.go @@ -737,22 +737,23 @@ func (tp *provider) TestLs(t *testing.T) { defer cancel() api, err := tp.makeAPI(ctx) if err != nil { - t.Error(err) + t.Fatal(err) } r := strings.NewReader("content-of-file") p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ "0": files.NewMapDirectory(map[string]files.Node{ - "name-of-file": files.NewReaderFile(r), + "name-of-file": files.NewReaderFile(r), + "name-of-symlink": files.NewLinkFile("/foo/bar", nil), }), })) if err != nil { - t.Error(err) + t.Fatal(err) } entries, err := api.Unixfs().Ls(ctx, p) if err != nil { - t.Error(err) + t.Fatal(err) } entry := <-entries @@ -760,13 +761,33 @@ func (tp *provider) TestLs(t *testing.T) { t.Fatal(entry.Err) } if entry.Size != 15 { - t.Fatalf("expected size = 15, got %d", entry.Size) + t.Errorf("expected size = 15, got %d", entry.Size) } if entry.Name != "name-of-file" { - t.Fatalf("expected name = name-of-file, got %s", entry.Name) + t.Errorf("expected name = name-of-file, got %s", entry.Name) + } + if entry.Type != coreiface.TFile { + t.Errorf("wrong type %s", entry.Type) } if entry.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" { - t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", entry.Cid) + t.Errorf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", entry.Cid) + } + entry = <-entries + if entry.Err != nil { + t.Fatal(entry.Err) + } + if entry.Type != coreiface.TSymlink { + t.Errorf("wrong type %s", entry.Type) + } + if entry.Name != "name-of-symlink" { + t.Errorf("expected name = name-of-symlink, got %s", entry.Name) + } + if entry.Target.String() != "/foo/bar" { + t.Errorf("expected symlink target to be /foo/bar, got %s", entry.Target) + } + + if int(entry.Size) != len(entry.Target.String()) { + t.Errorf("expected size = %d, got %d", len(entry.Target.String()), entry.Size) } if l, ok := <-entries; ok { t.Errorf("didn't expect a second link") From 368881fa4a30814112d1b2096c37c91f5fd16976 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 5 Mar 2019 09:54:43 -0800 Subject: [PATCH 4/6] switch symlink target type to string (path can't represent relative paths) --- tests/unixfs.go | 6 +++--- unixfs.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unixfs.go b/tests/unixfs.go index b8b22e5..79dedf1 100644 --- a/tests/unixfs.go +++ b/tests/unixfs.go @@ -782,12 +782,12 @@ func (tp *provider) TestLs(t *testing.T) { if entry.Name != "name-of-symlink" { t.Errorf("expected name = name-of-symlink, got %s", entry.Name) } - if entry.Target.String() != "/foo/bar" { + if entry.Target != "/foo/bar" { t.Errorf("expected symlink target to be /foo/bar, got %s", entry.Target) } - if int(entry.Size) != len(entry.Target.String()) { - t.Errorf("expected size = %d, got %d", len(entry.Target.String()), entry.Size) + if int(entry.Size) != len(entry.Target) { + t.Errorf("expected size = %d, got %d", len(entry.Target), entry.Size) } if l, ok := <-entries; ok { t.Errorf("didn't expect a second link") diff --git a/unixfs.go b/unixfs.go index d0e3ec5..f9508f1 100644 --- a/unixfs.go +++ b/unixfs.go @@ -53,7 +53,7 @@ type DirEntry struct { // Only filled when asked to resolve the directory entry. Size uint64 // The size of the file in bytes (or the size of the symlink). Type FileType // The type of the file. - Target Path // The symlink target (if a symlink). + Target string // The symlink target (if a symlink). Err error } From 7a7cf9694be27b62820f05e2ac11bb4a57bab982 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 6 Mar 2019 16:41:05 -0800 Subject: [PATCH 5/6] remove target size requirement It's complicated. We need to carefully think through how sizes work. --- tests/unixfs.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unixfs.go b/tests/unixfs.go index 79dedf1..bbcb668 100644 --- a/tests/unixfs.go +++ b/tests/unixfs.go @@ -786,9 +786,6 @@ func (tp *provider) TestLs(t *testing.T) { t.Errorf("expected symlink target to be /foo/bar, got %s", entry.Target) } - if int(entry.Size) != len(entry.Target) { - t.Errorf("expected size = %d, got %d", len(entry.Target), entry.Size) - } if l, ok := <-entries; ok { t.Errorf("didn't expect a second link") if l.Err != nil { From 4ba2c7db714fbe2086287930da94959f10ff5fed Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 6 Mar 2019 16:42:06 -0800 Subject: [PATCH 6/6] gx publish 0.1.12 --- .gx/lastpubver | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gx/lastpubver b/.gx/lastpubver index 1515479..229c6c1 100644 --- a/.gx/lastpubver +++ b/.gx/lastpubver @@ -1 +1 @@ -0.1.11: QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6 +0.1.12: QmY1veddRDFZZBUFp2Mysw7mf2sSmbx1ZGH5v11tTMCYN5 diff --git a/package.json b/package.json index 3e50b56..4cec815 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,6 @@ "license": "", "name": "interface-go-ipfs-core", "releaseCmd": "git commit -a -m \"gx publish $VERSION\"", - "version": "0.1.11" + "version": "0.1.12" }