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

Commit

Permalink
feat: complete files.stat with the 'with-local' option (#227)
Browse files Browse the repository at this point in the history
* complete files.stat with the 'with-local' option

* add tests for ipfs files stat --with-local
  • Loading branch information
MichaelMure authored and daviddias committed Mar 12, 2018
1 parent c4934ca commit 5969fed
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
5 changes: 5 additions & 0 deletions SPEC/FILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ Where:
- `options` is an optional Object that might contain the following keys:
- `hash` is a Boolean value to return only the hash.
- `size` is a Boolean value to return only the size.
- `withLocal` is a Boolean value to compute the amount of the dag that is local, and if possible the total size.

`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:

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


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

Expand Down
54 changes: 52 additions & 2 deletions js/src/files-mfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ module.exports = (common) => {
blocks: 1,
size: 13,
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T',
cumulativeSize: 71
cumulativeSize: 71,
withLocality: false,
local: undefined,
sizeLocal: undefined
})
done()
})
Expand All @@ -295,7 +298,54 @@ module.exports = (common) => {
blocks: 2,
size: 0,
hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto',
cumulativeSize: 216
cumulativeSize: 216,
withLocality: false,
local: undefined,
sizeLocal: undefined
})
done()
})
})

it('stat withLocal file', function (done) {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
this.skip()
}

ipfs.files.stat('/test/b', {'withLocal': true}, (err, stat) => {
expect(err).to.not.exist()
expect(stat).to.eql({
type: 'file',
blocks: 1,
size: 13,
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T',
cumulativeSize: 71,
withLocality: true,
local: true,
sizeLocal: 71
})
done()
})
})

it('stat withLocal dir', function (done) {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
this.skip()
}

ipfs.files.stat('/test', {'withLocal': true}, (err, stat) => {
expect(err).to.not.exist()
expect(stat).to.eql({
type: 'directory',
blocks: 2,
size: 0,
hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto',
cumulativeSize: 216,
withLocality: true,
local: true,
sizeLocal: 216
})
done()
})
Expand Down
33 changes: 32 additions & 1 deletion js/src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = (common) => {
this.timeout(40 * 1000)

let ipfs
let withGo

function fixture (path) {
return loadFixture(path, 'interface-ipfs-core')
Expand Down Expand Up @@ -60,7 +61,11 @@ module.exports = (common) => {
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
node.id((err, id) => {
expect(err).to.not.exist()
withGo = id.agentVersion.startsWith('go-ipfs')
done()
})
})
})
})
Expand Down Expand Up @@ -1002,5 +1007,31 @@ module.exports = (common) => {
)
})
})

describe('.stat', () => {
before((done) => ipfs.files.add(smallFile.data, done))

it('stat outside of mfs', function (done) {
if (!withGo) {
console.log('Not supported in js-ipfs yet')
this.skip()
}

ipfs.files.stat('/ipfs/' + smallFile.cid, (err, stat) => {
expect(err).to.not.exist()
expect(stat).to.eql({
type: 'file',
blocks: 0,
size: 12,
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
cumulativeSize: 20,
withLocality: false,
local: undefined,
sizeLocal: undefined
})
done()
})
})
})
})
}

0 comments on commit 5969fed

Please sign in to comment.