Skip to content

Commit 09fd4ed

Browse files
authored
fix: mask file mode (#39)
Only expose documented bits to calling code but respect them when turning the entry into a protobuf.
1 parent 2f02eb5 commit 09fd4ed

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class Data {
8080
this.fanout = fanout
8181
this.blockSizes = blockSizes || []
8282
this.mtime = mtime || new Date(0)
83-
this.mode = mode
83+
this.mode = mode || mode === 0 ? (mode & 0xFFF) : undefined
84+
this._originalMode = mode
8485

8586
if (this.mode === undefined && type === 'file') {
8687
this.mode = DEFAULT_FILE_MODE
@@ -151,8 +152,8 @@ class Data {
151152

152153
let mode
153154

154-
if (!isNaN(parseInt(this.mode))) {
155-
mode = this.mode
155+
if (this.mode || this.mode === 0) {
156+
mode = (this._originalMode & 0xFFFFF000) | (this.mode & 0xFFF)
156157

157158
if (mode === DEFAULT_FILE_MODE && this.type === 'file') {
158159
mode = undefined

test/unixfs-format.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const directory = loadFixture('test/fixtures/directory.unixfs')
1313
const file = loadFixture('test/fixtures/file.txt.unixfs')
1414
const symlink = loadFixture('test/fixtures/symlink.txt.unixfs')
1515
const { Buffer } = require('buffer')
16+
const protons = require('protons')
17+
const unixfsData = protons(require('../src/unixfs.proto')).Data
1618

1719
describe('unixfs-format', () => {
1820
it('defaults to file', () => {
@@ -145,6 +147,20 @@ describe('unixfs-format', () => {
145147
expect(UnixFS.unmarshal(data.marshal())).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000))
146148
})
147149

150+
it('does not overwrite unknown mode bits', () => {
151+
const mode = 0xFFFFFFF // larger than currently defined mode bits
152+
const buf = unixfsData.encode({
153+
Type: 0,
154+
mode
155+
})
156+
157+
const unmarshaled = UnixFS.unmarshal(buf)
158+
const marshaled = unmarshaled.marshal()
159+
160+
const entry = unixfsData.decode(marshaled)
161+
expect(entry).to.have.property('mode', mode)
162+
})
163+
148164
// figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526
149165
it.skip('metadata', () => {})
150166

0 commit comments

Comments
 (0)