gpt4 book ai didi

node.js - 如何在 Node.js 中使用 chmod

转载 作者:IT老高 更新时间:2023-10-28 21:55:20 26 4
gpt4 key购买 nike

如何在 Node.js 中使用 chmod?

fs包里面有个方法,应该可以这样,但是不知道第二个参数取什么。

fs.chmod(path, mode, [callback])

Asynchronous chmod(2). No arguments other than a possible exception are given to the completion callback.

fs.chmodSync(path, mode)

Synchronous chmod(2).

(来自Node.js documentation)

如果我这样做

fs.chmodSync('test', 0755);

什么都没有发生(文件未更改为该模式)。

fs.chmodSync('test', '+x');

也不行。

顺便说一句,我正在使用 Windows 机器。

最佳答案

根据其源代码/lib/fs.js在第 508 行:

fs.chmodSync = function(path, mode) {
return binding.chmod(pathModule._makeLong(path), modeNum(mode));
};

和第 203 行:

function modeNum(m, def) {
switch (typeof m) {
case 'number': return m;
case 'string': return parseInt(m, 8);
default:
if (def) {
return modeNum(def);
} else {
return undefined;
}
}
}

它需要一个八进制数或一个字符串。

例如

fs.chmodSync('test', 0755);
fs.chmodSync('test', '755');

在你的情况下它不起作用,因为文件模式只存在于 *nix 机器上。

关于node.js - 如何在 Node.js 中使用 chmod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8756639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com