Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

tweak the Ls interface #14

Merged
merged 6 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.11: QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6
0.1.12: QmY1veddRDFZZBUFp2Mysw7mf2sSmbx1ZGH5v11tTMCYN5
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

49 changes: 33 additions & 16 deletions tests/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,39 +737,56 @@ 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)
}

links, err := api.Unixfs().Ls(ctx, p)
entries, err := api.Unixfs().Ls(ctx, p)
if err != nil {
t.Error(err)
t.Fatal(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.Errorf("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.Errorf("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.Type != coreiface.TFile {
t.Errorf("wrong type %s", entry.Type)
}
if l, ok := <-links; ok {
if entry.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
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 != "/foo/bar" {
t.Errorf("expected symlink target to be /foo/bar, got %s", entry.Target)
}

if l, ok := <-entries; ok {
t.Errorf("didn't expect a second link")
if l.Err != nil {
t.Error(l.Err)
Expand Down
51 changes: 37 additions & 14 deletions unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,21 +15,45 @@ 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
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 "<unknown file type>"
}
}

// 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 string // The symlink target (if a symlink).

Err error
}
Expand All @@ -51,5 +74,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)
}