Skip to content

Commit

Permalink
fix(unixfs): issue ipfs#5055
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Sep 26, 2018
1 parent 1e0d53f commit bdc2d9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ The JSON output contains type information.
switch link.Cid.Type() {
case cid.Raw:
// No need to check with raw leaves
t = unixfspb.Data_File
t = unixfs.TFile
case cid.DagProtobuf:
linkNode, err := link.GetNode(req.Context(), dserv)
if err == ipld.ErrNotFound && !resolve {
Expand Down Expand Up @@ -193,7 +193,7 @@ The JSON output contains type information.
fmt.Fprintln(w, "Hash\tSize\tName")
}
for _, link := range object.Links {
if link.Type == unixfspb.Data_Directory {
if link.Type == unixfs.TDirectory {
link.Name += "/"
}
fmt.Fprintf(w, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
Expand Down
11 changes: 5 additions & 6 deletions core/commands/unixfs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
e "github.com/ipfs/go-ipfs/core/commands/e"
unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
unixfspb "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/pb"
merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
Expand Down Expand Up @@ -135,13 +134,13 @@ possible, please use 'ipfs ls' instead.
}

switch t {
case unixfspb.Data_File:
case unixfs.TFile:
break
case unixfspb.Data_HAMTShard:
case unixfs.THAMTShard:
// We need a streaming ls API for this.
res.SetError(fmt.Errorf("cannot list large directories yet"), cmdkit.ErrNormal)
return
case unixfspb.Data_Directory:
case unixfs.TDirectory:
links := make([]LsLink, len(merkleNode.Links()))
output.Objects[hash].Links = links
for i, link := range merkleNode.Links() {
Expand All @@ -167,14 +166,14 @@ possible, please use 'ipfs ls' instead.
Hash: link.Cid.String(),
Type: t.String(),
}
if t == unixfspb.Data_File {
if t == unixfs.TFile {
lsLink.Size = d.FileSize()
} else {
lsLink.Size = link.Size
}
links[i] = lsLink
}
case unixfspb.Data_Symlink:
case unixfs.TSymlink:
res.SetError(fmt.Errorf("cannot list symlinks yet"), cmdkit.ErrNormal)
return
default:
Expand Down
49 changes: 25 additions & 24 deletions fuse/readonly/readonly_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

core "github.com/ipfs/go-ipfs/core"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
ftpb "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/pb"
ft "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
mdag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"

Expand All @@ -21,7 +21,6 @@ import (
fs "gx/ipfs/QmSJBsmLP1XMjv8hxYg2rUMdPDB7YUpyBo9idjrJ6Cmq6F/fuse/fs"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
proto "gx/ipfs/QmdxUuburamoF6zF9qjeQC4WYcWGbWuRmdLacMEsW8ioD8/gogo-protobuf/proto"
)

var log = logging.Logger("fuse/ipfs")
Expand Down Expand Up @@ -93,13 +92,16 @@ func (*Root) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
type Node struct {
Ipfs *core.IpfsNode
Nd ipld.Node
cached *ftpb.Data
cached *ft.FSNode
}

func (s *Node) loadData() error {
if pbnd, ok := s.Nd.(*mdag.ProtoNode); ok {
s.cached = new(ftpb.Data)
return proto.Unmarshal(pbnd.Data(), s.cached)
fsn, err := ft.FSNodeFromBytes(pbnd.Data())
if err != nil {
return err
}
s.cached = fsn
}
return nil
}
Expand All @@ -119,23 +121,23 @@ func (s *Node) Attr(ctx context.Context, a *fuse.Attr) error {
return fmt.Errorf("readonly: loadData() failed: %s", err)
}
}
switch s.cached.GetType() {
case ftpb.Data_Directory, ftpb.Data_HAMTShard:
switch s.cached.Type() {
case ft.TDirectory, ft.THAMTShard:
a.Mode = os.ModeDir | 0555
case ftpb.Data_File:
size := s.cached.GetFilesize()
case ft.TFile:
size := s.cached.FileSize()
a.Mode = 0444
a.Size = uint64(size)
a.Blocks = uint64(len(s.Nd.Links()))
case ftpb.Data_Raw:
case ft.TRaw:
a.Mode = 0444
a.Size = uint64(len(s.cached.GetData()))
a.Size = uint64(len(s.cached.Data()))
a.Blocks = uint64(len(s.Nd.Links()))
case ftpb.Data_Symlink:
case ft.TSymlink:
a.Mode = 0777 | os.ModeSymlink
a.Size = uint64(len(s.cached.GetData()))
a.Size = uint64(len(s.cached.Data()))
default:
return fmt.Errorf("invalid data type - %s", s.cached.GetType())
return fmt.Errorf("invalid data type - %s", s.cached.Type())
}
return nil
}
Expand Down Expand Up @@ -192,21 +194,20 @@ func (s *Node) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
case *mdag.RawNode:
t = fuse.DT_File
case *mdag.ProtoNode:
var data ftpb.Data
if err := proto.Unmarshal(nd.Data(), &data); err != nil {
if fsn, err := ft.FSNodeFromBytes(nd.Data()); err != nil {
log.Warning("failed to unmarshal protonode data field:", err)
} else {
switch data.GetType() {
case ftpb.Data_Directory, ftpb.Data_HAMTShard:
switch fsn.Type() {
case ft.TDirectory, ft.THAMTShard:
t = fuse.DT_Dir
case ftpb.Data_File, ftpb.Data_Raw:
case ft.TFile, ft.TRaw:
t = fuse.DT_File
case ftpb.Data_Symlink:
case ft.TSymlink:
t = fuse.DT_Link
case ftpb.Data_Metadata:
case ft.TMetadata:
log.Error("metadata object in fuse should contain its wrapped type")
default:
log.Error("unrecognized protonode data type: ", data.GetType())
log.Error("unrecognized protonode data type: ", fsn.Type())
}
}
}
Expand All @@ -230,10 +231,10 @@ func (s *Node) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fu
}

func (s *Node) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
if s.cached == nil || s.cached.GetType() != ftpb.Data_Symlink {
if s.cached == nil || s.cached.Type() != ft.TSymlink {
return "", fuse.Errno(syscall.EINVAL)
}
return string(s.cached.GetData()), nil
return string(s.cached.Data()), nil
}

func (s *Node) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
Expand Down

0 comments on commit bdc2d9b

Please sign in to comment.