Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 68bd372

Browse files
authored
fix: allow writing starting at offset beyond file length (#71)
There was a test for this but it didn't check the contents of the entire file, only starting from the difference between the offset and the file length. Oops. Fixes #53
1 parent 5747297 commit 68bd372

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/core/write.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ const write = async (context, source, destination, options) => {
128128

129129
// pad start of file if necessary
130130
if (options.offset > 0) {
131-
if (destination.unixfs && destination.unixfs.fileSize() > options.offset) {
131+
if (destination.unixfs) {
132132
log(`Writing first ${options.offset} bytes of original file`)
133133

134134
sources.push(
@@ -139,6 +139,15 @@ const write = async (context, source, destination, options) => {
139139
})
140140
}
141141
)
142+
143+
if (destination.unixfs.fileSize() < options.offset) {
144+
const extra = options.offset - destination.unixfs.fileSize()
145+
146+
log(`Writing zeros for extra ${extra} bytes`)
147+
sources.push(
148+
asyncZeroes(extra)
149+
)
150+
}
142151
} else {
143152
log(`Writing zeros for first ${options.offset} bytes`)
144153
sources.push(

test/core/write.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,13 @@ describe('write', () => {
373373
const stats = await mfs.stat(path)
374374
expect(stats.size).to.equal(newContent.length + offset)
375375

376-
const buffer = Buffer.concat(await all(mfs.read(path, {
377-
offset: offset - 5
378-
})))
376+
const buffer = Buffer.concat(await all(mfs.read(path)))
377+
378+
if (content[Symbol.asyncIterator]) {
379+
content = Buffer.concat(await all(content))
380+
}
379381

380-
expect(buffer).to.deep.equal(Buffer.concat([Buffer.from([0, 0, 0, 0, 0]), newContent]))
382+
expect(buffer).to.deep.equal(Buffer.concat([content, Buffer.from([0, 0, 0, 0, 0]), newContent]))
381383
})
382384
})
383385

0 commit comments

Comments
 (0)