Nodejs File Permissions

旧城冷巷雨未停 提交于 2019-11-28 08:17:25
var checkPermission = function (file, mask, cb){
    fs.stat (file, function (error, stats){
        if (error){
            cb (error, false);
        }else{
            cb (null, !!(mask & parseInt ((stats.mode & parseInt ("777", 8)).toString (8)[0])));
        }
    });
};

canExecute():

checkPermission (<path>, 1, cb);

canRead():

checkPermission (<path>, 4, cb);

canWrite():

checkPermission (<path>, 2, cb);

The number format is platform dependent, so you can't, reliably.

When NodeJs starts exposing the underlying S_ISDIR function and the S_IRUSR and similar constants, you can.

Some information on the stat format: http://linux.die.net/man/2/stat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!