Durable Write With fsync
NodeJS
Hard
3 views
Problem Description
Write text to a file and fsync so it is flushed, then print DONE.
Input Format
stdin: file path.
Output Format
Print DONE/FAIL.
Constraints
Use fs.openSync and fs.fsyncSync.
Official Solution
const fs = require('fs');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);
try {
const fd = fs.openSync(p, 'w');
fs.writeFileSync(fd, 'meetcode', 'utf8');
fs.fsyncSync(fd);
fs.closeSync(fd);
console.log('DONE');
} catch (e) {
console.log('FAIL');
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!