Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 83eb99b

Browse files
committed
fix: fix ls crash (#2546)
* sanitises input to replace multiple slashes with single slashes * removes trailing slashes * uses `0` for padding when `link.depth` results in negative padding N.b will be resolved in a better way by ipfs/js-ipfs-unixfs-exporter#24
1 parent d4d6dfe commit 83eb99b

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/cli/commands/ls.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ module.exports = {
4848
const multihashWidth = Math.max.apply(null, links.map((file) => file.hash.length))
4949
const sizeWidth = Math.max.apply(null, links.map((file) => String(file.size).length))
5050

51+
// replace multiple slashes
52+
key = key.replace(/\/(\/+)/g, '/')
53+
54+
// strip trailing flash
55+
if (key.endsWith('/')) {
56+
key = key.replace(/(\/+)$/, '')
57+
}
58+
5159
let pathParts = key.split('/')
5260

5361
if (key.startsWith('/ipfs/')) {
@@ -56,7 +64,10 @@ module.exports = {
5664

5765
links.forEach(link => {
5866
const fileName = link.type === 'dir' ? `${link.name || ''}/` : link.name
59-
const padding = link.depth - pathParts.length
67+
68+
// todo: fix this by resolving https://github.com/ipfs/js-ipfs-unixfs-exporter/issues/24
69+
const padding = Math.max(link.depth - pathParts.length, 0)
70+
6071
print(
6172
rightpad(link.hash, multihashWidth + 1) +
6273
rightpad(link.size || '-', sizeWidth + 1) +

test/cli/ls.js

+48
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,54 @@ describe('ls', () => runOnAndOff((thing) => {
2525
)
2626
})
2727

28+
it('supports a trailing slash', async function () {
29+
this.timeout(20 * 1000)
30+
const out = await ipfs('ls Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z/')
31+
expect(out).to.eql(
32+
'QmamKEPmEH9RUsqRQsfNf5evZQDQPYL9KXg1ADeT7mkHkT - blocks/\n' +
33+
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3928 config\n' +
34+
'QmUqyZtPmsRy1U5Mo8kz2BAMmk1hfJ7yW1KAFTMB2odsFv - datastore/\n' +
35+
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU - init-docs/\n' +
36+
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 2 version\n'
37+
)
38+
})
39+
40+
it('supports multiple trailing slashes', async function () {
41+
this.timeout(20 * 1000)
42+
const out = await ipfs('ls Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z///')
43+
expect(out).to.eql(
44+
'QmamKEPmEH9RUsqRQsfNf5evZQDQPYL9KXg1ADeT7mkHkT - blocks/\n' +
45+
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3928 config\n' +
46+
'QmUqyZtPmsRy1U5Mo8kz2BAMmk1hfJ7yW1KAFTMB2odsFv - datastore/\n' +
47+
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU - init-docs/\n' +
48+
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 2 version\n'
49+
)
50+
})
51+
52+
it('supports multiple intermediate slashes', async function () {
53+
this.timeout(20 * 1000)
54+
const out = await ipfs('ls Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z///init-docs')
55+
expect(out).to.eql(
56+
'QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V 1677 about\n' +
57+
'QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y 189 contact\n' +
58+
'QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC - docs/\n' +
59+
'QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 311 help\n' +
60+
'QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha 1717 quick-start\n' +
61+
'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB 1091 readme\n' +
62+
'QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ 1016 security-notes\n' +
63+
'QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te - tour/\n'
64+
)
65+
})
66+
67+
it('supports recursive listing through intermediate directories', async function () {
68+
this.timeout(20 * 1000)
69+
const out = await ipfs('ls -r Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z/blocks/CIQLB')
70+
expect(out).to.eql(
71+
'QmQ8ag7ysVyCMzJGFjxrUStwWtniQ69c7G9aezbmsKeNYD 10849 CIQLBK52T5EHVHZY5URTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data\n' +
72+
'QmaSjzSSRanYzRGPXQY6m5SWfSkkfcnzNkurJEQc4chPJx 10807 CIQLBS5HG4PRCRQ7O4EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data\n'
73+
)
74+
})
75+
2876
it('prints nothing for non-existant hashes', async function () {
2977
if (thing.on) {
3078
// If the daemon is on, ls should search until it hits a timeout

0 commit comments

Comments
 (0)