fs.lchown()方法用於異步更改給定路徑的所有者和組,但是,如果路徑為1,它不會引用符號鏈接,這與fs.chown()方法確實取消引用鏈接到其路徑是不同的。該函數接受可用於設置各自所有者和組的用戶ID和組ID。它不返回任何東西。
用法:
fs.lchown( fd, uid, gid, callback )
參數:此方法接受上述和以下所述的四個參數:
- fd:它是一個整數,表示必須更改所有者和組的文件的文件描述符。
- uid:該數字表示與要設置的所有者相對應的用戶ID。
- gid:該數字表示與要設置的組相對應的組ID。
- callback:該方法執行時將調用該函數。
- err:如果方法失敗,將引發錯誤。
以下示例說明了Node.js中的fs.lchown()方法:
範例1:本示例使用lchown()方法顯示所有者和組的設置。
// Node.js program to demonstrate the
// fs.lchown() method
// Import the filesystem module
const fs = require('fs');
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 "geeksforgeeks" with user id 1200
// New group is "editor" with group id 1201
fs.lchown(symlinkpath, 1200, 1201, (err) => {
if (err)
console.log(err);
else {
console.log("Given uid and gid set successfully");
}
});
在運行代碼之前:
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
代碼輸出:
Given uid and gid set successfully
運行代碼後:
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 geeksforgeeks editor 16 Apr 26 09:15 symlinkFile -> example_file.txt
範例2:本示例顯示了chown()和lchown()在取消引用符號鏈接方麵的比較。
// Node.js program to demonstrate the
// fs.lchown() method
// Import the filesystem module
const fs = require('fs');
let filepath = "example_file.txt";
let symlinkpath1 = "symlinkFile1";
let symlinkpath2 = "symlinkFile2";
// Create two symlinks to the file
fs.symlinkSync(filepath, symlinkpath1);
fs.symlinkSync(filepath, symlinkpath2);
// Use lchown() on the first symlink
// New owner is "geeksforgeeks" with user id 1200
// New group is "owner" with group id 1300
fs.lchown(symlinkpath1, 1200, 1300, (err) => {
if (err)
console.log(err);
else {
console.log("lchownSync:uid and gid set successfully");
}
});
// Use chown() on the second symlink
// New owner is "max" with user id 1100
// New group is "author" with group id 1202
fs.chown(symlinkpath1, 1100, 1202, (err) => {
if (err)
console.log(err);
else {
console.log("chownSync:uid and gid set successfully");
}
});
在運行代碼之前:
xubuntu@xubuntu:~/Desktop/fs-lchown$ ls -l total 4 -rw-rw--w- 1 xubuntu xubuntu 6 Apr 26 09:15 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 777 Apr 26 09:16 index.js
代碼輸出:
lchown:uid and gid set successfully chown:uid and gid set successfully
運行代碼後:
xubuntu@xubuntu:~/Desktop/fs-lchown$ ls -l total 4 -rw-rw--w- 1 max author 6 Apr 26 09:15 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 777 Apr 26 09:16 index.js lrwxrwxrwx 1 geeksforgeeks owner 16 Apr 26 09:15 symlinkFile1 -> example_file.txt lrwxrwxrwx 1 root root 16 Apr 26 09:15 symlinkFile2 -> example_file.txt
參考: https://nodejs.org/api/fs.html#fs_fs_lchown_path_uid_gid_callback
相關用法
- Node.js fsPromises.lchown()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs. lchown() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。