const fs = require(
'fs'
);
const fd = fs.openSync(
'example_file.txt'
,
'r'
);
console.log(
"Giving only read permission to the user"
);
fs.fchmod(fd, fs.constants.S_IRUSR, (err) => {
if
(err)
throw
err;
console.log(
"Current File Mode:"
,
fs.statSync(
"example_file.txt"
).mode);
console.log(
"File Contents:"
,
fs.readFileSync(
"example_file.txt"
,
'utf8'
));
try
{
console.log(
"Trying to write to file"
);
fs.writeFileSync(
'example_file.txt'
,
"Hello"
);
}
catch
(e) {
console.log(
"Error Found, Code:"
, e.code);
}
console.log(
"\nGiving both read and write "
+
"permission to the user"
);
fs.fchmod(fd, fs.constants.S_IRUSR |
fs.constants.S_IWUSR, (err) => {
if
(err)
throw
err;
console.log(
"Current File Mode:"
,
fs.statSync(
"example_file.txt"
).mode);
console.log(
"Trying to write to file"
);
fs.writeFileSync(
'example_file.txt'
,
"This file has been written over."
);
console.log(
"File Contents:"
,
fs.readFileSync(
"example_file.txt"
,
'utf8'
));
});
});