62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
const mb = Buffer.alloc(1024 ** 2);
|
|
const manpage = `
|
|
FALLOCATE(1) User Commands FALLOCATE(1)
|
|
|
|
NAME
|
|
fallocate - allocate space to a file
|
|
|
|
SYNOPSIS
|
|
/fallocate/[size]
|
|
|
|
DESCRIPTION
|
|
fallocate is used to allocate space for a file purely through the
|
|
sheer power of cloud-based services. this is a very good idea and
|
|
i see absolutely nothing wrong with it.
|
|
|
|
AUTHORS
|
|
me
|
|
|
|
REPORTING BUGS
|
|
you can't
|
|
|
|
cloud-linux 2.37.2 2022-03-15 FALLOCATE(1)
|
|
`.trim();
|
|
|
|
function parseSize(size) {
|
|
const [_, exp, mult] = size.match(/^([0-9]+)(.)?$/);
|
|
const num = BigInt(parseInt(exp, 10));
|
|
switch(mult?.toLowerCase()) {
|
|
case "k": case "kib": return num * 1024n;
|
|
case "m": case "mib": return num * 1024n ** 2n;
|
|
case "g": case "gib": return num * 1024n ** 3n;
|
|
case "t": case "tib": return num * 1024n ** 4n;
|
|
case "kb": return num * 1000n;
|
|
case "mb": return num * 1000n ** 2n;
|
|
case "gb": return num * 1000n ** 3n;
|
|
case "tb": return num * 1000n ** 4n;
|
|
default: return num;
|
|
}
|
|
}
|
|
|
|
module.exports = (app, dir, log) => {
|
|
app.get("/fallocate", (req, res) => {
|
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
res.end(manpage);
|
|
});
|
|
|
|
app.get("/fallocate/:size", (req, res) => {
|
|
try {
|
|
let size = parseSize(req.params.size);
|
|
log.warn(`fallocating ${req.params.size} (${size}) bytes`);
|
|
res.writeHead(200, { "Content-Length": parseFloat(size), "Content-Type": "application/octet-stream" });
|
|
log.debug("wrote head");
|
|
while(size > 0n) {
|
|
res.write(size >= 1024n ** 2n ? mb : Buffer.alloc(size));
|
|
size -= 1024n ** 2n;
|
|
}
|
|
res.end();
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
});
|
|
}
|