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

Commit

Permalink
feat: handle raw nodes in mfs (#48)
Browse files Browse the repository at this point in the history
Right now we assume that all nodes are `dag-pb` nodes and that they only contain `dag-pb` nodes. This PR allows us to handle `raw` nodes when `stat`ing and `ls`ing IPFS paths.
  • Loading branch information
achingbrain authored Apr 8, 2019
1 parent 4a36b42 commit ad1df5a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/core/ls-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ module.exports = (context) => {
return cb(err)
}

if (Buffer.isBuffer(result.node)) {
return cb(null, {
name: file.name,
type: 0,
hash: formatCid(file.cid, options.cidBase),
size: result.node.length
})
}

const meta = UnixFs.unmarshal(result.node.data)

cb(null, {
Expand Down
13 changes: 13 additions & 0 deletions src/core/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ module.exports = (context) => {
node, cid
} = result

if (Buffer.isBuffer(node)) {
return cb(null, {
hash: formatCid(cid, options.cidBase),
size: node.length,
cumulativeSize: node.length,
blocks: 0,
type: 'file', // really?
local: undefined,
sizeLocal: undefined,
withLocality: false
})
}

const meta = unmarshal(node.data)
let blocks = node.links.length

Expand Down
58 changes: 58 additions & 0 deletions test/ls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const randomBytes = require('./helpers/random-bytes')
const CID = require('cids')
const {
FILE_TYPES
} = require('../src')
Expand All @@ -17,6 +19,7 @@ const {

describe('ls', () => {
let mfs
let largeFile = randomBytes(490668)

before(async () => {
mfs = await createMfs()
Expand Down Expand Up @@ -216,6 +219,61 @@ describe('ls', () => {
}
})

it('lists a raw node', async () => {
const filePath = '/stat/large-file.txt'

await mfs.write(filePath, largeFile, {
create: true,
parents: true,
rawLeaves: true
})

const stats = await mfs.stat(filePath)
const result = await mfs.ipld.get(new CID(stats.hash))
const node = result.value
const child = node.links[0]

expect(child.cid.codec).to.equal('raw')

const rawNodeContents = await mfs.ls(`/ipfs/${child.cid}/`, {
long: true
})

expect(rawNodeContents[0].type).to.equal(0) // this is what go does
expect(rawNodeContents[0].hash).to.equal(child.cid.toBaseEncodedString())
})

it('lists a raw node in an mfs directory', async () => {
const filePath = '/stat/large-file.txt'

await mfs.write(filePath, largeFile, {
create: true,
parents: true,
rawLeaves: true
})

const stats = await mfs.stat(filePath)
const cid = new CID(stats.hash)
const result = await mfs.ipld.get(cid)
const node = result.value
const child = node.links[0]

expect(child.cid.codec).to.equal('raw')

const dir = `/dir-with-raw-${Date.now()}`
const path = `${dir}/raw-${Date.now()}`

await mfs.mkdir(dir)
await mfs.cp(`/ipfs/${child.cid.toBaseEncodedString()}`, path)

const rawNodeContents = await mfs.ls(path, {
long: true
})

expect(rawNodeContents[0].type).to.equal(0) // this is what go does
expect(rawNodeContents[0].hash).to.equal(child.cid.toBaseEncodedString())
})

it('lists a sharded directory contents', async () => {
const shardSplitThreshold = 10
const fileCount = 11
Expand Down
51 changes: 51 additions & 0 deletions test/stat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const randomBytes = require('./helpers/random-bytes')
const CID = require('cids')

const {
createMfs,
Expand Down Expand Up @@ -153,6 +154,56 @@ describe('stat', () => {
expect(stats.type).to.equal('file')
})

it('stats a raw node', async () => {
const filePath = '/stat/large-file.txt'

await mfs.write(filePath, largeFile, {
create: true,
parents: true,
rawLeaves: true
})

const stats = await mfs.stat(filePath)
const result = await mfs.ipld.get(new CID(stats.hash))
const node = result.value
const child = node.links[0]

expect(child.cid.codec).to.equal('raw')

const rawNodeStats = await mfs.stat(`/ipfs/${child.cid.toBaseEncodedString()}`)

expect(rawNodeStats.hash).to.equal(child.cid.toBaseEncodedString())
expect(rawNodeStats.type).to.equal('file') // this is what go does
})

it('stats a raw node in an mfs directory', async () => {
const filePath = '/stat/large-file.txt'

await mfs.write(filePath, largeFile, {
create: true,
parents: true,
rawLeaves: true
})

const stats = await mfs.stat(filePath)
const result = await mfs.ipld.get(new CID(stats.hash))
const node = result.value
const child = node.links[0]

expect(child.cid.codec).to.equal('raw')

const dir = `/dir-with-raw-${Date.now()}`
const path = `${dir}/raw-${Date.now()}`

await mfs.mkdir(dir)
await mfs.cp(`/ipfs/${child.cid.toBaseEncodedString()}`, path)

const rawNodeStats = await mfs.stat(path)

expect(rawNodeStats.hash).to.equal(child.cid.toBaseEncodedString())
expect(rawNodeStats.type).to.equal('file') // this is what go does
})

it('stats a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(mfs)

Expand Down

0 comments on commit ad1df5a

Please sign in to comment.