-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
doc: fix filehandle.truncate sample codes #20913
Closed
shisama
wants to merge
4
commits into
nodejs:master
from
shisama:doc-fs-promises-filehandle-truncate
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3572,8 +3572,8 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); | |
// Prints: Node.js | ||
|
||
async function doTruncate() { | ||
const fd = await fsPromises.open('temp.txt', 'r+'); | ||
await fsPromises.ftruncate(fd, 4); | ||
const filehandle = await fsPromises.open('temp.txt', 'r+'); | ||
await filehandle.truncate(4); | ||
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node | ||
} | ||
|
||
|
@@ -3591,12 +3591,13 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); | |
// Prints: Node.js | ||
|
||
async function doTruncate() { | ||
const fd = await fsPromises.open('temp.txt', 'r+'); | ||
await fsPromises.ftruncate(fd, 10); | ||
const filehandle = await fsPromises.open('temp.txt', 'r+'); | ||
await filehandle.truncate(10); | ||
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0 | ||
} | ||
|
||
doTruncate().catch(console.error); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: unneeded empty line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vsemozhetbyt Fixed it. Thanks. |
||
``` | ||
|
||
The last three bytes are null bytes (`'\0'`), to compensate the over-truncation. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should really be....
While the
filehandle
will close automatically on garbage collection in order to prevent the leak, it should be closed manually. A process warning would be emitted if it is allowed to close on gc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell sorry for the tangent: we really need a better resources story in general - if this is tricky consider how hard this is with 3 concurrent handles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing that might work here API wise is a disposer:
This doesn't solve the harder problem of dealing with multiple resources at once though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I certainly don't disagree :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell
Thank you for your comment. The codes were fixed with
filehandle.close
. Please check them again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell - what do you think the right avenue to discuss such a cross-cutting concern is? I'm not sure what the right team/working-group/avenue is.
I'm a little lost here :)