Commit 4acab79d authored by W. Trevor King's avatar W. Trevor King

core/commands/unixfs/ls: Explicitily record stat in LsObject

Instead of abusing a LsLink for non-directory objects [1].

[1]: https://github.com/ipfs/go-ipfs/pull/1348#discussion_r32680669

License: MIT
Signed-off-by: default avatarW. Trevor King <wking@tremily.us>
parent 5fd4812b
...@@ -24,6 +24,9 @@ type LsLink struct { ...@@ -24,6 +24,9 @@ type LsLink struct {
} }
type LsObject struct { type LsObject struct {
Hash string
Size uint64
Type string
Links []LsLink Links []LsLink
} }
...@@ -85,8 +88,6 @@ directories, the child size is the IPFS link size. ...@@ -85,8 +88,6 @@ directories, the child size is the IPFS link size.
continue continue
} }
output.Objects[hash] = &LsObject{}
unixFSNode, err := unixfs.FromBytes(merkleNode.Data) unixFSNode, err := unixfs.FromBytes(merkleNode.Data)
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
...@@ -94,22 +95,19 @@ directories, the child size is the IPFS link size. ...@@ -94,22 +95,19 @@ directories, the child size is the IPFS link size.
} }
t := unixFSNode.GetType() t := unixFSNode.GetType()
output.Objects[hash] = &LsObject{
Hash: key.String(),
Type: t.String(),
Size: unixFSNode.GetFilesize(),
}
switch t { switch t {
default: default:
res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation) res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation)
return return
case unixfspb.Data_File: case unixfspb.Data_File:
key, err := merkleNode.Key() break
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(),
}}
case unixfspb.Data_Directory: case unixfspb.Data_Directory:
links := make([]LsLink, len(merkleNode.Links)) links := make([]LsLink, len(merkleNode.Links))
output.Objects[hash].Links = links output.Objects[hash].Links = links
...@@ -159,10 +157,10 @@ directories, the child size is the IPFS link size. ...@@ -159,10 +157,10 @@ directories, the child size is the IPFS link size.
return nil, fmt.Errorf("unresolved hash: %s", hash) return nil, fmt.Errorf("unresolved hash: %s", hash)
} }
if len(object.Links) == 1 && object.Links[0].Hash == hash { if object.Type == "Directory" {
nonDirectories = append(nonDirectories, argument)
} else {
directories = append(directories, argument) directories = append(directories, argument)
} else {
nonDirectories = append(nonDirectories, argument)
} }
} }
sort.Strings(nonDirectories) sort.Strings(nonDirectories)
......
...@@ -114,14 +114,10 @@ test_ls_cmd() { ...@@ -114,14 +114,10 @@ test_ls_cmd() {
}, },
"Objects": { "Objects": {
"QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": { "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": {
"Links": [ "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
{ "Size": 1024,
"Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024", "Type": "File",
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd", "Links": null
"Size": 1024,
"Type": "File"
}
]
} }
} }
} }
...@@ -145,6 +141,9 @@ test_ls_cmd() { ...@@ -145,6 +141,9 @@ test_ls_cmd() {
}, },
"Objects": { "Objects": {
"QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss": { "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss": {
"Hash": "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss",
"Size": 0,
"Type": "Directory",
"Links": [ "Links": [
{ {
"Name": "128", "Name": "128",
...@@ -161,14 +160,10 @@ test_ls_cmd() { ...@@ -161,14 +160,10 @@ test_ls_cmd() {
] ]
}, },
"QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": { "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": {
"Links": [ "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
{ "Size": 1024,
"Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024", "Type": "File",
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd", "Links": null
"Size": 1024,
"Type": "File"
}
]
} }
} }
} }
......
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