fs.fchown()方法用於更改給定文件描述符的所有者和組。該函數接受用戶ID和組ID,這些用戶ID和組ID可用於設置各自的所有者和組。它具有一個回調函數,該函數返回該函數失敗時可能發生的任何錯誤。
用法:
fs.fchown( fd, uid, gid, callback )
參數:此方法接受上述和以下所述的四個參數:
- fd:它是一個整數,表示必須更改所有者和組的文件的文件描述符。
- uid:該數字表示與要設置的所有者相對應的用戶ID。
- gid:該數字表示與要設置的組相對應的組ID。
- callback:該方法執行時將調用該函數。
- err:如果方法失敗,將引發錯誤。
以下示例說明了Node.js中的fs.fchown()方法:
範例1:本示例顯示所有者的設置。
// 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");
});
在運行代碼之前:
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
代碼輸出:
Given uid and gid set successfully
運行代碼後:
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
範例2:本示例顯示了組的設置。
// 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");
});
在運行代碼之前:
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
代碼輸出:
Given uid and gid set successfully
運行代碼後:
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
參考: https://nodejs.org/api/fs.html#fs_fs_fchown_fd_uid_gid_callback
相關用法
- Node.js GM sharpen()用法及代碼示例
- Node.js GM paint()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM drawRectangle()用法及代碼示例
- Node.js GM orderedDither()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM emboss()用法及代碼示例
- Node.js GM despeckle()用法及代碼示例
- Node.js GM shave()用法及代碼示例
- Node.js GM write()用法及代碼示例
- Node.js GM spread()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.fchown() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。