diff --git a/src/index.js b/src/index.js index 5e0c9bb8..6331bdc1 100644 --- a/src/index.js +++ b/src/index.js @@ -80,7 +80,8 @@ class Data { this.fanout = fanout this.blockSizes = blockSizes || [] this.mtime = mtime || new Date(0) - this.mode = mode + this.mode = mode || mode === 0 ? (mode & 0xFFF) : undefined + this._originalMode = mode if (this.mode === undefined && type === 'file') { this.mode = DEFAULT_FILE_MODE @@ -151,8 +152,8 @@ class Data { let mode - if (!isNaN(parseInt(this.mode))) { - mode = this.mode + if (this.mode || this.mode === 0) { + mode = (this._originalMode & 0xFFFFF000) | (this.mode & 0xFFF) if (mode === DEFAULT_FILE_MODE && this.type === 'file') { mode = undefined diff --git a/test/unixfs-format.spec.js b/test/unixfs-format.spec.js index 766d99be..6190fbf4 100644 --- a/test/unixfs-format.spec.js +++ b/test/unixfs-format.spec.js @@ -13,6 +13,8 @@ const directory = loadFixture('test/fixtures/directory.unixfs') const file = loadFixture('test/fixtures/file.txt.unixfs') const symlink = loadFixture('test/fixtures/symlink.txt.unixfs') const { Buffer } = require('buffer') +const protons = require('protons') +const unixfsData = protons(require('../src/unixfs.proto')).Data describe('unixfs-format', () => { it('defaults to file', () => { @@ -145,6 +147,20 @@ describe('unixfs-format', () => { expect(UnixFS.unmarshal(data.marshal())).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000)) }) + it('does not overwrite unknown mode bits', () => { + const mode = 0xFFFFFFF // larger than currently defined mode bits + const buf = unixfsData.encode({ + Type: 0, + mode + }) + + const unmarshaled = UnixFS.unmarshal(buf) + const marshaled = unmarshaled.marshal() + + const entry = unixfsData.decode(marshaled) + expect(entry).to.have.property('mode', mode) + }) + // figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526 it.skip('metadata', () => {})