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

Change to avoid overwriting data length bytes #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions frame/recoverable.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,19 @@ module.exports = function (blocks, blockSize, offsetCodec) {
/**
* Overwrites an item at `offset` with null bytes.
*
* The only bytes we need to change are the actual data bytes, so we can skip
* the book-end bytes that record the length of the data and *only* touch the
* data bytes themselves.
*
* @param {number} offset - the offset of the item to overwrite
* @param {function} cb - callback that returns any error as an argument
*/
const overwrite = (offset, cb) => {
blocks.readUInt32BE(offset, function (err, len) {
if (err) return cb(err)

const bookend = Buffer.alloc(4)
bookend.writeUInt32BE(len, 0)

const nullBytes = Buffer.alloc(len)
const full = Buffer.concat([bookend, nullBytes, bookend])

blocks.write(full, offset, cb)
blocks.write(nullBytes, offset + 4, cb)
})
}

Expand Down