-
Notifications
You must be signed in to change notification settings - Fork 0
/
chmod2.js
28 lines (21 loc) · 1.2 KB
/
chmod2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//chmod syncronous to allow read and write permission
const fs = require("fs");
console.log("giving only read permission");
fs.chmodSync("hardik3.txt", 0o400); //fs.constants.S_IRUSR is not working why?
console.log("current file mode:", fs.statSync("hardik3.txt").mode); //fs/statsync
console.log("File contents : ", fs.readFileSync("hardik3.txt", "utf8"));
try {
console.log("trying to write to file");
fs.writeFileSync("hardik3.txt", "writing");
}
catch (e) {
console.log("error found, code:", e.code);
}
//allowing both read and write permission
console.log("allowing both read and write permission");
fs.chmodSync("hardik3.txt", 0o400 | 0o200);
console.log("\ncurrent file mode: ", fs.statSync("hardik3.txt").mode); //statSync gets all the stats of files and
//.mode gives the mode of file from all the stats
console.log("trying to write in file");
fs.writeFileSync("hardik3.txt", "new edited word");
console.log("file contents: ", fs.readFileSync("hardik3.txt", "utf8"));