Skip to content

Commit

Permalink
fix: ignore high mode bits passed to constructor (#53)
Browse files Browse the repository at this point in the history
The UnixFS Spec [says](https://github.com/ipfs/specs/blob/master/UNIXFS.md#metadata) high mode bits not defined in the version of the spec supported by a given implementation should be ignored but also persisted to ensure forwards compatibility.  The change here:

1. Ignores high bits passed to the constructor
1. Respects high bits read out of a protobuf though does not expose them
1. Writes high bits back to a protobuf but only if they were read from a protobuf to begin with
  • Loading branch information
achingbrain authored Jun 4, 2020
1 parent 9e24119 commit 8e8d83d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"abort-controller": "^3.0.0",
"aegir": "^21.9.0",
"aegir": "^22.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"detect-node": "^2.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"aegir": "^21.9.0",
"aegir": "^22.0.0",
"chai": "^4.2.0",
"cids": "^0.8.0",
"detect-node": "^2.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"aegir": "^21.9.0",
"aegir": "^22.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"nyc": "^15.0.0"
Expand Down
8 changes: 6 additions & 2 deletions packages/ipfs-unixfs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,18 @@ class Data {
static unmarshal (marshaled) {
const decoded = unixfsData.decode(marshaled)

return new Data({
const data = new Data({
type: types[decoded.Type],
data: decoded.hasData() ? decoded.Data : undefined,
blockSizes: decoded.blocksizes,
mode: decoded.hasMode() ? decoded.mode : undefined,
mtime: decoded.hasMtime() ? decoded.mtime : undefined
})

// make sure we honor the original mode
data._originalMode = decoded.hasMode() ? decoded.mode : undefined

return data
}

constructor (...args) {
Expand All @@ -158,7 +163,6 @@ class Data {
this.hashType = hashType
this.fanout = fanout
this.blockSizes = blockSizes || []
this._originalMode = mode

const parsedMode = parseMode(mode)

Expand Down
61 changes: 61 additions & 0 deletions packages/ipfs-unixfs/test/unixfs-format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,67 @@ describe('unixfs-format', () => {
expect(entry).to.have.property('mode', mode)
})

it('omits default file mode from protobuf', () => {
const entry = new UnixFS({
type: 'file',
mode: 0o644
})

const marshaled = entry.marshal()

const protobuf = unixfsData.decode(marshaled)
expect(protobuf.hasMode()).to.be.false()
})

it('omits default directory mode from protobuf', () => {
const entry = new UnixFS({
type: 'directory',
mode: 0o755
})

const marshaled = entry.marshal()

const protobuf = unixfsData.decode(marshaled)
expect(protobuf.hasMode()).to.be.false()
})

it('respects high bits in mode read from buffer', () => {
const mode = 0o0100644 // similar to output from fs.stat
const buf = unixfsData.encode({
Type: unixfsData.DataType.File,
mode
})

const entry = UnixFS.unmarshal(buf)

// should have truncated mode to bits in the version of the spec this module supports
expect(entry).to.have.property('mode', 0o644)

const marshaled = entry.marshal()

const protobuf = unixfsData.decode(marshaled)
expect(protobuf).to.have.property('mode', mode)
})

it('ignores high bits in mode passed to constructor', () => {
const mode = 0o0100644 // similar to output from fs.stat
const entry = new UnixFS({
type: 'file',
mode
})

// should have truncated mode to bits in the version of the spec this module supports
expect(entry).to.have.property('mode', 0o644)

const marshaled = entry.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)

expect(unmarshaled).to.have.property('mode', 0o644)

const protobuf = unixfsData.decode(marshaled)
expect(protobuf.hasMode()).to.be.false()
})

// figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526
it('metadata', () => {
const entry = new UnixFS({
Expand Down

0 comments on commit 8e8d83d

Please sign in to comment.