Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fs.writeFile and fs.writeFileSync ignores options.mode and always writes with permission '644' #25756

Closed
kaizhu256 opened this issue Jul 23, 2015 · 9 comments
Labels

Comments

@kaizhu256
Copy link

bug reproduced on node v0.10 and v0.12, on both linux and mac with the following code:

var fs = require('fs');
fs.writeFileSync('a00.txt', 'hello', { mode: '666' });
console.log(fs.statSync('a00.txt').mode.toString(8));
// output: 100644

@thefourtheye
Copy link

Just tried with v0.12.7 and I got 100666

@kaizhu256
Copy link
Author

image

here's screenshot on my macbook air (late 2014, running mavericks with homebrew node v0.12.7)

@kaizhu256
Copy link
Author

another screenshot running debian unstable on linode

image

@jfmercer
Copy link

I've gotten some strange results. It works when I use '0755', but not when I use '0666'. Because octals are forbidden in strict mode, I've passed them as strings to parseInt().

'0755':

image

'0666':

image

@targos
Copy link
Member

targos commented Jul 26, 2015

I can reproduce the results in the OP with v0.12.7. (OSX 10.10)

@jasnell
Copy link
Member

jasnell commented Jul 28, 2015

Yes, I'm able to reproduce this. The documentation even states that the default mode is 666, but when calling writeFileSync without the mode param, the mode will still be set as 644. /cc @joyent/node-tsc

@jfmercer
Copy link

My workaround was to use fs.chmod, e.g., fs.chmod('file.txt', '0755');, after the file had been created with fs.writeFile. Note that I had to pass the octal number as a string because JS strict mode prohibits the use of octal numbers, but not parsable octal strings.

Still, I'd like fs.writeFile and fs.writeFileSync work as described in the documentation.

@targos
Copy link
Member

targos commented Jul 28, 2015

I found that this is in fact not an issue, but just how UNIX file permissions are handled.
Whatever mode you pass to writeFile, it will be combined with the umask during the open system call.

You can change the umask with process.umask(newValue). If you set it to 0, the mode will be set exactly to the value you pass to the fs methods.

See nodejs/node#2249 and nodejs/node#1104 for more discussion about this problem.

@jasnell
Copy link
Member

jasnell commented Jul 28, 2015

Yeah, I suspected that was the case but hadn't investigated. Unfortunately the documentation is not clear on that at all. I'm going to tag this as a documentation fix. If you're up to it, a PR fixing up the docs is always welcome ;-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants