@cryptotaxi247 / kubo / commits / 3003f9385

Fix "files stat" to work on raw nodes.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Oct 19, 2017 at 16:08 UTC 3003f93854681ba4296cffaf542f1786da8c0dca
2 files changed +46 -25
core/commands/files/files.go
+33 -25
@@ -168,38 +168,46 @@ func statNode(ds dag.DAGService, fsn mfs.FSNode) (*Object, error) {
168
169 c := nd.Cid()
170
171 - pbnd, ok := nd.(*dag.ProtoNode)
172 - if !ok {
173 - return nil, dag.ErrNotProtobuf
174 - }
175 -
176 - d, err := ft.FromBytes(pbnd.Data())
177 - if err != nil {
178 - return nil, err
179 - }
180 -
171 cumulsize, err := nd.Size()
172 if err != nil {
173 return nil, err
174 }
175
186 - var ndtype string
187 - switch fsn.Type() {
188 - case mfs.TDir:
189 - ndtype = "directory"
190 - case mfs.TFile:
191 - ndtype = "file"
176 + switch n := nd.(type) {
177 + case *dag.ProtoNode:
178 + d, err := ft.FromBytes(n.Data())
179 + if err != nil {
180 + return nil, err
181 + }
182 +
183 + var ndtype string
184 + switch fsn.Type() {
185 + case mfs.TDir:
186 + ndtype = "directory"
187 + case mfs.TFile:
188 + ndtype = "file"
189 + default:
190 + return nil, fmt.Errorf("unrecognized node type: %s", fsn.Type())
191 + }
192 +
193 + return &Object{
194 + Hash: c.String(),
195 + Blocks: len(nd.Links()),
196 + Size: d.GetFilesize(),
197 + CumulativeSize: cumulsize,
198 + Type: ndtype,
199 + }, nil
200 + case *dag.RawNode:
201 + return &Object{
202 + Hash: c.String(),
203 + Blocks: 0,
204 + Size: cumulsize,
205 + CumulativeSize: cumulsize,
206 + Type: "file",
207 + }, nil
208 default:
193 - return nil, fmt.Errorf("Unrecognized node type: %s", fsn.Type())
209 + return nil, fmt.Errorf("not unixfs node (proto or raw)")
210 }
195 -
196 - return &Object{
197 - Hash: c.String(),
198 - Blocks: len(nd.Links()),
199 - Size: d.GetFilesize(),
200 - CumulativeSize: cumulsize,
201 - Type: ndtype,
202 - }, nil
211 }
212
213 var FilesCpCmd = &cmds.Command{
test/sharness/t0250-files-api.sh
+13
@@ -177,6 +177,19 @@ test_files_api() {
177 test_cmp ls_l_expected ls_l_actual
178 '
179
180 + test_expect_success "can stat file $EXTRA" '
181 + ipfs files stat /cats/file1 > file1stat_orig
182 + '
183 +
184 + test_expect_success "stat output looks good" '
185 + grep -v CumulativeSize: file1stat_orig > file1stat_actual &&
186 + echo "$FILE1" > file1stat_expect &&
187 + echo "Size: 4" >> file1stat_expect &&
188 + echo "ChildBlocks: 0" >> file1stat_expect &&
189 + echo "Type: file" >> file1stat_expect &&
190 + test_cmp file1stat_expect file1stat_actual
191 + '
192 +
193 test_expect_success "can read file $EXTRA" '
194 ipfs files read /cats/file1 > file1out
195 '