Commit dbee8cc1 authored by Steven Allen's avatar Steven Allen

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.
parent ed12f3e4
...@@ -750,26 +750,25 @@ func (tp *provider) TestLs(t *testing.T) { ...@@ -750,26 +750,25 @@ func (tp *provider) TestLs(t *testing.T) {
t.Error(err) t.Error(err)
} }
links, err := api.Unixfs().Ls(ctx, p) entries, err := api.Unixfs().Ls(ctx, p)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
linkRes := <-links entry := <-entries
if linkRes.Err != nil { if entry.Err != nil {
t.Fatal(linkRes.Err) t.Fatal(entry.Err)
} }
link := linkRes.Link if entry.Size != 15 {
if linkRes.Size != 15 { t.Fatalf("expected size = 15, got %d", entry.Size)
t.Fatalf("expected size = 15, got %d", link.Size)
} }
if link.Name != "name-of-file" { if entry.Name != "name-of-file" {
t.Fatalf("expected name = name-of-file, got %s", link.Name) t.Fatalf("expected name = name-of-file, got %s", entry.Name)
} }
if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" { if entry.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid) 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") t.Errorf("didn't expect a second link")
if l.Err != nil { if l.Err != nil {
t.Error(l.Err) t.Error(l.Err)
......
...@@ -4,9 +4,8 @@ import ( ...@@ -4,9 +4,8 @@ import (
"context" "context"
"github.com/ipfs/interface-go-ipfs-core/options" "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/go-ipfs-files" cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format" files "github.com/ipfs/go-ipfs-files"
"github.com/ipfs/go-unixfs"
) )
type AddEvent struct { type AddEvent struct {
...@@ -16,21 +15,30 @@ type AddEvent struct { ...@@ -16,21 +15,30 @@ type AddEvent struct {
Size string `json:",omitempty"` Size string `json:",omitempty"`
} }
// FileType is an enum of possible UnixFS file types.
type FileType int32 type FileType int32
const ( const (
TRaw = FileType(unixfs.TRaw) // TUnknown means the file type isn't known (e.g., it hasn't been
TFile = FileType(unixfs.TFile) // resolved).
TDirectory = FileType(unixfs.TDirectory) TUnknown FileType = iota
TMetadata = FileType(unixfs.TMetadata) // TFile is a regular file.
TSymlink = FileType(unixfs.TSymlink) TFile
THAMTShard = FileType(unixfs.THAMTShard) // TDirectory is a directory.
TDirectory
// TSymlink is a symlink.
TSymlink
) )
type LsLink struct { // DirEntry is a directory entry returned by `Ls`.
Link *ipld.Link type DirEntry struct {
Size uint64 Name string
Type FileType 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 Err error
} }
...@@ -51,5 +59,5 @@ type UnixfsAPI interface { ...@@ -51,5 +59,5 @@ type UnixfsAPI interface {
// Ls returns the list of links in a directory. Links aren't guaranteed to be // Ls returns the list of links in a directory. Links aren't guaranteed to be
// returned in order // returned in order
Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan LsLink, error) Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan DirEntry, error)
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment