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 doesn't work with non-seekable files #31926

Closed
mildsunrise opened this issue Feb 24, 2020 · 5 comments
Closed

fs.writeFile doesn't work with non-seekable files #31926

mildsunrise opened this issue Feb 24, 2020 · 5 comments
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@mildsunrise
Copy link
Member

mildsunrise commented Feb 24, 2020

  • Version: 13.7.0
  • Platform: Linux
  • Subsystem: fs

I see that fs.writeFile* fails with non-seekable files, because it does a positioned write:

fs.writeFileSync('/sys/kernel/debug/tracing/trace_clock', 'mono')
openat(AT_FDCWD, "/sys/kernel/debug/tracing/trace_clock", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 19
pwrite64(19, "mono", 4, 0)              = -1 ESPIPE (Illegal seek)

fs.readFile works correctly since it does a regular read, and I also found that createWriteStream was fixed to work with non-seekable files (#19329).
Is this known/intentional? If not, would a fix be accepted? (I haven't investigated further yet)

@addaleax addaleax added the fs Issues and PRs related to the fs subsystem / file system. label Feb 24, 2020
@addaleax
Copy link
Member

👍

I think fixing this might be as straightforward as switching the position parameter from 0 to null in the writeFile() function.

The only thing that I could see being tricky about this is that this changes behaviour when an fd is being passed where the write position had already been advanced before. Maybe it would be best to only perform this change for freshly opened files, i.e. when a path is being passed?

@mildsunrise
Copy link
Member Author

Oh true, I didn't remember that fds are accepted... yes, for fds it would change behaviour, but at the same time it's a bug people would likely not rely on, and it's inconsistent with fs.write.

if we don't want to change behaviour, I suppose we can fix it only for paths like you said, and then a semver-major full fix?

@mildsunrise
Copy link
Member Author

btw, it looks like the behaviours are different between writeFile:

node/lib/fs.js

Line 1300 in 9c70292

const position = /a/.test(flag) ? null : 0;

and writeFileSync, which does avoid setting pos = 0 for fds:

node/lib/fs.js

Line 1321 in 9c70292

let position = (/a/.test(flag) || isUserFd) ? null : 0;

@addaleax
Copy link
Member

@mildsunrise Yeah, that’s … not great. I’m not sure what to do about that, besides what you suggested above – first only fix this for paths, then make behaviour consistent in a semver-major change.

@mildsunrise
Copy link
Member Author

I overlooked the code again... it turns out the semver-major change we were talking about was already decided in #23433 and landed in #23709 at v12. So we just need to fix it for paths :) I'll make a PR

mildsunrise added a commit to mildsunrise/node that referenced this issue Mar 10, 2020
Completely disables the use of positioned writes at
writeFile and writeFileSync, which allows it to work
with non-seekable files.

Fixes: nodejs#31926

PR-URL: nodejs#32006
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this issue Mar 10, 2020
Completely disables the use of positioned writes at
writeFile and writeFileSync, which allows it to work
with non-seekable files.

Fixes: #31926

Backport-PR-URL: #32172
PR-URL: #32006
Reviewed-By: Anna Henningsen <anna@addaleax.net>
targos pushed a commit that referenced this issue Apr 20, 2020
Completely disables the use of positioned writes at
writeFile and writeFileSync, which allows it to work
with non-seekable files.

Fixes: #31926

Backport-PR-URL: #32172
PR-URL: #32006
Reviewed-By: Anna Henningsen <anna@addaleax.net>
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this issue May 5, 2024
Completely disables the use of positioned writes at
writeFile and writeFileSync, which allows it to work
with non-seekable files.

Fixes: nodejs/node#31926
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