Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.writeFile(Sync): mode option is ignored in some cases #2249

Closed
targos opened this issue Jul 26, 2015 · 4 comments
Closed

fs.writeFile(Sync): mode option is ignored in some cases #2249

targos opened this issue Jul 26, 2015 · 4 comments
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@targos
Copy link
Member

targos commented Jul 26, 2015

Original issue: nodejs/node-v0.x-archive#25756

fs.writeFile(Sync) has a bug regarding the mode option:

  • If the file is not existing, it is sometimes created with a different mode (see test case, I didn't try all possible combinations). (not-a-bug, umask at work)
  • If the file is already existing, its mode is kept unchanged.

Our tests are not catching this because we use 755 and it is one of the working cases.

I suspect a bug in libuv's open method. The mode seems to be correctly passed from our code.

Test case (tested on OSX 10.10):

'use strict';

const filename = 'a00.txt';
const fs = require('fs');

remove(); write();
logMode('new file, 666 (default)');
remove(); write(0o756);
logMode('new file, 756');
remove(); write(0o565);
logMode('new file, 565');
remove(); write(0o755);
logMode('new file, 755');
remove(); write(0o444);
logMode('new file, 444');
chmod(0o777);
logMode('chmod, 777');
write(0o666);
logMode('replace, 666');
write(0o756);
logMode('replace, 756');
write(0o565);
logMode('replace, 565');
write(0o755);
logMode('replace, 755');
write(0o444);
logMode('replace, 444');

function remove() {
    try {
        fs.unlinkSync(filename);
    } catch (e) {}
}
function chmod(mode) {
    fs.chmodSync(filename, mode);
}
function write(mode) {
    fs.writeFileSync(filename, 'hello', {mode: mode});
}
function logMode(message) {
    console.log(fs.statSync(filename).mode.toString(8) + ' (' + message + ')');
}

Output :

100644 (new file, 666 (default))
100754 (new file, 756)
100545 (new file, 565)
100755 (new file, 755)
100444 (new file, 444)
100777 (chmod, 777)
100777 (replace, 666)
100777 (replace, 756)
100777 (replace, 565)
100777 (replace, 755)
100777 (replace, 444)

/cc @saghul ?

@targos targos added the fs Issues and PRs related to the fs subsystem / file system. label Jul 26, 2015
@targos
Copy link
Member Author

targos commented Jul 26, 2015

OK I did a bad pre-search, first issue is not an issue: #1104

But the second one is different and it feels wrong that the mode is not altered when replacing the file.

@targos
Copy link
Member Author

targos commented Jul 26, 2015

I think it may be just a documentation issue.

@bnoordhuis
Copy link
Member

If the file is already existing, its mode is kept unchanged.

This is the expected behavior, at least if you're a POSIX C programmer. From man 2 open:

mode specifies the permissions to use in case a new file is created.

@targos
Copy link
Member Author

targos commented Jul 27, 2015

I get it now, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

No branches or pull requests

2 participants