`fs.mkdir`正在使用与指定权限不同的权限创建目录 [英] `fs.mkdir` is creating the directory with different permissions than those specified

查看:110
本文介绍了`fs.mkdir`正在使用与指定权限不同的权限创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的节点程序使用的模块,因此需要root才能运行.这是因为它必须在端口80上运行HTTP服务器(众所周知,较低的端口需要root才能使用).

Due to the modules my node program uses, it requires root to run. This is because it has to run a HTTP server on port 80 (and as you know, lower ports like that require root in order to be used).

但是,该程序还使用 fs 方法创建文件.我不介意root是否是所有者,只要我可以更改权限即可...但这正是我遇到的麻烦.

But, this program also uses a fs method to create a file. I don't mind if root is the owner as long as I can change the permissions... But that's exactly where I'm hitting the bump.

我正在使用 fs.mkdir 函数创建一个目录,如下所示:

I'm using the fs.mkdir function to create a directory like so:

(假设此文件名为 create.js )

fs.mkdir(__dirname + "/.loud", 0777, function(err){
  if (err) console.log("Failed to create file at " + __dirname + "/.loud");
});

执行它时(请记住,通过sudo, sudo节点create.js ),它实际上会创建文件夹...但是提供的权限( 0777 )是错误!...

When executing it (Remember, via sudo, sudo node create.js) it does in fact create the folder... But the permissions provided (0777) are wrong!...

如果我没记错的话, 7 表示读,写和执行.将其用于3个字段( 777 +前导 0 以八进制表示)应该意味着每个人都可以在此文件夹中读取,写入和执行...但是,这是检查文件夹的权限时得到的:

If I'm not mistaken, 7 implies read, write, and execute. Using it for the 3 fields (777 + a leading 0 to represent it as an octal) should imply that everyone can read, write, and execute inside this folder... But here is what I get when checking the folder's permissions:

[imac] jamen : ~/Tests/mkdir $ ls -a -l .
...
drwxr-xr-x 2 root  root  4096 Jun 12 22:27 .loud

因此,让我们分开开始权限部分:

So, lets seperate the beginning permissions part:

d, rwx, r-x, r-x

所以,我不知道 d 是什么意思(请在评论中告诉我),所以我认为可以将其排除在外.这是其他的意思:

So, I don't know what the d means (feel free to tell me in the comments), so I think we can throw that out. Here is what the other means:

  1. 所有者可以按预期方式读取,写入和执行
  2. 该小组可以读取和执行(什么?!我们指定了 7 ,这意味着它也应该能够编写???)
  3. 其他人都可以阅读和执行(同样,我们也指定了 7 ,每个人都应该也可以写吗?)
  1. The owner can read, write, and execute (as expected)
  2. The group can read, and execute (What?! We specified 7, meaning it should also be able to write???)
  3. Everyone else can read, and execute (Again... We also specified 7, should everyone also be able to write?)

因此,为了解决问题,我进行了一项尝试,并

So, in attempt to solve, I went on a venture and found this post on SE.Unix, and after reading it, I thought I might as well try to specify the permissions in decimal format, instead of octal format...

这是我更改 create.js 文件的方式:

Here is how I changed our create.js file:

fs.mkdir(__dirname + "/.loud", 511, function(err){
  if (err) console.log("Failed to create file at " + __dirname + "/.loud");
});

(因为八进制的0777被表示为十进制的511)

(Since 0777 in octal is represented as 511 in decimal)

但是,不幸的是,这给出了完全相同的结果(是的,我确实在再次运行文件夹之前删除了该文件夹,否则NodeJS会在回调中传递错误):

But, this unfortunately gives the same exact results (yes, I did delete the folder before running this again, otherwise NodeJS would have passed an error in the callback):

[imac] jamen : ~/Tests/mkdir $ ls -a -l .
...
drwxr-xr-x 2 root  root  4096 Jun 12 22:35 .loud

我是否对Unix的权限方案有误解,还是在NodeJS中犯了一个错误?

Am I misunderstanding something about Unix's permissions scheme, or am I making a mistake in NodeJS?

我将如何解决此问题,以便文件夹获得权限 0777 ,这意味着所有者为rwx permissoins,该组具有rwx permissoins,而其他所有人都具有rwx权限...?

How would I fix this so the folder gets the permissions 0777, implying the owner as rwx permissoins, the group has rwx permissoins, and everyone else has rwx permissions...?

推荐答案

因为它适用 umask 在您的模式下.在控制台中键入 umask ,您将看到022或0022.

Because it applies umask on your mode. Type umask in your console, you will see 022 or 0022.

您可以使用 process.umask(newmask);

这篇关于`fs.mkdir`正在使用与指定权限不同的权限创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆