Node.js fsPromises.lchown() Method
The fsPromises.lchown() method is used to change the ownership of a file then resolves the Promise with no arguments upon success. The function accepts a user id and group id that can be used to set the respective owner and group.
Syntax:
fsPromises.lchown( path, uid, gid )
Parameters: This method accepts three parameters as mentioned above and described below:
- path: It is a String, Buffer or URL that denotes the path of the file of which the owner and group has to be changed.
- uid: It is an integer that denotes the user id that corresponds to the owner to be set.
- gid: It is an integer that denotes the group id that corresponds to the group to be set.
Note: This method is only implemented on macOS.
Example: Node.js program to demonstrate the fsPromises.lchown() method.
Filename: index.js
// Node.js program to demonstrate the // fsPromises.lchown() method // Import the filesystem module const fs = require( 'fs' ); const fsPromises = fs.promises; let filepath = "example_file.txt" ; let symlinkpath = "symlinkFile" ; // Create a symlink to the file fs.symlinkSync(filepath, symlinkpath); // Set the owner and group of the // symbolic link to a new one // New owner is "w3wiki" with // user id 1200 New group is "editor" // with group id 1201 fs.lchown(symlinkpath, 1200, 1201) .then( function () { console.log( "Given uid and gid set successfully" ); }) . catch ( function (error) { console.log(error); }); |
Step to run this program: Run index.js file using the following command:
node index.js
Before Running the Code:
xubuntu@xubuntu: ~/Desktop/fs-lchown$ ls -l total 4 -rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 05:10 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 05:15 index.js
Output of the Code:
Given uid and gid set successfully
After Running the Code:
xubuntu@xubuntu: ~/Desktop/fs-lchown$ ls -l total 4 -rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 05:10 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 05:15 index.js lrwxrwxrwx 1 w3wiki editor 16 Apr 26 09:15 symlinkFile -> example_file.txt