Skip to content

fix: mask file mode #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achingbrain just want to doublecheck I am reading the precedence right. Are you saying:

assign mode & 0xFFF IF mode is non-zero numeric ( truthy) OR literal 0 OTHERWISE undefined

The rest looks great

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct, truthy or literal 0

this._originalMode = mode

if (this.mode === undefined && type === 'file') {
this.mode = DEFAULT_FILE_MODE
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/unixfs-format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {})

Expand Down