Skip to content

Commit c8619ea

Browse files
committed
doc: improve fs.truncate functions' documentation
The default value of the `len` parameter is zero and it is included in the documenetation. This patch also has examples of how `ftruncate` can be used. PR-URL: #7648 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 82c7a9c commit c8619ea

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

doc/api/fs.md

+47-5
Original file line numberDiff line numberDiff line change
@@ -739,19 +739,60 @@ added: v0.8.6
739739
-->
740740

741741
* `fd` {Integer}
742-
* `len` {Integer}
742+
* `len` {Integer} default = `0`
743743
* `callback` {Function}
744744

745745
Asynchronous ftruncate(2). No arguments other than a possible exception are
746746
given to the completion callback.
747747

748+
If the file referred to by the file descriptor was larger than `len` bytes, only
749+
the first `len` bytes will be retained in the file.
750+
751+
For example, the following program retains only the first four bytes of the file
752+
753+
```js
754+
console.log(fs.readFileSync('temp.txt', 'utf8'));
755+
// prints Node.js
756+
757+
// get the file descriptor of the file to be truncated
758+
const fd = fs.openSync('temp.txt', 'r+');
759+
760+
// truncate the file to first four bytes
761+
fs.ftruncate(fd, 4, (err) => {
762+
assert.ifError(err);
763+
console.log(fs.readFileSync('temp.txt', 'utf8'));
764+
});
765+
// prints Node
766+
```
767+
768+
If the file previously was shorter than `len` bytes, it is extended, and the
769+
extended part is filled with null bytes ('\0'). For example,
770+
771+
```js
772+
console.log(fs.readFileSync('temp.txt', 'utf-8'));
773+
// prints Node.js
774+
775+
// get the file descriptor of the file to be truncated
776+
const fd = fs.openSync('temp.txt', 'r+');
777+
778+
// truncate the file to 10 bytes, whereas the actual size is 7 bytes
779+
fs.ftruncate(fd, 10, (err) => {
780+
assert.ifError(!err);
781+
console.log(fs.readFileSync('temp.txt'));
782+
});
783+
// prints <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>
784+
// ('Node.js\0\0\0' in UTF8)
785+
```
786+
787+
The last three bytes are null bytes ('\0'), to compensate the over-truncation.
788+
748789
## fs.ftruncateSync(fd, len)
749790
<!-- YAML
750791
added: v0.8.6
751792
-->
752793

753794
* `fd` {Integer}
754-
* `len` {Integer}
795+
* `len` {Integer} default = `0`
755796

756797
Synchronous ftruncate(2). Returns `undefined`.
757798

@@ -1368,7 +1409,7 @@ added: v0.8.6
13681409
-->
13691410

13701411
* `path` {String | Buffer}
1371-
* `len` {Integer}
1412+
* `len` {Integer} default = `0`
13721413
* `callback` {Function}
13731414

13741415
Asynchronous truncate(2). No arguments other than a possible exception are
@@ -1381,9 +1422,10 @@ added: v0.8.6
13811422
-->
13821423

13831424
* `path` {String | Buffer}
1384-
* `len` {Integer}
1425+
* `len` {Integer} default = `0`
13851426

1386-
Synchronous truncate(2). Returns `undefined`.
1427+
Synchronous truncate(2). Returns `undefined`. A file descriptor can also be
1428+
passed as the first argument. In this case, `fs.ftruncateSync()` is called.
13871429

13881430
## fs.unlink(path, callback)
13891431
<!-- YAML

0 commit comments

Comments
 (0)