Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 5969fed

Browse files
MichaelMuredaviddias
authored andcommitted
feat: complete files.stat with the 'with-local' option (#227)
* complete files.stat with the 'with-local' option * add tests for ipfs files stat --with-local
1 parent c4934ca commit 5969fed

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

SPEC/FILES.md

+5
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ Where:
618618
- `options` is an optional Object that might contain the following keys:
619619
- `hash` is a Boolean value to return only the hash.
620620
- `size` is a Boolean value to return only the size.
621+
- `withLocal` is a Boolean value to compute the amount of the dag that is local, and if possible the total size.
621622

622623
`callback` must follow the `function (err, stat) {}` signature, where `err` is an Error if the operation was not successful and `stat` is an Object with the following keys:
623624

@@ -626,6 +627,10 @@ Where:
626627
- `cumulativeSize` is an integer with the cumulative size in Bytes.
627628
- `blocks` is an integer indicating the number of blocks.
628629
- `type` is a string that can be either `directory` or `file`.
630+
- `withLocality` is a boolean to indicate if locality information are present.
631+
- `local` is a boolean to indicate if the queried dag is fully present locally.
632+
- `sizeLocal` is an integer indicating the cumulative size of the data present locally.
633+
629634

630635
If no `callback` is passed, a promise is returned.
631636

js/src/files-mfs.js

+52-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ module.exports = (common) => {
276276
blocks: 1,
277277
size: 13,
278278
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T',
279-
cumulativeSize: 71
279+
cumulativeSize: 71,
280+
withLocality: false,
281+
local: undefined,
282+
sizeLocal: undefined
280283
})
281284
done()
282285
})
@@ -295,7 +298,54 @@ module.exports = (common) => {
295298
blocks: 2,
296299
size: 0,
297300
hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto',
298-
cumulativeSize: 216
301+
cumulativeSize: 216,
302+
withLocality: false,
303+
local: undefined,
304+
sizeLocal: undefined
305+
})
306+
done()
307+
})
308+
})
309+
310+
it('stat withLocal file', function (done) {
311+
if (!withGo) {
312+
console.log('Not supported in js-ipfs yet')
313+
this.skip()
314+
}
315+
316+
ipfs.files.stat('/test/b', {'withLocal': true}, (err, stat) => {
317+
expect(err).to.not.exist()
318+
expect(stat).to.eql({
319+
type: 'file',
320+
blocks: 1,
321+
size: 13,
322+
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T',
323+
cumulativeSize: 71,
324+
withLocality: true,
325+
local: true,
326+
sizeLocal: 71
327+
})
328+
done()
329+
})
330+
})
331+
332+
it('stat withLocal dir', function (done) {
333+
if (!withGo) {
334+
console.log('Not supported in js-ipfs yet')
335+
this.skip()
336+
}
337+
338+
ipfs.files.stat('/test', {'withLocal': true}, (err, stat) => {
339+
expect(err).to.not.exist()
340+
expect(stat).to.eql({
341+
type: 'directory',
342+
blocks: 2,
343+
size: 0,
344+
hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto',
345+
cumulativeSize: 216,
346+
withLocality: true,
347+
local: true,
348+
sizeLocal: 216
299349
})
300350
done()
301351
})

js/src/files.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = (common) => {
2424
this.timeout(40 * 1000)
2525

2626
let ipfs
27+
let withGo
2728

2829
function fixture (path) {
2930
return loadFixture(path, 'interface-ipfs-core')
@@ -60,7 +61,11 @@ module.exports = (common) => {
6061
factory.spawnNode((err, node) => {
6162
expect(err).to.not.exist()
6263
ipfs = node
63-
done()
64+
node.id((err, id) => {
65+
expect(err).to.not.exist()
66+
withGo = id.agentVersion.startsWith('go-ipfs')
67+
done()
68+
})
6469
})
6570
})
6671
})
@@ -1002,5 +1007,31 @@ module.exports = (common) => {
10021007
)
10031008
})
10041009
})
1010+
1011+
describe('.stat', () => {
1012+
before((done) => ipfs.files.add(smallFile.data, done))
1013+
1014+
it('stat outside of mfs', function (done) {
1015+
if (!withGo) {
1016+
console.log('Not supported in js-ipfs yet')
1017+
this.skip()
1018+
}
1019+
1020+
ipfs.files.stat('/ipfs/' + smallFile.cid, (err, stat) => {
1021+
expect(err).to.not.exist()
1022+
expect(stat).to.eql({
1023+
type: 'file',
1024+
blocks: 0,
1025+
size: 12,
1026+
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
1027+
cumulativeSize: 20,
1028+
withLocality: false,
1029+
local: undefined,
1030+
sizeLocal: undefined
1031+
})
1032+
done()
1033+
})
1034+
})
1035+
})
10051036
})
10061037
}

0 commit comments

Comments
 (0)