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: W. Trevor King <wking@tremily.us>
W. Trevor King committed
Jun 18, 2015 at 17:10 UTC
4acab79d66f7d8f0c82385ec2456b3d0dcea42de
2 files changed
+25
-32
core/commands/unixfs/ls.go
+14
-16
@@ -24,6 +24,9 @@ type LsLink struct {
24
}
25
26
type LsObject struct {
27
+ Hash string
28
+ Size uint64
29
+ Type string
30
Links []LsLink
31
}
32
@@ -85,8 +88,6 @@ directories, the child size is the IPFS link size.
88
continue
89
}
90
88
- output.Objects[hash] = &LsObject{}
89
-
91
unixFSNode, err := unixfs.FromBytes(merkleNode.Data)
92
if err != nil {
93
res.SetError(err, cmds.ErrNormal)
@@ -94,22 +95,19 @@ directories, the child size is the IPFS link size.
95
}
96
97
t := unixFSNode.GetType()
98
+
99
+ output.Objects[hash] = &LsObject{
100
+ Hash: key.String(),
101
+ Type: t.String(),
102
+ Size: unixFSNode.GetFilesize(),
103
+ }
104
+
105
switch t {
106
default:
107
res.SetError(fmt.Errorf("unrecognized type: %s", t), cmds.ErrImplementation)
108
return
109
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
111
case unixfspb.Data_Directory:
112
links := make([]LsLink, len(merkleNode.Links))
113
output.Objects[hash].Links = links
@@ -159,10 +157,10 @@ directories, the child size is the IPFS link size.
157
return nil, fmt.Errorf("unresolved hash: %s", hash)
158
}
159
162
- if len(object.Links) == 1 && object.Links[0].Hash == hash {
163
- nonDirectories = append(nonDirectories, argument)
164
- } else {
160
+ if object.Type == "Directory" {
161
directories = append(directories, argument)
162
+ } else {
163
+ nonDirectories = append(nonDirectories, argument)
164
}
165
}
166
sort.Strings(nonDirectories)
test/sharness/t0200-unixfs-ls.sh
+11
-16
@@ -114,14 +114,10 @@ test_ls_cmd() {
114
},
115
"Objects": {
116
"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
121
}
122
}
123
}
@@ -145,6 +141,9 @@ test_ls_cmd() {
141
},
142
"Objects": {
143
"QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss": {
144
+ "Hash": "QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss",
145
+ "Size": 0,
146
+ "Type": "Directory",
147
"Links": [
148
{
149
"Name": "128",
@@ -161,14 +160,10 @@ test_ls_cmd() {
160
]
161
},
162
"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
167
}
168
}
169
}