Skip to content
Related Articles

Related Articles

Improve Article
Node.js | fs.fchmodSync() Method
  • Last Updated : 25 Jun, 2020

The fs.fchmodSync() method is an inbuilt application programming of fs module which is used to synchronously change the permissions of a given file descriptor. These permissions can be specified as a parameter using string constants or octal numbers that corresponding to their respective file modes.

Note: The Windows platform only supports the changing of the write permission. It also does not support the distinction between the permissions of user, group, or others.

Syntax:

fs.fchmodSync( fd, mode )

Parameters: This method accepts two parameters as mentioned above and described below:

  • fd: It is an integer which denotes the file descriptor of the file of which the permission has to be changed.
  • mode: It is string constant or octal constant that denotes the permission to be granted. The logical OR operator can be used to separate multiple permissions.

Below examples illustrate the fs.fchmodSync method in Node.js:



Example 1: This example shows the usage of string constants and OR operator to give the file permissions.




// Node.js program to demonstrate the
// fs.fchmodSync method
  
// Import the filesystem module
const fs = require('fs');
  
// Getting the file descriptor
const fd = fs.openSync('example_file.txt', 'r');
  
// Allowing only read permission
console.log("Giving only read permission to user");
fs.fchmodSync(fd, fs.constants.S_IRUSR);
  
// Check the file mode
console.log("Current File Mode:",
    fs.statSync("example_file.txt").mode);
  
// Reading the file
console.log("File Contents:",
    fs.readFileSync("example_file.txt", 'utf8'));
  
// Trying to write to file
try {
  console.log("Trying to write to file");
  fs.writeFileSync('example_file.txt', "Hello");
}
catch (e) {
  console.log("Error Found, Code:", e.code);
}
  
// Allowing both read and write permission
console.log("\nGiving both read and write permission to user");
fs.fchmodSync(fd, fs.constants.S_IRUSR | fs.constants.S_IWUSR);
  
// Check the file mode
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'));

Output:

Giving only read permission to user
Current File Mode: 33024
File Contents: Hello World
Trying to write to file
Error Found, Code: EACCES

Giving both read and write permission to user
Current File Mode: 33152
Trying to write to file
File Contents: This file has been written over.

Example 2: This example shows the usage of octal constants to give the file permissions.




// Node.js program to demonstrate the
// fs.fchmodSync method
  
// Import the filesystem module
const fs = require('fs');
  
// Getting the file descriptor
const fd = fs.openSync('example_file.txt', 'r');
  
// Allowing only read permission
console.log("Giving only read permission to everyone");
fs.fchmodSync(fd, 0o444);
  
// Check the file mode
console.log("Current File Mode:"
    fs.statSync("example_file.txt").mode);
  
// Reading the file
console.log("File Contents:"
    fs.readFileSync("example_file.txt", 'utf8'));
  
// Trying to write to file
try {
  console.log("Trying to write to file");
  fs.writeFileSync('example_file.txt', "Hello");
}
catch (e) {
  console.log("Error Found, Code:", e.code);
}
  
// Allowing both read and write permission
console.log("\nGiving both read and "
    + "write permission to everyone");
fs.fchmodSync(fd, 0o666);
  
// Check the file mode
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'));

Output:

Giving only read permission to everyone
Current File Mode: 33060
File Contents: Hello World
Trying to write to file
Error Found, Code: EACCES

Giving both read and write permission to everyone
Current File Mode: 33206
Trying to write to file
File Contents: This file has been written over.

Reference: https://nodejs.org/api/fs.html#fs_fs_fchmodsync_fd_mode




My Personal Notes arrow_drop_up
Recommended Articles
Page :