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

fix: truncate to larger size #875

Merged
merged 2 commits into from Oct 28, 2022
Merged

Conversation

toyobayashi
Copy link
Contributor

No description provided.

@G-Rath
Copy link
Collaborator

G-Rath commented Oct 28, 2022

Could you give a bit more a context about this change? why is it a problem, how did you discover it, what does Node do? etc

@toyobayashi
Copy link
Contributor Author

@G-Rath

const fs = require('fs')

const fd = fs.openSync('./test', 'w');
fs.writeFileSync(fd, '12345');
console.log(fs.readFileSync('./test', 'utf8') === '12345');
fs.truncateSync('./test', 10);
console.log(fs.readFileSync('./test', 'utf8') === '12345\0\0\0\0\0');

According to https://nodejs.org/dist/latest-v18.x/docs/api/fs.html#fsftruncatefd-len-callback

If the file previously was shorter than len bytes, it is extended, and the extended part is filled with null bytes ('\0'):

@toyobayashi
Copy link
Contributor Author

I'm using memfs in browser, before this change, ftruncateSync just throws RangeError if truncate to a larger size

image

@G-Rath G-Rath merged commit 25027fb into streamich:master Oct 28, 2022
streamich pushed a commit that referenced this pull request Oct 28, 2022
## [3.4.8](v3.4.7...v3.4.8) (2022-10-28)

### Bug Fixes

* extend with null bytes when calling `truncate` with a `len` greater than the current file size ([#875](#875)) ([25027fb](25027fb))
@streamich
Copy link
Owner

🎉 This PR is included in version 3.4.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants