镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@seishun
  • 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"?

Activity

  1. added
    discussIssues opened for discussion and feedback.
    fsIssues and PRs related to file-system APIs and the fs module.
    windowsIssues and PRs related to the Windows platform.
    on Jul 31, 2017
  2. bnoordhuis commented on Jul 31, 2017

    @bnoordhuis
    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.

  3. seishun commented on Jul 31, 2017

    @seishun
    ContributorAuthor

    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.

  4. bnoordhuis commented on Jul 31, 2017

    @bnoordhuis
    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.'

  5. tniessen commented on Jul 31, 2017

    @tniessen
    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() 😞

  6. bnoordhuis commented on Jul 31, 2017

    @bnoordhuis
    Member

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

  7. seishun commented on Jul 31, 2017

    @seishun
    ContributorAuthor

    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.

  8. tniessen commented on Jul 31, 2017

    @tniessen
    Member

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

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

  9. seishun commented on Jul 31, 2017

    @seishun
    ContributorAuthor

    Indeed it does... 🤦‍♂️

  10. bnoordhuis commented on Aug 8, 2017

    @bnoordhuis
    Member

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

  11. bnoordhuis commented on Sep 21, 2017

    @bnoordhuis
    Member

    Ping @seishun.

    edit: is #15409 the doc fix?

  12. seishun commented on Sep 21, 2017

    @seishun
    ContributorAuthor

    Yes, it is.

  13. tniessen commented on Sep 21, 2017

    @tniessen
    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussIssues opened for discussion and feedback.fsIssues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions