Open In App

Node.js fs.fchown() Function

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.fchown() method is used to change the owner and group of the given file descriptor. The function accepts a user id and group id that can be used to set the respective owner and group. It has a callback function that returns any error that may occur if the function fails.

Syntax:

fs.fchown( fd, uid, gid, callback )

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

  • fd: It is an integer which denotes the file descriptor of the file of which the owner and group has to be changed.
  • uid: It is a number that denotes the user id that corresponds to the owner to be set.
  • gid: It is a number that denotes the group id that corresponds to the group to be set.
  • 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.fchown() method in Node.js:

Example 1: This example shows the setting of the owner.




// Node.js program to demonstrate the
// fs.fchown() method
  
// Import the filesystem module
const fs = require('fs');
  
let fd = fs.openSync("example_file.txt", "r");
  
// Set the owner to a new one keeping the group same
// New owner is "geek" with user id 1027
// The old group is "xubuntu" with group id 999
fs.fchown(fd, 1027, 999, (error) => {
  if (error)
    console.log("Error Code:", error);
  else
    console.log("uid and gid set successfully");
});


Before Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js

Output of the Code:

Given uid and gid set successfully

After Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 geek xubuntu 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js

Example 2: This example shows the setting of the group.




// Node.js program to demonstrate the
// fs.fchown() method
  
// Import the filesystem module
const fs = require('fs');
  
let fd = fs.openSync("example_file.txt", "r");
  
// Set the owner to a new one keeping the group same
// New owner is "raj" with user id 1025
// New group is "author" with group id 1031
fs.fchown(fd, 1025, 1031, (error) => {
  if (error)
    console.log("Error Code:", error);
  else
    console.log("uid and gid set successfully");
});


Before Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js

Output of the Code:

Given uid and gid set successfully

After Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 raj author 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads