Node.js fsPromises.chown() 方法
fsPromises.chown () 方法用于更改文件的所有权,然后在成功时不带参数地解析Promise 。该函数接受可用于设置各自所有者和组的用户 ID 和组 ID。
句法:
fsPromises.chown( path, uid, gid)
参数:此方法接受三个参数,如上所述,如下所述:
- path:它是一个字符串、缓冲区或 URL,表示必须更改所有者和组的文件的路径。
- uid:是一个整数,表示要设置的所有者对应的用户id。
- gid:它是一个整数,表示与要设置的组对应的组id。
Javascript
// Node.js program to demonstrate the
// fsPromises.chown() method
// Import the filesystem module
const fs = require('fs');
let filepath = "example_file.txt";
// Set the owner to a new one keeping the group same
// New owner is "GeeksforGeeks" with user id 4567
fsPromises.chown(filepath, 4567, 999 => {
console.log("uid and gid set successfully.");
});
Javascript
// Node.js program to demonstrate the
// fsPromises.chown() method
// Import the filesystem module
const fs = require('fs');
let filepath = "example_file.txt";
// Set the owner and group both to a new one
// New owner is "nitin" with owner id 8900
// New group is "author" with group id 1024
fsPromises.chown(filepath, 8900, 1024 => {
console.log("uid and gid set successfully!");
});
运行代码之前:
xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js
代码输出:
Given uid and gid set successfully
.
运行代码后:
xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 geeksforgeeks xubuntu 4 May 26 04:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js
示例 2:此示例显示了组的设置。
Javascript
// Node.js program to demonstrate the
// fsPromises.chown() method
// Import the filesystem module
const fs = require('fs');
let filepath = "example_file.txt";
// Set the owner and group both to a new one
// New owner is "nitin" with owner id 8900
// New group is "author" with group id 1024
fsPromises.chown(filepath, 8900, 1024 => {
console.log("uid and gid set successfully!");
});
运行代码之前:
xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:19 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js
代码输出:
Given uid and gid set successfully!
运行代码后:
xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 nitin author 4 May 26 04:19 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js