Created
August 28, 2017 15:15
-
-
Save mceachen/deaadeb2efe4b8b8b41e66a3c99ba1d5 to your computer and use it in GitHub Desktop.
`punch`: set zeroes in random locations to simulate file corruption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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