Skip to content

Commit bda81cd

Browse files
Merge pull request #5045 from schomatis/fix/cmd/files-ls-file-long
cmd: fix `files ls` to report hash and size for files
2 parents 7927196 + cbc239c commit bda81cd

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

core/commands/files.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,24 @@ Examples:
462462
return
463463
case *mfs.File:
464464
_, name := gopath.Split(path)
465-
out := &filesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}}
465+
out := &filesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name}}}
466+
if long {
467+
out.Entries[0].Type = int(fsn.Type())
468+
469+
size, err := fsn.Size()
470+
if err != nil {
471+
res.SetError(err, cmdkit.ErrNormal)
472+
return
473+
}
474+
out.Entries[0].Size = size
475+
476+
nd, err := fsn.GetNode()
477+
if err != nil {
478+
res.SetError(err, cmdkit.ErrNormal)
479+
return
480+
}
481+
out.Entries[0].Hash = nd.Cid().String()
482+
}
466483
res.SetOutput(out)
467484
return
468485
default:

test/sharness/t0250-files-api.sh

+12
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ test_files_api() {
186186
test_cmp ls_l_expected ls_l_actual
187187
'
188188

189+
test_expect_success "file has correct hash and size listed with -l" '
190+
echo "file1 $FILE1 4" > ls_l_expected &&
191+
ipfs files ls -l /cats/file1 > ls_l_actual &&
192+
test_cmp ls_l_expected ls_l_actual
193+
'
194+
195+
test_expect_success "file shows up with the correct name" '
196+
echo "file1" > ls_l_expected &&
197+
ipfs files ls /cats/file1 > ls_l_actual &&
198+
test_cmp ls_l_expected ls_l_actual
199+
'
200+
189201
test_expect_success "can stat file $EXTRA" '
190202
ipfs files stat /cats/file1 > file1stat_orig
191203
'

0 commit comments

Comments
 (0)