Skip to content

Instantly share code, notes, and snippets.

@mceachen
Created August 28, 2017 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mceachen/deaadeb2efe4b8b8b41e66a3c99ba1d5 to your computer and use it in GitHub Desktop.
Save mceachen/deaadeb2efe4b8b8b41e66a3c99ba1d5 to your computer and use it in GitHub Desktop.
`punch`: set zeroes in random locations to simulate file corruption
#!/usr/bin/env node
// Usage: punch <inputfile> [number of holes]
const fs = require("fs")
const process = require("process")
const file = process.argv[2]
const times = process.argv[3] || 1
const stat = fs.statSync(file)
function randomInt(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
}
const buf = fs.readFileSync(file)
const len = buf.byteLength
console.log(file + " is " + len + " bytes.")
const start = len > 1024 ? 1024 : 10
for (let i = 0; i < times; i++) {
const loc = randomInt(start, len) // don't overwrite the header
console.log("Punching at " + loc)
buf[loc] = 0
}
fs.writeFileSync(file, buf)
console.log("punched!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment