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

Can't open hidden files in 'w' mode on Windows - document, fix, or do nothing? #14553

Closed
seishun opened this issue Jul 31, 2017 · 12 comments
Closed
Labels
discuss Issues opened for discussions and feedbacks. fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform.

Comments

@seishun
Copy link
Contributor

seishun commented Jul 31, 2017

  • Version: all
  • Platform: Windows
  • Subsystem: fs

Trying to open a hidden file on Windows in 'w' mode throws EPERM:

> fs.openSync('hidden.txt', 'w')
Error: EPERM: operation not permitted, open 'C:\hidden.txt'
    at Object.fs.openSync (fs.js:651:18)
    at repl:1:4
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:433:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)
    at REPLServer.Interface._onLine (readline.js:278:10)

This matches the behavior of Microsoft C runtime library:

#include <cstdio>
#include <cerrno>

int main()
{

	FILE* f = fopen("hidden.txt", "w");
	if (f) {
		puts("success");
	} else if (errno == EACCES) {
		perror("EACCES");
	} else {
		perror("something else");
	}
}

Output if "hidden.txt" is hidden:

EACCES: Permission denied

And it matches the behavior of Python since it purportedly uses _wfopen under the hood:

>>> open("hidden.txt", "w")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 13] Permission denied: 'hidden.txt'

However, this behavior has caused at least two issues down the road:

So, my questions are:

  • Does Node.js really have to match CRT's behavior here? Would anything break if it started allowing opening hidden files in 'w' mode?
  • If we can't or don't want to "fix" this, do we want to document this behavior, or do we just say "does the same thing CRT does"?
@seishun seishun added discuss Issues opened for discussions and feedbacks. fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform. labels Jul 31, 2017
@bnoordhuis
Copy link
Member

Does Node.js really have to match CRT's behavior here?

I'm not 100% sure but reading fs__open() in deps/uv/src/win/fs.c (which uses the CreateFile NT API, not the CRT), I don't see anything that suggests it's a policy enforced by libuv.

@seishun
Copy link
Contributor Author

seishun commented Jul 31, 2017

It doesn't enforce it explicitly, no. It just never passes the FILE_ATTRIBUTE_HIDDEN flag, causing CreateFile to fail when called on a hidden file.

@bnoordhuis
Copy link
Member

I don't know if that is fixable. Passing it by default is not an option and retrying the CreateFile() call on error is race-y. All signs seem to point to 'documentation fix.'

@tniessen
Copy link
Member

It seems like the only way to write to hidden files (without setting FILE_ATTRIBUTE_HIDDEN) is by setting the disposition to either TRUNCATE_EXISTING or CREATE_NEW, and I don't think it is possible to use these values in fs.open() 😞

@bnoordhuis
Copy link
Member

Hm, does that mean 'wx' instead of 'w' works?

@seishun
Copy link
Contributor Author

seishun commented Jul 31, 2017

Passing it by default is not an option

Why not? FILE_ATTRIBUTE_HIDDEN works for both hidden and non-hidden files.

Hm, does that mean 'wx' instead of 'w' works?

No, 'wx' maps to O_TRUNC | O_CREAT | O_WRONLY | O_EXCL, which maps to CREATE_NEW, which fails if the file already exists.

@tniessen
Copy link
Member

Why not? FILE_ATTRIBUTE_HIDDEN works for both hidden and non-hidden files.

But won't this cause new files to be hidden?

@seishun
Copy link
Contributor Author

seishun commented Jul 31, 2017

Indeed it does... 🤦‍♂️

@bnoordhuis
Copy link
Member

So, what is the consensus here? Documentation fix? Any volunteers?

bzoz added a commit to JaneaSystems/node that referenced this issue Sep 14, 2017
@bnoordhuis
Copy link
Member

bnoordhuis commented Sep 21, 2017

Ping @seishun.

edit: is #15409 the doc fix?

@seishun
Copy link
Contributor Author

seishun commented Sep 21, 2017

Yes, it is.

@tniessen
Copy link
Member

So the question at hand is whether we want to deviate from CRT here or not. If we do, this needs an upstream implementation, and I am not sure an atomic solution exists.

MylesBorins pushed a commit that referenced this issue Oct 23, 2017
PR-URL: #15409
Fixes: #14553
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
addaleax pushed a commit to ayojs/ayo that referenced this issue Oct 26, 2017
PR-URL: nodejs/node#15409
Fixes: nodejs/node#14553
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
addaleax pushed a commit to ayojs/ayo that referenced this issue Dec 7, 2017
PR-URL: nodejs/node#15409
Fixes: nodejs/node#14553
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Issues opened for discussions and feedbacks. fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants