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

Commit 585a142

Browse files
authored
fix: do not list raw nodes in a dag as directories (#3155)
`ipfs ls QMfoo` was listing raw nodes as directories when present in a DAG. Also does not try to parse chmod strings as octals to allow `+x` etc on the command line the same as the programattic API
1 parent 335c13d commit 585a142

File tree

7 files changed

+68
-17
lines changed

7 files changed

+68
-17
lines changed

packages/ipfs/src/cli/commands/files/chmod.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict'
22

33
const {
4-
asBoolean,
5-
asOctal
4+
asBoolean
65
} = require('../../utils')
76
const parseDuration = require('parse-duration').default
87

@@ -17,8 +16,7 @@ module.exports = {
1716
describe: 'The MFS path to change the mode of'
1817
},
1918
mode: {
20-
type: 'int',
21-
coerce: asOctal,
19+
type: 'string',
2220
describe: 'The mode to use'
2321
},
2422
recursive: {

packages/ipfs/src/core/utils.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ const mapFile = (file, options) => {
155155
name: file.name,
156156
depth: file.path.split('/').length,
157157
size: 0,
158-
type: 'dir'
158+
type: 'file'
159159
}
160160

161161
if (file.unixfs) {
162+
output.type = file.unixfs.type === 'directory' ? 'dir' : 'file'
163+
162164
if (file.unixfs.type === 'file') {
163165
output.size = file.unixfs.fileSize()
164-
output.type = 'file'
165166

166167
if (options.includeContent) {
167168
output.content = file.content()

packages/ipfs/src/http/api/resources/files-regular.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,11 @@ exports.ls = {
405405
Hash: cidToString(link.cid, { base: cidBase }),
406406
Size: link.size,
407407
Type: toTypeCode(link.type),
408-
Depth: link.depth,
409-
Mode: link.mode.toString(8).padStart(4, '0')
408+
Depth: link.depth
409+
}
410+
411+
if (link.mode != null) {
412+
output.Mode = link.mode.toString(8).padStart(4, '0')
410413
}
411414

412415
if (link.mtime) {

packages/ipfs/src/http/api/resources/files/chmod.js

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const mfsChmod = {
5656
timeout
5757
}
5858
} = request
59+
5960
await ipfs.files.chmod(path, mode, {
6061
recursive,
6162
hashAlg,

packages/ipfs/test/cli/files/chmod.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ describe('chmod', () => {
3232
expect(ipfs.files.chmod.callCount).to.equal(1)
3333
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
3434
path,
35-
parseInt(mode, 8),
35+
mode,
36+
defaultOptions
37+
])
38+
})
39+
40+
it('should update the mode for a file with a string', async () => {
41+
await cli(`files chmod +x ${path}`, { ipfs })
42+
43+
expect(ipfs.files.chmod.callCount).to.equal(1)
44+
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
45+
path,
46+
'+x',
3647
defaultOptions
3748
])
3849
})
@@ -43,7 +54,7 @@ describe('chmod', () => {
4354
expect(ipfs.files.chmod.callCount).to.equal(1)
4455
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
4556
path,
46-
parseInt(mode, 8), {
57+
mode, {
4758
...defaultOptions,
4859
recursive: true
4960
}
@@ -56,7 +67,7 @@ describe('chmod', () => {
5667
expect(ipfs.files.chmod.callCount).to.equal(1)
5768
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
5869
path,
59-
parseInt(mode, 8), {
70+
mode, {
6071
...defaultOptions,
6172
recursive: true
6273
}
@@ -69,7 +80,7 @@ describe('chmod', () => {
6980
expect(ipfs.files.chmod.callCount).to.equal(1)
7081
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
7182
path,
72-
parseInt(mode, 8), {
83+
mode, {
7384
...defaultOptions,
7485
flush: false
7586
}
@@ -82,7 +93,7 @@ describe('chmod', () => {
8293
expect(ipfs.files.chmod.callCount).to.equal(1)
8394
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
8495
path,
85-
parseInt(mode, 8), {
96+
mode, {
8697
...defaultOptions,
8798
flush: false
8899
}
@@ -95,7 +106,7 @@ describe('chmod', () => {
95106
expect(ipfs.files.chmod.callCount).to.equal(1)
96107
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
97108
path,
98-
parseInt(mode, 8), {
109+
mode, {
99110
...defaultOptions,
100111
hashAlg: 'sha3-256'
101112
}
@@ -108,7 +119,7 @@ describe('chmod', () => {
108119
expect(ipfs.files.chmod.callCount).to.equal(1)
109120
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
110121
path,
111-
parseInt(mode, 8), {
122+
mode, {
112123
...defaultOptions,
113124
hashAlg: 'sha3-256'
114125
}
@@ -121,7 +132,7 @@ describe('chmod', () => {
121132
expect(ipfs.files.chmod.callCount).to.equal(1)
122133
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
123134
path,
124-
parseInt(mode, 8), {
135+
mode, {
125136
...defaultOptions,
126137
shardSplitThreshold: 10
127138
}
@@ -134,7 +145,7 @@ describe('chmod', () => {
134145
expect(ipfs.files.chmod.callCount).to.equal(1)
135146
expect(ipfs.files.chmod.getCall(0).args).to.deep.equal([
136147
path,
137-
parseInt(mode, 8), {
148+
mode, {
138149
...defaultOptions,
139150
timeout: 1000
140151
}

packages/ipfs/test/http-api/inject/files.js

+27
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,33 @@ describe('/files', () => {
370370
})
371371
})
372372

373+
it('should list directory contents without unixfs v1.5 fields', async () => {
374+
ipfs.ls.withArgs(`${cid}`, defaultOptions).returns([{
375+
name: 'link',
376+
cid,
377+
size: 10,
378+
type: 'file',
379+
depth: 1
380+
}])
381+
382+
const res = await http({
383+
method: 'POST',
384+
url: `/api/v0/ls?arg=${cid}`
385+
}, { ipfs })
386+
387+
expect(res).to.have.property('statusCode', 200)
388+
expect(res).to.have.deep.nested.property('result.Objects[0]', {
389+
Hash: `${cid}`,
390+
Links: [{
391+
Depth: 1,
392+
Hash: cid.toString(),
393+
Name: 'link',
394+
Size: 10,
395+
Type: 2
396+
}]
397+
})
398+
})
399+
373400
it('should list directory contents recursively', async () => {
374401
ipfs.ls.withArgs(`${cid}`, {
375402
...defaultOptions,

packages/ipfs/test/http-api/inject/mfs/chmod.js

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ describe('/files/chmod', () => {
4343
expect(ipfs.files.chmod.calledWith(path, mode, defaultOptions)).to.be.true()
4444
})
4545

46+
it('should update the mode for a file as a string', async () => {
47+
await http({
48+
method: 'POST',
49+
url: `/api/v0/files/chmod?arg=${path}&mode=-x`
50+
}, { ipfs })
51+
52+
expect(ipfs.files.chmod.callCount).to.equal(1)
53+
expect(ipfs.files.chmod.calledWith(path, '-x', defaultOptions)).to.be.true()
54+
})
55+
4656
it('should update the mode recursively', async () => {
4757
await http({
4858
method: 'POST',

0 commit comments

Comments
 (0)