Skip to content
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

fix: ignore high mode bits passed to constructor #53

Merged
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
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