Node.js | fs.fchmod() Method

The fs.fchmod() method is used to 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 does not support the distinction between the permissions of users, groups, or others.

Syntax:

fs.fchmod( fd, mode, callback )

Parameters: This method accept three parameters as mentioned above and described below:

  • fd: It is an integer value 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.
  • callback: It is a function that would be called when the method is executed.
    • err: It is an error that would be thrown if the method fails.

Below examples illustrate the fs.fchmod() method in Node.js:



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.fchmod() 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 the user");
fs.fchmod(fd, fs.constants.S_IRUSR, (err) => {
  if (err) throw err;
  
  // 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 the user");
  
  fs.fchmod(fd, fs.constants.S_IRUSR | 
          fs.constants.S_IWUSR, (err) => {
    if (err) throw err;
  
    // 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'));
  });
});

chevron_right


Output:

Giving only read permission to the user
Current File Mode: 33024
File Contents: This file has been written over.
Trying to write to file
Error Found, Code: EACCES

Giving both read and write permission to the 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.fchmod() 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.fchmod(fd, 0o444, (err) => {
  if (err) throw err;
  
  // 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.fchmod(fd, 0o666, (err) => {
    if (err) throw err;
  
    // 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'));
  });
});

chevron_right


Output:

Giving only read permission to everyone
Current File Mode: 33060
File Contents: This file has been written over.
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_fchmod_fd_mode_callback




My Personal Notes arrow_drop_up

Im listening

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.