如何使用node.js测试文件权限? [英] How to test file permissions using node.js?
问题描述
如何检查正在运行的node.js进程对给定文件具有的权限(读/写/执行)?
How can I check to see the permissions (read/write/execute) that a running node.js process has on a given file?
我希望 fs.Stats
对象具有一些信息关于权限,但我看不到任何权限.是否有一些内置功能可以让我进行此类检查?例如:
I was hoping that the fs.Stats
object had some information about permissions but I don't see any. Is there some built-in function that will allow me to do such checks? For example:
var filename = '/path/to/some/file';
if (fs.canRead(filename)) // OK...
if (fs.canWrite(filename)) // OK...
if (fs.canExecute(filename)) // OK...
当然,我不必尝试在每种模式下打开文件并以否定确认的方式处理错误,对吗?一定有一种更简单的方法...
Surely I don't have to attempt to open the file in each of those modes and handle an error as the negative affirmation, right? There's got to be a simpler way...
推荐答案
我来晚了,但是,我一直在寻找与您相同的原因,并了解了这一点.
I am late, but, I was looking for same reasons as yours and learnt about this.
fs.access
是您所需要的.可从节点v0.11.15获得.
fs.access
is the one you need. It is available from node v0.11.15.
function canWrite(path, callback) {
fs.access(path, fs.W_OK, function(err) {
callback(null, !err);
});
}
canWrite('/some/file/or/folder', function(err, isWritable) {
console.log(isWritable); // true or false
});
这篇关于如何使用node.js测试文件权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!