Skip to content

Commit 4acab79

Browse files
committedJun 20, 2015
core/commands/unixfs/ls: Explicitily record stat in LsObject
Instead of abusing a LsLink for non-directory objects [1]. [1]: #1348 (comment) License: MIT Signed-off-by: W. Trevor King <wking@tremily.us>
1 parent 5fd4812 commit 4acab79

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed
 

‎core/commands/unixfs/ls.go

+14-16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type LsLink struct {
2424
}
2525

2626
type LsObject struct {
27+
Hash string
28+
Size uint64
29+
Type string
2730
Links []LsLink
2831
}
2932

@@ -85,31 +88,26 @@ directories, the child size is the IPFS link size.
8588
continue
8689
}
8790

88-
output.Objects[hash] = &LsObject{}
89-
9091
unixFSNode, err := unixfs.FromBytes(merkleNode.Data)
9192
if err != nil {
9293
res.SetError(err, cmds.ErrNormal)
9394
return
9495
}
9596

9697
t := unixFSNode.GetType()
98+
99+
output.Objects[hash] = &LsObject{
100+
Hash: key.String(),
101+
Type: t.String(),
102+
Size: unixFSNode.GetFilesize(),
103+
}
104+
97105
switch t {
98106
default:
99107
res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation)
100108
return
101109
case unixfspb.Data_File:
102-
key, err := merkleNode.Key()
103-
if err != nil {
104-
res.SetError(err, cmds.ErrNormal)
105-
return
106-
}
107-
output.Objects[hash].Links = []LsLink{LsLink{
108-
Name: fpath,
109-
Hash: key.String(),
110-
Type: t.String(),
111-
Size: unixFSNode.GetFilesize(),
112-
}}
110+
break
113111
case unixfspb.Data_Directory:
114112
links := make([]LsLink, len(merkleNode.Links))
115113
output.Objects[hash].Links = links
@@ -159,10 +157,10 @@ directories, the child size is the IPFS link size.
159157
return nil, fmt.Errorf("unresolved hash: %s", hash)
160158
}
161159

162-
if len(object.Links) == 1 && object.Links[0].Hash == hash {
163-
nonDirectories = append(nonDirectories, argument)
164-
} else {
160+
if object.Type == "Directory" {
165161
directories = append(directories, argument)
162+
} else {
163+
nonDirectories = append(nonDirectories, argument)
166164
}
167165
}
168166
sort.Strings(nonDirectories)

‎test/sharness/t0200-unixfs-ls.sh

+11-16
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,10 @@ test_ls_cmd() {
114114
},
115115
"Objects": {
116116
"QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": {
117-
"Links": [
118-
{
119-
"Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024",
120-
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
121-
"Size": 1024,
122-
"Type": "File"
123-
}
124-
]
117+
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
118+
"Size": 1024,
119+
"Type": "File",
120+
"Links": null
125121
}
126122
}
127123
}
@@ -145,6 +141,9 @@ test_ls_cmd() {
145141
},
146142
"Objects": {
147143
"QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss": {
144+
"Hash": "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss",
145+
"Size": 0,
146+
"Type": "Directory",
148147
"Links": [
149148
{
150149
"Name": "128",
@@ -161,14 +160,10 @@ test_ls_cmd() {
161160
]
162161
},
163162
"QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd": {
164-
"Links": [
165-
{
166-
"Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024",
167-
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
168-
"Size": 1024,
169-
"Type": "File"
170-
}
171-
]
163+
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
164+
"Size": 1024,
165+
"Type": "File",
166+
"Links": null
172167
}
173168
}
174169
}

0 commit comments

Comments
 (0)
Please sign in to comment.