diff --git a/core/commands/unixfs/ls.go b/core/commands/unixfs/ls.go index ed2c0b1339d..989f6576022 100644 --- a/core/commands/unixfs/ls.go +++ b/core/commands/unixfs/ls.go @@ -24,6 +24,9 @@ type LsLink struct { } type LsObject struct { + Hash string + Size uint64 + Type string Links []LsLink } @@ -85,8 +88,6 @@ directories, the child size is the IPFS link size. continue } - output.Objects[hash] = &LsObject{} - unixFSNode, err := unixfs.FromBytes(merkleNode.Data) if err != nil { res.SetError(err, cmds.ErrNormal) @@ -94,22 +95,19 @@ directories, the child size is the IPFS link size. } t := unixFSNode.GetType() + + output.Objects[hash] = &LsObject{ + Hash: key.String(), + Type: t.String(), + Size: unixFSNode.GetFilesize(), + } + switch t { default: res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation) return case unixfspb.Data_File: - key, err := merkleNode.Key() - if err != nil { - res.SetError(err, cmds.ErrNormal) - return - } - output.Objects[hash].Links = []LsLink{LsLink{ - Name: fpath, - Hash: key.String(), - Type: t.String(), - Size: unixFSNode.GetFilesize(), - }} + break case unixfspb.Data_Directory: links := make([]LsLink, len(merkleNode.Links)) output.Objects[hash].Links = links @@ -159,10 +157,10 @@ directories, the child size is the IPFS link size. return nil, fmt.Errorf("unresolved hash: %s", hash) } - if len(object.Links) == 1 && object.Links[0].Hash == hash { - nonDirectories = append(nonDirectories, argument) - } else { + if object.Type == "Directory" { directories = append(directories, argument) + } else { + nonDirectories = append(nonDirectories, argument) } } sort.Strings(nonDirectories) diff --git a/test/sharness/t0200-unixfs-ls.sh b/test/sharness/t0200-unixfs-ls.sh index 81e7ac2515d..70dcd185223 100755 --- a/test/sharness/t0200-unixfs-ls.sh +++ b/test/sharness/t0200-unixfs-ls.sh @@ -114,14 +114,10 @@ test_ls_cmd() { }, "Objects": { "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": { - "Links": [ - { - "Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024", - "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd", - "Size": 1024, - "Type": "File" - } - ] + "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd", + "Size": 1024, + "Type": "File", + "Links": null } } } @@ -145,6 +141,9 @@ test_ls_cmd() { }, "Objects": { "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss": { + "Hash": "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss", + "Size": 0, + "Type": "Directory", "Links": [ { "Name": "128", @@ -161,14 +160,10 @@ test_ls_cmd() { ] }, "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": { - "Links": [ - { - "Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024", - "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd", - "Size": 1024, - "Type": "File" - } - ] + "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd", + "Size": 1024, + "Type": "File", + "Links": null } } }