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 after truncate doesn't write at beginning of the file #24923

Closed
kghost opened this issue Dec 9, 2018 · 3 comments
Closed

fs writeFile after truncate doesn't write at beginning of the file #24923

kghost opened this issue Dec 9, 2018 · 3 comments
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@kghost
Copy link

kghost commented Dec 9, 2018

  • Version: v11.3.0
  • Platform: Win 10

Test case:

var fs = require('fs').promises;

async function bug() {
  await fs.writeFile('test', '012345678901234567890123456789');
  const fd = await fs.open('test', 'r+');
  await fd.readFile({ encoding: 'utf8' });
  await fd.truncate(0);
  await fd.writeFile('Extra data not at beginning');
  await fd.close();
}

bug();

This will result a file with lots of '\0' at beginning of the file.

And w/o the ability to seek, there is no way to write at the beginning of the file.

@vsemozhetbyt vsemozhetbyt added the fs Issues and PRs related to the fs subsystem / file system. label Dec 9, 2018
@thefourtheye
Copy link
Contributor

truncate doesn't change the offset of the file descriptor. If you like to write from a specific position, I would recommend one of the filehandle.write variants.

@skyzyx
Copy link

skyzyx commented Apr 16, 2019

#22554

@sam-github
Copy link
Contributor

@kghost does https://github.com/nodejs/node/pull/27282/files clarify for you why this occurs?

sam-github added a commit to sam-github/node that referenced this issue Apr 22, 2019
This is a continuing source of confusion, attempt to make it even more
clear.

Fixes: nodejs#24923
targos pushed a commit that referenced this issue Apr 27, 2019
This is a continuing source of confusion, attempt to make it even more
clear.

Fixes: #24923

PR-URL: #27282
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
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

Successfully merging a pull request may close this issue.

5 participants