From 32107d02d39821949dac17f51de2df5c911fc692 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Wed, 23 May 2018 22:48:47 +0900 Subject: [PATCH 1/4] doc: fix filehandle.truncate sample codes --- doc/api/fs.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 05869268632a01..1a02356c9529db 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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); + ``` The last three bytes are null bytes (`'\0'`), to compensate the over-truncation. From 89fc02085673b89bee79aef6ad2562b92a508ce5 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Thu, 24 May 2018 00:07:57 +0900 Subject: [PATCH 2/4] doc: fix filehandle.truncate sample codes to call filehandle.close Fix sample codes because `filehandle.close` was not called when error occured. `filehandle.close` should be called manually. --- doc/api/fs.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 1a02356c9529db..aadf6843346db7 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3572,8 +3572,15 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js async function doTruncate() { - const filehandle = await fsPromises.open('temp.txt', 'r+'); - await filehandle.truncate(4); + let filehandle; + try { + filehandle = await fsPromises.open('temp.txt', 'r+'); + await filehandle.truncate(4); + } finally { + if (filehandle !== null) { + await filehandle.close(); + } + } console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node } @@ -3591,8 +3598,15 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js async function doTruncate() { - const filehandle = await fsPromises.open('temp.txt', 'r+'); - await filehandle.truncate(10); + let filehandle; + try { + filehandle = await fsPromises.open('temp.txt', 'r+'); + await filehandle.truncate(10); + } finally { + if (filehandle !== null) { + await filehandle.close(); + } + } console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0 } From dda739e085b531ba23ae0d6aa736544f1bce7e52 Mon Sep 17 00:00:00 2001 From: shisama Date: Thu, 24 May 2018 13:35:11 +0900 Subject: [PATCH 3/4] doc: remove unneeded empty line in fs.md --- doc/api/fs.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index aadf6843346db7..af1580e67c31cc 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3611,7 +3611,6 @@ async function doTruncate() { } doTruncate().catch(console.error); - ``` The last three bytes are null bytes (`'\0'`), to compensate the over-truncation. From 01a67c112a0859557edcc9f832b07c3cd5955d54 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Thu, 24 May 2018 21:30:34 +0900 Subject: [PATCH 4/4] doc: fix sample codes for filehandle.truncate Fix sample codes for filehandle.truncate to make them more clearly. --- doc/api/fs.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index af1580e67c31cc..3242e10eaa2080 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3572,12 +3572,13 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js async function doTruncate() { - let filehandle; + let filehandle = null; try { filehandle = await fsPromises.open('temp.txt', 'r+'); await filehandle.truncate(4); } finally { - if (filehandle !== null) { + if (filehandle) { + // close the file if it is opened. await filehandle.close(); } } @@ -3598,12 +3599,13 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js async function doTruncate() { - let filehandle; + let filehandle = null; try { filehandle = await fsPromises.open('temp.txt', 'r+'); await filehandle.truncate(10); } finally { - if (filehandle !== null) { + if (filehandle) { + // close the file if it is opened. await filehandle.close(); } }