Skip to content

Commit 00e5ea0

Browse files
committed
fix: allow mtime and mode to be optional
Uses protobuf messages to enable having mode and mtime as actually optional values.
1 parent 46d15cd commit 00e5ea0

File tree

3 files changed

+72
-43
lines changed

3 files changed

+72
-43
lines changed

src/index.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ function Data (type, data) {
3232
this.data = data
3333
this.blockSizes = []
3434

35-
if (this.type === 'file') {
36-
this.mode = parseInt('0644', 8)
37-
}
38-
39-
if (this.type === 'directory' || this.type === 'hamt-sharded-directory') {
40-
this.mode = parseInt('0755', 8)
41-
}
42-
4335
this.addBlockSize = (size) => {
4436
this.blockSizes.push(size)
4537
}
@@ -92,12 +84,22 @@ function Data (type, data) {
9284
blockSizes = undefined
9385
}
9486

95-
if ((this.type === 'directory' || this.type === 'hamt-sharded-directory') && this.mode === parseInt('0755', 8)) {
96-
delete this.mode
87+
let mode
88+
89+
if (this.mode != null && this.mode >= 0) {
90+
mode = {
91+
value: this.mode
92+
}
9793
}
9894

99-
if (this.type === 'file' && this.mode === parseInt('0644', 8)) {
100-
delete this.mode
95+
let mtime
96+
97+
if (this.mtime != null && this.mtime >= 0) {
98+
mtime = {
99+
value: [
100+
this.mtime
101+
]
102+
}
101103
}
102104

103105
return unixfsData.encode({
@@ -107,27 +109,29 @@ function Data (type, data) {
107109
blocksizes: blockSizes,
108110
hashType: this.hashType,
109111
fanout: this.fanout,
110-
mode: this.mode,
111-
mtime: this.mtime
112+
mode: mode,
113+
mtime: mtime
112114
})
113115
}
114116
}
115117

116118
// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
117119
Data.unmarshal = (marsheled) => {
118120
const decoded = unixfsData.decode(marsheled)
121+
119122
if (!decoded.Data) {
120123
decoded.Data = undefined
121124
}
125+
122126
const obj = new Data(types[decoded.Type], decoded.Data)
123127
obj.blockSizes = decoded.blocksizes
124128

125-
if (decoded.mode) {
126-
obj.mode = decoded.mode
129+
if (decoded.mode != null) {
130+
obj.mode = decoded.mode.value
127131
}
128132

129-
if (decoded.mtime) {
130-
obj.mtime = decoded.mtime
133+
if (decoded.mtime != null) {
134+
obj.mtime = decoded.mtime.value[0]
131135
}
132136

133137
return obj

src/unixfs.proto.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ module.exports = `message Data {
1616
repeated uint64 blocksizes = 4;
1717
optional uint64 hashType = 5;
1818
optional uint64 fanout = 6;
19-
optional uint32 mode = 7;
20-
optional int64 mtime = 8;
19+
optional Mode mode = 7;
20+
optional Mtime mtime = 8;
2121
}
2222
2323
message Metadata {
2424
optional string MimeType = 1;
25+
}
26+
27+
message Mode {
28+
optional uint32 value = 1;
29+
}
30+
31+
message Mtime {
32+
repeated int64 value = 1;
2533
}`

test/unixfs-format.spec.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,33 @@ describe('unixfs-format', () => {
7979
expect(data.blockSizes).to.not.deep.equal(unmarshalled.blockSizes)
8080
})
8181

82-
it('default mode for files', () => {
82+
it('mode', () => {
83+
const mode = parseInt('0555', 8)
8384
const data = new UnixFS('file')
84-
expect(data.mode).to.equal(parseInt('0644', 8))
85-
const marshalled = data.marshal()
86-
const unmarshalled = UnixFS.unmarshal(marshalled)
87-
expect(unmarshalled.mode).to.equal(parseInt('0644', 8))
88-
})
85+
data.mode = mode
8986

90-
it('default mode for directories', () => {
91-
const data = new UnixFS('directory')
92-
expect(data.mode).to.equal(parseInt('0755', 8))
93-
const marshalled = data.marshal()
94-
const unmarshalled = UnixFS.unmarshal(marshalled)
95-
expect(unmarshalled.mode).to.equal(parseInt('0755', 8))
87+
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode)
9688
})
9789

98-
it('default mode for hamt-sharded-directories', () => {
99-
const data = new UnixFS('hamt-sharded-directory')
100-
expect(data.mode).to.equal(parseInt('0755', 8))
101-
const marshalled = data.marshal()
102-
const unmarshalled = UnixFS.unmarshal(marshalled)
103-
expect(unmarshalled.mode).to.equal(parseInt('0755', 8))
90+
it('removes mode', () => {
91+
const mode = parseInt('0555', 8)
92+
const data = new UnixFS('file')
93+
data.mode = mode
94+
95+
const unmarshalled = UnixFS.unmarshal(data.marshal())
96+
expect(unmarshalled).to.have.property('mode', mode)
97+
98+
delete unmarshalled.mode
99+
100+
expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mode')
104101
})
105102

106-
it('mode', () => {
107-
const mode = parseInt('0555', 8)
103+
it('sets mode to 0', () => {
104+
const mode = 0
108105
const data = new UnixFS('file')
109106
data.mode = mode
110-
const marshalled = data.marshal()
111-
const unmarshalled = UnixFS.unmarshal(marshalled)
112-
expect(unmarshalled.mode).to.equal(mode)
107+
108+
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode)
113109
})
114110

115111
it('mtime', () => {
@@ -121,6 +117,27 @@ describe('unixfs-format', () => {
121117
expect(unmarshalled.mtime).to.equal(mtime)
122118
})
123119

120+
it('removes mtime', () => {
121+
const mtime = parseInt(Date.now() / 1000)
122+
const data = new UnixFS('file')
123+
data.mtime = mtime
124+
125+
const unmarshalled = UnixFS.unmarshal(data.marshal())
126+
expect(unmarshalled).to.have.property('mtime', mtime)
127+
128+
delete unmarshalled.mtime
129+
130+
expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mtime')
131+
})
132+
133+
it('sets mtime to 0', () => {
134+
const mtime = 0
135+
const data = new UnixFS('file')
136+
data.mtime = mtime
137+
138+
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mtime', mtime)
139+
})
140+
124141
// figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526
125142
it.skip('metadata', () => {})
126143

0 commit comments

Comments
 (0)