fs.fchownSync()方法用於同步更改給定文件描述符的所有者和組。該函數接受可用於設置各自所有者和組的用戶ID和組ID。它不返回任何東西。
用法:
fs.fchownSync( fd, uid, gid )
參數:此方法接受上述和以下所述的三個參數:
- fd:它是一個整數,表示必須更改所有者和組的文件的文件描述符。
- uid:該數字表示與要設置的所有者相對應的用戶ID。
- gid:該數字表示與要設置的組相對應的組ID。
以下示例說明了Node.js中的fs.fchownSync()方法:
範例1:本示例顯示所有者的設置。
// Node.js program to demonstrate the
// fs.fchownSync() method
// Import the filesystem module
const fs = require('fs');
// Get the file descriptor for the file
let fd = fs.openSync("example_file.txt", "r");
// Set the owner to a new one keeping the group same
// New owner is "geeksforgeeks" with user id 1010
// The old group is "xubuntu" with group id 999
fs.fchownSync(fd, 1010, 999);
console.log("uid and gid set successfully");
在運行代碼之前:
xubuntu@xubuntu:~/Desktop/fs-fchownSync$ ls -l total 8 -rw-rw--w- 1 xubuntu xubuntu 4 Apr 22 09:10 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 22 09:15 index.js
代碼輸出:
Given uid and gid set successfully
運行代碼後:
xubuntu@xubuntu:~/Desktop/fs-fchownSync$ ls -l total 8 -rw-rw--w- 1 geeksforgeeks xubuntu 4 Apr 22 09:10 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 22 09:15 index.js
範例2:本示例顯示了組的設置。
// Node.js program to demonstrate the
// fs.fchownSync() method
// Import the filesystem module
const fs = require('fs');
// Get the file descriptor for the file
let fd = fs.openSync("example_file.txt", "r");
// Set the group to a new one keeping the owner same
// The old owner is "xubuntu" with user id 999
// New group is "author" with group id 1015
fs.fchownSync(fd, 999, 1015);
console.log("uid and gid set successfully");
在運行代碼之前:
xubuntu@xubuntu:~/Desktop/fs-fchownSync$ ls -l total 8 -rw-rw--w- 1 xubuntu xubuntu 4 Apr 22 09:10 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 22 09:15 index.js
代碼輸出:
Given uid and gid set successfully
運行代碼後:
xubuntu@xubuntu:~/Desktop/fs-fchownSync$ ls -l total 8 -rw-rw--w- 1 xubuntu author 4 Apr 22 09:10 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 22 09:15 index.js
參考: https://nodejs.org/api/fs.html#fs_fs_fchownsync_fd_uid_gid
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs. fchownSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。