Skip to content

Commit

Permalink
core/commands/unixfs/ls: Explicitily record stat in LsObject
Browse files Browse the repository at this point in the history
Instead of abusing a LsLink for non-directory objects [1].

[1]: #1348 (comment)

License: MIT
Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Jun 19, 2015
1 parent a891b30 commit 2110f95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
30 changes: 14 additions & 16 deletions core/commands/unixfs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type LsLink struct {
}

type LsObject struct {
Hash string
Size uint64
Type string
Links []LsLink
}

Expand Down Expand Up @@ -85,31 +88,26 @@ 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)
return
}

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
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 11 additions & 16 deletions test/sharness/t0200-unixfs-ls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -145,6 +141,9 @@ test_ls_cmd() {
},
"Objects": {
"QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss": {
"Hash": "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss",
"Size": 0,
"Type": "Directory",
"Links": [
{
"Name": "128",
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 2110f95

Please sign in to comment.