`fs.mkdir` creates a directory with different permissions than specified

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

But this program also uses a method fs

to create a file. I don't mind if root is the owner as long as I can change the permissions ... But exactly where I hit hit.

I am using a function fs.mkdir

to create a directory like this:

(Let's assume this file has a name create.js

)

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

      

When doing this (remember, via sudo, sudo node create.js

) it actually creates the folder ... But the permissions ( 0777

) provided are not correct! ...

If I am not mistaken, 7

implies reading, writing and executing. By using it for 3 fields ( 777

+ leading 0

to represent it as octal), it should be assumed that anyone can read, write and execute inside this folder ... But here's what I get when checking the folder permissions:

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

      

So let's split the initial resolution part:

d, rwx, r-x, r-x

      

So, I don't know what that means d

(feel free to tell me in the comments), so I think we can release it. Here's what the other means:

  • Owner can read, write and execute (as expected)
  • The group can read and execute (what ?! We pointed out 7

    , that is, he must also be able to write ???)
  • Everyone else can read and execute (again ... did we also specify 7

    if everyone should write as well?)

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

This 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");
});

      

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

But this unfortunately gives exact exact results (yes, I deleted the folder before rerunning, otherwise NodeJS would have passed the error in the callback):

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

      

Am I misunderstanding something about the Unix permission scheme, or am I wrong about NodeJS?

How can I fix this so that the folder gets permissions 0777

, assuming the owner is rwx permissoins, the group has rwx permissoins, and everyone else has rights to rwx ...?

+3


source to share


1 answer


Because it applies the umask in your mode. Enter umask

in your console, you will see 022 or 0022.



You can overwrite it for a node process with process.umask(newmask);

+1


source







All Articles